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 enum HANDSHAKE_FLAGS 00060 { 00061 HANDSHAKE_SENDONLY = 0x80 00062 }; 00063 00064 00065 public: 00066 enum COMMUNICATION_MODE 00067 { 00068 MODE_BIDIRECTIONAL = 0, // bidirectional 00069 MODE_SENDONLY = 1 // unidirectional, no reception of bundles 00070 }; 00071 00080 Client(COMMUNICATION_MODE mode, string app, ibrcommon::tcpstream &stream); 00081 Client(string app, ibrcommon::tcpstream &stream); 00082 00086 virtual ~Client(); 00087 00091 void connect(); 00092 00096 bool isConnected(); 00097 00098 void close(); 00099 00103 virtual void eventShutdown(); 00104 virtual void eventTimeout(); 00105 virtual void eventError(); 00106 virtual void eventConnectionUp(const StreamContactHeader &header); 00107 virtual void eventConnectionDown(); 00108 virtual void eventBundleRefused(); 00109 virtual void eventBundleForwarded(); 00110 virtual void eventBundleAck(size_t ack); 00111 00112 dtn::api::Bundle getBundle(size_t timeout = 0); 00113 00114 protected: 00120 virtual void received(const dtn::streams::StreamContactHeader &h); 00121 virtual void received(const dtn::api::Bundle &b); 00122 00123 private: 00124 ibrcommon::tcpstream &_stream; 00125 COMMUNICATION_MODE _mode; 00126 string _app; 00127 bool _connected; 00128 bool _async; 00129 dtn::streams::StreamContactHeader _header; 00130 Client::AsyncReceiver _receiver; 00131 00132 ibrcommon::ThreadSafeQueue<dtn::api::Bundle> _inqueue; 00133 }; 00134 } 00135 } 00136 00137 #endif /* CLIENT_H_ */
1.6.3