IBR-DTNSuite  0.10
ClientHandler.h
Go to the documentation of this file.
1 /*
2  * ClientHandler.h
3  *
4  * Copyright (C) 2011 IBR, TU Braunschweig
5  *
6  * Written-by: Johannes Morgenroth <morgenroth@ibr.cs.tu-bs.de>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21 
22 #ifndef CLIENTHANDLER_H_
23 #define CLIENTHANDLER_H_
24 
25 #include "api/Registration.h"
26 #include "core/EventReceiver.h"
27 #include "core/Node.h"
30 #include <string>
31 
32 namespace dtn
33 {
34  namespace api
35  {
36  class ApiServerInterface;
37  class ClientHandler;
38 
40  {
41  public:
42  virtual ~ProtocolHandler() = 0;
43 
44  virtual void run() = 0;
45  virtual void finally() = 0;
46  virtual void setup() {};
47  virtual void __cancellation() throw () = 0;
48 
49  protected:
50  ProtocolHandler(ClientHandler &client, ibrcommon::socketstream &stream);
52  ibrcommon::socketstream &_stream;
53  };
54 
55  class ClientHandler : public ibrcommon::DetachedThread
56  {
57  public:
59  {
60  API_STATUS_CONTINUE = 100,
61  API_STATUS_OK = 200,
62  API_STATUS_CREATED = 201,
63  API_STATUS_ACCEPTED = 202,
64  API_STATUS_FOUND = 302,
65  API_STATUS_BAD_REQUEST = 400,
66  API_STATUS_UNAUTHORIZED = 401,
67  API_STATUS_FORBIDDEN = 403,
68  API_STATUS_NOT_FOUND = 404,
69  API_STATUS_NOT_ALLOWED = 405,
70  API_STATUS_NOT_ACCEPTABLE = 406,
71  API_STATUS_CONFLICT = 409,
72  API_STATUS_INTERNAL_ERROR = 500,
73  API_STATUS_NOT_IMPLEMENTED = 501,
74  API_STATUS_SERVICE_UNAVAILABLE = 503,
75  API_STATUS_VERSION_NOT_SUPPORTED = 505
76  };
77 
79  virtual ~ClientHandler();
80 
81  Registration& getRegistration();
82  ApiServerInterface& getAPIServer();
83 
88  void switchRegistration(Registration &reg);
89 
90  protected:
91  void run() throw ();
92  void finally() throw ();
93  void setup() throw ();
94  void __cancellation() throw ();
95 
96  private:
97  void error(STATUS_CODES code, const std::string &msg);
98  void processCommand(const std::vector<std::string> &cmd);
99 
100  ApiServerInterface &_srv;
101  Registration *_registration;
102  ibrcommon::Mutex _write_lock;
103  ibrcommon::socketstream *_stream;
104  dtn::data::EID _endpoint;
105 
106  ProtocolHandler *_handler;
107  };
108 
110  {
111  public:
112  virtual ~ApiServerInterface() {};
113  virtual void connectionUp(ClientHandler *conn) = 0;
114  virtual void connectionDown(ClientHandler *conn) = 0;
115  virtual void freeRegistration(Registration &reg) = 0;
116  virtual Registration& getRegistration(const std::string &handle) = 0;
117  };
118  }
119 }
120 #endif /* CLIENTHANDLER_H_ */