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  .AddConstructor<PacketSocketClient> ()
48  .AddAttribute ("MaxPackets",
49  "The maximum number of packets the application will send (zero means infinite)",
50  UintegerValue (100),
52  MakeUintegerChecker<uint32_t> ())
53  .AddAttribute ("Interval",
54  "The time to wait between packets", TimeValue (Seconds (1.0)),
56  MakeTimeChecker ())
57  .AddAttribute ("PacketSize",
58  "Size of packets generated (bytes).",
59  UintegerValue (1024),
61  MakeUintegerChecker<uint32_t> ())
62  .AddTraceSource ("Tx", "A packet has been sent",
64  "ns3::Packet::PacketAddressTracedCallback")
65  ;
66  return tid;
67 }
68 
70 {
71  NS_LOG_FUNCTION (this);
72  m_sent = 0;
73  m_socket = 0;
74  m_sendEvent = EventId ();
75  m_peerAddressSet = false;
76 }
77 
79 {
80  NS_LOG_FUNCTION (this);
81 }
82 
83 void
85 {
86  NS_LOG_FUNCTION (this << addr);
87  m_peerAddress = addr;
88  m_peerAddressSet = true;
89 }
90 
91 void
93 {
94  NS_LOG_FUNCTION (this);
96 }
97 
98 void
100 {
101  NS_LOG_FUNCTION (this);
102  NS_ASSERT_MSG (m_peerAddressSet, "Peer address not set");
103 
104  if (m_socket == 0)
105  {
106  TypeId tid = TypeId::LookupByName ("ns3::PacketSocketFactory");
108 
111  }
112 
115 }
116 
117 void
119 {
120  NS_LOG_FUNCTION (this);
122  m_socket->Close ();
123 }
124 
125 void
127 {
128  NS_LOG_FUNCTION (this);
130 
131  Ptr<Packet> p = Create<Packet> (m_size);
132 
133  std::stringstream peerAddressStringStream;
134  peerAddressStringStream << PacketSocketAddress::ConvertFrom (m_peerAddress);
135 
136  if ((m_socket->Send (p)) >= 0)
137  {
138  NS_LOG_INFO ("TraceDelay TX " << m_size << " bytes to "
139  << peerAddressStringStream.str () << " Uid: "
140  << p->GetUid () << " Time: "
141  << (Simulator::Now ()).GetSeconds ());
142  }
143  else
144  {
145  NS_LOG_INFO ("Error while sending " << m_size << " bytes to "
146  << peerAddressStringStream.str ());
147  }
148  m_sent++;
149 
150  if ((m_sent < m_maxPackets) || (m_maxPackets == 0))
151  {
154  }
155 }
156 
157 } // Namespace ns3
uint32_t m_maxPackets
Maximum number of packets the application will send.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
virtual void StopApplication(void)
Application specific shutdown code.
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:44
an address for a packet socket
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
Definition: assert.h:61
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:244
bool m_peerAddressSet
Sanity check.
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:311
Callback< R > MakeNullCallback(void)
Definition: callback.h:1436
static EventId Schedule(Time const &time, MEM mem_ptr, OBJ obj)
Schedule an event to expire at the relative time "time" is reached.
Definition: simulator.h:819
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
Ptr< const AttributeChecker > MakeTimeChecker(const Time min, const Time max)
Helper to make a Time checker with bounded range.
Definition: time.cc:439
uint32_t m_sent
Counter for sent packets.
Time m_interval
Packet inter-send time.
virtual void StartApplication(void)
Application specific startup code.
The base class for all ns3 applications.
Definition: application.h:60
EventId m_sendEvent
Event to send the next packet.
AttributeValue implementation for Time.
Definition: nstime.h:921
Hold an unsigned integer type.
Definition: uinteger.h:44
Ptr< Node > GetNode() const
Definition: application.cc:103
static TypeId GetTypeId(void)
Get the type ID.
void SetRecvCallback(Callback< void, Ptr< Socket > >)
Notify application when new data is available to be read.
Definition: socket.cc:127
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:70
Ptr< Socket > m_socket
Socket.
virtual int Connect(const Address &address)=0
Initiate a connection to a remote host.
virtual void DoDispose(void)
Destructor implementation.
Definition: application.cc:82
virtual int Bind(const Address &address)=0
Allocate a local endpoint for this socket.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
virtual void DoDispose(void)
Destructor implementation.
static EventId ScheduleNow(MEM mem_ptr, OBJ obj)
Schedule an event to expire Now.
Definition: simulator.h:980
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:922
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:223
TracedCallback< Ptr< const Packet >, const Address & > m_txTrace
Traced Callback: sent packets, source address.
#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:84
An identifier for simulation events.
Definition: event-id.h:53
void SetRemote(PacketSocketAddress addr)
set the remote address and protocol to be used
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:859
void Send(void)
Send a packet.
static PacketSocketAddress ConvertFrom(const Address &address)
PacketSocketAddress m_peerAddress
Remote peer address.
virtual int Send(Ptr< Packet > p, uint32_t flags)=0
Send data (or dummy data) to the remote host.
virtual int Close(void)=0
Close a socket.
bool IsExpired(void) const
This method is syntactic sugar for the ns3::Simulator::IsExpired method.
Definition: event-id.cc:59
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
a unique identifier for an interface.
Definition: type-id.h:51
TypeId SetParent(TypeId tid)
Definition: type-id.cc:631
uint32_t m_size
Size of the sent packet.
static TypeId LookupByName(std::string name)
Definition: type-id.cc:556