pion-net  4.0.9
WebServer.hpp
1 // ------------------------------------------------------------------
2 // pion-net: a C++ framework for building lightweight HTTP interfaces
3 // ------------------------------------------------------------------
4 // Copyright (C) 2007-2008 Atomic Labs, Inc. (http://www.atomiclabs.com)
5 //
6 // Distributed under the Boost Software License, Version 1.0.
7 // See http://www.boost.org/LICENSE_1_0.txt
8 //
9 
10 #ifndef __PION_WEBSERVER_HEADER__
11 #define __PION_WEBSERVER_HEADER__
12 
13 #include <string>
14 #include <boost/asio.hpp>
15 #include <boost/bind.hpp>
16 #include <boost/shared_ptr.hpp>
17 #include <pion/PionConfig.hpp>
18 #include <pion/PionException.hpp>
19 #include <pion/PionPlugin.hpp>
20 #include <pion/PluginManager.hpp>
21 #include <pion/net/HTTPServer.hpp>
22 #include <pion/net/WebService.hpp>
23 
24 
25 namespace pion { // begin namespace pion
26 namespace net { // begin namespace net (Pion Network Library)
27 
31 class PION_NET_API WebServer :
32  public HTTPServer
33 {
34 
35 public:
36 
39  public:
40  ServiceNotFoundException(const std::string& resource)
41  : PionException("No web services are identified by the resource: ", resource) {}
42  };
43 
46  public:
47  ConfigNotFoundException(const std::string& file)
48  : PionException("Web service configuration file not found: ", file) {}
49  };
50 
53  public:
54  ConfigParsingException(const std::string& file)
55  : PionException("Unable to parse configuration file: ", file) {}
56  };
57 
60  public:
61  AuthConfigException(const std::string& error_msg)
62  : PionException("Error in web server authorization config: ", error_msg) {}
63  };
64 
67  public:
68  WebServiceException(const std::string& resource, const std::string& file)
69  : PionException(std::string("WebService (") + resource,
70  std::string("): ") + file)
71  {}
72  };
73 
74 
76  virtual ~WebServer() { clear(); }
77 
83  explicit WebServer(const unsigned int tcp_port = 0)
84  : HTTPServer(tcp_port)
85  {
86  setLogger(PION_GET_LOGGER("pion.net.WebServer"));
87  }
88 
94  explicit WebServer(const boost::asio::ip::tcp::endpoint& endpoint)
95  : HTTPServer(endpoint)
96  {
97  setLogger(PION_GET_LOGGER("pion.net.WebServer"));
98  }
99 
106  explicit WebServer(PionScheduler& scheduler, const unsigned int tcp_port = 0)
107  : HTTPServer(scheduler, tcp_port)
108  {
109  setLogger(PION_GET_LOGGER("pion.net.WebServer"));
110  }
111 
118  WebServer(PionScheduler& scheduler, const boost::asio::ip::tcp::endpoint& endpoint)
119  : HTTPServer(scheduler, endpoint)
120  {
121  setLogger(PION_GET_LOGGER("pion.net.WebServer"));
122  }
123 
130  void addService(const std::string& resource, WebService *service_ptr);
131 
139  void loadService(const std::string& resource, const std::string& service_name);
140 
148  void setServiceOption(const std::string& resource,
149  const std::string& name, const std::string& value);
150 
163  void loadServiceConfig(const std::string& config_name);
164 
166  virtual void clear(void) {
167  if (isListening()) stop();
168  m_services.clear();
170  }
171 
172 
173 protected:
174 
176  virtual void beforeStarting(void) {
177  // call the start() method for each web service associated with this server
178  try { m_services.run(boost::bind(&WebService::start, _1)); }
179  catch (std::exception& e) {
180  // catch exceptions thrown by services since their exceptions may be free'd
181  // from memory before they are caught
182  throw WebServiceException("[Startup]", e.what());
183  }
184  }
185 
187  virtual void afterStopping(void) {
188  // call the stop() method for each web service associated with this server
189  try { m_services.run(boost::bind(&WebService::stop, _1)); }
190  catch (std::exception& e) {
191  // catch exceptions thrown by services since their exceptions may be free'd
192  // from memory before they are caught
193  throw WebServiceException("[Shutdown]", e.what());
194  }
195  }
196 
197 
198 private:
199 
201  typedef PluginManager<WebService> WebServiceManager;
202 
203 
205  WebServiceManager m_services;
206 };
207 
208 
210 typedef boost::shared_ptr<WebServer> WebServerPtr;
211 
212 
213 } // end namespace net
214 } // end namespace pion
215 
216 #endif
exception used to propagate exceptions thrown by web services
Definition: WebServer.hpp:66
virtual ~WebServer()
default destructor
Definition: WebServer.hpp:76
WebServer(PionScheduler &scheduler, const unsigned int tcp_port=0)
Definition: WebServer.hpp:106
exception thrown if the web service configuration file cannot be found
Definition: WebServer.hpp:45
virtual void start(void)
called when the web service's server is starting
Definition: WebService.hpp:65
virtual void clear(void)
clears the collection of resources recognized by the HTTP server
Definition: HTTPServer.hpp:147
virtual void stop(void)
called when the web service's server is stopping
Definition: WebService.hpp:68
WebServer(const unsigned int tcp_port=0)
Definition: WebServer.hpp:83
WebServer(PionScheduler &scheduler, const boost::asio::ip::tcp::endpoint &endpoint)
Definition: WebServer.hpp:118
exception thrown if there is an error parsing the authorization config
Definition: WebServer.hpp:59
exception thrown if a web service cannot be found
Definition: WebServer.hpp:38
virtual void beforeStarting(void)
called before the TCP server starts listening for new connections
Definition: WebServer.hpp:176
virtual void afterStopping(void)
called after the TCP server has stopped listening for new connections
Definition: WebServer.hpp:187
the following enables use of the lock-free cache
exception thrown if the plug-in file cannot be opened
Definition: WebServer.hpp:52
virtual void clear(void)
clears all the web services that are currently configured
Definition: WebServer.hpp:166
WebServer(const boost::asio::ip::tcp::endpoint &endpoint)
Definition: WebServer.hpp:94