IBR-DTNSuite  0.8
daemon/src/api/ClientHandler.h
Go to the documentation of this file.
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 void __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 &registration, ibrcommon::tcpstream *conn);
00065                         virtual ~ClientHandler();
00066 
00067                         Registration& getRegistration();
00068                         ApiServerInterface& getAPIServer();
00069 
00074                         void switchRegistration(Registration &reg);
00075 
00076                         void eventNodeAvailable(const dtn::core::Node &node);
00077                         void eventNodeUnavailable(const dtn::core::Node &node);
00078 
00079                 protected:
00080                         void run();
00081                         void finally();
00082                         void setup();
00083                         void __cancellation();
00084 
00085                 private:
00086                         void error(STATUS_CODES code, const std::string &msg);
00087                         void processCommand(const std::vector<std::string> &cmd);
00088 
00089                         ApiServerInterface &_srv;
00090                         Registration *_registration;
00091                         ibrcommon::Mutex _write_lock;
00092                         ibrcommon::tcpstream *_stream;
00093                         dtn::data::EID _endpoint;
00094 
00095                         ProtocolHandler *_handler;
00096                 };
00097 
00098                 class ApiServerInterface
00099                 {
00100                 public:
00101                         virtual ~ApiServerInterface() {};
00102                         virtual void processIncomingBundle(const dtn::data::EID &source, dtn::data::Bundle &bundle) = 0;
00103                         virtual void connectionUp(ClientHandler *conn) = 0;
00104                         virtual void connectionDown(ClientHandler *conn) = 0;
00105                         virtual void freeRegistration(Registration &reg) = 0;
00106                         virtual Registration& getRegistration(const std::string &handle) = 0;
00107                 };
00108         }
00109 }
00110 #endif /* CLIENTHANDLER_H_ */