author | Tom Henderson <tomh@tomh.org> |
Fri, 20 Aug 2010 12:17:19 -0700 | |
changeset 6589 | 9c325569fb01 |
parent 5100 | d0cafb0ebeeb |
permissions | -rw-r--r-- |
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 |
||
4487
0d67f380c764
Backed out changeset b7faa7398d1e
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
4483
diff
changeset
|
42 |
NS_LOG_COMPONENT_DEFINE ("WiFiDistanceApps"); |
3570 | 43 |
|
44 |
TypeId |
|
45 |
Sender::GetTypeId(void) |
|
46 |
{ |
|
47 |
static TypeId tid = TypeId ("Sender") |
|
48 |
.SetParent<Application> () |
|
49 |
.AddConstructor<Sender> () |
|
4487
0d67f380c764
Backed out changeset b7faa7398d1e
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
4483
diff
changeset
|
50 |
.AddAttribute ("PacketSize", "The size of packets transmitted.", |
3570 | 51 |
UintegerValue(64), |
52 |
MakeUintegerAccessor(&Sender::m_pktSize), |
|
53 |
MakeUintegerChecker<uint32_t>(1)) |
|
4487
0d67f380c764
Backed out changeset b7faa7398d1e
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
4483
diff
changeset
|
54 |
.AddAttribute("Destination", "Target host address.", |
0d67f380c764
Backed out changeset b7faa7398d1e
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
4483
diff
changeset
|
55 |
Ipv4AddressValue("255.255.255.255"), |
0d67f380c764
Backed out changeset b7faa7398d1e
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
4483
diff
changeset
|
56 |
MakeIpv4AddressAccessor(&Sender::m_destAddr), |
0d67f380c764
Backed out changeset b7faa7398d1e
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
4483
diff
changeset
|
57 |
MakeIpv4AddressChecker()) |
0d67f380c764
Backed out changeset b7faa7398d1e
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
4483
diff
changeset
|
58 |
.AddAttribute("Port", "Destination app port.", |
0d67f380c764
Backed out changeset b7faa7398d1e
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
4483
diff
changeset
|
59 |
UintegerValue(1603), |
0d67f380c764
Backed out changeset b7faa7398d1e
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
4483
diff
changeset
|
60 |
MakeUintegerAccessor(&Sender::m_destPort), |
0d67f380c764
Backed out changeset b7faa7398d1e
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
4483
diff
changeset
|
61 |
MakeUintegerChecker<uint32_t>()) |
0d67f380c764
Backed out changeset b7faa7398d1e
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
4483
diff
changeset
|
62 |
.AddAttribute("NumPackets", "Total number of packets to send.", |
0d67f380c764
Backed out changeset b7faa7398d1e
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
4483
diff
changeset
|
63 |
UintegerValue(30), |
0d67f380c764
Backed out changeset b7faa7398d1e
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
4483
diff
changeset
|
64 |
MakeUintegerAccessor(&Sender::m_numPkts), |
0d67f380c764
Backed out changeset b7faa7398d1e
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
4483
diff
changeset
|
65 |
MakeUintegerChecker<uint32_t>(1)) |
0d67f380c764
Backed out changeset b7faa7398d1e
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
4483
diff
changeset
|
66 |
.AddAttribute ("Interval", "Delay between transmissions.", |
0d67f380c764
Backed out changeset b7faa7398d1e
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
4483
diff
changeset
|
67 |
RandomVariableValue(ConstantVariable(0.5)), |
0d67f380c764
Backed out changeset b7faa7398d1e
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
4483
diff
changeset
|
68 |
MakeRandomVariableAccessor(&Sender::m_interval), |
0d67f380c764
Backed out changeset b7faa7398d1e
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
4483
diff
changeset
|
69 |
MakeRandomVariableChecker()) |
3570 | 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 |
} |
|
4487
0d67f380c764
Backed out changeset b7faa7398d1e
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
4483
diff
changeset
|
108 |
|
3570 | 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 (); |
|
4487
0d67f380c764
Backed out changeset b7faa7398d1e
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
4483
diff
changeset
|
127 |
NS_LOG_INFO("Sending packet at " << Simulator::Now() << " to " << |
0d67f380c764
Backed out changeset b7faa7398d1e
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
4483
diff
changeset
|
128 |
m_destAddr); |
3570 | 129 |
|
130 |
Ptr<Packet> packet = Create<Packet>(m_pktSize); |
|
131 |
||
132 |
TimestampTag timestamp; |
|
133 |
timestamp.SetTimestamp(Simulator::Now()); |
|
4502 | 134 |
packet->AddByteTag (timestamp); |
3570 | 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 |
||
4487
0d67f380c764
Backed out changeset b7faa7398d1e
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
4483
diff
changeset
|
143 |
if (++m_count < m_numPkts) { |
0d67f380c764
Backed out changeset b7faa7398d1e
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
4483
diff
changeset
|
144 |
m_sendEvent = Simulator::Schedule(Seconds(m_interval.GetValue()), |
3570 | 145 |
&Sender::SendPacket, this); |
146 |
} |
|
4487
0d67f380c764
Backed out changeset b7faa7398d1e
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
4483
diff
changeset
|
147 |
|
3570 | 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; |
|
4582 | 253 |
// Should never not be found since the sender is adding it, but |
254 |
// you never know. |
|
255 |
if (packet->FindFirstMatchingByteTag(timestamp)) { |
|
256 |
Time tx = timestamp.GetTimestamp(); |
|
4487
0d67f380c764
Backed out changeset b7faa7398d1e
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
4483
diff
changeset
|
257 |
|
4582 | 258 |
if (m_delay != 0) { |
259 |
m_delay->Update(Simulator::Now() - tx); |
|
260 |
} |
|
3570 | 261 |
} |
4487
0d67f380c764
Backed out changeset b7faa7398d1e
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
4483
diff
changeset
|
262 |
|
3570 | 263 |
if (m_calc != 0) { |
264 |
m_calc->Update(); |
|
265 |
} |
|
266 |
||
267 |
// end receiving packets |
|
268 |
} |
|
269 |
||
270 |
// end Receiver::Receive |
|
271 |
} |
|
272 |
||
273 |
||
274 |
||
275 |
||
276 |
//---------------------------------------------------------------------- |
|
277 |
//-- TimestampTag |
|
278 |
//------------------------------------------------------ |
|
279 |
TypeId |
|
280 |
TimestampTag::GetTypeId(void) |
|
281 |
{ |
|
282 |
static TypeId tid = TypeId ("TimestampTag") |
|
283 |
.SetParent<Tag> () |
|
284 |
.AddConstructor<TimestampTag> () |
|
285 |
.AddAttribute ("Timestamp", |
|
286 |
"Some momentous point in time!", |
|
287 |
EmptyAttributeValue(), |
|
288 |
MakeTimeAccessor(&TimestampTag::GetTimestamp), |
|
289 |
MakeTimeChecker()) |
|
290 |
; |
|
291 |
return tid; |
|
292 |
} |
|
293 |
TypeId |
|
294 |
TimestampTag::GetInstanceTypeId(void) const |
|
295 |
{ |
|
296 |
return GetTypeId (); |
|
297 |
} |
|
298 |
||
299 |
uint32_t |
|
300 |
TimestampTag::GetSerializedSize (void) const |
|
301 |
{ |
|
302 |
return 8; |
|
303 |
} |
|
304 |
void |
|
305 |
TimestampTag::Serialize (TagBuffer i) const |
|
306 |
{ |
|
307 |
int64_t t = m_timestamp.GetNanoSeconds(); |
|
308 |
i.Write((const uint8_t *)&t, 8); |
|
309 |
} |
|
310 |
void |
|
311 |
TimestampTag::Deserialize (TagBuffer i) |
|
312 |
{ |
|
313 |
int64_t t; |
|
314 |
i.Read((uint8_t *)&t, 8); |
|
315 |
m_timestamp = NanoSeconds(t); |
|
316 |
} |
|
317 |
||
318 |
void |
|
319 |
TimestampTag::SetTimestamp(Time time) |
|
320 |
{ |
|
321 |
m_timestamp = time; |
|
322 |
} |
|
323 |
Time |
|
324 |
TimestampTag::GetTimestamp(void) const |
|
325 |
{ |
|
326 |
return m_timestamp; |
|
327 |
} |
|
328 |
||
329 |
void |
|
330 |
TimestampTag::Print(std::ostream &os) const |
|
331 |
{ |
|
332 |
os << "t=" << m_timestamp; |
|
333 |
} |