00001 /* 00002 * ApiClient.h 00003 * 00004 * Created on: 24.06.2009 00005 * Author: morgenro 00006 */ 00007 00008 00009 #ifndef CLIENT_H_ 00010 #define CLIENT_H_ 00011 00012 #include "ibrdtn/api/Bundle.h" 00013 #include "ibrdtn/data/Bundle.h" 00014 #include "ibrdtn/streams/StreamConnection.h" 00015 #include "ibrcommon/net/tcpstream.h" 00016 #include "ibrcommon/thread/Mutex.h" 00017 #include "ibrcommon/thread/MutexLock.h" 00018 #include "ibrcommon/Exceptions.h" 00019 #include "ibrcommon/thread/ThreadSafeQueue.h" 00020 00021 using namespace dtn::data; 00022 using namespace dtn::streams; 00023 00024 namespace dtn 00025 { 00026 namespace api 00027 { 00028 class ConnectionException : public ibrcommon::Exception 00029 { 00030 public: 00031 ConnectionException(string what = "A connection error occurred.") throw() : ibrcommon::Exception(what) 00032 { 00033 }; 00034 }; 00035 00043 class Client : public StreamConnection, public StreamConnection::Callback 00044 { 00045 private: 00046 class AsyncReceiver : public ibrcommon::JoinableThread 00047 { 00048 public: 00049 AsyncReceiver(Client &client); 00050 virtual ~AsyncReceiver(); 00051 00052 void run(); 00053 00054 private: 00055 Client &_client; 00056 bool _shutdown; 00057 }; 00058 00059 00060 public: 00061 enum COMMUNICATION_MODE 00062 { 00063 MODE_BIDIRECTIONAL = 0, // bidirectional 00064 MODE_SENDONLY = 1 // unidirectional, no reception of bundles 00065 }; 00066 00075 Client(COMMUNICATION_MODE mode, string app, ibrcommon::tcpstream &stream); 00076 Client(string app, ibrcommon::tcpstream &stream); 00077 00081 virtual ~Client(); 00082 00086 void connect(); 00087 00091 bool isConnected(); 00092 00093 void close(); 00094 00098 virtual void eventShutdown(); 00099 virtual void eventTimeout(); 00100 virtual void eventError(); 00101 virtual void eventConnectionUp(const StreamContactHeader &header); 00102 virtual void eventConnectionDown(); 00103 virtual void eventBundleRefused(); 00104 virtual void eventBundleForwarded(); 00105 virtual void eventBundleAck(size_t ack); 00106 00107 dtn::api::Bundle getBundle(size_t timeout = 0); 00108 00109 protected: 00115 virtual void received(const dtn::streams::StreamContactHeader &h); 00116 virtual void received(const dtn::api::Bundle &b); 00117 00118 private: 00119 ibrcommon::tcpstream &_stream; 00120 COMMUNICATION_MODE _mode; 00121 string _app; 00122 bool _connected; 00123 bool _async; 00124 dtn::streams::StreamContactHeader _header; 00125 Client::AsyncReceiver _receiver; 00126 00127 ibrcommon::ThreadSafeQueue<dtn::api::Bundle> _inqueue; 00128 }; 00129 } 00130 } 00131 00132 #endif /* CLIENT_H_ */
1.5.6