src/applications/udp-echo/udp-echo-client.h
author Wilson Thong <wilsonwk@ee.cityu.edu.hk>
Thu Aug 13 09:47:53 2009 +0200 (2009-08-13)
changeset 4710 e56da5fd6697
parent 4645 d53223aae797
child 5152 f14eff131d13
permissions -rw-r--r--
bug 638: add missing tx trace source
     1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
     2 /*
     3  * Copyright 2007 University of Washington
     4  * 
     5  * This program is free software; you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License version 2 as
     7  * published by the Free Software Foundation;
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program; if not, write to the Free Software
    16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    17  */
    18 
    19 #ifndef __UDP_ECHO_CLIENT_H__
    20 #define __UDP_ECHO_CLIENT_H__
    21 
    22 #include "ns3/application.h"
    23 #include "ns3/event-id.h"
    24 #include "ns3/ptr.h"
    25 #include "ns3/ipv4-address.h"
    26 #include "ns3/traced-callback.h"
    27 
    28 namespace ns3 {
    29 
    30 class Socket;
    31 class Packet;
    32 
    33 /**
    34  * \ingroup udpecho
    35  * \brief A Udp Echo client
    36  *
    37  * Every packet sent should be returned by the server and received here.
    38  */
    39 class UdpEchoClient : public Application 
    40 {
    41 public:
    42   static TypeId GetTypeId (void);
    43 
    44   UdpEchoClient ();
    45 
    46   virtual ~UdpEchoClient ();
    47 
    48   void SetRemote (Ipv4Address ip, uint16_t port);
    49 
    50   /**
    51    * Set the data size of the packet (the number of bytes that are sent as data
    52    * to the server).  The contents of the data are set to unspecified (don't
    53    * care) by this call.
    54    *
    55    * \warning If you have set the fill data for the echo client using one of the
    56    * SetFill calls, this will undo those effects.
    57    *
    58    * \param dataSize The size of the echo data you want to sent.
    59    */
    60   void SetDataSize (uint32_t dataSize);
    61 
    62   /**
    63    * Get the number of data bytes that will be sent to the server.
    64    *
    65    * \warning The number of bytes may be modified by calling any one of the 
    66    * SetFill methods.  If you have called SetFill, then the number of 
    67    * data bytes will correspond to the size of an initialized data buffer.
    68    * If you have not called a SetFill method, the number of data bytes will
    69    * correspond to the number of don't care bytes that will be sent.
    70    *
    71    * \returns The number of data bytes.
    72    */
    73   uint32_t GetDataSize (void) const;
    74 
    75   /**
    76    * Set the data fill of the packet (what is sent as data to the server) to 
    77    * the zero-terminated contents of the fill string string.
    78    *
    79    * \warning The size of resulting echo packets will be automatically adjusted
    80    * to reflect the size of the fill string -- this means that the PacketSize
    81    * attribute may be changed as a result of this call.
    82    *
    83    * \param fill The string to use as the actual echo data bytes.
    84    */
    85   void SetFill (std::string fill);
    86 
    87   /**
    88    * Set the data fill of the packet (what is sent as data to the server) to 
    89    * the repeated contents of the fill byte.  i.e., the fill byte will be 
    90    * used to initialize the contents of the data packet.
    91    * 
    92    * \warning The size of resulting echo packets will be automatically adjusted
    93    * to reflect the dataSize parameter -- this means that the PacketSize
    94    * attribute may be changed as a result of this call.
    95    *
    96    * \param fill The byte to be repeated in constructing the packet data..
    97    * \param dataSize The desired size of the resulting echo packet data.
    98    */
    99   void SetFill (uint8_t fill, uint32_t dataSize);
   100 
   101   /**
   102    * Set the data fill of the packet (what is sent as data to the server) to
   103    * the contents of the fill buffer, repeated as many times as is required.
   104    *
   105    * Initializing the packet to the contents of a provided single buffer is 
   106    * accomplished by setting the fillSize set to your desired dataSize
   107    * (and providing an appropriate buffer).
   108    *
   109    * \warning The size of resulting echo packets will be automatically adjusted
   110    * to reflect the dataSize parameter -- this means that the PacketSize
   111    * attribute of the Application may be changed as a result of this call.
   112    *
   113    * \param fill The fill pattern to use when constructing packets.
   114    * \param fillSize The number of bytes in the provided fill pattern.
   115    * \param dataSize The desired size of the final echo data.
   116    */
   117   void SetFill (uint8_t *fill, uint32_t fillSize, uint32_t dataSize);
   118 
   119 protected:
   120   virtual void DoDispose (void);
   121 
   122 private:
   123 
   124   virtual void StartApplication (void);
   125   virtual void StopApplication (void);
   126 
   127   void ScheduleTransmit (Time dt);
   128   void Send (void);
   129 
   130   void HandleRead (Ptr<Socket> socket);
   131 
   132   uint32_t m_count;
   133   Time m_interval;
   134   uint32_t m_size;
   135 
   136   uint32_t m_dataSize;
   137   uint8_t *m_data;
   138 
   139   uint32_t m_sent;
   140   Ptr<Socket> m_socket;
   141   Ipv4Address m_peerAddress;
   142   uint16_t m_peerPort;
   143   EventId m_sendEvent;
   144   /// Callbacks for tracing the packet Tx events
   145   TracedCallback<Ptr<const Packet> > m_txTrace;
   146 };
   147 
   148 } // namespace ns3
   149 
   150 #endif // __UDP_ECHO_CLIENT_H__
   151