xorp

xrl_router.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 #ifndef __LIBXIPC_XRL_ROUTER_HH__
00023 #define __LIBXIPC_XRL_ROUTER_HH__
00024 
00025 #include "libxorp/xorp.h"
00026 #include "libxorp/callback.hh"
00027 
00028 #include "xrl.hh"
00029 #include "xrl_sender.hh"
00030 #include "xrl_dispatcher.hh"
00031 #include "xrl_pf.hh"
00032 #include "finder_constants.hh"
00033 #include "finder_client_observer.hh"
00034 
00035 class DispatchState;
00036 
00037 class FinderClient;
00038 class FinderClientXrlTarget;
00039 class FinderTcpAutoConnector;
00040 class FinderDBEntry;
00041 class XrlRouterDispatchState;
00042 
00043 class XrlRouter :
00044     public XrlDispatcher,
00045     public XrlSender,
00046     public FinderClientObserver
00047 {
00048 public:
00049     typedef XrlSender::Callback XrlCallback;
00050     typedef XrlRouterDispatchState DispatchState;
00051 
00052 public:
00053     XrlRouter(EventLoop&    e,
00054           const char*   class_name,
00055           const char*   finder_address,
00056           uint16_t      finder_port)
00057     throw (InvalidAddress);
00058 
00059     XrlRouter(EventLoop&    e,
00060           const char*   class_name,
00061           IPv4      finder_address,
00062           uint16_t      finder_port)
00063     throw (InvalidAddress);
00064 
00065     virtual ~XrlRouter();
00066 
00072     bool add_listener(XrlPFListener* listener);
00073 
00078     void finalize();
00079 
00083     bool finalized() const      { return _finalized; }
00084 
00088     bool connected() const;
00089 
00094     bool connect_failed() const;
00095 
00100     bool ready() const;
00101 
00105     bool failed() const;
00106 
00107     virtual string toString() const;
00108 
00118     bool send(const Xrl& xrl, const XrlCallback& cb);
00119 
00124     bool pending() const;
00125 
00133     bool add_handler_internal(const string& cmd,
00134                   const XrlRecvAsyncCallback& rcb);
00135 
00139     EventLoop& eventloop()      { return _e; }
00140 
00141     const string& instance_name() const { return _instance_name; }
00142 
00143     const string& class_name() const    { return XrlCmdMap::name(); }
00144 
00145     IPv4     finder_address() const;
00146 
00147     uint16_t finder_port() const;
00148 
00149     XI* lookup_xrl(const string& name) const;
00150 
00151 #if 0
00152     void batch_start(const string& target);
00153     void batch_stop(const string& target);
00154 #endif
00155 
00156 protected:
00160     virtual void finder_connect_event();
00161 
00165     virtual void finder_disconnect_event();
00166 
00178     virtual void finder_ready_event(const string& tgt_name);
00179 
00180     void dispatch_xrl(const string&     method_name,
00181               const XrlArgs&        inputs,
00182               XrlDispatcherCallback outputs) const;
00183 
00189     void resolve_callback(const XrlError&     e,
00190               const FinderDBEntry*    dbe,
00191               XrlRouterDispatchState* ds);
00192 
00197     void send_callback(const XrlError&  e,
00198                XrlArgs*     reply,
00199                XrlPFSender* sender, // un-used, should be ref-ptr if we ever actually use this.
00200                XrlCallback  user_callback);
00201 
00207     bool send_resolved(const Xrl&       xrl,
00208                const FinderDBEntry* dbe,
00209                const XrlCallback&   dispatch_cb,
00210                bool  direct_call);
00211 
00212     void initialize(const char* class_name,
00213             IPv4    finder_addr,
00214             uint16_t    finder_port);
00215 
00216 private:
00217     ref_ptr<XrlPFSender> lookup_sender(const Xrl& xrl, FinderDBEntry *dbe);
00218 
00219 protected:
00220     EventLoop&          _e;
00221     FinderClient*       _fc;
00222     FinderClientXrlTarget*  _fxt;
00223     FinderTcpAutoConnector* _fac;
00224     string          _instance_name;
00225     bool            _finalized;
00226 
00227     list<XrlPFListener*>    _listeners;     // listeners
00228     list<XrlRouterDispatchState*> _dsl;         // dispatch state
00229     list< ref_ptr<XrlPFSender> > _senders;      // active senders
00230 
00231     static uint32_t     _icnt;          // instance count
00232 
00233 private:
00234     typedef map<string, XI*>        XIM;
00235 
00236     mutable XIM         _xi_cache;
00237 };
00238 
00253 void wait_until_xrl_router_is_ready(EventLoop& e, XrlRouter& xr);
00254 
00255 #endif // __LIBXIPC_XRL_ROUTER_HH__
 All Classes Namespaces Functions Variables Typedefs Enumerations