Savarese Software Research Corporation
ws_lua/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_WS_LUA_SERVICE_H
00023 #define __SSRC_WSPR_WS_LUA_SERVICE_H
00024 
00025 #include <ssrc/wispers/ws_lua/ModuleContext.h>
00026 #include <ssrc/wispers/ws/service.h>
00027 #include <ssrc/wispers/utility/DynamicLibrary.h>
00028 #include <ssrc/wispers/lua/lua.h>
00029 
00030 #include <boost/ptr_container/ptr_unordered_map.hpp>
00031 
00032 __BEGIN_NS_SSRC_WSPR_WS_LUA
00033 
00034 using ssrc::spread::Message;
00035 using NS_SSRC_WISP_PROTOCOL::MessageInfo;
00036 using NS_SSRC_WSPR_WS::WebService;
00037 using NS_SSRC_WSPR_WS::WebServiceCall;
00038 using NS_SSRC_WSPR_WS::CallParameter;
00039 using NS_SSRC_WSPR_WS::PatternParameter;
00040 using NS_SSRC_WSPR_WS::parameter_sequence;
00041 using NS_SSRC_WSPR_UTILITY::LoadError;
00042 
00043 using std::string;
00044 
00045 struct ServiceInitializer {
00046   typedef std::vector<string> module_list;
00047   string module_dir;
00048   module_list modules;
00049 };
00050 
00051 struct CallEntry {
00052   int ref;
00053   bool one_way;
00054   bool requires_session;
00055   parameter_sequence parameters;
00056 
00057   CallEntry() { }
00058 
00059   CallEntry(const int ref) :
00060     ref(ref), one_way(false), requires_session(true), parameters(0)
00061   { }
00062 };
00063 
00064 typedef boost::ptr_unordered_map<string, CallEntry> call_map_type;
00065 
00066 class Service : public WebService {
00067   typedef WebService super;
00068   friend class ssrc::wisp::service::ServiceProtocolProcessor<packing_traits>;
00069 
00070   WISP_IMPORT(ServiceInitializer, module_list);
00071 
00072   struct ModuleEntry {
00073     int module_ref;
00074     int unload_ref;
00075 
00076     explicit
00077     ModuleEntry(int module_ref = LUA_NOREF, int unload_ref = LUA_NOREF) :
00078       module_ref(module_ref), unload_ref(unload_ref)
00079     { }
00080   };
00081 
00082   // For now, all modules share the same state.  We may decide to change that.
00083   Lua::lua_State *_lua_state;
00084   call_map_type _call_map;
00085   ModuleContext _context;
00086   typedef std::vector<ModuleEntry> module_entry_list;
00087   module_entry_list _modules;
00088 
00089   virtual void transition(State state);
00090 
00091   void reply_error(const string & error, const MessageInfo & msginfo) {
00092     MessageResponse response;
00093     response.set_error(error);
00094     _caller.reply<CallResponse>(msginfo.sender(), msginfo.token(), response, Message::FIFOSelfDiscard);
00095   }
00096 
00097   void do_call(const WebServiceCall & call, const MessageInfo & msginfo);
00098 
00099   void load_module(const string & module_dir, const string & module)
00100     SSRC_DECL_THROW(LoadError);
00101 
00102   void load_modules(const string & module_dir, const module_list & modules)
00103     SSRC_DECL_THROW(LoadError);
00104 
00105   void unload_module(const ModuleEntry & module);
00106 
00107   void unload_modules();
00108 
00109 public:
00110 
00111   Service(caller_type & caller, const ServiceInitializer & initializer)
00112     SSRC_DECL_THROW(LoadError) :
00113     super(caller),
00114     _lua_state(Lua::luaL_newstate()), _call_map(), _context(),
00115     _modules(0)
00116   {
00117     super::add_service_type(WSPR_WS_TYPE("lua"));
00118     _modules.reserve(initializer.modules.size());
00119     load_modules(initializer.module_dir, initializer.modules);
00120   }
00121 
00122   virtual ~Service() { unload_modules(); Lua::lua_close(_lua_state); }
00123 };
00124 
00125 __END_NS_SSRC_WSPR_WS_LUA
00126 
00127 #endif

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