LEFT | RIGHT |
(no file at all) | |
| 1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 /* |
| 3 * Copyright (c) 2014 Universita' di Firenze |
| 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 * Author: Tommaso Pecorella <tommaso.pecorella@unifi.it> |
| 19 */ |
| 20 |
| 21 #include "ns3/log.h" |
| 22 #include "ns3/nstime.h" |
| 23 #include "ns3/packet-socket-address.h" |
| 24 #include "ns3/packet-socket.h" |
| 25 #include "ns3/packet-socket-factory.h" |
| 26 #include "ns3/socket.h" |
| 27 #include "ns3/simulator.h" |
| 28 #include "ns3/socket-factory.h" |
| 29 #include "ns3/packet.h" |
| 30 #include "ns3/uinteger.h" |
| 31 #include "ns3/abort.h" |
| 32 #include "packet-socket-client.h" |
| 33 #include <cstdlib> |
| 34 #include <cstdio> |
| 35 |
| 36 namespace ns3 { |
| 37 |
| 38 NS_LOG_COMPONENT_DEFINE ("PacketSocketClient"); |
| 39 NS_OBJECT_ENSURE_REGISTERED (PacketSocketClient); |
| 40 |
| 41 TypeId |
| 42 PacketSocketClient::GetTypeId (void) |
| 43 { |
| 44 static TypeId tid = TypeId ("ns3::PacketSocketClient") |
| 45 .SetParent<Application> () |
| 46 .AddConstructor<PacketSocketClient> () |
| 47 .AddAttribute ("MaxPackets", |
| 48 "The maximum number of packets the application will send (zer
o means infinite)", |
| 49 UintegerValue (100), |
| 50 MakeUintegerAccessor (&PacketSocketClient::m_maxPackets), |
| 51 MakeUintegerChecker<uint32_t> ()) |
| 52 .AddAttribute ("Interval", |
| 53 "The time to wait between packets", TimeValue (Seconds (1.0))
, |
| 54 MakeTimeAccessor (&PacketSocketClient::m_interval), |
| 55 MakeTimeChecker ()) |
| 56 .AddAttribute ("PacketSize", |
| 57 "Size of packets generated (bytes).", |
| 58 UintegerValue (1024), |
| 59 MakeUintegerAccessor (&PacketSocketClient::m_size), |
| 60 MakeUintegerChecker<uint32_t> ()) |
| 61 .AddTraceSource ("Tx", "A packet has been sent", |
| 62 MakeTraceSourceAccessor (&PacketSocketClient::m_txTrace)) |
| 63 ; |
| 64 return tid; |
| 65 } |
| 66 |
| 67 PacketSocketClient::PacketSocketClient () |
| 68 { |
| 69 NS_LOG_FUNCTION (this); |
| 70 m_sent = 0; |
| 71 m_socket = 0; |
| 72 m_sendEvent = EventId (); |
| 73 m_peerAddressSet = false; |
| 74 } |
| 75 |
| 76 PacketSocketClient::~PacketSocketClient () |
| 77 { |
| 78 NS_LOG_FUNCTION (this); |
| 79 } |
| 80 |
| 81 void |
| 82 PacketSocketClient::SetRemote (PacketSocketAddress addr) |
| 83 { |
| 84 NS_LOG_FUNCTION (this << addr); |
| 85 m_peerAddress = addr; |
| 86 m_peerAddressSet = true; |
| 87 } |
| 88 |
| 89 void |
| 90 PacketSocketClient::DoDispose (void) |
| 91 { |
| 92 NS_LOG_FUNCTION (this); |
| 93 Application::DoDispose (); |
| 94 } |
| 95 |
| 96 void |
| 97 PacketSocketClient::StartApplication (void) |
| 98 { |
| 99 NS_LOG_FUNCTION (this); |
| 100 NS_ASSERT_MSG (m_peerAddressSet, "Local address not set"); |
| 101 |
| 102 if (m_socket == 0) |
| 103 { |
| 104 TypeId tid = TypeId::LookupByName ("ns3::PacketSocketFactory"); |
| 105 m_socket = Socket::CreateSocket (GetNode (), tid); |
| 106 |
| 107 m_socket->Bind (m_peerAddress); |
| 108 m_socket->Connect (m_peerAddress); |
| 109 } |
| 110 |
| 111 m_socket->SetRecvCallback (MakeNullCallback<void, Ptr<Socket> > ()); |
| 112 m_sendEvent = Simulator::ScheduleNow (&PacketSocketClient::Send, this); |
| 113 } |
| 114 |
| 115 void |
| 116 PacketSocketClient::StopApplication (void) |
| 117 { |
| 118 NS_LOG_FUNCTION (this); |
| 119 Simulator::Cancel (m_sendEvent); |
| 120 m_socket->Close (); |
| 121 } |
| 122 |
| 123 void |
| 124 PacketSocketClient::Send (void) |
| 125 { |
| 126 NS_LOG_FUNCTION (this); |
| 127 NS_ASSERT (m_sendEvent.IsExpired ()); |
| 128 |
| 129 Ptr<Packet> p = Create<Packet> (m_size); |
| 130 |
| 131 std::stringstream peerAddressStringStream; |
| 132 peerAddressStringStream << PacketSocketAddress::ConvertFrom (m_peerAddress); |
| 133 |
| 134 if ((m_socket->Send (p)) >= 0) |
| 135 { |
| 136 NS_LOG_INFO ("TraceDelay TX " << m_size << " bytes to " |
| 137 << peerAddressStringStream.str () << " Uid:
" |
| 138 << p->GetUid () << " Time: " |
| 139 << (Simulator::Now ()).GetSeconds ()); |
| 140 } |
| 141 else |
| 142 { |
| 143 NS_LOG_INFO ("Error while sending " << m_size << " bytes to " |
| 144 << peerAddressStringStream.str ()); |
| 145 } |
| 146 m_sent++; |
| 147 |
| 148 if ((m_sent < m_maxPackets) || (m_maxPackets == 0)) |
| 149 { |
| 150 m_txTrace (p, m_peerAddress); |
| 151 m_sendEvent = Simulator::Schedule (m_interval, &PacketSocketClient::Send,
this); |
| 152 } |
| 153 } |
| 154 |
| 155 } // Namespace ns3 |
LEFT | RIGHT |