00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #ifndef __PION_WEBSERVER_HEADER__
00011 #define __PION_WEBSERVER_HEADER__
00012
00013 #include <string>
00014 #include <boost/asio.hpp>
00015 #include <boost/bind.hpp>
00016 #include <boost/shared_ptr.hpp>
00017 #include <pion/PionConfig.hpp>
00018 #include <pion/PionException.hpp>
00019 #include <pion/PionPlugin.hpp>
00020 #include <pion/PluginManager.hpp>
00021 #include <pion/net/HTTPServer.hpp>
00022 #include <pion/net/WebService.hpp>
00023
00024
00025 namespace pion {
00026 namespace net {
00027
00031 class PION_NET_API WebServer :
00032 public HTTPServer
00033 {
00034
00035 public:
00036
00038 class ServiceNotFoundException : public PionException {
00039 public:
00040 ServiceNotFoundException(const std::string& resource)
00041 : PionException("No web services are identified by the resource: ", resource) {}
00042 };
00043
00045 class ConfigNotFoundException : public PionException {
00046 public:
00047 ConfigNotFoundException(const std::string& file)
00048 : PionException("Web service configuration file not found: ", file) {}
00049 };
00050
00052 class ConfigParsingException : public PionException {
00053 public:
00054 ConfigParsingException(const std::string& file)
00055 : PionException("Unable to parse configuration file: ", file) {}
00056 };
00057
00059 class AuthConfigException : public PionException {
00060 public:
00061 AuthConfigException(const std::string& error_msg)
00062 : PionException("Error in web server authorization config: ", error_msg) {}
00063 };
00064
00066 class WebServiceException : public PionException {
00067 public:
00068 WebServiceException(const std::string& resource, const std::string& file)
00069 : PionException(std::string("WebService (") + resource,
00070 std::string("): ") + file)
00071 {}
00072 };
00073
00074
00076 virtual ~WebServer() { clear(); }
00077
00083 explicit WebServer(const unsigned int tcp_port = 0)
00084 : HTTPServer(tcp_port)
00085 {
00086 setLogger(PION_GET_LOGGER("pion.net.WebServer"));
00087 }
00088
00094 explicit WebServer(const boost::asio::ip::tcp::endpoint& endpoint)
00095 : HTTPServer(endpoint)
00096 {
00097 setLogger(PION_GET_LOGGER("pion.net.WebServer"));
00098 }
00099
00106 explicit WebServer(PionScheduler& scheduler, const unsigned int tcp_port = 0)
00107 : HTTPServer(scheduler, tcp_port)
00108 {
00109 setLogger(PION_GET_LOGGER("pion.net.WebServer"));
00110 }
00111
00118 WebServer(PionScheduler& scheduler, const boost::asio::ip::tcp::endpoint& endpoint)
00119 : HTTPServer(scheduler, endpoint)
00120 {
00121 setLogger(PION_GET_LOGGER("pion.net.WebServer"));
00122 }
00123
00130 void addService(const std::string& resource, WebService *service_ptr);
00131
00139 void loadService(const std::string& resource, const std::string& service_name);
00140
00148 void setServiceOption(const std::string& resource,
00149 const std::string& name, const std::string& value);
00150
00163 void loadServiceConfig(const std::string& config_name);
00164
00166 virtual void clear(void) {
00167 if (isListening()) stop();
00168 m_services.clear();
00169 HTTPServer::clear();
00170 }
00171
00172
00173 protected:
00174
00176 virtual void beforeStarting(void) {
00177
00178 try { m_services.run(boost::bind(&WebService::start, _1)); }
00179 catch (std::exception& e) {
00180
00181
00182 throw WebServiceException("[Startup]", e.what());
00183 }
00184 }
00185
00187 virtual void afterStopping(void) {
00188
00189 try { m_services.run(boost::bind(&WebService::stop, _1)); }
00190 catch (std::exception& e) {
00191
00192
00193 throw WebServiceException("[Shutdown]", e.what());
00194 }
00195 }
00196
00197
00198 private:
00199
00201 typedef PluginManager<WebService> WebServiceManager;
00202
00203
00205 WebServiceManager m_services;
00206 };
00207
00208
00210 typedef boost::shared_ptr<WebServer> WebServerPtr;
00211
00212
00213 }
00214 }
00215
00216 #endif