Web Wispers 1.2.2 C++ Unit Test Coverage
Current view: top level - ssrc/wispers/ws_lua - service.h (source / functions) Hit Total Coverage
Test: Web Wispers 1.2.2 C++ Unit Tests Lines: 16 22 72.7 %
Date: 2012-04-09 Functions: 7 10 70.0 %
Branches: 19 46 41.3 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * Copyright 2006-2009 Savarese Software Research Corporation
       3                 :            :  *
       4                 :            :  * Licensed under the Apache License, Version 2.0 (the "License");
       5                 :            :  * you may not use this file except in compliance with the License.
       6                 :            :  * You may obtain a copy of the License at
       7                 :            :  *
       8                 :            :  *     https://www.savarese.com/software/ApacheLicense-2.0
       9                 :            :  *
      10                 :            :  * Unless required by applicable law or agreed to in writing, software
      11                 :            :  * distributed under the License is distributed on an "AS IS" BASIS,
      12                 :            :  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      13                 :            :  * See the License for the specific language governing permissions and
      14                 :            :  * limitations under the License.
      15                 :            :  */
      16                 :            : 
      17                 :            : /**
      18                 :            :  * @file
      19                 :            :  * This header defines the Lua Web service class.
      20                 :            :  */
      21                 :            : 
      22                 :            : #ifndef __SSRC_WSPR_WS_LUA_SERVICE_H
      23                 :            : #define __SSRC_WSPR_WS_LUA_SERVICE_H
      24                 :            : 
      25                 :            : #include <ssrc/wispers/ws_lua/ModuleContext.h>
      26                 :            : #include <ssrc/wispers/ws/service.h>
      27                 :            : #include <ssrc/wispers/utility/DynamicLibrary.h>
      28                 :            : #include <ssrc/wispers/lua/lua.h>
      29                 :            : 
      30                 :            : #include <boost/ptr_container/ptr_unordered_map.hpp>
      31                 :            : 
      32                 :            : __BEGIN_NS_SSRC_WSPR_WS_LUA
      33                 :            : 
      34                 :            : using ssrc::spread::Message;
      35                 :            : using NS_SSRC_WISP_PROTOCOL::MessageInfo;
      36                 :            : using NS_SSRC_WSPR_WS::WebService;
      37                 :            : using NS_SSRC_WSPR_WS::WebServiceCall;
      38                 :            : using NS_SSRC_WSPR_WS::CallParameter;
      39                 :            : using NS_SSRC_WSPR_WS::PatternParameter;
      40                 :            : using NS_SSRC_WSPR_WS::parameter_sequence;
      41                 :            : using NS_SSRC_WSPR_UTILITY::LoadError;
      42                 :            : 
      43                 :            : using std::string;
      44                 :            : 
      45   [ +  -  +  - ]:          2 : struct ServiceInitializer {
      46                 :            :   typedef std::vector<string> module_list;
      47                 :            :   string module_dir;
      48                 :            :   module_list modules;
      49                 :            : };
      50                 :            : 
      51                 :          2 : struct CallEntry {
      52                 :            :   int ref;
      53                 :            :   bool one_way;
      54                 :            :   bool requires_session;
      55                 :            :   parameter_sequence parameters;
      56                 :            : 
      57                 :            :   CallEntry() { }
      58                 :            : 
      59                 :          2 :   CallEntry(const int ref) :
      60         [ +  - ]:          2 :     ref(ref), one_way(false), requires_session(true), parameters(0)
      61                 :          2 :   { }
      62                 :            : };
      63                 :            : 
      64                 :            : typedef boost::ptr_unordered_map<string, CallEntry> call_map_type;
      65                 :            : 
      66                 :            : class Service : public WebService {
      67                 :            :   typedef WebService super;
      68                 :            :   friend class ssrc::wisp::service::ServiceProtocolProcessor<packing_traits>;
      69                 :            : 
      70                 :            :   WISP_IMPORT(ServiceInitializer, module_list);
      71                 :            : 
      72                 :          0 :   struct ModuleEntry {
      73                 :            :     int module_ref;
      74                 :            :     int unload_ref;
      75                 :            : 
      76                 :            :     explicit
      77                 :          1 :     ModuleEntry(int module_ref = LUA_NOREF, int unload_ref = LUA_NOREF) :
      78                 :          1 :       module_ref(module_ref), unload_ref(unload_ref)
      79                 :          1 :     { }
      80                 :            :   };
      81                 :            : 
      82                 :            :   // For now, all modules share the same state.  We may decide to change that.
      83                 :            :   Lua::lua_State *_lua_state;
      84                 :            :   call_map_type _call_map;
      85                 :            :   ModuleContext _context;
      86                 :            :   typedef std::vector<ModuleEntry> module_entry_list;
      87                 :            :   module_entry_list _modules;
      88                 :            : 
      89                 :            :   virtual void transition(State state);
      90                 :            : 
      91                 :          0 :   void reply_error(const string & error, const MessageInfo & msginfo) {
      92   [ #  #  #  # ]:          0 :     MessageResponse response;
      93         [ #  # ]:          0 :     response.set_error(error);
      94         [ #  # ]:          0 :     _caller.reply<CallResponse>(msginfo.sender(), msginfo.token(), response, Message::FIFOSelfDiscard);
      95                 :          0 :   }
      96                 :            : 
      97                 :            :   void do_call(const WebServiceCall & call, const MessageInfo & msginfo);
      98                 :            : 
      99                 :            :   void load_module(const string & module_dir, const string & module)
     100                 :            :     SSRC_DECL_THROW(LoadError);
     101                 :            : 
     102                 :            :   void load_modules(const string & module_dir, const module_list & modules)
     103                 :            :     SSRC_DECL_THROW(LoadError);
     104                 :            : 
     105                 :            :   void unload_module(const ModuleEntry & module);
     106                 :            : 
     107                 :            :   void unload_modules();
     108                 :            : 
     109                 :            : public:
     110                 :            : 
     111                 :          1 :   Service(caller_type & caller, const ServiceInitializer & initializer)
     112                 :            :     SSRC_DECL_THROW(LoadError) :
     113                 :            :     super(caller),
     114         [ +  - ]:          1 :     _lua_state(Lua::luaL_newstate()), _call_map(), _context(),
     115   [ +  -  +  -  :          2 :     _modules(0)
                   +  - ]
     116                 :            :   {
     117   [ +  -  +  -  :          1 :     super::add_service_type(WSPR_WS_TYPE("lua"));
                   +  - ]
     118   [ +  -  +  - ]:          1 :     _modules.reserve(initializer.modules.size());
     119         [ +  - ]:          1 :     load_modules(initializer.module_dir, initializer.modules);
     120                 :          1 :   }
     121                 :            : 
     122   [ +  -  +  -  :          1 :   virtual ~Service() { unload_modules(); Lua::lua_close(_lua_state); }
          +  -  +  -  +  
                -  -  + ]
     123                 :            : };
     124                 :            : 
     125                 :            : __END_NS_SSRC_WSPR_WS_LUA
     126                 :            : 
     127                 :            : #endif