IBR-DTNSuite
0.8
|
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 { 00031 class ConnectionException : public ibrcommon::Exception 00032 { 00033 public: 00034 ConnectionException(string what = "A connection error occurred.") throw() : ibrcommon::Exception(what) 00035 { 00036 }; 00037 }; 00038 00042 class ConnectionTimeoutException : public ConnectionException 00043 { 00044 public: 00045 ConnectionTimeoutException(string what = "Timeout.") throw() : ConnectionException(what) 00046 { 00047 }; 00048 }; 00049 00053 class ConnectionAbortedException : public ConnectionException 00054 { 00055 public: 00056 ConnectionAbortedException(string what = "Aborted.") throw() : ConnectionException(what) 00057 { 00058 }; 00059 }; 00060 00070 class Client : public StreamConnection, public StreamConnection::Callback 00071 { 00072 private: 00080 class AsyncReceiver : public ibrcommon::JoinableThread 00081 { 00082 public: 00088 AsyncReceiver(Client &client); 00089 00094 virtual ~AsyncReceiver(); 00095 00096 protected: 00102 void run(); 00103 00108 void __cancellation(); 00109 00110 private: 00111 // member variable for the reference to the client object 00112 Client &_client; 00113 00114 bool _running; 00115 }; 00116 00120 enum HANDSHAKE_FLAGS 00121 { 00122 HANDSHAKE_NONE = 0x0, 00123 HANDSHAKE_SENDONLY = 0x80 00124 }; 00125 00126 00127 public: 00131 enum COMMUNICATION_MODE 00132 { 00133 MODE_BIDIRECTIONAL = 0, 00134 MODE_SENDONLY = 1 00135 }; 00136 00148 Client(const std::string &app, ibrcommon::tcpstream &stream, const COMMUNICATION_MODE mode = MODE_BIDIRECTIONAL); 00149 Client(const std::string &app, const dtn::data::EID &group, ibrcommon::tcpstream &stream, const COMMUNICATION_MODE mode = MODE_BIDIRECTIONAL); 00150 00154 virtual ~Client(); 00155 00159 void connect(); 00160 00165 void close(); 00166 00170 void abort(); 00171 00177 virtual void eventConnectionDown(); 00178 00184 virtual void eventBundleAck(size_t ack); 00185 00190 virtual void eventShutdown(StreamConnection::ConnectionShutdownCases) {}; 00191 00196 virtual void eventTimeout() {}; 00197 00202 virtual void eventError() {}; 00203 00209 virtual void eventConnectionUp(const StreamContactHeader&) {}; 00210 00215 virtual void eventBundleRefused() {}; 00216 00221 virtual void eventBundleForwarded() {}; 00222 00230 dtn::api::Bundle getBundle(size_t timeout = 0) throw (ConnectionException); 00231 00232 // public variable 00233 size_t lastack; 00234 00235 protected: 00241 virtual void received(const dtn::streams::StreamContactHeader&) {}; 00242 00248 virtual void received(const dtn::api::Bundle &b); 00249 00250 private: 00251 // tcp stream reference to send/receive data to the daemon 00252 ibrcommon::tcpstream &_stream; 00253 00254 // communication mode flags 00255 COMMUNICATION_MODE _mode; 00256 00257 // own application suffix 00258 std::string _app; 00259 00260 // group to join 00261 dtn::data::EID _group; 00262 00263 // The asynchronous receiver thread which receives incoming bundles 00264 Client::AsyncReceiver _receiver; 00265 00266 // the queue for incoming bundles, when used in synchronous mode 00267 ibrcommon::Queue<dtn::api::Bundle> _inqueue; 00268 }; 00269 } 00270 } 00271 00272 #endif /* CLIENT_H_ */