A Discrete-Event Network Simulator
API
packet-socket-client.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014 Universita' di Firenze
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Author: Tommaso Pecorella <tommaso.pecorella@unifi.it>
18  */
19 
20 #include "packet-socket-client.h"
21 
22 #include "ns3/abort.h"
23 #include "ns3/log.h"
24 #include "ns3/nstime.h"
25 #include "ns3/packet-socket-address.h"
26 #include "ns3/packet-socket-factory.h"
27 #include "ns3/packet-socket.h"
28 #include "ns3/packet.h"
29 #include "ns3/simulator.h"
30 #include "ns3/socket-factory.h"
31 #include "ns3/socket.h"
32 #include "ns3/uinteger.h"
33 
34 #include <cstdio>
35 #include <cstdlib>
36 
37 namespace ns3
38 {
39 
40 NS_LOG_COMPONENT_DEFINE("PacketSocketClient");
41 
42 NS_OBJECT_ENSURE_REGISTERED(PacketSocketClient);
43 
44 TypeId
46 {
47  static TypeId tid =
48  TypeId("ns3::PacketSocketClient")
50  .SetGroupName("Network")
51  .AddConstructor<PacketSocketClient>()
52  .AddAttribute(
53  "MaxPackets",
54  "The maximum number of packets the application will send (zero means infinite)",
55  UintegerValue(100),
57  MakeUintegerChecker<uint32_t>())
58  .AddAttribute("Interval",
59  "The time to wait between packets",
60  TimeValue(Seconds(1.0)),
63  .AddAttribute("PacketSize",
64  "Size of packets generated (bytes).",
65  UintegerValue(1024),
67  MakeUintegerChecker<uint32_t>())
68  .AddAttribute("Priority",
69  "Priority assigned to the packets generated.",
70  UintegerValue(0),
73  MakeUintegerChecker<uint8_t>())
74  .AddTraceSource("Tx",
75  "A packet has been sent",
77  "ns3::Packet::AddressTracedCallback");
78  return tid;
79 }
80 
82 {
83  NS_LOG_FUNCTION(this);
84  m_sent = 0;
85  m_socket = nullptr;
86  m_sendEvent = EventId();
87  m_peerAddressSet = false;
88 }
89 
91 {
92  NS_LOG_FUNCTION(this);
93 }
94 
95 void
97 {
98  NS_LOG_FUNCTION(this << addr);
99  m_peerAddress = addr;
100  m_peerAddressSet = true;
101 }
102 
103 void
105 {
106  NS_LOG_FUNCTION(this);
108 }
109 
110 void
112 {
113  m_priority = priority;
114  if (m_socket)
115  {
116  m_socket->SetPriority(priority);
117  }
118 }
119 
120 uint8_t
122 {
123  return m_priority;
124 }
125 
126 void
128 {
129  NS_LOG_FUNCTION(this);
130  NS_ASSERT_MSG(m_peerAddressSet, "Peer address not set");
131 
132  if (!m_socket)
133  {
134  TypeId tid = TypeId::LookupByName("ns3::PacketSocketFactory");
136 
139 
140  if (m_priority)
141  {
143  }
144  }
145 
148 }
149 
150 void
152 {
153  NS_LOG_FUNCTION(this);
155  m_socket->Close();
156 }
157 
158 void
160 {
161  NS_LOG_FUNCTION(this);
163 
164  Ptr<Packet> p = Create<Packet>(m_size);
165 
166  std::stringstream peerAddressStringStream;
167  peerAddressStringStream << PacketSocketAddress::ConvertFrom(m_peerAddress);
168 
169  if ((m_socket->Send(p)) >= 0)
170  {
172  NS_LOG_INFO("TraceDelay TX " << m_size << " bytes to " << peerAddressStringStream.str()
173  << " Uid: " << p->GetUid()
174  << " Time: " << (Simulator::Now()).GetSeconds());
175  }
176  else
177  {
178  NS_LOG_INFO("Error while sending " << m_size << " bytes to "
179  << peerAddressStringStream.str());
180  }
181  m_sent++;
182 
183  if ((m_sent < m_maxPackets) || (m_maxPackets == 0))
184  {
186  }
187 }
188 
189 } // Namespace ns3
The base class for all ns3 applications.
Definition: application.h:61
void DoDispose() override
Destructor implementation.
Definition: application.cc:85
Ptr< Node > GetNode() const
Definition: application.cc:107
An identifier for simulation events.
Definition: event-id.h:55
bool IsExpired() const
This method is syntactic sugar for the ns3::Simulator::IsExpired method.
Definition: event-id.cc:69
uint64_t GetUid() const
Returns the packet's Uid.
Definition: packet.cc:412
an address for a packet socket
static PacketSocketAddress ConvertFrom(const Address &address)
uint32_t m_sent
Counter for sent packets.
void StopApplication() override
Application specific shutdown code.
TracedCallback< Ptr< const Packet >, const Address & > m_txTrace
Traced Callback: sent packets, source address.
Ptr< Socket > m_socket
Socket.
void DoDispose() override
Destructor implementation.
uint8_t GetPriority() const
Query the priority value of this socket.
EventId m_sendEvent
Event to send the next packet.
uint32_t m_maxPackets
Maximum number of packets the application will send.
void SetPriority(uint8_t priority)
Manually set the socket priority.
static TypeId GetTypeId()
Get the type ID.
bool m_peerAddressSet
Sanity check.
void SetRemote(PacketSocketAddress addr)
set the remote address and protocol to be used
uint32_t m_size
Size of the sent packet.
uint8_t m_priority
Priority of the sent packets.
void StartApplication() override
Application specific startup code.
PacketSocketAddress m_peerAddress
Remote peer address.
Time m_interval
Packet inter-send time.
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:568
static void Cancel(const EventId &id)
Set the cancel bit on this event: the event's associated function will not be invoked when it expires...
Definition: simulator.cc:276
static Time Now()
Return the current simulation virtual time.
Definition: simulator.cc:199
static EventId ScheduleNow(FUNC f, Ts &&... args)
Schedule an event to expire Now.
Definition: simulator.h:606
virtual int Send(Ptr< Packet > p, uint32_t flags)=0
Send data (or dummy data) to the remote host.
void SetRecvCallback(Callback< void, Ptr< Socket >> receivedData)
Notify application when new data is available to be read.
Definition: socket.cc:126
void SetPriority(uint8_t priority)
Manually set the socket priority.
Definition: socket.cc:384
virtual int Connect(const Address &address)=0
Initiate a connection to a remote host.
static Ptr< Socket > CreateSocket(Ptr< Node > node, TypeId tid)
This method wraps the creation of sockets that is performed on a given node by a SocketFactory specif...
Definition: socket.cc:72
virtual int Close()=0
Close a socket.
virtual int Bind(const Address &address)=0
Allocate a local endpoint for this socket.
AttributeValue implementation for Time.
Definition: nstime.h:1423
a unique identifier for an interface.
Definition: type-id.h:60
static TypeId LookupByName(std::string name)
Get a TypeId by name.
Definition: type-id.cc:839
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:935
Hold an unsigned integer type.
Definition: uinteger.h:45
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition: assert.h:66
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
Definition: assert.h:86
Ptr< const AttributeAccessor > MakeTimeAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Definition: nstime.h:1424
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Definition: uinteger.h:46
Callback< R, Args... > MakeNullCallback()
Definition: callback.h:750
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:275
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:46
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1336
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< const AttributeChecker > MakeTimeChecker(const Time min, const Time max)
Helper to make a Time checker with bounded range.
Definition: time.cc:535