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/Queue.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 00036 class ConnectionTimeoutException : public ibrcommon::Exception 00037 { 00038 public: 00039 ConnectionTimeoutException(string what = "Timeout.") throw() : ibrcommon::Exception(what) 00040 { 00041 }; 00042 }; 00043 00044 class ConnectionAbortedException : public ibrcommon::Exception 00045 { 00046 public: 00047 ConnectionAbortedException(string what = "Aborted.") throw() : ibrcommon::Exception(what) 00048 { 00049 }; 00050 }; 00051 00059 class Client : public StreamConnection, public StreamConnection::Callback 00060 { 00061 private: 00062 class AsyncReceiver : public ibrcommon::JoinableThread 00063 { 00064 public: 00065 AsyncReceiver(Client &client); 00066 virtual ~AsyncReceiver(); 00067 00068 protected: 00069 void run(); 00070 bool __cancellation(); 00071 00072 private: 00073 Client &_client; 00074 }; 00075 00076 enum HANDSHAKE_FLAGS 00077 { 00078 HANDSHAKE_SENDONLY = 0x80 00079 }; 00080 00081 00082 public: 00083 enum COMMUNICATION_MODE 00084 { 00085 MODE_BIDIRECTIONAL = 0, // bidirectional 00086 MODE_SENDONLY = 1 // unidirectional, no reception of bundles 00087 }; 00088 00097 Client(COMMUNICATION_MODE mode, string app, ibrcommon::tcpstream &stream); 00098 Client(string app, ibrcommon::tcpstream &stream); 00099 00103 virtual ~Client(); 00104 00108 void connect(); 00109 00113 bool isConnected(); 00114 00115 void close(); 00116 00120 virtual void eventShutdown(StreamConnection::ConnectionShutdownCases csc); 00121 virtual void eventTimeout(); 00122 virtual void eventError(); 00123 virtual void eventConnectionUp(const StreamContactHeader &header); 00124 virtual void eventConnectionDown(); 00125 virtual void eventBundleRefused(); 00126 virtual void eventBundleForwarded(); 00127 virtual void eventBundleAck(size_t ack); 00128 00129 dtn::api::Bundle getBundle(size_t timeout = 0); 00130 00131 size_t lastack; 00132 00133 protected: 00139 virtual void received(const dtn::streams::StreamContactHeader &h); 00140 virtual void received(const dtn::api::Bundle &b); 00141 00142 private: 00143 ibrcommon::tcpstream &_stream; 00144 COMMUNICATION_MODE _mode; 00145 string _app; 00146 bool _connected; 00147 bool _async; 00148 dtn::streams::StreamContactHeader _header; 00149 Client::AsyncReceiver _receiver; 00150 00151 ibrcommon::Queue<dtn::api::Bundle> _inqueue; 00152 }; 00153 } 00154 } 00155 00156 #endif /* CLIENT_H_ */
1.7.1