-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathudp-client.cc
173 lines (153 loc) · 5.91 KB
/
udp-client.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2007,2008,2009 INRIA, UDCAST
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation;
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Author: Amine Ismail <amine.ismail@sophia.inria.fr>
* <amine.ismail@udcast.com>
*/
#include "ns3/log.h"
#include "ns3/ipv4-address.h"
#include "ns3/nstime.h"
#include "ns3/inet-socket-address.h"
#include "ns3/inet6-socket-address.h"
#include "ns3/socket.h"
#include "ns3/simulator.h"
#include "ns3/socket-factory.h"
#include "ns3/packet.h"
#include "ns3/uinteger.h"
#include "udp-client.h"
#include "seq-ts-header.h"
#include <cstdlib>
#include <cstdio>
namespace ns3
{
NS_LOG_COMPONENT_DEFINE("UdpClient");
NS_OBJECT_ENSURE_REGISTERED(UdpClient);
TypeId
UdpClient::GetTypeId(void) {
static TypeId tid = TypeId("ns3::UdpClient")
.SetParent<Application> ()
.SetGroupName("Applications")
.AddConstructor<UdpClient> ()
.AddAttribute("MaxPackets",
"The maximum number of packets the application will send",
UintegerValue(100),
MakeUintegerAccessor(&UdpClient::m_count),
MakeUintegerChecker<uint32_t> ())
.AddAttribute("Interval",
"The time to wait between packets", TimeValue(Seconds(1.0)),
MakeTimeAccessor(&UdpClient::m_interval),
MakeTimeChecker())
.AddAttribute("RemoteAddress",
"The destination Address of the outbound packets",
AddressValue(),
MakeAddressAccessor(&UdpClient::m_peerAddress),
MakeAddressChecker())
.AddAttribute("RemotePort", "The destination port of the outbound packets",
UintegerValue(100),
MakeUintegerAccessor(&UdpClient::m_peerPort),
MakeUintegerChecker<uint16_t> ())
.AddAttribute("PacketSize",
"Size of packets generated. The minimum packet size is 12 bytes which is the size of the header carrying the sequence number and the time stamp.",
UintegerValue(1024),
MakeUintegerAccessor(&UdpClient::m_size),
MakeUintegerChecker<uint32_t> (12, 1500))
;
return tid;
}
UdpClient::UdpClient() {
NS_LOG_FUNCTION(this);
m_sent = 0;
m_socket = 0;
m_sendEvent = EventId();
}
UdpClient::~UdpClient() {
NS_LOG_FUNCTION(this);
}
void
UdpClient::SetRemote(Ipv4Address ip, uint16_t port) {
NS_LOG_FUNCTION(this << ip << port);
m_peerAddress = Address(ip);
m_peerPort = port;
}
void
UdpClient::SetRemote(Ipv6Address ip, uint16_t port) {
NS_LOG_FUNCTION(this << ip << port);
m_peerAddress = Address(ip);
m_peerPort = port;
}
void
UdpClient::SetRemote(Address ip, uint16_t port) {
NS_LOG_FUNCTION(this << ip << port);
m_peerAddress = ip;
m_peerPort = port;
}
void
UdpClient::DoDispose(void) {
NS_LOG_FUNCTION(this);
Application::DoDispose();
}
void
UdpClient::StartApplication(void) {
NS_LOG_FUNCTION(this);
if (m_socket == 0) {
TypeId tid = TypeId::LookupByName("ns3::UdpSocketFactory");
m_socket = Socket::CreateSocket(GetNode(), tid);
if (Ipv4Address::IsMatchingType(m_peerAddress) == true) {
m_socket->Bind();
m_socket->Connect(InetSocketAddress(Ipv4Address::ConvertFrom(m_peerAddress), m_peerPort));
} else if (Ipv6Address::IsMatchingType(m_peerAddress) == true) {
m_socket->Bind6();
m_socket->Connect(Inet6SocketAddress(Ipv6Address::ConvertFrom(m_peerAddress), m_peerPort));
}
}
m_socket->SetRecvCallback(MakeNullCallback<void, Ptr<Socket> > ());
m_sendEvent = Simulator::Schedule(Seconds(0.0), &UdpClient::Send, this);
}
void
UdpClient::StopApplication(void) {
NS_LOG_FUNCTION(this);
Simulator::Cancel(m_sendEvent);
}
void
UdpClient::Send(void) {
NS_LOG_FUNCTION(this);
NS_ASSERT(m_sendEvent.IsExpired());
SeqTsHeader seqTs;
seqTs.SetSeq(m_sent);
Ptr<Packet> p = Create<Packet> (m_size - (8 + 4)); // 8+4 : the size of the seqTs header
p->AddHeader(seqTs);
std::stringstream peerAddressStringStream;
if (Ipv4Address::IsMatchingType(m_peerAddress)) {
peerAddressStringStream << Ipv4Address::ConvertFrom(m_peerAddress);
} else if (Ipv6Address::IsMatchingType(m_peerAddress)) {
peerAddressStringStream << Ipv6Address::ConvertFrom(m_peerAddress);
}
if ((m_socket->Send(p)) >= 0) {
++m_sent;
NS_LOG_INFO("TraceDelay TX " << m_size << " bytes to "
<< peerAddressStringStream.str() << " Uid: "
<< p->GetUid() << " Time: "
<< (Simulator::Now()).GetSeconds());
} else {
NS_LOG_INFO("Error while sending " << m_size << " bytes to "
<< peerAddressStringStream.str());
}
if (m_sent < m_count) {
m_sendEvent = Simulator::Schedule(m_interval, &UdpClient::Send, this);
}
}
} // Namespace ns3