xorp

finder_messenger.hh

00001 // -*- c-basic-offset: 4; tab-width: 8; indent-tabs-mode: t -*-
00002 // vim:set sts=4 ts=8:
00003 
00004 // Copyright (c) 2001-2011 XORP, Inc and Others
00005 //
00006 // This program is free software; you can redistribute it and/or modify
00007 // it under the terms of the GNU Lesser General Public License, Version
00008 // 2.1, June 1999 as published by the Free Software Foundation.
00009 // Redistribution and/or modification of this program under the terms of
00010 // any other version of the GNU Lesser General Public License is not
00011 // permitted.
00012 // 
00013 // This program is distributed in the hope that it will be useful, but
00014 // WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For more details,
00016 // see the GNU Lesser General Public License, Version 2.1, a copy of
00017 // which can be found in the XORP LICENSE.lgpl file.
00018 // 
00019 // XORP, Inc, 2953 Bunker Hill Lane, Suite 204, Santa Clara, CA 95054, USA;
00020 // http://xorp.net
00021 
00022 // $XORP: xorp/libxipc/finder_messenger.hh,v 1.22 2008/10/02 21:57:20 bms Exp $
00023 
00024 #ifndef __LIBXIPC_FINDER_MESSENGER_HH__
00025 #define __LIBXIPC_FINDER_MESSENGER_HH__
00026 
00027 
00028 
00029 #include "libxorp/eventloop.hh"
00030 #include "xrl_cmd_map.hh"
00031 #include "xrl_sender.hh"
00032 
00033 class FinderMessengerBase;
00034 
00038 class FinderMessengerManager {
00039 public:
00043     virtual ~FinderMessengerManager() {}
00044 
00048     virtual void messenger_birth_event(FinderMessengerBase*) = 0;
00049 
00053     virtual void messenger_death_event(FinderMessengerBase*) = 0;
00054 
00058     virtual void messenger_active_event(FinderMessengerBase*) = 0;
00059 
00063     virtual void messenger_inactive_event(FinderMessengerBase*) = 0;
00064 
00069     virtual void messenger_stopped_event(FinderMessengerBase*) = 0;
00070 
00075     virtual bool manages(const FinderMessengerBase*) const = 0;
00076 };
00077 
00086 class FinderMessengerBase : public XrlSender
00087 {
00088 public:
00089     typedef XrlSender::Callback SendCallback;
00090 
00091 public:
00092     FinderMessengerBase(EventLoop& e,
00093             FinderMessengerManager* fmm,
00094             XrlCmdMap& cmds);
00095     
00096     virtual ~FinderMessengerBase();
00097 
00098     virtual bool send(const Xrl& xrl, const SendCallback& scb) = 0;
00099     virtual bool pending() const = 0;
00100 
00101     XrlCmdMap& command_map();
00102     EventLoop& eventloop();
00103 
00104     void unhook_manager();
00105     FinderMessengerManager* manager();
00106     
00107 protected:
00111     void dispatch_xrl(uint32_t seqno, const Xrl& x);
00112     
00113     bool dispatch_xrl_response(uint32_t seqno,
00114                    const XrlError& e,
00115                    XrlArgs*);
00116 
00117     bool store_xrl_response(uint32_t seqno, const SendCallback& scb);
00118     
00119     virtual void reply(uint32_t seqno,
00120                const XrlError& e,
00121                const XrlArgs* reply_args) = 0;
00122 
00123     void response_timeout(uint32_t seqno);
00124 
00125 private:
00126     void
00127     dispatch_xrl_cb(const XrlCmdError &e,
00128             const XrlArgs *reply_args,
00129             uint32_t seqno);
00130 
00131     class ResponseState {
00132     public:
00133     ResponseState(uint32_t         seqno,
00134               const SendCallback&  cb,
00135               FinderMessengerBase* fmb)
00136         : scb(cb)
00137     {
00138         expiry = fmb->eventloop().new_oneoff_after_ms(RESPONSE_TIMEOUT_MS,
00139             callback(fmb,  &FinderMessengerBase::response_timeout,
00140                  seqno));
00141     }
00142 #ifdef XORP_USE_USTL
00143     ResponseState() { }
00144 #endif
00145 
00146     SendCallback scb;
00147     XorpTimer    expiry;
00148 
00149     static const uint32_t RESPONSE_TIMEOUT_MS = 30000;
00150     };
00151     typedef map<uint32_t, ResponseState> SeqNoResponseMap;
00152 
00153     friend class ResponseState;
00154 
00155 private:
00156     EventLoop&              _eventloop;
00157     FinderMessengerManager*     _manager;
00158     SeqNoResponseMap            _expected_responses;
00159     XrlCmdMap&              _cmds;
00160 };
00161 
00163 //
00164 // Inline methods
00165 
00166 inline XrlCmdMap&
00167 FinderMessengerBase::command_map()
00168 {
00169     return _cmds;
00170 }
00171 
00172 inline EventLoop&
00173 FinderMessengerBase::eventloop()
00174 {
00175     return _eventloop;
00176 }
00177 
00178 inline FinderMessengerManager*
00179 FinderMessengerBase::manager()
00180 {
00181     return _manager;
00182 }
00183 
00184 #endif // __LIBXIPC_FINDER_MESSENGER_HH__
 All Classes Namespaces Functions Variables Typedefs Enumerations