/**
 * @file
 * $Id$
 * $Revision$
 * $Author$
 * $Date$
 *
 * This file is part of The iWear Framework.
 *
 * The iWear Framework is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by the
 * Free Software Foundation as in version 2 of the License.

 * 
 * The iWear Framework is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * The iWear Framework; if not, write to the Free Software Foundation, Inc., 59
 * Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#ifndef __IWREMOTE_CALLPROVIDER_H
#define __IWREMOTE_CALLPROVIDER_H

#include <iwremote/remoteconnection.h>
// we need to include remoteconnection first otherwise boost::multi_index would
// not work
#include <iwear/utility.h>
#include <iwear/uid.h>
#include <iwear/shmemory.h>
#include <iwear/debugstream.h>
#include <iwear/threadlocked.h>

#include <queue>
#include <vector>
using std::vector;

namespace iwear
{
    namespace net
    {
class RPCStream;	
class RemoteObject;
class CallManager;
class HostConnector;
/**
 * A Call Provider is a base class for (not only plugins) that provide some
 * data transfer mechanisms. Each RemoteObject on the Client Side will have
 * some CallProvider that provides the necessary mechanisms for it. At
 * Connection Discovery Phase, or at Reconnection Phase this CallProvider will
 * be (re)set to reflect a new connection method. It might later be embedded
 * into some connection descriptor pointer that the RemoteObject will hold.
 *
 * A Call Provider will provide the necessary rpcstreams to do a call. This
 * means that either for every connection (stream)type a single CallProvider is
 * needed or that the CallProvider knows how to distinguish.
 *
 * @todo TODO Maybe create the ConnectionDescriptor as a polymorphic class we
 * can *upcast* (brr, ugly) and use then ?
 */
class CallProvider : public virtual ThreadLocked
{
    //XXX Need to somehow get rid of this !!
friend class SocketRemoteConnection;
friend class CallManager;
private:
protected:
    CallManager* cman;
    /**
     * This is a queue containing all the System messages that were received by
     * other threads. We should first empty this queue and then receive new
     * requests.
     */
PH1  std::queue<std::pair<RPCStream*,RemoteConnectionPtr> > msgq;

PH1  std::set<RemoteConnectionPtr > remote_connections;

PH1  void interpret_channelstream( chanid_t chan, RPCStream* rpcs, RemoteConnectionPtr rc );

PH1  void do_call( const oid_t& oid, const hostid_t& hid, RemoteConnectionPtr rcon,
	    RPCStream*, chanid_t chan, uint32_t func, uint32_t fver);

PH1  void insert_system( RPCStream*, RemoteConnectionPtr );
public:

    /**
     * This function is called *from* the RemoteConnection itself to tell the
     * callprovider it has been connected.
     */
PH1 virtual void connected( RemoteConnectionPtr ) = 0;    

PH1 virtual void disconnected( RemoteConnectionPtr ) = 0;    

    CallProvider( CallManager* );
    virtual ~CallProvider();

    virtual void check_channels( void );
/* deprecated
    virtual RPCStream& get_callrpcstream( uint32_t callid, RemoteObject& ) = 0;

    virtual RPCStream& get_retrpcstream( uint32_t callid, RemoteObject& ) = 0;

    virtual void search_object( RPCStream& ) = 0;
*/

    /**
     * Check for a stream thats destination is the system and return it. If
     * after the timeout for this callprovider (e.g. listening on socket) no
     * stream could be found, it returns NULL.
     * @note this is the function that triggers the look after all the
     * connections, like accepting incoming connections, reading out of other
     * connections into local buffers etc. So make sure you call it frequently.
     */
    virtual std::pair<RPCStream*,RemoteConnectionPtr> get_system_stream( double timeout ) = 0;

    virtual void _debug_print_state( void ) { d_dbg << "No state available" << std::endl; }

    /**
     * Gets a brand new connection to the host. If the HostConnector already
     * had some information about the connection, it will be overridden.
     */
    virtual RemoteConnectionPtr get_connection( HostConnector& ) = 0;
};

}
}
#endif
