CCAFFEINE  0.8.8
LinewiseClient.h
00001 #ifdef CCAFE_THREADS
00002 
00003 #ifndef LinewiseClient_seen
00004 #define LinewiseClient_seen
00005 
00017 #include <assert.h>
00018 #define CLIENT_TRACE(x) printf("CLIENT: "); printf x
00019 
00020 class LinewiseClient : public virtual Client {
00021  private:
00022   JCPN(Vector) clientOutputListeners;
00023   int clientId;
00024   JCPN(Vector) output;
00025   JCPN(BufferedReader)* reader;
00026   JCPN(Writer)* writer;
00027   boolean finished;
00028   boolean amDisconnected;
00029   Connection* connection;
00030   char* name;
00031   CCAFEThreadPool* threadpool;
00032   CCAFEReadWriteMutex mutex;
00033  protected:
00034   void notifyOutput(char* s);
00035   void addOutput(char* s);
00036  public:
00037   LinewiseClient(Connection* connect, CCAFEThreadPool* threadpool) {
00038     this->threadpool = threadpool;
00039     this->connection = connect;
00040     writer = connection->getOut();
00041     reader = new JCPN(BufferedReader)(connection->getIn());
00042     finished = FALSE;
00043     amDisconnected = FALSE;
00044     connect->setReadCallback(this);
00045     connect->setConnectCallback(this);
00046   };
00047 
00053   void* run();
00054   
00056   void readReady(Connection* connection);
00057 
00059   void connected(Connection* connection);
00060   void disconnected(Connection* connection);
00061 
00062   void shutdown (); 
00063   void disconnect ();
00064   void addClientOutputListener(ClientOutputListener* l)
00065     {clientOutputListeners.addElement(l); };
00066   void removeClientOutputListener(ClientOutputListener* l)
00067     {clientOutputListeners.removeElement(l); };
00068   JCPN(Vector)* getOutput() { return &output; };
00069   void write(char* s)
00070     { 
00071       CCAFEReadLock lock(&mutex); 
00072       CLIENT_TRACE (("Writing string: %s to connection\n", s)); 
00073       if (writer) 
00074         { 
00075           writer->write(s); 
00076           writer->write("\n");
00077           writer->flush();
00078         }
00079     };
00080   void flush()
00081     { CCAFEReadLock lock(&mutex); if (writer) writer->flush(); };
00082   int getId()
00083     { return (connection->getId()); };
00084   char* getSourceName() // BUGBUG - really need a string class
00085     { CCAFEReadLock lock(&mutex); return connection->getName(); };
00086 
00088   // boolean equals(Object* other); pointer comparison (which is what the 
00089   // version of equals inherited from JCPN(Object) does) is sufficient, do not
00090   // need to override equals.
00091   
00092   // hash based on the id of the client 
00093   virtual int hashValue();
00094 };
00095 #endif
00096 #else
00097 extern int ccafe_no_linewiseclient;
00098 #endif // CCAFE_THREADS