3570
|
1 |
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
|
|
2 |
/*
|
|
3 |
* This program is free software; you can redistribute it and/or modify
|
|
4 |
* it under the terms of the GNU General Public License version 2 as
|
|
5 |
* published by the Free Software Foundation;
|
|
6 |
*
|
|
7 |
* This program is distributed in the hope that it will be useful,
|
|
8 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
9 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
10 |
* GNU General Public License for more details.
|
|
11 |
*
|
|
12 |
* You should have received a copy of the GNU General Public License
|
|
13 |
* along with this program; if not, write to the Free Software
|
|
14 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
15 |
*
|
|
16 |
* Authors: Joe Kopena <tjkopena@cs.drexel.edu>
|
|
17 |
*
|
|
18 |
* These applications are used in the WiFi Distance Test experiment,
|
|
19 |
* described and implemented in test02.cc. That file should be in the
|
|
20 |
* same place as this file. The applications have two very simple
|
|
21 |
* jobs, they just generate and receive packets. We could use the
|
|
22 |
* standard Application classes included in the NS-3 distribution.
|
|
23 |
* These have been written just to change the behavior a little, and
|
|
24 |
* provide more examples.
|
|
25 |
*
|
|
26 |
*/
|
|
27 |
|
|
28 |
#include <ostream>
|
|
29 |
|
|
30 |
#include "ns3/core-module.h"
|
|
31 |
#include "ns3/common-module.h"
|
|
32 |
#include "ns3/simulator-module.h"
|
|
33 |
#include "ns3/node-module.h"
|
|
34 |
#include "ns3/internet-stack-module.h"
|
|
35 |
|
|
36 |
#include "ns3/stats-module.h"
|
|
37 |
|
|
38 |
#include "wifi-example-apps.h"
|
|
39 |
|
|
40 |
using namespace ns3;
|
|
41 |
|
|
42 |
NS_LOG_COMPONENT_DEFINE ("WiFiDistanceApps");
|
|
43 |
|
|
44 |
TypeId
|
|
45 |
Sender::GetTypeId(void)
|
|
46 |
{
|
|
47 |
static TypeId tid = TypeId ("Sender")
|
|
48 |
.SetParent<Application> ()
|
|
49 |
.AddConstructor<Sender> ()
|
|
50 |
.AddAttribute ("PacketSize", "The size of packets transmitted.",
|
|
51 |
UintegerValue(64),
|
|
52 |
MakeUintegerAccessor(&Sender::m_pktSize),
|
|
53 |
MakeUintegerChecker<uint32_t>(1))
|
|
54 |
.AddAttribute("Destination", "Target host address.",
|
|
55 |
Ipv4AddressValue("255.255.255.255"),
|
|
56 |
MakeIpv4AddressAccessor(&Sender::m_destAddr),
|
|
57 |
MakeIpv4AddressChecker())
|
|
58 |
.AddAttribute("Port", "Destination app port.",
|
|
59 |
UintegerValue(1603),
|
|
60 |
MakeUintegerAccessor(&Sender::m_destPort),
|
|
61 |
MakeUintegerChecker<uint32_t>())
|
|
62 |
.AddAttribute("NumPackets", "Total number of packets to send.",
|
|
63 |
UintegerValue(30),
|
|
64 |
MakeUintegerAccessor(&Sender::m_numPkts),
|
|
65 |
MakeUintegerChecker<uint32_t>(1))
|
|
66 |
.AddAttribute ("Interval", "Delay between transmissions.",
|
|
67 |
RandomVariableValue(ConstantVariable(0.5)),
|
|
68 |
MakeRandomVariableAccessor(&Sender::m_interval),
|
|
69 |
MakeRandomVariableChecker())
|
|
70 |
.AddTraceSource ("Tx", "A new packet is created and is sent",
|
|
71 |
MakeTraceSourceAccessor (&Sender::m_txTrace))
|
|
72 |
;
|
|
73 |
return tid;
|
|
74 |
}
|
|
75 |
|
|
76 |
|
|
77 |
Sender::Sender()
|
|
78 |
{
|
|
79 |
NS_LOG_FUNCTION_NOARGS ();
|
|
80 |
m_socket = 0;
|
|
81 |
}
|
|
82 |
|
|
83 |
Sender::~Sender()
|
|
84 |
{
|
|
85 |
NS_LOG_FUNCTION_NOARGS ();
|
|
86 |
}
|
|
87 |
|
|
88 |
void
|
|
89 |
Sender::DoDispose (void)
|
|
90 |
{
|
|
91 |
NS_LOG_FUNCTION_NOARGS ();
|
|
92 |
|
|
93 |
m_socket = 0;
|
|
94 |
// chain up
|
|
95 |
Application::DoDispose ();
|
|
96 |
}
|
|
97 |
|
|
98 |
void Sender::StartApplication()
|
|
99 |
{
|
|
100 |
NS_LOG_FUNCTION_NOARGS ();
|
|
101 |
|
|
102 |
if (m_socket == 0) {
|
|
103 |
Ptr<SocketFactory> socketFactory = GetNode()->GetObject<SocketFactory>
|
|
104 |
(UdpSocketFactory::GetTypeId());
|
|
105 |
m_socket = socketFactory->CreateSocket ();
|
|
106 |
m_socket->Bind ();
|
|
107 |
}
|
|
108 |
|
|
109 |
m_count = 0;
|
|
110 |
|
|
111 |
Simulator::Cancel(m_sendEvent);
|
|
112 |
m_sendEvent = Simulator::ScheduleNow(&Sender::SendPacket, this);
|
|
113 |
|
|
114 |
// end Sender::StartApplication
|
|
115 |
}
|
|
116 |
|
|
117 |
void Sender::StopApplication()
|
|
118 |
{
|
|
119 |
NS_LOG_FUNCTION_NOARGS ();
|
|
120 |
Simulator::Cancel(m_sendEvent);
|
|
121 |
// end Sender::StopApplication
|
|
122 |
}
|
|
123 |
|
|
124 |
void Sender::SendPacket()
|
|
125 |
{
|
|
126 |
// NS_LOG_FUNCTION_NOARGS ();
|
|
127 |
NS_LOG_INFO("Sending packet at " << Simulator::Now() << " to " <<
|
|
128 |
m_destAddr);
|
|
129 |
|
|
130 |
Ptr<Packet> packet = Create<Packet>(m_pktSize);
|
|
131 |
|
|
132 |
TimestampTag timestamp;
|
|
133 |
timestamp.SetTimestamp(Simulator::Now());
|
|
134 |
packet->AddTag(timestamp);
|
|
135 |
|
|
136 |
// Could connect the socket since the address never changes; using SendTo
|
|
137 |
// here simply because all of the standard apps do not.
|
|
138 |
m_socket->SendTo(packet, 0, InetSocketAddress(m_destAddr, m_destPort));
|
|
139 |
|
|
140 |
// Report the event to the trace.
|
|
141 |
m_txTrace(packet);
|
|
142 |
|
|
143 |
if (++m_count < m_numPkts) {
|
|
144 |
m_sendEvent = Simulator::Schedule(Seconds(m_interval.GetValue()),
|
|
145 |
&Sender::SendPacket, this);
|
|
146 |
}
|
|
147 |
|
|
148 |
// end Sender::SendPacket
|
|
149 |
}
|
|
150 |
|
|
151 |
|
|
152 |
|
|
153 |
|
|
154 |
//----------------------------------------------------------------------
|
|
155 |
//-- Receiver
|
|
156 |
//------------------------------------------------------
|
|
157 |
TypeId
|
|
158 |
Receiver::GetTypeId(void)
|
|
159 |
{
|
|
160 |
static TypeId tid = TypeId ("Receiver")
|
|
161 |
.SetParent<Application> ()
|
|
162 |
.AddConstructor<Receiver> ()
|
|
163 |
.AddAttribute("Port", "Listening port.",
|
|
164 |
UintegerValue(1603),
|
|
165 |
MakeUintegerAccessor(&Receiver::m_port),
|
|
166 |
MakeUintegerChecker<uint32_t>())
|
|
167 |
;
|
|
168 |
return tid;
|
|
169 |
}
|
|
170 |
|
|
171 |
Receiver::Receiver() :
|
|
172 |
m_calc(0),
|
|
173 |
m_delay(0)
|
|
174 |
{
|
|
175 |
NS_LOG_FUNCTION_NOARGS ();
|
|
176 |
m_socket = 0;
|
|
177 |
}
|
|
178 |
|
|
179 |
Receiver::~Receiver()
|
|
180 |
{
|
|
181 |
NS_LOG_FUNCTION_NOARGS ();
|
|
182 |
}
|
|
183 |
|
|
184 |
void
|
|
185 |
Receiver::DoDispose (void)
|
|
186 |
{
|
|
187 |
NS_LOG_FUNCTION_NOARGS ();
|
|
188 |
|
|
189 |
m_socket = 0;
|
|
190 |
// chain up
|
|
191 |
Application::DoDispose ();
|
|
192 |
}
|
|
193 |
|
|
194 |
void
|
|
195 |
Receiver::StartApplication()
|
|
196 |
{
|
|
197 |
NS_LOG_FUNCTION_NOARGS ();
|
|
198 |
|
|
199 |
if (m_socket == 0) {
|
|
200 |
Ptr<SocketFactory> socketFactory = GetNode()->GetObject<SocketFactory>
|
|
201 |
(UdpSocketFactory::GetTypeId());
|
|
202 |
m_socket = socketFactory->CreateSocket();
|
|
203 |
InetSocketAddress local =
|
|
204 |
InetSocketAddress(Ipv4Address::GetAny(), m_port);
|
|
205 |
m_socket->Bind(local);
|
|
206 |
}
|
|
207 |
|
|
208 |
m_socket->SetRecvCallback(MakeCallback(&Receiver::Receive, this));
|
|
209 |
|
|
210 |
// end Receiver::StartApplication
|
|
211 |
}
|
|
212 |
|
|
213 |
void
|
|
214 |
Receiver::StopApplication()
|
|
215 |
{
|
|
216 |
NS_LOG_FUNCTION_NOARGS ();
|
|
217 |
|
|
218 |
if (m_socket != 0) {
|
|
219 |
m_socket->SetRecvCallback(MakeNullCallback<void, Ptr<Socket> > ());
|
|
220 |
}
|
|
221 |
|
|
222 |
// end Receiver::StopApplication
|
|
223 |
}
|
|
224 |
|
|
225 |
void
|
|
226 |
Receiver::SetCounter(Ptr<CounterCalculator<> > calc)
|
|
227 |
{
|
|
228 |
m_calc = calc;
|
|
229 |
// end Receiver::SetCounter
|
|
230 |
}
|
|
231 |
void
|
|
232 |
Receiver::SetDelayTracker(Ptr<TimeMinMaxAvgTotalCalculator> delay)
|
|
233 |
{
|
|
234 |
m_delay = delay;
|
|
235 |
// end Receiver::SetDelayTracker
|
|
236 |
}
|
|
237 |
|
|
238 |
void
|
|
239 |
Receiver::Receive(Ptr<Socket> socket)
|
|
240 |
{
|
|
241 |
// NS_LOG_FUNCTION (this << socket << packet << from);
|
|
242 |
|
|
243 |
Ptr<Packet> packet;
|
|
244 |
Address from;
|
|
245 |
while (packet = socket->RecvFrom(from)) {
|
|
246 |
if (InetSocketAddress::IsMatchingType (from)) {
|
|
247 |
InetSocketAddress address = InetSocketAddress::ConvertFrom (from);
|
|
248 |
NS_LOG_INFO ("Received " << packet->GetSize() << " bytes from " <<
|
|
249 |
address.GetIpv4());
|
|
250 |
}
|
|
251 |
|
|
252 |
TimestampTag timestamp;
|
|
253 |
packet->FindFirstMatchingTag(timestamp);
|
|
254 |
Time tx = timestamp.GetTimestamp();
|
|
255 |
|
|
256 |
if (m_delay != 0) {
|
|
257 |
m_delay->Update(Simulator::Now() - tx);
|
|
258 |
}
|
|
259 |
|
|
260 |
if (m_calc != 0) {
|
|
261 |
m_calc->Update();
|
|
262 |
}
|
|
263 |
|
|
264 |
// end receiving packets
|
|
265 |
}
|
|
266 |
|
|
267 |
// end Receiver::Receive
|
|
268 |
}
|
|
269 |
|
|
270 |
|
|
271 |
|
|
272 |
|
|
273 |
//----------------------------------------------------------------------
|
|
274 |
//-- TimestampTag
|
|
275 |
//------------------------------------------------------
|
|
276 |
TypeId
|
|
277 |
TimestampTag::GetTypeId(void)
|
|
278 |
{
|
|
279 |
static TypeId tid = TypeId ("TimestampTag")
|
|
280 |
.SetParent<Tag> ()
|
|
281 |
.AddConstructor<TimestampTag> ()
|
|
282 |
.AddAttribute ("Timestamp",
|
|
283 |
"Some momentous point in time!",
|
|
284 |
EmptyAttributeValue(),
|
|
285 |
MakeTimeAccessor(&TimestampTag::GetTimestamp),
|
|
286 |
MakeTimeChecker())
|
|
287 |
;
|
|
288 |
return tid;
|
|
289 |
}
|
|
290 |
TypeId
|
|
291 |
TimestampTag::GetInstanceTypeId(void) const
|
|
292 |
{
|
|
293 |
return GetTypeId ();
|
|
294 |
}
|
|
295 |
|
|
296 |
uint32_t
|
|
297 |
TimestampTag::GetSerializedSize (void) const
|
|
298 |
{
|
|
299 |
return 8;
|
|
300 |
}
|
|
301 |
void
|
|
302 |
TimestampTag::Serialize (TagBuffer i) const
|
|
303 |
{
|
|
304 |
int64_t t = m_timestamp.GetNanoSeconds();
|
|
305 |
i.Write((const uint8_t *)&t, 8);
|
|
306 |
}
|
|
307 |
void
|
|
308 |
TimestampTag::Deserialize (TagBuffer i)
|
|
309 |
{
|
|
310 |
int64_t t;
|
|
311 |
i.Read((uint8_t *)&t, 8);
|
|
312 |
m_timestamp = NanoSeconds(t);
|
|
313 |
}
|
|
314 |
|
|
315 |
void
|
|
316 |
TimestampTag::SetTimestamp(Time time)
|
|
317 |
{
|
|
318 |
m_timestamp = time;
|
|
319 |
}
|
|
320 |
Time
|
|
321 |
TimestampTag::GetTimestamp(void) const
|
|
322 |
{
|
|
323 |
return m_timestamp;
|
|
324 |
}
|
|
325 |
|
|
326 |
void
|
|
327 |
TimestampTag::Print(std::ostream &os) const
|
|
328 |
{
|
|
329 |
os << "t=" << m_timestamp;
|
|
330 |
}
|