A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 NS_OBJECT_ENSURE_REGISTERED (PacketSocketClient);
40 
41 TypeId
43 {
44  static TypeId tid = TypeId ("ns3::PacketSocketClient")
46  .AddConstructor<PacketSocketClient> ()
47  .AddAttribute ("MaxPackets",
48  "The maximum number of packets the application will send (zero 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",
63  ;
64  return tid;
65 }
66 
68 {
69  NS_LOG_FUNCTION (this);
70  m_sent = 0;
71  m_socket = 0;
72  m_sendEvent = EventId ();
73  m_peerAddressSet = false;
74 }
75 
77 {
78  NS_LOG_FUNCTION (this);
79 }
80 
81 void
83 {
84  NS_LOG_FUNCTION (this << addr);
85  m_peerAddress = addr;
86  m_peerAddressSet = true;
87 }
88 
89 void
91 {
92  NS_LOG_FUNCTION (this);
94 }
95 
96 void
98 {
99  NS_LOG_FUNCTION (this);
100  NS_ASSERT_MSG (m_peerAddressSet, "Peer address not set");
101 
102  if (m_socket == 0)
103  {
104  TypeId tid = TypeId::LookupByName ("ns3::PacketSocketFactory");
106 
109  }
110 
113 }
114 
115 void
117 {
118  NS_LOG_FUNCTION (this);
120  m_socket->Close ();
121 }
122 
123 void
125 {
126  NS_LOG_FUNCTION (this);
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  {
152  }
153 }
154 
155 } // 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 the class in the ns-3 factory.
Definition: object-base.h:38
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:170
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:223
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:268
Callback< R > MakeNullCallback(void)
Definition: callback.h:1429
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:825
Ptr< const AttributeChecker > MakeTimeChecker(const Time min, const Time max)
Helper to make a Time checker with bounded range.
Definition: time.cc:444
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.
Attribute for objects of type ns3::Time.
Definition: nstime.h:912
Hold an unsigned integer type.
Definition: uinteger.h:46
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)
This method is called by Object::Dispose or by the object's destructor, whichever comes first...
Definition: application.cc:82
virtual int Bind(const Address &address)=0
Allocate a local endpoint for this socket.
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
virtual void DoDispose(void)
This method is called by Object::Dispose or by the object's destructor, whichever comes first...
static EventId ScheduleNow(MEM mem_ptr, OBJ obj)
Schedule an event to expire Now.
Definition: simulator.h:986
static Time Now(void)
Return the "current simulation time".
Definition: simulator.cc:180
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:46
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:845
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:53
a unique identifier for an interface.
Definition: type-id.h:49
TypeId SetParent(TypeId tid)
Definition: type-id.cc:610
uint32_t m_size
Size of the sent packet.
static TypeId LookupByName(std::string name)
Definition: type-id.cc:535