Changeset 3430

Show
Ignore:
Timestamp:
02/19/10 17:16:17 (5 months ago)
Author:
mgray
Message:

Fleshed out implementation of scriptable_generic_service and refactored AppServer? interfaces to use it correctly (and vice versa)

Location:
Enterprise/branches/0075_TR_SCRIPTING/AppServer
Files:
4 modified

Legend:

Unmodified
Added
Removed
  • Enterprise/branches/0075_TR_SCRIPTING/AppServer/I_ApplicationService.hpp

    r2358 r3430  
    2727#include "Configuration.hpp" 
    2828 
     29#include <Zen/Core/Memory/managed_ptr.hpp> 
     30 
     31#include <Zen/Core/Plugins/I_StartupShutdownParticipant.hpp> 
     32 
    2933#include <Zen/Enterprise/AppServer/I_RequestHandler.hpp> 
    30  
    31 #include <Zen/Core/Memory/managed_ptr.hpp> 
    3234 
    3335//-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~ 
     
    4244class APPSERVER_DLL_LINK I_ApplicationService 
    4345:   public I_RequestHandler 
     46,   public Plugins::I_StartupShutdownParticipant 
    4447{ 
    4548    /// @name Types 
  • Enterprise/branches/0075_TR_SCRIPTING/AppServer/I_Message.hpp

    r3345 r3430  
    8585    virtual unsigned int getMessageId() const = 0; 
    8686 
     87    /// This method returns the message type. 
     88    virtual pMessageType_type getMessageType() const = 0; 
     89 
    8790    /// Deserialize this message from an input archive. 
    8891    /// The protocol adapter deserializes the header, creates the appropriate message 
  • Enterprise/branches/0075_TR_SCRIPTING/AppServer/I_RequestHandler.hpp

    r998 r3430  
    2828 
    2929#include <Zen/Core/Memory/managed_ptr.hpp> 
    30 #include <Zen/Core/Plugins/I_StartupShutdownParticipant.hpp> 
    3130 
    3231#include <boost/noncopyable.hpp> 
     
    4140 
    4241class APPSERVER_DLL_LINK I_RequestHandler 
    43 :   public Plugins::I_StartupShutdownParticipant 
     42:   boost::noncopyable 
    4443{ 
    4544    /// @name Types 
     
    7574}   // namespace AppServer 
    7675}   // namespace Enterprise 
     76namespace Memory { 
     77    // I_ResponseHandler is managed by factory 
     78    template<> 
     79    struct is_managed_by_factory<Zen::Enterprise::AppServer::I_RequestHandler> : public boost::true_type{}; 
     80}   // namespace Memory 
    7781}   // namespace Zen 
    7882//-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~ 
  • Enterprise/branches/0075_TR_SCRIPTING/AppServer/scriptable_generic_service.hpp

    r3428 r3430  
    3838#include <Zen/Enterprise/AppServer/I_ApplicationServer.hpp> 
    3939 
     40#include <Zen/Enterprise/AppServer/I_MessageType.hpp> 
    4041#include <Zen/Enterprise/AppServer/I_Request.hpp> 
     42#include <Zen/Enterprise/AppServer/I_RequestHandler.hpp> 
     43#include <Zen/Enterprise/AppServer/I_Response.hpp> 
    4144#include <Zen/Enterprise/AppServer/I_ResponseHandler.hpp> 
    4245#include <Zen/Enterprise/AppServer/I_ProtocolService.hpp> 
     
    5760//-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~ 
    5861; 
    59  
    60 //-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~ 
    61 /// Handler Base. 
    62 /// Used to make the destructor visible. 
    63 struct handler_base 
     62/// base response handler. 
     63/// Used to make the I_ResponseHandler destructor public. 
     64class base_response_handler 
     65: public I_ResponseHandler 
     66{ 
     67public: 
     68    virtual ~base_response_handler() 
     69    { 
     70    } 
     71 
     72};  // class base_response_handler 
     73 
     74//-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~ 
     75/// Server side response handler. 
     76/// When the server recieves a request as a message (i.e. not as an 
     77/// internal request, but as a request recieved via a protocol adapter) 
     78/// a server_response_handler is created and the request is dispatched 
     79/// to the appropriate request handler.  When the request handler 
     80/// is finished and has a reply, it invokes I_ResponseHandler::handleReponse() 
     81/// with the reply.  This lightweight handler simply sends the response 
     82/// back to the application server which in turn (filters?) and then sends 
     83/// the message to the client. 
     84class server_response_handler 
     85:   public base_response_handler 
     86{ 
     87public: 
     88    virtual void handleResponse(pResponse_type _pResponse) 
     89    { 
     90        // Send the response back to the app server. 
     91        m_appServer.handleMessage(_pResponse); 
     92    } 
     93 
     94    server_response_handler(I_ApplicationServer& _appServer) 
     95    :   m_appServer(_appServer)  
     96    { 
     97    } 
     98 
     99    virtual ~server_response_handler() 
     100    { 
     101    } 
     102 
     103    static inline void destroy(Memory::managed_weak_ptr<I_ResponseHandler> _pResponseHandler) 
     104    { 
     105        delete dynamic_cast<base_response_handler*>(_pResponseHandler.get()); 
     106    } 
     107 
     108private: 
     109    I_ApplicationServer&        m_appServer; 
     110}; 
     111 
     112//-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~ 
     113/// Response handler for the client side. 
     114/// When a client sends a request, it creates this response handler and 
     115/// associates a request, payload and handler function with the request. 
     116/// When the reply comes back from the server, the function is invoked,  
     117/// passing the original request, the response and the payload. 
     118/// This provides a mechanism by which the client application can 
     119/// have additional data associated with the request. 
     120template<typename Request_type, typename Payload_type> 
     121struct client_response_handler 
    64122:   public I_ResponseHandler 
    65 { 
    66     virtual ~handler_base() 
    67     { 
    68     } 
    69 }; 
    70  
    71 //-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~ 
    72 template<typename Request_type, typename Payload_type> 
    73 struct response_handler 
    74 :   public handler_base 
    75123{ 
    76124    typedef typename Request_type::pRequest_type                pRequest_type; 
     
    83131    } 
    84132 
    85     response_handler(pRequest_type _pRequest, Payload_type _payload, Function_type _function) 
     133    client_response_handler(pRequest_type _pRequest, Payload_type _payload, Function_type _function) 
    86134    :   m_function(_function) 
    87135    ,   m_pRequest(_pRequest) 
     
    90138    } 
    91139 
    92     virtual ~response_handler() 
     140    virtual ~client_response_handler() 
    93141    { 
    94142    } 
     
    99147    pRequest_type               m_pRequest; 
    100148    Payload_type                m_payload; 
    101 };  // struct response_handler 
     149};  // struct client_response_handler 
     150 
     151//-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~ 
     152struct request_handler_base 
     153:   public I_RequestHandler 
     154{ 
     155    virtual ~request_handler_base() 
     156    { 
     157    } 
     158};  // struct request_handler_base 
     159 
     160//-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~ 
     161struct request_handler 
     162:   public request_handler_base 
     163{ 
     164    typedef boost::function<void(pRequest_type, pResponseHandler_type)>    Function_type; 
     165 
     166    virtual void handleRequest(pRequest_type _pRequest, pResponseHandler_type _pResponseHandler) 
     167    { 
     168        m_function(_pRequest, _pResponseHandler); 
     169    } 
     170 
     171    request_handler(Function_type _function) 
     172    :   m_function(_function) 
     173    { 
     174    } 
     175 
     176    virtual ~request_handler() 
     177    { 
     178    } 
     179 
     180public: 
     181 
     182    Function_type               m_function; 
     183};  // struct request_handler 
    102184 
    103185//-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~ 
     
    165247    typedef Memory::managed_ptr<I_ResponseHandler>                  pResponseHandler_type; 
    166248    typedef std::map<unsigned int, pResponseHandler_type>           ResponseHandlers_type; 
     249 
     250    typedef Zen::Memory::managed_ptr<I_MessageType>                 pMessageType_type; 
     251    typedef Zen::Memory::managed_ptr<I_RequestHandler>              pRequestHandler_type; 
     252    typedef std::map<pMessageType_type, pRequestHandler_type>       RequestHandlers_type; 
     253 
     254    //typedef Zen::Memory::managed_ptr<I_MessageHandler>              pMessageHandler_type; 
     255    //typedef std::map<pMessageType_type, pMessageHandler_type>       MessageHandlers_type; 
    167256    /// @} 
    168257 
     
    195284    template<typename Request_type, typename Payload_type> 
    196285    void send(create_request<Request_type, Payload_type>& _request, boost::function<void(pResponse_type, Request_type&, Payload_type)> _function); 
     286 
     287    void registerRequestHandler(pMessageType_type _pMessageType, boost::function<void(pRequest_type, pResponseHandler_type)> _function); 
     288    void unregisterRequestHandler(pMessageType_type _pMessageType); 
     289 
     290    void registerMessageHandler(pMessageType_type _pMessageType, boost::function<void(pMessage_type)> _function); 
     291    void unregisterMessageHandler(pMessageType_type _pMessageType); 
    197292    /// @} 
    198293 
     
    211306    /// Map from getRequestId() to the response handler. 
    212307    ResponseHandlers_type                                m_responseHandlers; 
     308 
     309    /// Map from pMessageType_type to the request handler. 
     310    RequestHandlers_type                                m_requestHandlers; 
     311 
     312    /// Map from pMessageType_type to the message handler. 
     313    //MessageHandlers_type                                m_messageHandlers; 
    213314 
    214315    /// Mutex to guard m_responseHandlers. 
     
    339440        // TODO Implement 
    340441 
    341         // TODO If this is a request, create a lightweight response handler then 
    342         // dispatch to scriptable_generic_service::handleRequest. 
    343         // This code path is invoked only when the message is coming from a  
    344         // protocol adapter.  When the response handler handleResponse() is called, 
    345         // it should simply dispatch to AppServer::handleMessage().  Since 
    346         // the destination endpoint is outbound, the app server will send it 
    347         // via the appropriate protocol adapter. 
     442        // Check to see if it's a request 
     443        Zen::Memory::managed_ptr<I_Request> pRequest( 
     444            _pMessage.as<Zen::Memory::managed_ptr<I_Request> >()); 
     445 
     446        if( pRequest.isValid() ) 
     447        { 
     448            // TODO If this is a request, create a lightweight response handler then 
     449            // dispatch to scriptable_generic_service::handleRequest. 
     450            // This code path is invoked only when the message is coming from a  
     451            // protocol adapter.  When the response handler handleResponse() is called, 
     452            // it should simply dispatch to AppServer::handleMessage().  Since 
     453            // the destination endpoint is outbound, the app server will send it 
     454            // via the appropriate protocol adapter. 
     455 
     456            // If we use detail::client_response_handler here, where do payload and function  
     457            // come from? 
     458            RequestHandlers_type::iterator iter = m_requestHandlers.find(pRequest->getMessageType()); 
     459 
     460            if (iter != m_requestHandlers.end()) 
     461            { 
     462                pResponseHandler_type pResponseHandler 
     463                ( 
     464                    new detail::server_response_handler(this->getApplicationServer()), 
     465                    &detail::server_response_handler::destroy 
     466                ); 
     467                iter->second->handleRequest(pRequest, pResponseHandler); 
     468            } 
     469            else 
     470            { 
     471                // No request handler. 
     472                // TODO Pass the request to a default request handler. 
     473                throw Utility::runtime_exception("scriptable_generic_service::handleMessage(): Error, no handler specified for message type"); 
     474            } 
     475        } 
     476 
    348477        throw Utility::runtime_exception("scriptable_generic_service::handleMessage(): Error, handling request or message is not implemented."); 
    349478    } 
     
    351480 
    352481//-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~ 
    353 inline void destroyHandler(Memory::managed_weak_ptr<I_ResponseHandler> _pHandler) 
    354 { 
    355     delete dynamic_cast<detail::handler_base*>(_pHandler.get()); 
     482inline void destroyResponseHandler(Memory::managed_weak_ptr<I_ResponseHandler> _pHandler) 
     483{ 
     484    delete dynamic_cast<detail::base_response_handler*>(_pHandler.get()); 
    356485} 
    357486 
     
    364493{ 
    365494    // Send an outbound request. 
    366     detail::response_handler<Request_type, Payload_type>* pRawHandler = new detail::response_handler<Request_type, Payload_type>(_request.m_pRequest, _request.m_payload, _function); 
    367  
    368     Memory::managed_ptr<I_ResponseHandler> pHandler(pRawHandler, destroyHandler); 
     495    detail::client_response_handler<Request_type, Payload_type>* pRawHandler =  
     496        new detail::client_response_handler<Request_type, Payload_type>( 
     497            _request.m_pRequest,  
     498            _request.m_payload,  
     499            _function 
     500        ); 
     501 
     502    Memory::managed_ptr<I_ResponseHandler> pHandler(pRawHandler, destroyResponseHandler); 
    369503 
    370504    { 
     
    383517{ 
    384518    // Handle an inbound request. 
    385  
    386     // TODO Create a m_requestHandler map for mapping request types (id?) to 
    387     // a handler function, then dispatch it here. 
    388     throw Utility::runtime_exception("scriptable_generic_service::handleRequest(): Error, not implemented."); 
     519    RequestHandlers_type::iterator iter = m_requestHandlers.find(_pRequest->getMessageType()); 
     520    if( iter != m_requestHandlers.end() ) 
     521    { 
     522        iter->second->handleRequest(_pRequest, _pResponseHandler); 
     523    } 
    389524 
    390525    // This code was here for cases where the App Server sent outgoing requests  
     
    407542 
    408543//-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~ 
     544inline void destroyRequestHandler(Memory::managed_weak_ptr<I_RequestHandler> _pRequestHandler) 
     545{ 
     546    delete dynamic_cast<detail::request_handler_base*>(_pRequestHandler.get()); 
     547} 
     548 
     549//-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~ 
     550template<typename BaseClass_type, typename Class_type> 
     551inline 
     552void 
     553scriptable_generic_service<BaseClass_type, Class_type>::registerRequestHandler(pMessageType_type _pMessageType, boost::function<void(pRequest_type, pResponseHandler_type)> _function) 
     554{ 
     555    RequestHandlers_type::iterator iter = m_requestHandlers.find(_pMessageType); 
     556    if( iter == m_requestHandlers.end() ) 
     557    { 
     558        I_RequestHandler* pRaw = new detail::request_handler(_function); 
     559 
     560        pRequestHandler_type pRequestHandler( 
     561            pRaw, 
     562            destroyRequestHandler 
     563        ); 
     564        m_requestHandlers[_pMessageType] = pRequestHandler; 
     565    } 
     566 
     567    /// TODO Exception? 
     568} 
     569 
     570//-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~ 
     571template<typename BaseClass_type, typename Class_type> 
     572inline 
     573void 
     574scriptable_generic_service<BaseClass_type, Class_type>::unregisterRequestHandler(pMessageType_type _pMessageType) 
     575{ 
     576    RequestHandlers_type::iterator iter = m_requestHandlers.find(_pMessageType); 
     577    if( iter != m_requestHandlers.end() ) 
     578    { 
     579        m_requestHandlers.erase(iter); 
     580    } 
     581 
     582    /// TODO Exception? 
     583} 
     584 
     585//-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~ 
     586template<typename BaseClass_type, typename Class_type> 
     587inline 
     588void 
     589scriptable_generic_service<BaseClass_type, Class_type>::registerMessageHandler(pMessageType_type _pMessageType, boost::function<void(pMessage_type)> _function) 
     590{ 
     591    throw Zen::Utility::runtime_exception("scriptable_generic_service::registerMessageHandler() : Error, not implemented."); 
     592} 
     593 
     594//-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~ 
     595template<typename BaseClass_type, typename Class_type> 
     596inline 
     597void 
     598scriptable_generic_service<BaseClass_type, Class_type>::unregisterMessageHandler(pMessageType_type _pMessageType) 
     599{ 
     600    throw Zen::Utility::runtime_exception("scriptable_generic_service::unregisterMessageHandler() : Error, not implemented."); 
     601} 
     602 
     603//-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~ 
    409604}   // namespace AppServer 
    410605}   // namespace Enterprise