A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
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
21
24#include "packet-socket.h"
25
26#include "ns3/abort.h"
27#include "ns3/log.h"
28#include "ns3/nstime.h"
29#include "ns3/packet.h"
30#include "ns3/simulator.h"
31#include "ns3/socket-factory.h"
32#include "ns3/socket.h"
33#include "ns3/uinteger.h"
34
35#include <cstdio>
36#include <cstdlib>
37
38namespace ns3
39{
40
41NS_LOG_COMPONENT_DEFINE("PacketSocketClient");
42
43NS_OBJECT_ENSURE_REGISTERED(PacketSocketClient);
44
45TypeId
47{
48 static TypeId tid =
49 TypeId("ns3::PacketSocketClient")
51 .SetGroupName("Network")
52 .AddConstructor<PacketSocketClient>()
53 .AddAttribute(
54 "MaxPackets",
55 "The maximum number of packets the application will send (zero means infinite)",
56 UintegerValue(100),
58 MakeUintegerChecker<uint32_t>())
59 .AddAttribute("Interval",
60 "The time to wait between packets",
61 TimeValue(Seconds(1.0)),
64 .AddAttribute("PacketSize",
65 "Size of packets generated (bytes).",
66 UintegerValue(1024),
68 MakeUintegerChecker<uint32_t>())
69 .AddAttribute("Priority",
70 "Priority assigned to the packets generated.",
74 MakeUintegerChecker<uint8_t>())
75 .AddTraceSource("Tx",
76 "A packet has been sent",
78 "ns3::Packet::AddressTracedCallback");
79 return tid;
80}
81
83{
84 NS_LOG_FUNCTION(this);
85 m_sent = 0;
86 m_socket = nullptr;
88 m_peerAddressSet = false;
89}
90
92{
93 NS_LOG_FUNCTION(this);
94}
95
96void
98{
99 NS_LOG_FUNCTION(this << addr);
100 m_peerAddress = addr;
101 m_peerAddressSet = true;
102}
103
104void
106{
107 NS_LOG_FUNCTION(this);
109}
110
111void
113{
114 m_priority = priority;
115 if (m_socket)
116 {
117 m_socket->SetPriority(priority);
118 }
119}
120
121uint8_t
123{
124 return m_priority;
125}
126
127void
129{
130 NS_LOG_FUNCTION(this);
131 NS_ASSERT_MSG(m_peerAddressSet, "Peer address not set");
132
133 if (!m_socket)
134 {
135 TypeId tid = TypeId::LookupByName("ns3::PacketSocketFactory");
137
140
141 if (m_priority)
142 {
144 }
145 }
146
149}
150
151void
153{
154 NS_LOG_FUNCTION(this);
156 m_socket->Close();
157}
158
159void
161{
162 NS_LOG_FUNCTION(this);
164
165 Ptr<Packet> p = Create<Packet>(m_size);
166
167 std::stringstream peerAddressStringStream;
168 peerAddressStringStream << PacketSocketAddress::ConvertFrom(m_peerAddress);
169
170 if ((m_socket->Send(p)) >= 0)
171 {
173 NS_LOG_INFO("TraceDelay TX " << m_size << " bytes to " << peerAddressStringStream.str()
174 << " Uid: " << p->GetUid()
175 << " Time: " << (Simulator::Now()).GetSeconds());
176 }
177 else
178 {
179 NS_LOG_INFO("Error while sending " << m_size << " bytes to "
180 << peerAddressStringStream.str());
181 }
182 m_sent++;
183
184 if ((m_sent < m_maxPackets) || (m_maxPackets == 0))
185 {
186 if (m_interval.IsZero())
187 {
189 m_maxPackets == 0,
190 "Generating infinite packets at the same time does not seem to be a good idea");
191 Send();
192 }
193 else
194 {
196 }
197 }
198}
199
200} // Namespace ns3
The base class for all ns3 applications.
Definition: application.h:62
void DoDispose() override
Destructor implementation.
Definition: application.cc:86
Ptr< Node > GetNode() const
Definition: application.cc:108
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
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.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:571
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:285
static Time Now()
Return the current simulation virtual time.
Definition: simulator.cc:208
static EventId ScheduleNow(FUNC f, Ts &&... args)
Schedule an event to expire Now.
Definition: simulator.h:605
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:386
virtual int Connect(const Address &address)=0
Initiate a connection to a remote host.
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:72
virtual int Close()=0
Close a socket.
virtual int Bind(const Address &address)=0
Allocate a local endpoint for this socket.
bool IsZero() const
Exactly equivalent to t == 0.
Definition: nstime.h:315
AttributeValue implementation for Time.
Definition: nstime.h:1413
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:836
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:932
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 AttributeChecker > MakeTimeChecker()
Helper to make an unbounded Time checker.
Definition: nstime.h:1434
Ptr< const AttributeAccessor > MakeTimeAccessor(T1 a1)
Definition: nstime.h:1414
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
Definition: uinteger.h:46
Callback< R, Args... > MakeNullCallback()
Definition: callback.h:747
#define NS_ABORT_MSG_IF(cond, msg)
Abnormal program termination if a condition is true, with a message.
Definition: abort.h:108
#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:1326
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.