xorp

finder_xrl_queue.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_xrl_queue.hh,v 1.17 2008/10/02 21:57:21 bms Exp $
00023 
00024 #ifndef __LIBXIPC_FINDER_XRL_QUEUE_HH__
00025 #define __LIBXIPC_FINDER_XRL_QUEUE_HH__
00026 
00027 
00028 #include "libxorp/ref_ptr.hh"
00029 
00030 class FinderXrlCommandBase;
00031 
00041 class FinderXrlCommandQueue
00042 {
00043 public:
00044     typedef ref_ptr<FinderXrlCommandBase> Command;
00045 
00046 public:
00047     FinderXrlCommandQueue(FinderMessengerBase* messenger);
00048     FinderXrlCommandQueue(const FinderXrlCommandQueue& oq);
00049     FinderXrlCommandQueue() { _m = NULL; }
00050     ~FinderXrlCommandQueue();
00051 
00052     FinderMessengerBase& messenger()    { return *_m; }
00053 
00054     void enqueue(const Command& cmd);
00055 
00056 #ifndef XORP_USE_USTL
00057 private:
00058 #endif
00059     FinderXrlCommandQueue& operator=(const FinderXrlCommandQueue&);
00060 
00061 protected:
00062     void push();
00063     void dispatch_one();
00064     EventLoop& eventloop();
00065 
00066 protected:
00067     friend class FinderXrlCommandBase;
00068     void crank();
00069     void kill_messenger();
00070 
00071 private:
00072     FinderMessengerBase* _m;
00073     list<Command>    _cmds;
00074     bool         _pending;
00075     XorpTimer        _dispatcher;
00076 };
00077 
00081 class FinderXrlCommandBase
00082 {
00083 public:
00084     FinderXrlCommandBase(FinderXrlCommandQueue& q) : _queue(q) {}
00085     virtual ~FinderXrlCommandBase() {}
00086 
00087     FinderXrlCommandQueue& queue()  { return _queue; }
00088     FinderMessengerBase& messenger()    { return _queue.messenger(); }
00089 
00090     virtual bool dispatch() = 0;
00091 
00092     void dispatch_cb(const XrlError& e)
00093     {
00094     if (e != XrlCmdError::OKAY()) {
00095         XLOG_ERROR("Sent xrl got response %s\n", e.str().c_str());
00096         queue().kill_messenger();
00097         return ;
00098     }
00099     queue().crank();
00100     }
00101 
00102 protected:
00103     FinderXrlCommandQueue& _queue;
00104 };
00105 
00106 #include "xrl/interfaces/finder_client_xif.hh"
00107 
00111 class FinderSendHelloToClient : public FinderXrlCommandBase {
00112 public:
00113     FinderSendHelloToClient(FinderXrlCommandQueue& q,
00114                 const string&      tgtname)
00115     : FinderXrlCommandBase(q), _tgtname(tgtname)
00116     {
00117     }
00118 
00119     bool dispatch()
00120     {
00121     XrlFinderClientV0p2Client client(&(queue().messenger()));
00122     return client.send_hello(_tgtname.c_str(),
00123           callback(static_cast<FinderXrlCommandBase*>(this),
00124                &FinderXrlCommandBase::dispatch_cb));
00125     }
00126 
00127 protected:
00128     string _tgtname;
00129 };
00130 
00134 class FinderSendRemoveXrl : public FinderXrlCommandBase
00135 {
00136 public:
00137     FinderSendRemoveXrl(FinderXrlCommandQueue& q,
00138             const string&          tgtname,
00139             const string&          xrl)
00140     : FinderXrlCommandBase(q), _tgtname(tgtname), _xrl(xrl)
00141     {
00142     }
00143 
00144     ~FinderSendRemoveXrl()
00145     {
00146     _tgtname = _xrl = "croak";
00147     }
00148 
00149     bool dispatch()
00150     {
00151     XrlFinderClientV0p2Client client(&(queue().messenger()));
00152     return client.send_remove_xrl_from_cache(_tgtname.c_str(), _xrl,
00153           callback(static_cast<FinderXrlCommandBase*>(this),
00154                &FinderXrlCommandBase::dispatch_cb));
00155     }
00156 
00157 protected:
00158     string _tgtname;
00159     string _xrl;
00160 };
00161 
00165 class FinderSendRemoveXrls : public FinderXrlCommandBase
00166 {
00167 public:
00168     FinderSendRemoveXrls(FinderXrlCommandQueue& q,
00169              const string&      tgtname)
00170     : FinderXrlCommandBase(q), _tgtname(tgtname)
00171     {
00172     }
00173 
00174     ~FinderSendRemoveXrls()
00175     {
00176     _tgtname = "croak";
00177     }
00178 
00179     bool dispatch()
00180     {
00181     XrlFinderClientV0p2Client client(&(queue().messenger()));
00182     return client.send_remove_xrls_for_target_from_cache(
00183              _tgtname.c_str(), _tgtname,
00184              callback(static_cast<FinderXrlCommandBase*>(this),
00185                   &FinderXrlCommandBase::dispatch_cb));
00186     }
00187 
00188 protected:
00189     string _tgtname;
00190 };
00191 
00196 class FinderSendTunneledXrl : public FinderXrlCommandBase
00197 {
00198 public:
00199     FinderSendTunneledXrl(FinderXrlCommandQueue& q,
00200               const string&      tgtname,
00201               const string&      xrl)
00202     : FinderXrlCommandBase(q), _tgtname(tgtname), _xrl(xrl)
00203     {
00204     }
00205 
00206     ~FinderSendTunneledXrl()
00207     {
00208     _tgtname = "croak";
00209     }
00210 
00211     void dispatch_cb(const XrlError& e,
00212              const uint32_t* /* p_errno */,
00213              const string*   /* p_errtxt */)
00214     {
00215     //  if (e != XrlError::OKAY())
00216     FinderXrlCommandBase::dispatch_cb(e);
00217     // TODO: XXX
00218     }
00219 
00220     bool dispatch()
00221     {
00222     XrlFinderClientV0p2Client client(&(queue().messenger()));
00223     return client.send_dispatch_tunneled_xrl(_tgtname.c_str(), _xrl,
00224          callback(this, &FinderSendTunneledXrl::dispatch_cb));
00225     }
00226 
00227 protected:
00228     string _tgtname;
00229     string _xrl;
00230 };
00231 
00232 #endif // __LIBXIPC_FINDER_XRL_QUEUE_HH__
 All Classes Namespaces Functions Variables Typedefs Enumerations