|
IBR-DTNSuite 0.6
|
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 bool __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 00153 virtual ~Client(); 00154 00158 void connect(); 00159 00164 void close(); 00165 00171 virtual void eventConnectionDown(); 00172 00178 virtual void eventBundleAck(size_t ack); 00179 00184 virtual void eventShutdown(StreamConnection::ConnectionShutdownCases) {}; 00185 00190 virtual void eventTimeout() {}; 00191 00196 virtual void eventError() {}; 00197 00203 virtual void eventConnectionUp(const StreamContactHeader&) {}; 00204 00209 virtual void eventBundleRefused() {}; 00210 00215 virtual void eventBundleForwarded() {}; 00216 00224 dtn::api::Bundle getBundle(size_t timeout = 0) throw (ConnectionException); 00225 00226 // public variable 00227 size_t lastack; 00228 00229 protected: 00235 virtual void received(const dtn::streams::StreamContactHeader&) {}; 00236 00242 virtual void received(const dtn::api::Bundle &b); 00243 00244 private: 00245 // tcp stream reference to send/receive data to the daemon 00246 ibrcommon::tcpstream &_stream; 00247 00248 // communication mode flags 00249 COMMUNICATION_MODE _mode; 00250 00251 // own application suffix 00252 string _app; 00253 00254 // The asynchronous receiver thread which receives incoming bundles 00255 Client::AsyncReceiver _receiver; 00256 00257 // the queue for incoming bundles, when used in synchronous mode 00258 ibrcommon::Queue<dtn::api::Bundle> _inqueue; 00259 }; 00260 } 00261 } 00262 00263 #endif /* CLIENT_H_ */