KISSCPP
a C++ library for rapid application development
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
request_router.hpp
Go to the documentation of this file.
1 // File : request_router.hpp
2 // Author: Dirk J. Botha <bothadj@gmail.com>
3 //
4 // This file is part of kisscpp library.
5 //
6 // The kisscpp library is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU Lesser General Public License as published by
8 // the Free Software Foundation, either version 3 of the License, or
9 // (at your option) any later version.
10 //
11 // The kisscpp library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public License
17 // along with the kisscpp library. If not, see <http://www.gnu.org/licenses/>.
18 
19 #ifndef _SERVER_REQUEST_ROUTER_HPP
20 #define _SERVER_REQUEST_ROUTER_HPP
21 
22 #include <iostream>
23 
24 #include <string>
25 #include <map>
26 #include <boost/noncopyable.hpp>
27 #include <boost/shared_ptr.hpp>
28 #include "boost_ptree.hpp"
29 #include "request_handler.hpp"
30 #include "logstream.hpp"
31 #include "request_status.hpp"
32 
33 namespace kisscpp
34 {
35  typedef std::map<std::string, RequestHandlerPtr> requestHandlerMapType;
36  typedef requestHandlerMapType::iterator requestHandlerMapTypeIter;
37  typedef std::map<std::string, std::string> requestHandlerInfoList;
38  typedef requestHandlerInfoList::iterator requestHandlerInfoListIter;
39  typedef boost::shared_ptr<requestHandlerInfoList> sharedRequestHandlerInfoList;
40 
41  //--------------------------------------------------------------------------------
42  // The router for all incoming requests.
43  class RequestRouter : private boost::noncopyable
44  {
45  public:
46  explicit RequestRouter() {};
47 
48  //--------------------------------------------------------------------------------
50  {
51  LogStream log(__PRETTY_FUNCTION__);
52  requestHandlerMap[_handler->commandId()] = _handler;
53  }
54 
55  // Handle a request and produce a reply.
56  //--------------------------------------------------------------------------------
57  //SharedPtree route_request(const SharedPtree request)
58  void route_request(const BoostPtree &request, BoostPtree &response)
59  {
60  LogStream log(__PRETTY_FUNCTION__);
61  try {
62  std::string command = request.get<std::string>("kcm-cmd");
63 
64  if(requestHandlerMap.find(command) != requestHandlerMap.end()) {
65  try {
66  requestHandlerMap[command]->run(request, response);
67  } catch (boost::property_tree::ptree_bad_path &e) {
68  response.put("kcm-sts", RQST_MISSING_PARAMETER);
69  response.put("kcm-erm", e.what());
70  }
71  } else {
72  response.put("kcm-sts", RQST_COMMAND_NOT_SUPPORTED);
73  response.put("kcm-erm","Unrecognized command: " + command);
74  }
75  } catch (boost::property_tree::ptree_bad_path &e) {
76  response.put("kcm-sts", RQST_MISSING_PARAMETER);
77  response.put("kcm-erm", e.what());
78  }
79  }
80 
81  //--------------------------------------------------------------------------------
83  {
85 
86  retval.reset(new requestHandlerInfoList());
87 
88  for(requestHandlerMapTypeIter itr = requestHandlerMap.begin(); itr != requestHandlerMap.end(); ++itr) {
89  (*retval)[itr->first] = itr->second->getDescription();
90  }
91 
92  return retval;
93  }
94 
95  private:
96  requestHandlerMapType requestHandlerMap;
97  };
98 
99  typedef boost::shared_ptr<RequestRouter> sharedRequestRouter;
100 }
101 
102 #endif
No matching identifier for the supplied value of kcm-cmd was found.
Definition: request_status.hpp:34
Definition: request_router.hpp:43
sharedRequestHandlerInfoList getHandlerDescriptions()
Definition: request_router.hpp:82
requestHandlerInfoList::iterator requestHandlerInfoListIter
Definition: request_router.hpp:38
used to indicate the absence of an expected parameter
Definition: request_status.hpp:36
std::map< std::string, RequestHandlerPtr > requestHandlerMapType
Definition: request_router.hpp:35
boost::property_tree::ptree BoostPtree
Definition: boost_ptree.hpp:31
boost::shared_ptr< RequestHandler > RequestHandlerPtr
Definition: request_handler.hpp:54
boost::shared_ptr< requestHandlerInfoList > sharedRequestHandlerInfoList
Definition: request_router.hpp:39
requestHandlerMapType::iterator requestHandlerMapTypeIter
Definition: request_router.hpp:36
Definition: logstream.hpp:145
std::map< std::string, std::string > requestHandlerInfoList
Definition: request_router.hpp:37
RequestRouter()
Definition: request_router.hpp:46
boost::shared_ptr< RequestRouter > sharedRequestRouter
Definition: request_router.hpp:99
void route_request(const BoostPtree &request, BoostPtree &response)
Definition: request_router.hpp:58
void register_handler(RequestHandlerPtr _handler)
Definition: request_router.hpp:49