xorp

xrl_pf_stcp.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 __LIBXIPC_XRL_PF_STCP_HH__
00023 #define __LIBXIPC_XRL_PF_STCP_HH__
00024 
00025 #include "xrl_pf.hh"
00026 #include "libxorp/asyncio.hh"
00027 #include "libxorp/buffered_asyncio.hh"
00028 
00029 class Xrl;
00030 
00031 // ----------------------------------------------------------------------------
00032 // XRL Protocol Family : Simplest TCP
00033 
00034 class STCPRequestHandler;
00035 class XrlPFSTCPSender;
00036 class RequestState;
00037 
00041 class XrlPFSTCPListener : public XrlPFListener {
00042 public:
00043     XrlPFSTCPListener(EventLoop& e, XrlDispatcher* xr = 0, uint16_t port = 0)
00044     throw (XrlPFConstructorError);
00045     virtual ~XrlPFSTCPListener();
00046 
00047     virtual const char* address() const  { return _address_slash_port.c_str(); }
00048     virtual const char* protocol() const { return _protocol; }
00049 
00050     void add_request_handler(STCPRequestHandler* h);
00051     void remove_request_handler(const STCPRequestHandler* h);
00052     void connect_hook(XorpFd fd, IoEventType type);
00053     bool response_pending() const;
00054 
00055     virtual string toString() const;
00056 
00057 protected:
00058     XrlPFSTCPListener(EventLoop* e, XrlDispatcher* xr = 0);
00059 
00060     XorpFd  _sock;
00061     string  _address_slash_port;
00062 
00063 private:
00064     list<STCPRequestHandler*>   _request_handlers;
00065 
00066     static const char*      _protocol;
00067 };
00068 
00072 class XrlPFSTCPSender : public XrlPFSender {
00073 public:
00074     XrlPFSTCPSender(const string& name, EventLoop& e, const char* address = 0,
00075     TimeVal keepalive_period = DEFAULT_SENDER_KEEPALIVE_PERIOD)
00076     throw (XrlPFConstructorError);
00077     XrlPFSTCPSender(const string& name, EventLoop* e, const char* address = 0,
00078             TimeVal keepalive_period = DEFAULT_SENDER_KEEPALIVE_PERIOD);
00079     virtual ~XrlPFSTCPSender();
00080 
00081     bool send(const Xrl&            x,
00082           bool              direct_call,
00083           const XrlPFSender::SendCallback&  cb);
00084 
00085     bool            sends_pending() const;
00086     bool            alive() const           { return _sock.is_valid(); }
00087     virtual const char* protocol() const;
00088     static const char*  protocol_name()         { return _protocol; }
00089     void            set_keepalive_time(const TimeVal& time);
00090     const TimeVal&  keepalive_time() const      { return _keepalive_time; }
00091     virtual string toString() const; // for debugging
00092 
00093 protected:
00094     void construct();
00095 
00096     XorpFd _sock;
00097 
00098 private:
00099     void die(const char* reason, bool verbose = true);
00100 
00101     void update_writer(AsyncFileWriter::Event   e,
00102                const uint8_t*       buffer,
00103                size_t           buffer_bytes,
00104                size_t           bytes_done);
00105 
00106     RequestState* find_request(uint32_t seqno);
00107 
00108     void read_event(BufferedAsyncReader*    reader,
00109             BufferedAsyncReader::Event  ev,
00110             uint8_t*            buffer,
00111             size_t          buffer_bytes);
00112 
00113     typedef map<uint32_t, ref_ptr<RequestState> > RequestMap;
00114     void send_request(RequestState*);
00115     void dispose_request(RequestMap::iterator ptr);
00116 
00117     void start_keepalives();
00118     void stop_keepalives();
00119     void defer_keepalives();
00120     bool send_keepalive();
00121 
00122 public:
00123     static const TimeVal     DEFAULT_SENDER_KEEPALIVE_PERIOD;
00124 
00125 private:
00126     uint32_t             _uid;
00127 
00128     // Transmission related
00129     AsyncFileWriter*          _writer;
00130 
00131     list<ref_ptr<RequestState> > _requests_waiting; // All requests pending
00132 
00133     RequestMap           _requests_sent;    // All requests pending
00134 
00135     uint32_t             _current_seqno;
00136     size_t           _active_bytes;
00137     size_t           _active_requests;
00138 
00139     // Tunable timer variables
00140     TimeVal         _keepalive_time;
00141 
00142     // Reception related
00143     BufferedAsyncReader*     _reader;
00144     vector<uint8_t>      _reply;
00145 
00146     // Keepalive related
00147     XorpTimer            _keepalive_timer;
00148     TimeVal          _keepalive_last_fired;
00149     bool             _keepalive_sent;
00150 
00151     // General stuff
00152     static const char*       _protocol;
00153     static uint32_t      _next_uid;
00154 };
00155 
00156 #endif // __LIBXIPC_XRL_PF_STCP_HH__
 All Classes Namespaces Functions Variables Typedefs Enumerations