A Discrete-Event Network Simulator
API
packet-socket-client.cc
Go to the documentation of this file.
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 
40 NS_OBJECT_ENSURE_REGISTERED (PacketSocketClient);
41 
42 TypeId
44 {
45  static TypeId tid = TypeId ("ns3::PacketSocketClient")
47  .SetGroupName("Network")
48  .AddConstructor<PacketSocketClient> ()
49  .AddAttribute ("MaxPackets",
50  "The maximum number of packets the application will send (zero means infinite)",
51  UintegerValue (100),
53  MakeUintegerChecker<uint32_t> ())
54  .AddAttribute ("Interval",
55  "The time to wait between packets", TimeValue (Seconds (1.0)),
57  MakeTimeChecker ())
58  .AddAttribute ("PacketSize",
59  "Size of packets generated (bytes).",
60  UintegerValue (1024),
62  MakeUintegerChecker<uint32_t> ())
63  .AddAttribute ("Priority",
64  "Priority assigned to the packets generated.",
65  UintegerValue (0),
68  MakeUintegerChecker<uint8_t> ())
69  .AddTraceSource ("Tx", "A packet has been sent",
71  "ns3::Packet::AddressTracedCallback")
72  ;
73  return tid;
74 }
75 
77 {
78  NS_LOG_FUNCTION (this);
79  m_sent = 0;
80  m_socket = 0;
81  m_sendEvent = EventId ();
82  m_peerAddressSet = false;
83 }
84 
86 {
87  NS_LOG_FUNCTION (this);
88 }
89 
90 void
92 {
93  NS_LOG_FUNCTION (this << addr);
94  m_peerAddress = addr;
95  m_peerAddressSet = true;
96 }
97 
98 void
100 {
101  NS_LOG_FUNCTION (this);
103 }
104 
105 void
107 {
108  m_priority = priority;
109  if (m_socket)
110  {
111  m_socket->SetPriority (priority);
112  }
113 }
114 
115 uint8_t
117 {
118  return m_priority;
119 }
120 
121 void
123 {
124  NS_LOG_FUNCTION (this);
125  NS_ASSERT_MSG (m_peerAddressSet, "Peer address not set");
126 
127  if (m_socket == 0)
128  {
129  TypeId tid = TypeId::LookupByName ("ns3::PacketSocketFactory");
131 
134 
135  if (m_priority)
136  {
138  }
139  }
140 
143 }
144 
145 void
147 {
148  NS_LOG_FUNCTION (this);
150  m_socket->Close ();
151 }
152 
153 void
155 {
156  NS_LOG_FUNCTION (this);
158 
159  Ptr<Packet> p = Create<Packet> (m_size);
160 
161  std::stringstream peerAddressStringStream;
162  peerAddressStringStream << PacketSocketAddress::ConvertFrom (m_peerAddress);
163 
164  if ((m_socket->Send (p)) >= 0)
165  {
167  NS_LOG_INFO ("TraceDelay TX " << m_size << " bytes to "
168  << peerAddressStringStream.str () << " Uid: "
169  << p->GetUid () << " Time: "
170  << (Simulator::Now ()).GetSeconds ());
171  }
172  else
173  {
174  NS_LOG_INFO ("Error while sending " << m_size << " bytes to "
175  << peerAddressStringStream.str ());
176  }
177  m_sent++;
178 
179  if ((m_sent < m_maxPackets) || (m_maxPackets == 0))
180  {
182  }
183 }
184 
185 } // Namespace ns3
The base class for all ns3 applications.
Definition: application.h:61
virtual void DoDispose(void)
Destructor implementation.
Definition: application.cc:83
Ptr< Node > GetNode() const
Definition: application.cc:104
An identifier for simulation events.
Definition: event-id.h:54
bool IsExpired(void) const
This method is syntactic sugar for the ns3::Simulator::IsExpired method.
Definition: event-id.cc:65
uint64_t GetUid(void) const
Returns the packet's Uid.
Definition: packet.cc:390
an address for a packet socket
static PacketSocketAddress ConvertFrom(const Address &address)
static TypeId GetTypeId(void)
Get the type ID.
uint32_t m_sent
Counter for sent packets.
virtual void DoDispose(void)
Destructor implementation.
uint8_t GetPriority(void) const
Query the priority value of this socket.
Ptr< Socket > m_socket
Socket.
virtual void StartApplication(void)
Application specific startup code.
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.
virtual void StopApplication(void)
Application specific shutdown code.
void Send(void)
Send a packet.
bool m_peerAddressSet
Sanity check.
TracedCallback< Ptr< const Packet >, const Address & > m_txTrace
Traced Callback: sent packets, source address.
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.
PacketSocketAddress m_peerAddress
Remote peer address.
Time m_interval
Packet inter-send time.
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:268
static EventId Schedule(Time const &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:556
static EventId ScheduleNow(FUNC f, Ts &&... args)
Schedule an event to expire Now.
Definition: simulator.h:587
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:195
virtual int Send(Ptr< Packet > p, uint32_t flags)=0
Send data (or dummy data) to the remote host.
void SetPriority(uint8_t priority)
Manually set the socket priority.
Definition: socket.cc:389
virtual int Connect(const Address &address)=0
Initiate a connection to a remote host.
virtual int Close(void)=0
Close a socket.
void SetRecvCallback(Callback< void, Ptr< Socket > > receivedData)
Notify application when new data is available to be read.
Definition: socket.cc:128
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:71
virtual int Bind(const Address &address)=0
Allocate a local endpoint for this socket.
AttributeValue implementation for Time.
Definition: nstime.h:1308
a unique identifier for an interface.
Definition: type-id.h:59
static TypeId LookupByName(std::string name)
Get a TypeId by name.
Definition: type-id.cc:829
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:922
Hold an unsigned integer type.
Definition: uinteger.h:44
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition: assert.h:67
#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:88
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:1309
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:45
Callback< R, Ts... > MakeNullCallback(void)
Definition: callback.h:1688
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#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:281
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1244
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:522