xorp

test_peer.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 // $XORP: xorp/bgp/harness/test_peer.hh,v 1.20 2008/10/02 21:56:26 bms Exp $
00021 
00022 #ifndef __BGP_HARNESS_TEST_PEER_HH__
00023 #define __BGP_HARNESS_TEST_PEER_HH__
00024 
00025 #include "xrl/targets/test_peer_base.hh"
00026 #include "bgp/packet.hh"
00027 
00028 
00029 class TestPeer {
00030 public:
00031     TestPeer(EventLoop& eventloop, XrlRouter& xrlrouter, const char *server,
00032          bool verbose);
00033     ~TestPeer();
00034     bool done();
00035     void register_coordinator(const string& name);
00036     void register_genid(const uint32_t& genid);
00037     bool packetisation(const string& protocol);
00038     void use_4byte_asnums(bool use);
00039     bool connect(const string& host, const uint32_t& port, 
00040          string& error_string);
00041     bool listen(const string& host, const uint32_t& port,
00042         string& error_string);
00043     bool bind(const string& host, const uint32_t& port,
00044           string& error_string);
00045     bool send(const vector<uint8_t>& data, string& error_string);
00046     void send_complete(AsyncFileWriter::Event ev, const uint8_t *buf,
00047                const size_t buf_bytes, const size_t offset);
00048     bool zap(XorpFd &fd, const char *msg);
00049     bool disconnect(string& error_string);
00050     void reset();
00051     bool terminate(string& error_string);
00052 
00053 protected:
00054     void connect_attempt(XorpFd fd, IoEventType type);
00055     void receive(XorpFd fd, IoEventType type);
00056     enum status {
00057     GOOD,
00058     CLOSED,
00059     ERROR
00060     };
00061     void datain(status st, uint8_t *ptr, size_t len, string error = "");
00062     void queue_data(status st, uint8_t *ptr, size_t len, string error);
00063     void sendit();
00064     void xrl_callback(const XrlError& error);
00065 
00066 private:
00067     TestPeer(const TestPeer&);
00068     TestPeer& operator=(const TestPeer&);
00069 
00070 private:
00071     EventLoop& _eventloop;
00072     XrlRouter& _xrlrouter;
00073 
00074     /* these are needed so we know how to decode ASnums (4-byte or 2-byte); */
00075     LocalData* _localdata;
00076     BGPPeerData* _peerdata;
00077 
00078     const char *_server;
00079     const bool _verbose;
00080 
00081     bool _done;
00082     XorpFd _s;
00083     XorpFd _listen;
00084     XorpFd _bind;
00085     AsyncFileWriter *_async_writer;
00086     string _coordinator;
00087     uint32_t _genid;
00088 
00089     bool _bgp;  // Perform BGP packetisation
00090     bool _use_4byte_asnums; // Assume AS numbers from peer are 4-byte.
00091 
00092     struct Queued {
00093     uint32_t secs;
00094     uint32_t micro;
00095     status st;
00096     size_t len;
00097     vector<uint8_t> v;
00098     string error;
00099     };
00100     queue <Queued> _xrl_queue;
00101     static const int FLYING_LIMIT = 100;    // XRL's allowed in flight at
00102                         // one time.
00103     int _flying;
00104 
00105     size_t _bgp_bytes;
00106     uint8_t _bgp_buf[BGPPacket::MAXPACKETSIZE]; // Maximum allowed BGP message
00107 };
00108 
00109 class XrlTestPeerTarget : XrlTestPeerTargetBase {
00110 public:
00111     XrlTestPeerTarget(XrlRouter *r, TestPeer& test_peer, bool trace);
00112 
00116     XrlCmdError common_0_1_get_target_name(
00117     // Output values, 
00118     string& name);
00119 
00120     XrlCmdError common_0_1_startup() { return XrlCmdError::OKAY(); }
00121 
00125     XrlCmdError common_0_1_get_version(
00126     // Output values, 
00127     string& version);
00128 
00132     XrlCmdError common_0_1_get_status(
00133                       // Output values,
00134                       uint32_t& status,
00135                       string&   reason);
00136 
00140     XrlCmdError common_0_1_shutdown();
00141 
00145     XrlCmdError test_peer_0_1_register(
00146     // Input values, 
00147        const string&    coordinator,
00148        const uint32_t&  genid);
00149 
00153     XrlCmdError test_peer_0_1_packetisation(
00154     // Input values, 
00155     const string&   protocol);
00156 
00160     XrlCmdError test_peer_0_1_use_4byte_asnums(
00161     // Input values, 
00162     const bool& use);
00163 
00171     XrlCmdError test_peer_0_1_connect(
00172     // Input values, 
00173     const string&   host, 
00174     const uint32_t& port);
00175 
00183     XrlCmdError test_peer_0_1_listen(
00184     // Input values, 
00185     const string&   address, 
00186     const uint32_t& port);
00187 
00195     XrlCmdError test_peer_0_1_bind(
00196     // Input values,
00197     const string&   address,
00198     const uint32_t& port);
00199 
00203     XrlCmdError test_peer_0_1_send(
00204     // Input values, 
00205     const vector<uint8_t>&  data);
00206 
00210     XrlCmdError test_peer_0_1_disconnect();
00211 
00215     XrlCmdError test_peer_0_1_reset();
00216 
00220     XrlCmdError test_peer_0_1_terminate();
00221 
00222 private:
00223     TestPeer& _test_peer;
00224     bool _trace;
00225 };
00226 
00227 
00228 #endif // __BGP_HARNESS_TEST_PEER_HH__
 All Classes Namespaces Functions Variables Typedefs Enumerations