xorp

routing_socket.hh

00001 // -*- c-basic-offset: 4; tab-width: 8; indent-tabs-mode: t -*-
00002 
00003 // Copyright (c) 2001-2011 XORP, Inc and Others
00004 //
00005 // This program is free software; you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License, Version 2, June
00007 // 1991 as published by the Free Software Foundation. Redistribution
00008 // and/or modification of this program under the terms of any other
00009 // version of the GNU General Public License is not permitted.
00010 // 
00011 // This program is distributed in the hope that it will be useful, but
00012 // WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For more details,
00014 // see the GNU General Public License, Version 2, a copy of which can be
00015 // found in the XORP LICENSE.gpl file.
00016 // 
00017 // XORP Inc, 2953 Bunker Hill Lane, Suite 204, Santa Clara, CA 95054, USA;
00018 // http://xorp.net
00019 
00020 
00021 #ifndef __FEA_DATA_PLANE_CONTROL_SOCKET_ROUTING_SOCKET_HH__
00022 #define __FEA_DATA_PLANE_CONTROL_SOCKET_ROUTING_SOCKET_HH__
00023 
00024 #include <xorp_config.h>
00025 #ifdef HAVE_ROUTING_SOCKETS
00026 
00027 
00028 
00029 #include "libxorp/eventloop.hh"
00030 #include "libxorp/exceptions.hh"
00031 
00032 
00033 class RoutingSocketObserver;
00034 struct RoutingSocketPlumber;
00035 
00041 class RoutingSocket :
00042     public NONCOPYABLE
00043 {
00044 public:
00045     RoutingSocket(EventLoop& eventloop);
00046     ~RoutingSocket();
00047 
00055     int start(int af, string& error_msg);
00056 
00063     int start(string& error_msg) { return start(AF_UNSPEC, error_msg); }
00064 
00071     int stop(string& error_msg);
00072 
00081     bool is_open() const { return _fd >= 0; }
00082 
00091     ssize_t write(const void* data, size_t nbytes);
00092 
00102     uint32_t seqno() const { return (_instance_no << 16 | _seqno); }
00103 
00109     pid_t pid() const { return _pid; }
00110 
00121     int force_read(string& error_msg);
00122 
00123 private:
00124     typedef list<RoutingSocketObserver*> ObserverList;
00125 
00131     void io_event(XorpFd fd, IoEventType type);
00132 
00133 private:
00134     static const size_t ROUTING_SOCKET_BYTES = 8*1024;  // Initial guess at msg size
00135 
00136 private:
00137     EventLoop&   _eventloop;
00138     int      _fd;
00139     ObserverList _ol;
00140 
00141     uint16_t     _seqno;    // Seqno of next write()
00142     uint16_t     _instance_no;  // Instance number of this routing socket
00143 
00144     static uint16_t _instance_cnt;
00145     static pid_t    _pid;
00146 
00147     friend class RoutingSocketPlumber; // class that hooks observers in and out
00148 };
00149 
00150 class RoutingSocketObserver {
00151 public:
00152     RoutingSocketObserver(RoutingSocket& rs);
00153 
00154     virtual ~RoutingSocketObserver();
00155 
00165     virtual void routing_socket_data(const vector<uint8_t>& buffer) = 0;
00166 
00170     RoutingSocket& routing_socket();
00171 
00172 private:
00173     RoutingSocket& _rs;
00174 };
00175 
00176 class RoutingSocketReader : public RoutingSocketObserver {
00177 public:
00178     RoutingSocketReader(RoutingSocket& rs);
00179     virtual ~RoutingSocketReader();
00180 
00189     int receive_data(RoutingSocket& rs, uint32_t seqno, string& error_msg);
00190 
00196     const vector<uint8_t>& buffer() const { return (_cache_data); }
00197 
00207     virtual void routing_socket_data(const vector<uint8_t>& buffer);
00208 
00209 private:
00210     RoutingSocket&  _rs;
00211 
00212     bool        _cache_valid;   // Cache data arrived.
00213     uint32_t        _cache_seqno;   // Seqno of routing socket data to
00214                     // cache so reading via routing
00215                     // socket can appear synchronous.
00216     vector<uint8_t> _cache_data;    // Cached routing socket data.
00217 };
00218 
00219 #endif
00220 #endif // __FEA_DATA_PLANE_CONTROL_SOCKET_ROUTING_SOCKET_HH__
 All Classes Namespaces Functions Variables Typedefs Enumerations