Savarese Software Research Corporation
service/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_SERVICE_SERVICE_H
00023 #define __SSRC_WSPR_SERVICE_SERVICE_H
00024 
00025 #include <ssrc/wispers/service/protocol.h>
00026 #include <ssrc/wispers/registry/protocol.h>
00027 
00028 #include <ssrc/wisp/service.h>
00029 
00030 __BEGIN_NS_SSRC_WSPR_SERVICE
00031 
00032 using NS_SSRC_SPREAD::MembershipInfo;
00033 using NS_SSRC_SPREAD::Message;
00034 using NS_SSRC_SPREAD::GroupList;
00035 
00036 using NS_SSRC_WISP_PROTOCOL::MessageInfo;
00037 
00038 using NS_SSRC_WISP_SERVICE::EventLoop;
00039 using NS_SSRC_WISP_SERVICE::EventHandler;
00040 using NS_SSRC_WISP_SERVICE::EventInfo;
00041 using NS_SSRC_WISP_SERVICE::ServiceEventHandler;
00042 using NS_SSRC_WISP_SERVICE::Service;
00043 using NS_SSRC_WISP_SERVICE::timeout_handler;
00044 using NS_SSRC_WISP_SERVICE::timeout_ptr;
00045 using NS_SSRC_WISP_SERVICE::message_handler_type;
00046 using NS_SSRC_WISP_SERVICE::message_handler_entry;
00047 using NS_SSRC_WISP_SERVICE::message_handler_map;
00048 
00049 class ServiceProtocolProcessor :
00050   public
00051 NS_SSRC_WISP_SERVICE::ServiceProtocolProcessor<ServiceProtocol::packing_traits>
00052 {
00053   friend class NS_SSRC_WISP_SERVICE::ServiceProtocolProcessor<packing_traits>;
00054   typedef NS_SSRC_WISP_SERVICE::ServiceProtocolProcessor<packing_traits> super;
00055 
00056   registry::service_type_list _service_types;
00057 
00058   void register_service_types(const string & reg) {
00059     if(_service_types.size() > 0) {
00060       GroupList dest;
00061       dest.add(reg);
00062       if(reg != registry::event_group_register())
00063         dest.add(registry::event_group_register());
00064       _caller.
00065         send<registry::CallReregister>(Message::Safe,
00066                                        dest, _caller.name(), _service_types);
00067     }
00068   }
00069 
00070 protected:
00071   WISP_IMPORT(ServiceProtocol, MessageStop);
00072   WISP_IMPORT(ServiceProtocol, MessageEchoRequest);
00073   WISP_IMPORT(ServiceProtocol, MessageReregister);
00074   WISP_IMPORT(ServiceProtocol, MessageStatusRequest);
00075   WISP_IMPORT(ServiceProtocol, CallStop);
00076   WISP_IMPORT(ServiceProtocol, CallEchoReply);
00077   WISP_IMPORT(ServiceProtocol, CallStatusReply);
00078 
00079   virtual void process_request(const MessageStop & msg, const MessageInfo &) {
00080     stop();
00081   }
00082 
00083   virtual void process_request(const MessageEchoRequest & msg,
00084                                const MessageInfo & msginfo)
00085   {
00086     _caller.
00087       reply<CallEchoReply>(msginfo.sender(), msginfo.token(),
00088                            Message::FIFOSelfDiscard);
00089   }
00090 
00091   virtual void process_request(const MessageReregister & msg,
00092                                const MessageInfo &)
00093   {
00094     register_service_types(msg.registry);
00095   }
00096 
00097   virtual properties_ptr get_status();
00098 
00099   void process_request(const MessageStatusRequest & msg,
00100                        const MessageInfo & msginfo);
00101 
00102   virtual void transition(State state) {
00103     super::transition(state);
00104     if(state == Started)
00105       register_service_types(registry::service_group_registry());
00106   }
00107 
00108 public:
00109   typedef registry::service_type_list::const_iterator service_type_iterator;
00110 
00111   explicit ServiceProtocolProcessor(caller_type & caller) :
00112     super(caller), _service_types(2)
00113   {
00114     _service_types.resize(0);
00115 
00116     WISP_SERVICE_REQUEST(MessageStop);
00117     WISP_SERVICE_REQUEST(MessageEchoRequest);
00118     WISP_SERVICE_REQUEST(MessageReregister);
00119     WISP_SERVICE_REQUEST(MessageStatusRequest);
00120   }
00121 
00122   virtual ~ServiceProtocolProcessor() { }
00123 
00124   void add_service_type(const string & service_type) {
00125     _service_types.push_back(service_type);
00126   }
00127 
00128   service_type_iterator service_types_begin() {
00129     return _service_types.begin();
00130   }
00131 
00132   service_type_iterator service_types_end() {
00133     return _service_types.end();
00134   }
00135 
00136   void start() {
00137     _caller.join(ServiceProtocol::service_group());
00138     transition(Starting);
00139   }
00140 
00141   // Services listening for membership messages should wait for self-leave
00142   // before transitioning to Stopped.
00143   void stop() {
00144     _caller.leave(ServiceProtocol::service_group());
00145     transition(Stopping);
00146   }
00147 
00148   void membership(const MessageInfo & msginfo,
00149                   const MembershipInfo & meminfo)
00150   {
00151     if(meminfo.is_self_leave() &&
00152        msginfo.sender() == ServiceProtocol::service_group())
00153       transition(Stopped);
00154     else if(state() != Stopping) {
00155       process_membership_message(msginfo, meminfo);
00156     }
00157   }
00158 };
00159 
00160 __END_NS_SSRC_WSPR_SERVICE
00161 
00162 #endif

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