xorp

finder_client.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_client.hh,v 1.29 2008/10/02 21:57:19 bms Exp $
00023 
00024 #ifndef __LIBXIPC_FINDER_CLIENT_HH__
00025 #define __LIBXIPC_FINDER_CLIENT_HH__
00026 
00027 
00028 
00029 
00030 
00031 #include "xrl/targets/finder_client_base.hh"
00032 #include "finder_messenger.hh"
00033 
00034 #include "xrl_pf.hh"
00035 
00036 class FinderClientOp;
00037 class FinderClientObserver;
00038 
00043 class FinderDBEntry {
00044 public:
00045     typedef list<Xrl> XRLS;
00046 
00047     FinderDBEntry(const string& key);
00048     FinderDBEntry(const string& key, const string& value);
00049 #ifdef XORP_USE_USTL
00050     FinderDBEntry() { }
00051 #endif
00052 
00053     const string&   key() const { return _key; }
00054     const list<string>& values() const  { return _values; }
00055     list<string>&   values()    { return _values; }
00056     const XRLS&     xrls() const;
00057     void        clear();
00058     void        pop_front();
00059 
00060 protected:
00061     string   _key;
00062     list<string> _values;
00063     mutable XRLS _xrls;
00064 };
00065 
00073 class FinderClientXrlCommandInterface
00074 {
00075 public:
00076     virtual ~FinderClientXrlCommandInterface() {}
00077     virtual void uncache_xrl(const string& xrl) = 0;
00078     virtual void uncache_xrls_from_target(const string& target) = 0;
00079     virtual XrlCmdError dispatch_tunneled_xrl(const string& xrl) = 0;
00080 };
00081 
00089 class FinderClient :
00090     public FinderMessengerManager, public FinderClientXrlCommandInterface
00091 {
00092 public:
00093     typedef
00094     XorpCallback2<void, const XrlError&, const FinderDBEntry*>::RefPtr
00095     QueryCallback;
00096     typedef ref_ptr<FinderClientOp> Operation;
00097     typedef list<Operation> OperationQueue;
00098 
00099     class InstanceInfo;
00100 
00101     typedef map<string, FinderDBEntry>  ResolvedTable;
00102     typedef map<string, string>     LocalResolvedTable;
00103     typedef vector<InstanceInfo>    InstanceList;
00104 
00105 public:
00109     FinderClient();
00110 
00114     virtual ~FinderClient();
00115 
00130     bool register_xrl_target(const string&    instance_name,
00131                  const string&    class_name,
00132                  const XrlDispatcher* dispatcher);
00133 
00142     bool unregister_xrl_target(const string& instance_name);
00143 
00158     bool register_xrl(const string& instance_name,
00159               const string& xrl,
00160               const string& pf_name,
00161               const string& pf_args);
00162 
00174     bool enable_xrls(const string& instance_name);
00175 
00189     void query(EventLoop&       eventloop,
00190            const string&        xrl,
00191            const QueryCallback& qcb);
00192 
00196     uint32_t queries_pending() const;
00197 
00205     const FinderDBEntry* query_cache(const string& xrl) const;
00206 
00215     void uncache_result(const FinderDBEntry* dbe);
00216 
00228     bool query_self(const string& incoming_xrl_command,
00229             string& local_xrl_command) const;
00230 
00240     bool forward_finder_xrl(const Xrl& x, const XrlPFSender::SendCallback& cb);
00241 
00245     FinderMessengerBase* messenger();
00246 
00250     OperationQueue& todo_list()     { return _todo_list; }
00251 
00255     OperationQueue& done_list()     { return _done_list; }
00256 
00260     void   notify_done(const FinderClientOp* completed);
00261 
00265     void   notify_failed(const FinderClientOp* completed);
00266 
00270     XrlCmdMap& commands()       { return _commands; }
00271 
00276     bool ready() const          { return _xrls_registered; }
00277 
00281     bool connected() const      { return _messenger != 0; }
00282 
00290     bool attach_observer(FinderClientObserver* o);
00291 
00300     bool detach_observer(FinderClientObserver* o);
00301 
00302 protected:
00303     // FinderMessengerManager interface
00304     void messenger_birth_event(FinderMessengerBase*);
00305     void messenger_death_event(FinderMessengerBase*);
00306     void messenger_active_event(FinderMessengerBase*);
00307     void messenger_inactive_event(FinderMessengerBase*);
00308     void messenger_stopped_event(FinderMessengerBase*);
00309     bool manages(const FinderMessengerBase*) const;
00310 
00311 protected:
00312     // FinderClientXrlCommandInterface
00313     void uncache_xrl(const string& xrl);
00314     void uncache_xrls_from_target(const string& target);
00315     XrlCmdError dispatch_tunneled_xrl(const string& xrl);
00316 
00317 private:
00318     void
00319     dispatch_tunneled_xrl_cb(const XrlError &e, const XrlArgs *a) const;
00320 
00321 protected:
00322     void crank();
00323     void prepare_for_restart();
00324 
00325 protected:
00326     InstanceList::iterator find_instance(const string& instance);
00327     InstanceList::const_iterator find_instance(const string& instance) const;
00328 
00329 protected:
00330     OperationQueue   _todo_list;
00331     OperationQueue   _done_list;
00332     ResolvedTable    _rt;
00333     LocalResolvedTable   _lrt;
00334     InstanceList     _ids;
00335 
00336     XrlCmdMap        _commands;
00337 
00338     FinderMessengerBase* _messenger;
00339     bool         _pending_result;
00340     bool         _xrls_registered;
00341 
00342     FinderClientObserver* _observer;
00343 };
00344 
00345 #endif // __LIBXIPC_FINDER_CLIENT_HH__
 All Classes Namespaces Functions Variables Typedefs Enumerations