xorp

ifmgr_cmd_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 #ifndef __IFMGR_CMD_QUEUE_HH__
00023 #define __IFMGR_CMD_QUEUE_HH__
00024 
00025 
00026 #include "libxorp/ref_ptr.hh"
00027 
00028 class IfMgrCommandBase;
00029 
00030 class IfMgrIfTree;
00031 class IfMgrIfAtom;
00032 class IfMgrVifAtom;
00033 class IfMgrIPv4Atom;
00034 #ifdef HAVE_IPV6
00035 class IfMgrIPv6Atom;
00036 #endif
00037 
00041 class IfMgrCommandSinkBase {
00042 public:
00043     typedef ref_ptr<IfMgrCommandBase> Cmd;
00044 public:
00045     virtual void push(const Cmd& cmd) = 0;
00046     virtual ~IfMgrCommandSinkBase();
00047 };
00048 
00055 class IfMgrCommandTee : public IfMgrCommandSinkBase {
00056 public:
00057     typedef IfMgrCommandSinkBase::Cmd Cmd;
00058 
00059 public:
00060     IfMgrCommandTee(IfMgrCommandSinkBase& o1, IfMgrCommandSinkBase& o2);
00061     void push(const Cmd& cmd);
00062 
00063 protected:
00064     IfMgrCommandSinkBase& _o1;
00065     IfMgrCommandSinkBase& _o2;
00066 };
00067 
00068 
00075 template <typename SinkType = IfMgrCommandSinkBase>
00076 class IfMgrNWayCommandTee : public IfMgrCommandSinkBase {
00077 public:
00078     typedef IfMgrCommandSinkBase::Cmd Cmd;
00079     typedef list<SinkType*> SinkList;
00080 
00081 public:
00082     void push(const Cmd& cmd);
00083 
00089     bool add_sink(SinkType* sink);
00090 
00096     bool remove_sink(SinkType* sink);
00097 
00098 protected:
00099     SinkList _sinks;
00100 };
00101 
00102 template <typename SinkType>
00103 void
00104 IfMgrNWayCommandTee<SinkType>::push(const Cmd& cmd)
00105 {
00106     typename SinkList::iterator i;
00107     for (i = _sinks.begin(); i != _sinks.end(); ++i) {
00108     (*i)->push(cmd);
00109     }
00110 }
00111 
00112 template <typename SinkType>
00113 bool
00114 IfMgrNWayCommandTee<SinkType>::add_sink(SinkType* o)
00115 {
00116     if (find(_sinks.begin(), _sinks.end(), o) != _sinks.end())
00117     return false;
00118     _sinks.push_back(o);
00119     return true;
00120 }
00121 
00122 template <typename SinkType>
00123 bool
00124 IfMgrNWayCommandTee<SinkType>::remove_sink(SinkType* o)
00125 {
00126     typename SinkList::iterator i = find(_sinks.begin(), _sinks.end(), o);
00127     if (i == _sinks.end())
00128     return false;
00129     _sinks.erase(i);
00130     return true;
00131 }
00132 
00133 
00142 class IfMgrCommandDispatcher : public IfMgrCommandSinkBase {
00143 public:
00144     typedef IfMgrCommandSinkBase::Cmd Cmd;
00145 
00146 public:
00151     IfMgrCommandDispatcher(IfMgrIfTree& tree);
00152 
00156     void push(const Cmd& cmd);
00157 
00163     virtual bool execute();
00164 
00165 protected:
00166     Cmd      _cmd;
00167     IfMgrIfTree& _iftree;
00168 };
00169 
00173 class IfMgrCommandQueueBase : public IfMgrCommandSinkBase {
00174 public:
00175     typedef IfMgrCommandSinkBase::Cmd Cmd;
00176 public:
00180     virtual void push(const Cmd& cmd) = 0;
00181 
00185     virtual bool empty() const = 0;
00186 
00192     virtual Cmd& front() = 0;
00193 
00199     virtual const Cmd& front() const = 0;
00200 
00204     virtual void pop_front() = 0;
00205 };
00206 
00210 class IfMgrCommandFifoQueue : public IfMgrCommandQueueBase {
00211 public:
00212     typedef IfMgrCommandQueueBase::Cmd Cmd;
00213 
00214 public:
00215     void    push(const Cmd& cmd);
00216     bool    empty() const;
00217     Cmd&    front();
00218     const Cmd&  front() const;
00219     void    pop_front();
00220 
00221 protected:
00222     list<Cmd> _fifo;
00223 };
00224 
00233 class IfMgrCommandIfClusteringQueue : public IfMgrCommandQueueBase {
00234 public:
00235     typedef IfMgrCommandQueueBase::Cmd Cmd;
00236     typedef list<Cmd> CmdList;
00237 
00238 public:
00239     IfMgrCommandIfClusteringQueue();
00240 
00241     void    push(const Cmd& cmd);
00242     bool    empty() const;
00243     Cmd&    front();
00244     const Cmd&  front() const;
00245     void    pop_front();
00246 
00247 protected:
00248     void    change_active_interface();
00249 
00250 protected:
00251     string      _current_ifname;    // ifname of commands current
00252                             // commands queue
00253                             // last command output
00254     CmdList     _future_cmds;       // commands with ifname not
00255                         // matching _current_ifname
00256     CmdList     _current_cmds;
00257 };
00258 
00259 
00263 class IfMgrIfTreeToCommands {
00264 public:
00268     IfMgrIfTreeToCommands(const IfMgrIfTree& tree)
00269     : _tree(tree)
00270     {}
00271 
00278     void convert(IfMgrCommandSinkBase& sink) const;
00279 
00280 protected:
00281     const IfMgrIfTree& _tree;
00282 };
00283 
00287 class IfMgrIfAtomToCommands {
00288 public:
00292     IfMgrIfAtomToCommands(const IfMgrIfAtom& interface)
00293     : _i(interface)
00294     {}
00295 
00303     void convert(IfMgrCommandSinkBase& sink) const;
00304 
00305 protected:
00306     const IfMgrIfAtom& _i;
00307 };
00308 
00312 class IfMgrVifAtomToCommands {
00313 public:
00320     IfMgrVifAtomToCommands(const string& ifn, const IfMgrVifAtom& vif)
00321     : _ifn(ifn), _v(vif)
00322     {}
00323 
00331     void convert(IfMgrCommandSinkBase& sink) const;
00332 
00333 protected:
00334     const string&   _ifn;       // Interface name
00335     const IfMgrVifAtom& _v;
00336 };
00337 
00341 class IfMgrIPv4AtomToCommands {
00342 public:
00351     IfMgrIPv4AtomToCommands(const string& ifn,
00352                 const string& vifn,
00353                 const IfMgrIPv4Atom& a)
00354     : _ifn(ifn), _vifn(vifn), _a(a)
00355     {}
00356 
00364     void convert(IfMgrCommandSinkBase& sink) const;
00365 
00366 protected:
00367     const string& _ifn;
00368     const string& _vifn;
00369     const IfMgrIPv4Atom& _a;
00370 };
00371 
00372 #ifdef HAVE_IPV6
00373 
00376 class IfMgrIPv6AtomToCommands {
00377 public:
00386     IfMgrIPv6AtomToCommands(const string& ifn,
00387                 const string& vifn,
00388                 const IfMgrIPv6Atom& a)
00389     : _ifn(ifn), _vifn(vifn), _a(a)
00390     {}
00391 
00399     void convert(IfMgrCommandSinkBase& sink) const;
00400 
00401 protected:
00402     const string&    _ifn;
00403     const string&    _vifn;
00404     const IfMgrIPv6Atom& _a;
00405 };
00406 #endif //ipv6
00407 
00408 #endif // __IFMGR_CMD_QUEUE_HH__
 All Classes Namespaces Functions Variables Typedefs Enumerations