CCAFFEINE  0.8.8
ConnectionManager.h
00001 #ifdef CCAFE_THREADS
00002 
00003 #ifndef ConnectionManager_seen
00004 #define ConnectionManager_seen
00005 
00006 namespace jcpp {
00007   class Writer;
00008   class Reader;
00009 } ENDSEMI
00010 
00011 class Connection;
00026 class ConnectionManager : public virtual JCPN(Object) {
00027 protected:
00028   friend class Connection;
00029   virtual void disconnectConnection(Connection* connection) = 0;
00030 public:
00031   ConnectionManager();
00032   ~ConnectionManager();
00033   virtual int makeConnection(int timeout) = 0;
00034 
00037   virtual int notifyReconnect() = 0;
00038 
00042   virtual void notifyReads() = 0;
00043   virtual CCAFEThreadSafeQueue* getConnections() = 0;
00044   virtual void shutdown() = 0; 
00047   virtual int getNumConnections() =0;
00048   virtual boolean isConnected() = 0;
00049   virtual boolean isServer() = 0;
00050 };
00051 
00052 
00055 class ReadCallback {
00056 public:
00057   virtual void readReady(Connection* connection) = 0;
00058 };
00061 class ConnectCallback {
00062 public:
00063   virtual void connected(Connection* connection) = 0;
00064   virtual void disconnected(Connection* connection) = 0;
00065 };
00066 
00067 
00077 class Connection : public virtual JCPN(Object) {
00078 private:
00079 
00080   ReadCallback* readCallback;
00081   ConnectCallback* connectCallback;
00082   ConnectionManager* manager;
00083 protected:
00084 
00085   // every implementation of ConnectionManager should call these functions
00086   // (from read, reconnect and disconnectConnection, respectively).
00087   friend class ConnectionManager;
00088 
00089   void doReadCallback() 
00090   {if (readCallback) readCallback->readReady(this); } ENDSEMI
00091 
00092   void doConnectCallback() 
00093   {if (connectCallback) connectCallback->connected(this); } ENDSEMI
00094 
00095   void doDisconnectCallback()
00096   { if (connectCallback) connectCallback->disconnected(this); } ENDSEMI
00097 
00098 public:
00099   Connection (ConnectionManager* mgr) 
00100   { manager = mgr; } ENDSEMI
00101 
00102   virtual void disconnect() 
00103   { if (manager) manager -> disconnectConnection (this); } ENDSEMI
00104 
00105   virtual void setReadCallback( ReadCallback* callback ) 
00106   { readCallback = callback; } ENDSEMI
00107 
00108   virtual void setConnectCallback( ConnectCallback* callback ) 
00109   { connectCallback = callback; } ENDSEMI
00110 
00111   // the reader and writer objects should be owned by the connection -
00112   // the connection should always keep a pointer to them and be prepared to
00113   // destroy them on shutdown.
00114   virtual JCPN(Reader)* getIn() =0;
00115   virtual JCPN(Writer)* getOut() =0;
00116   virtual void shutdown() =0;
00117   virtual char* getName() = 0; 
00118   virtual int getId() = 0;
00119 };
00120 
00121 #endif // cm seen
00122 #else
00123 extern int ccafe_no_connectionmanager;
00124 #endif // CCAFE_THREADS