|
IBR-DTNSuite 0.6
|
00001 /* 00002 * ClientHandler.h 00003 * 00004 * Created on: 24.06.2009 00005 * Author: morgenro 00006 */ 00007 00008 #ifndef CLIENTHANDLER_H_ 00009 #define CLIENTHANDLER_H_ 00010 00011 #include "api/Registration.h" 00012 #include "core/EventReceiver.h" 00013 #include "core/Node.h" 00014 #include <ibrcommon/thread/Thread.h> 00015 #include <ibrcommon/net/tcpstream.h> 00016 #include <string> 00017 00018 namespace dtn 00019 { 00020 namespace api 00021 { 00022 class ApiServerInterface; 00023 class ClientHandler; 00024 00025 class ProtocolHandler 00026 { 00027 public: 00028 virtual ~ProtocolHandler() = 0; 00029 00030 virtual void run() = 0; 00031 virtual void finally() = 0; 00032 virtual void setup() {}; 00033 virtual bool __cancellation() = 0; 00034 00035 protected: 00036 ProtocolHandler(ClientHandler &client, ibrcommon::tcpstream &stream); 00037 ClientHandler &_client; 00038 ibrcommon::tcpstream &_stream; 00039 }; 00040 00041 class ClientHandler : public ibrcommon::DetachedThread 00042 { 00043 public: 00044 enum STATUS_CODES 00045 { 00046 API_STATUS_CONTINUE = 100, 00047 API_STATUS_OK = 200, 00048 API_STATUS_CREATED = 201, 00049 API_STATUS_ACCEPTED = 202, 00050 API_STATUS_FOUND = 302, 00051 API_STATUS_BAD_REQUEST = 400, 00052 API_STATUS_UNAUTHORIZED = 401, 00053 API_STATUS_FORBIDDEN = 403, 00054 API_STATUS_NOT_FOUND = 404, 00055 API_STATUS_NOT_ALLOWED = 405, 00056 API_STATUS_NOT_ACCEPTABLE = 406, 00057 API_STATUS_CONFLICT = 409, 00058 API_STATUS_INTERNAL_ERROR = 500, 00059 API_STATUS_NOT_IMPLEMENTED = 501, 00060 API_STATUS_SERVICE_UNAVAILABLE = 503, 00061 API_STATUS_VERSION_NOT_SUPPORTED = 505 00062 }; 00063 00064 ClientHandler(ApiServerInterface &srv, Registration ®istration, ibrcommon::tcpstream *conn); 00065 virtual ~ClientHandler(); 00066 00067 Registration& getRegistration(); 00068 ApiServerInterface& getAPIServer(); 00069 00070 void eventNodeAvailable(const dtn::core::Node &node); 00071 void eventNodeUnavailable(const dtn::core::Node &node); 00072 00073 protected: 00074 void run(); 00075 void finally(); 00076 void setup(); 00077 bool __cancellation(); 00078 00079 private: 00080 void error(STATUS_CODES code, const std::string &msg); 00081 void processCommand(const std::vector<std::string> &cmd); 00082 00083 ApiServerInterface &_srv; 00084 Registration &_registration; 00085 ibrcommon::Mutex _write_lock; 00086 ibrcommon::tcpstream *_stream; 00087 dtn::data::EID _endpoint; 00088 00089 ProtocolHandler *_handler; 00090 }; 00091 00092 class ApiServerInterface 00093 { 00094 public: 00095 virtual void processIncomingBundle(const dtn::data::EID &source, dtn::data::Bundle &bundle) = 0; 00096 virtual void connectionUp(ClientHandler *conn) = 0; 00097 virtual void connectionDown(ClientHandler *conn) = 0; 00098 virtual void freeRegistration(Registration ®) = 0; 00099 }; 00100 } 00101 } 00102 #endif /* CLIENTHANDLER_H_ */