Savarese Software Research Corporation
relay/service.h
Go to the documentation of this file.
00001 /*
00002  * Copyright 2006-2009 Savarese Software Research Corporation
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     https://www.savarese.com/software/ApacheLicense-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00022 #ifndef __SSRC_WSPR_RELAY_SERVICE_H
00023 #define __SSRC_WSPR_RELAY_SERVICE_H
00024 
00025 #include <ssrc/wispers/ws/protocol.h>
00026 #include <ssrc/wispers/ws/ActionConfig.h>
00027 #include <ssrc/wispers/fcgi/parse_query.h>
00028 
00029 #include <ssrc/wispers/renderer/service.h>
00030 #include <ssrc/wispers/relay/protocol.h>
00031 #include <ssrc/wispers/lua/Properties.h>
00032 
00033 __BEGIN_NS_SSRC_WSPR_RELAY
00034 
00035 using std::string;
00036 using boost::filesystem::path;
00037 using NS_SSRC_WISP_PROTOCOL::MessageInfo;
00038 using NS_SSRC_WSPR_UTILITY::LoadError;
00039 using NS_SSRC_WSPR_FCGI::parameter_map;
00040 using NS_SSRC_WSPR_FCGI::parse_post_parameters;
00041 using NS_SSRC_WSPR_FCGI::parse_get_parameters;
00042 using NS_SSRC_WSPR_FCGI::HTTPRequestMethod;
00043 using NS_SSRC_WSPR_SESSION::SessionProtocol;
00044 using NS_SSRC_WSPR_WS::ActionConfig;
00045 using NS_SSRC_WSPR_LUA::PropertiesToTable;
00046 
00047 struct RelayInitializer : public renderer::RendererInitializer {
00048   string service_config_dir;
00049   string post_leaf_pattern;
00050 };
00051 
00056 class Relay : public renderer::Renderer {
00057   friend class NS_SSRC_WISP_SERVICE::ServiceProtocolProcessor<packing_traits>;
00058 
00059 public:
00060   typedef boost::shared_ptr<ActionConfig> action_config_ptr;
00061 
00062 private:
00063   typedef renderer::Renderer super;
00064   typedef RelayProtocol protocol_traits;
00065   typedef NS_SSRC_WSPR_WS::WebServiceProtocol ws_protocol;
00066 
00067   typedef std::unordered_map<string, action_config_ptr> action_config_map;
00068 
00069   WISP_IMPORT(ws_protocol, MessageResponse);
00070   WISP_IMPORT(ws_protocol, CallOneWay);
00071   WISP_IMPORT(ws_protocol, CallTwoWay);
00072 
00073   WISP_IMPORT(SessionProtocol, MessageSingleQueryResult);
00074   WISP_IMPORT(SessionProtocol, CallSingleQueryByDefault);
00075   WISP_IMPORT(SessionProtocol, CallCreateSession);
00076   WISP_IMPORT(SessionProtocol, CallGetSession);
00077 
00078   WISP_IMPORT(protocol_traits, MessageClearCaches);
00079 
00080   path _service_conf_dir;
00081   boost::regex _post_path_pattern;
00082   action_config_map _action_config_cache;
00083   PropertiesToTable _to_table;
00084 
00085   void http_post(const request_ptr & request, response_ptr & response,
00086                  parameter_map && parameters);
00087 
00088   inline const action_config_ptr get_action(const string & service_name,
00089                                             const string & action_name);
00090 
00091   void clear_caches();
00092 
00093   void process_request(const MessageClearCaches & msg,
00094                        const MessageInfo &);
00095 
00096   virtual void process_request(const MessageReloadConfig & msg,
00097                                const MessageInfo & msginfo);
00098 
00099 protected:
00100 
00101   virtual void set_lua_state(Lua::lua_State *state) {
00102     super::set_lua_state(state);
00103     _to_table.reset(state);
00104   }
00105 
00106 public:
00107 
00108   struct FinishPostContext {
00109     const HTTPRequestMethod request_method;
00110     const Relay::request_ptr request;
00111     Relay::response_ptr response;
00112     path parent_path;
00113     const action_config_ptr action;
00114     parameter_map parameters;
00115 
00116     FinishPostContext(const HTTPRequestMethod request_method,
00117                       const Relay::request_ptr & request,
00118                       Relay::response_ptr & response,
00119                       const path & parent_path,
00120                       const action_config_ptr & action,
00121                       parameter_map && parameters) :
00122       request_method(request_method), request(request), response(response),
00123       parent_path(parent_path), action(action),
00124       parameters(std::move(parameters))
00125     { }
00126   };
00127 
00128   Relay(caller_type & caller, const RelayInitializer & initializer)
00129     SSRC_DECL_THROW(LoadError, std::invalid_argument);
00130 
00131   virtual ~Relay() { }
00132 
00133   virtual void http_post(const request_ptr & request, response_ptr & response) {
00134     parameter_map parameters;
00135     parse_post_parameters(*request, parameters);
00136     http_post(request, response, std::move(parameters));
00137   }
00138 
00150   virtual void http_get(const request_ptr & request, response_ptr & response) {
00151     parameter_map parameters;
00152     parse_get_parameters(*request, parameters);
00153     if(parameters.empty()) {
00154       super::http_get(request, response);
00155     } else {
00156       http_post(request, response, std::move(parameters));
00157     }
00158   }
00159 
00160   void continue_post(FinishPostContext & context,
00161                      const MessageSingleQueryResult & session_result);
00162 
00163 
00164   void finish_post(FinishPostContext & context,
00165                    const MessageResponse & result);
00166 };
00167 
00168 __END_NS_SSRC_WSPR_RELAY
00169 
00170 #endif

Savarese Software Research Corporation
Copyright © 2006-2011 Savarese Software Research Corporation. All rights reserved.