00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "ns3/log.h"
00019 #include "ns3/ipv4-address.h"
00020 #include "ns3/nstime.h"
00021 #include "ns3/inet-socket-address.h"
00022 #include "ns3/socket.h"
00023 #include "ns3/simulator.h"
00024 #include "ns3/socket-factory.h"
00025 #include "ns3/packet.h"
00026 #include "ns3/uinteger.h"
00027 #include "udp-echo-client.h"
00028
00029 namespace ns3 {
00030
00031 NS_LOG_COMPONENT_DEFINE ("UdpEchoClientApplication");
00032 NS_OBJECT_ENSURE_REGISTERED (UdpEchoClient);
00033
00034 TypeId
00035 UdpEchoClient::GetTypeId (void)
00036 {
00037 static TypeId tid = TypeId ("ns3::UdpEchoClient")
00038 .SetParent<Application> ()
00039 .AddConstructor<UdpEchoClient> ()
00040 .AddAttribute ("MaxPackets",
00041 "The maximum number of packets the application will send",
00042 UintegerValue (100),
00043 MakeUintegerAccessor (&UdpEchoClient::m_count),
00044 MakeUintegerChecker<uint32_t> ())
00045 .AddAttribute ("Interval",
00046 "The time to wait between packets",
00047 TimeValue (Seconds (1.0)),
00048 MakeTimeAccessor (&UdpEchoClient::m_interval),
00049 MakeTimeChecker ())
00050 .AddAttribute ("RemoteAddress",
00051 "The destination Ipv4Address of the outbound packets",
00052 Ipv4AddressValue (),
00053 MakeIpv4AddressAccessor (&UdpEchoClient::m_peerAddress),
00054 MakeIpv4AddressChecker ())
00055 .AddAttribute ("RemotePort",
00056 "The destination port of the outbound packets",
00057 UintegerValue (0),
00058 MakeUintegerAccessor (&UdpEchoClient::m_peerPort),
00059 MakeUintegerChecker<uint16_t> ())
00060 .AddAttribute ("PacketSize", "Size of packets generated",
00061 UintegerValue (100),
00062 MakeUintegerAccessor (&UdpEchoClient::m_size),
00063 MakeUintegerChecker<uint32_t> ())
00064 ;
00065 return tid;
00066 }
00067
00068 UdpEchoClient::UdpEchoClient ()
00069 {
00070 NS_LOG_FUNCTION_NOARGS ();
00071 m_sent = 0;
00072 m_socket = 0;
00073 m_sendEvent = EventId ();
00074 }
00075
00076 UdpEchoClient::~UdpEchoClient()
00077 {
00078 NS_LOG_FUNCTION_NOARGS ();
00079 }
00080
00081 void
00082 UdpEchoClient::SetRemote (Ipv4Address ip, uint16_t port)
00083 {
00084 m_peerAddress = ip;
00085 m_peerPort = port;
00086 }
00087
00088 void
00089 UdpEchoClient::DoDispose (void)
00090 {
00091 NS_LOG_FUNCTION_NOARGS ();
00092 Application::DoDispose ();
00093 }
00094
00095 void
00096 UdpEchoClient::StartApplication (void)
00097 {
00098 NS_LOG_FUNCTION_NOARGS ();
00099
00100 if (m_socket == 0)
00101 {
00102 TypeId tid = TypeId::LookupByName ("ns3::UdpSocketFactory");
00103 m_socket = Socket::CreateSocket (GetNode(), tid);
00104 m_socket->Bind ();
00105 m_socket->Connect (InetSocketAddress (m_peerAddress, m_peerPort));
00106 }
00107
00108 m_socket->SetRecvCallback(MakeCallback(&UdpEchoClient::HandleRead, this));
00109
00110 ScheduleTransmit (Seconds(0.));
00111 }
00112
00113 void
00114 UdpEchoClient::StopApplication ()
00115 {
00116 NS_LOG_FUNCTION_NOARGS ();
00117
00118 if (m_socket != 0)
00119 {
00120 m_socket->SetRecvCallback(MakeNullCallback<void, Ptr<Socket> > ());
00121 }
00122
00123 Simulator::Cancel(m_sendEvent);
00124 }
00125
00126 void
00127 UdpEchoClient::ScheduleTransmit (Time dt)
00128 {
00129 NS_LOG_FUNCTION_NOARGS ();
00130 m_sendEvent = Simulator::Schedule(dt, &UdpEchoClient::Send, this);
00131 }
00132
00133 void
00134 UdpEchoClient::Send (void)
00135 {
00136 NS_LOG_FUNCTION_NOARGS ();
00137
00138 NS_ASSERT (m_sendEvent.IsExpired ());
00139
00140 Ptr<Packet> p = Create<Packet> (m_size);
00141 m_socket->Send (p);
00142 ++m_sent;
00143
00144 NS_LOG_INFO ("Sent " << m_size << " bytes to " << m_peerAddress);
00145
00146 if (m_sent < m_count)
00147 {
00148 ScheduleTransmit (m_interval);
00149 }
00150 }
00151
00152 void
00153 UdpEchoClient::HandleRead (Ptr<Socket> socket)
00154 {
00155 NS_LOG_FUNCTION (this << socket);
00156 Ptr<Packet> packet;
00157 Address from;
00158 while (packet = socket->RecvFrom (from))
00159 {
00160 if (InetSocketAddress::IsMatchingType (from))
00161 {
00162 InetSocketAddress address = InetSocketAddress::ConvertFrom (from);
00163 NS_LOG_INFO ("Received " << packet->GetSize() << " bytes from " <<
00164 address.GetIpv4());
00165 }
00166 }
00167 }
00168
00169 }