xorp

finder_tcp_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_tcp_messenger.hh,v 1.23 2008/10/02 21:57:21 bms Exp $
00023 
00024 #ifndef __LIBXIPC_FINDER_TCP_MESSENGER_HH__
00025 #define __LIBXIPC_FINDER_TCP_MESSENGER_HH__
00026 
00027 
00028 
00029 #include "libxorp/ref_ptr.hh"
00030 
00031 #include "finder_tcp.hh"
00032 #include "finder_msgs.hh"
00033 #include "finder_messenger.hh"
00034 
00035 class FinderTcpMessenger
00036     : public FinderMessengerBase, protected FinderTcpBase
00037 {
00038 public:
00039     FinderTcpMessenger(EventLoop&       e,
00040                FinderMessengerManager*  mm,
00041                XorpFd           sock,
00042                XrlCmdMap&       cmds);
00043 
00044     virtual ~FinderTcpMessenger();
00045 
00046     bool send(const Xrl& xrl, const SendCallback& scb);
00047 
00048     bool pending() const;
00049 
00050     void close()    { FinderTcpBase::close(); }
00051     
00052 protected:
00053     // FinderTcpBase methods
00054     bool read_event(int        errval,
00055             const uint8_t* data,
00056             uint32_t       data_bytes);
00057 
00058     void write_event(int        errval,
00059              const uint8_t* data,
00060              uint32_t       data_bytes);
00061 
00062     void close_event();
00063 
00064     void error_event();
00065     
00066 protected:
00067     void reply(uint32_t seqno, const XrlError& xe, const XrlArgs* reply_args);
00068 
00069 protected:
00070     void push_queue();
00071     void drain_queue();
00072     
00073     /* High water-mark to disable reads, ie reading faster than writing */
00074     static const uint32_t OUTQUEUE_BLOCK_READ_HI_MARK = 6;
00075 
00076     /* Low water-mark to enable reads again */
00077     static const uint32_t OUTQUEUE_BLOCK_READ_LO_MARK = 4;    
00078 
00079     typedef list<const FinderMessageBase*> OutputQueue;
00080     OutputQueue _out_queue;
00081 };
00082 
00086 class FinderTcpListener : public FinderTcpListenerBase {
00087 public:
00088     typedef FinderTcpListenerBase::AddrList Addr4List;
00089     typedef FinderTcpListenerBase::NetList Net4List;
00090 
00091     FinderTcpListener(EventLoop& e,
00092               FinderMessengerManager& mm,
00093               XrlCmdMap& cmds,
00094               IPv4 iface,
00095               uint16_t port,
00096               bool enabled = true)
00097     throw (InvalidAddress, InvalidPort);
00098 
00099     virtual ~FinderTcpListener();
00100 
00105     bool connection_event(XorpFd sock);
00106 
00107 protected:
00108     FinderMessengerManager& _mm;
00109     XrlCmdMap& _cmds;
00110 };
00111 
00112 class FinderTcpConnector {
00113 public:
00114     FinderTcpConnector(EventLoop&       e,
00115                FinderMessengerManager&  mm,
00116                XrlCmdMap&       cmds,
00117                IPv4         host,
00118                uint16_t         port);
00119     virtual ~FinderTcpConnector();
00120 
00128     int connect(FinderTcpMessenger*& created_messenger);
00129 
00130     IPv4 finder_address() const;
00131     uint16_t finder_port() const;
00132     
00133 protected:
00134     EventLoop&          _e;
00135     FinderMessengerManager& _mm;
00136     XrlCmdMap&          _cmds;
00137     IPv4            _host;
00138     uint16_t            _port;
00139 };
00140 
00146 class FinderTcpAutoConnector
00147     : public FinderTcpConnector, public FinderMessengerManager
00148 {
00149 public:
00150     FinderTcpAutoConnector(
00151                EventLoop&               e,
00152                FinderMessengerManager&  mm,
00153                XrlCmdMap&               cmds,
00154                IPv4                 host,
00155                uint16_t             port,
00156                bool                 enabled     = true,
00157                uint32_t         give_up_ms  = 0
00158                );
00159     virtual ~FinderTcpAutoConnector();
00160 
00161     void set_enabled(bool en);
00162     bool enabled() const;
00163     bool connected() const;
00164     bool connect_failed() const;
00165 
00166 protected:
00167     void do_auto_connect();
00168     void start_timer(uint32_t ms = 0);
00169     void stop_timer();
00170 
00171 protected:
00172     /*
00173      * Implement FinderMessengerManager interface to catch death of
00174      * active messenger to trigger auto-reconnect.  All methods are
00175      * forwarded to _real_manager.
00176      */
00177     void messenger_birth_event(FinderMessengerBase*);
00178     void messenger_death_event(FinderMessengerBase*);
00179     void messenger_active_event(FinderMessengerBase*);
00180     void messenger_inactive_event(FinderMessengerBase*);
00181     void messenger_stopped_event(FinderMessengerBase*);
00182     bool manages(const FinderMessengerBase*) const;
00183 
00184 protected:
00185     FinderMessengerManager& _real_manager;
00186     bool            _connected;
00187     bool            _connect_failed;
00188     bool            _enabled;
00189     bool            _once_active;
00190     XorpTimer           _retry_timer;
00191     XorpTimer           _giveup_timer;
00192     int             _last_error;
00193     size_t          _consec_error;
00194 
00195     static const uint32_t CONNECT_RETRY_PAUSE_MS = 100;
00196     static const uint32_t CONNECT_FAILS_BEFORE_LOGGING = 10;
00197 };
00198 
00199 #endif // __LIBXIPC_FINDER_TCP_MESSENGER_HH__
 All Classes Namespaces Functions Variables Typedefs Enumerations