author | Tom Henderson <tomh@tomh.org> |
Fri, 25 Feb 2011 10:32:35 -0800 | |
changeset 6834 | 036f9a0b9899 |
parent 6823 | a27f86fb4e55 |
child 7196 | 0f12b1572bca |
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" |
|
6823
a27f86fb4e55
Merge node and common modules into new network module
Tom Henderson <tomh@tomh.org>
parents:
6821
diff
changeset
|
31 |
#include "ns3/network-module.h" |
6834
036f9a0b9899
Rename internet-stack to internet, and organize module
Tom Henderson <tomh@tomh.org>
parents:
6823
diff
changeset
|
32 |
#include "ns3/internet-module.h" |
3570 | 33 |
|
34 |
#include "ns3/stats-module.h" |
|
35 |
||
36 |
#include "wifi-example-apps.h" |
|
37 |
||
38 |
using namespace ns3; |
|
39 |
||
4487
0d67f380c764
Backed out changeset b7faa7398d1e
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
4483
diff
changeset
|
40 |
NS_LOG_COMPONENT_DEFINE ("WiFiDistanceApps"); |
3570 | 41 |
|
42 |
TypeId |
|
43 |
Sender::GetTypeId(void) |
|
44 |
{ |
|
45 |
static TypeId tid = TypeId ("Sender") |
|
46 |
.SetParent<Application> () |
|
47 |
.AddConstructor<Sender> () |
|
4487
0d67f380c764
Backed out changeset b7faa7398d1e
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
4483
diff
changeset
|
48 |
.AddAttribute ("PacketSize", "The size of packets transmitted.", |
3570 | 49 |
UintegerValue(64), |
50 |
MakeUintegerAccessor(&Sender::m_pktSize), |
|
51 |
MakeUintegerChecker<uint32_t>(1)) |
|
4487
0d67f380c764
Backed out changeset b7faa7398d1e
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
4483
diff
changeset
|
52 |
.AddAttribute("Destination", "Target host address.", |
0d67f380c764
Backed out changeset b7faa7398d1e
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
4483
diff
changeset
|
53 |
Ipv4AddressValue("255.255.255.255"), |
0d67f380c764
Backed out changeset b7faa7398d1e
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
4483
diff
changeset
|
54 |
MakeIpv4AddressAccessor(&Sender::m_destAddr), |
0d67f380c764
Backed out changeset b7faa7398d1e
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
4483
diff
changeset
|
55 |
MakeIpv4AddressChecker()) |
0d67f380c764
Backed out changeset b7faa7398d1e
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
4483
diff
changeset
|
56 |
.AddAttribute("Port", "Destination app port.", |
0d67f380c764
Backed out changeset b7faa7398d1e
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
4483
diff
changeset
|
57 |
UintegerValue(1603), |
0d67f380c764
Backed out changeset b7faa7398d1e
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
4483
diff
changeset
|
58 |
MakeUintegerAccessor(&Sender::m_destPort), |
0d67f380c764
Backed out changeset b7faa7398d1e
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
4483
diff
changeset
|
59 |
MakeUintegerChecker<uint32_t>()) |
0d67f380c764
Backed out changeset b7faa7398d1e
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
4483
diff
changeset
|
60 |
.AddAttribute("NumPackets", "Total number of packets to send.", |
0d67f380c764
Backed out changeset b7faa7398d1e
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
4483
diff
changeset
|
61 |
UintegerValue(30), |
0d67f380c764
Backed out changeset b7faa7398d1e
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
4483
diff
changeset
|
62 |
MakeUintegerAccessor(&Sender::m_numPkts), |
0d67f380c764
Backed out changeset b7faa7398d1e
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
4483
diff
changeset
|
63 |
MakeUintegerChecker<uint32_t>(1)) |
0d67f380c764
Backed out changeset b7faa7398d1e
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
4483
diff
changeset
|
64 |
.AddAttribute ("Interval", "Delay between transmissions.", |
0d67f380c764
Backed out changeset b7faa7398d1e
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
4483
diff
changeset
|
65 |
RandomVariableValue(ConstantVariable(0.5)), |
0d67f380c764
Backed out changeset b7faa7398d1e
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
4483
diff
changeset
|
66 |
MakeRandomVariableAccessor(&Sender::m_interval), |
0d67f380c764
Backed out changeset b7faa7398d1e
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
4483
diff
changeset
|
67 |
MakeRandomVariableChecker()) |
3570 | 68 |
.AddTraceSource ("Tx", "A new packet is created and is sent", |
69 |
MakeTraceSourceAccessor (&Sender::m_txTrace)) |
|
70 |
; |
|
71 |
return tid; |
|
72 |
} |
|
73 |
||
74 |
||
75 |
Sender::Sender() |
|
76 |
{ |
|
77 |
NS_LOG_FUNCTION_NOARGS (); |
|
78 |
m_socket = 0; |
|
79 |
} |
|
80 |
||
81 |
Sender::~Sender() |
|
82 |
{ |
|
83 |
NS_LOG_FUNCTION_NOARGS (); |
|
84 |
} |
|
85 |
||
86 |
void |
|
87 |
Sender::DoDispose (void) |
|
88 |
{ |
|
89 |
NS_LOG_FUNCTION_NOARGS (); |
|
90 |
||
91 |
m_socket = 0; |
|
92 |
// chain up |
|
93 |
Application::DoDispose (); |
|
94 |
} |
|
95 |
||
96 |
void Sender::StartApplication() |
|
97 |
{ |
|
98 |
NS_LOG_FUNCTION_NOARGS (); |
|
99 |
||
100 |
if (m_socket == 0) { |
|
101 |
Ptr<SocketFactory> socketFactory = GetNode()->GetObject<SocketFactory> |
|
102 |
(UdpSocketFactory::GetTypeId()); |
|
103 |
m_socket = socketFactory->CreateSocket (); |
|
104 |
m_socket->Bind (); |
|
105 |
} |
|
4487
0d67f380c764
Backed out changeset b7faa7398d1e
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
4483
diff
changeset
|
106 |
|
3570 | 107 |
m_count = 0; |
108 |
||
109 |
Simulator::Cancel(m_sendEvent); |
|
110 |
m_sendEvent = Simulator::ScheduleNow(&Sender::SendPacket, this); |
|
111 |
||
112 |
// end Sender::StartApplication |
|
113 |
} |
|
114 |
||
115 |
void Sender::StopApplication() |
|
116 |
{ |
|
117 |
NS_LOG_FUNCTION_NOARGS (); |
|
118 |
Simulator::Cancel(m_sendEvent); |
|
119 |
// end Sender::StopApplication |
|
120 |
} |
|
121 |
||
122 |
void Sender::SendPacket() |
|
123 |
{ |
|
124 |
// NS_LOG_FUNCTION_NOARGS (); |
|
4487
0d67f380c764
Backed out changeset b7faa7398d1e
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
4483
diff
changeset
|
125 |
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
|
126 |
m_destAddr); |
3570 | 127 |
|
128 |
Ptr<Packet> packet = Create<Packet>(m_pktSize); |
|
129 |
||
130 |
TimestampTag timestamp; |
|
131 |
timestamp.SetTimestamp(Simulator::Now()); |
|
4502 | 132 |
packet->AddByteTag (timestamp); |
3570 | 133 |
|
134 |
// Could connect the socket since the address never changes; using SendTo |
|
135 |
// here simply because all of the standard apps do not. |
|
136 |
m_socket->SendTo(packet, 0, InetSocketAddress(m_destAddr, m_destPort)); |
|
137 |
||
138 |
// Report the event to the trace. |
|
139 |
m_txTrace(packet); |
|
140 |
||
4487
0d67f380c764
Backed out changeset b7faa7398d1e
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
4483
diff
changeset
|
141 |
if (++m_count < m_numPkts) { |
0d67f380c764
Backed out changeset b7faa7398d1e
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
4483
diff
changeset
|
142 |
m_sendEvent = Simulator::Schedule(Seconds(m_interval.GetValue()), |
3570 | 143 |
&Sender::SendPacket, this); |
144 |
} |
|
4487
0d67f380c764
Backed out changeset b7faa7398d1e
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
4483
diff
changeset
|
145 |
|
3570 | 146 |
// end Sender::SendPacket |
147 |
} |
|
148 |
||
149 |
||
150 |
||
151 |
||
152 |
//---------------------------------------------------------------------- |
|
153 |
//-- Receiver |
|
154 |
//------------------------------------------------------ |
|
155 |
TypeId |
|
156 |
Receiver::GetTypeId(void) |
|
157 |
{ |
|
158 |
static TypeId tid = TypeId ("Receiver") |
|
159 |
.SetParent<Application> () |
|
160 |
.AddConstructor<Receiver> () |
|
161 |
.AddAttribute("Port", "Listening port.", |
|
162 |
UintegerValue(1603), |
|
163 |
MakeUintegerAccessor(&Receiver::m_port), |
|
164 |
MakeUintegerChecker<uint32_t>()) |
|
165 |
; |
|
166 |
return tid; |
|
167 |
} |
|
168 |
||
169 |
Receiver::Receiver() : |
|
170 |
m_calc(0), |
|
171 |
m_delay(0) |
|
172 |
{ |
|
173 |
NS_LOG_FUNCTION_NOARGS (); |
|
174 |
m_socket = 0; |
|
175 |
} |
|
176 |
||
177 |
Receiver::~Receiver() |
|
178 |
{ |
|
179 |
NS_LOG_FUNCTION_NOARGS (); |
|
180 |
} |
|
181 |
||
182 |
void |
|
183 |
Receiver::DoDispose (void) |
|
184 |
{ |
|
185 |
NS_LOG_FUNCTION_NOARGS (); |
|
186 |
||
187 |
m_socket = 0; |
|
188 |
// chain up |
|
189 |
Application::DoDispose (); |
|
190 |
} |
|
191 |
||
192 |
void |
|
193 |
Receiver::StartApplication() |
|
194 |
{ |
|
195 |
NS_LOG_FUNCTION_NOARGS (); |
|
196 |
||
197 |
if (m_socket == 0) { |
|
198 |
Ptr<SocketFactory> socketFactory = GetNode()->GetObject<SocketFactory> |
|
199 |
(UdpSocketFactory::GetTypeId()); |
|
200 |
m_socket = socketFactory->CreateSocket(); |
|
201 |
InetSocketAddress local = |
|
202 |
InetSocketAddress(Ipv4Address::GetAny(), m_port); |
|
203 |
m_socket->Bind(local); |
|
204 |
} |
|
205 |
||
206 |
m_socket->SetRecvCallback(MakeCallback(&Receiver::Receive, this)); |
|
207 |
||
208 |
// end Receiver::StartApplication |
|
209 |
} |
|
210 |
||
211 |
void |
|
212 |
Receiver::StopApplication() |
|
213 |
{ |
|
214 |
NS_LOG_FUNCTION_NOARGS (); |
|
215 |
||
216 |
if (m_socket != 0) { |
|
217 |
m_socket->SetRecvCallback(MakeNullCallback<void, Ptr<Socket> > ()); |
|
218 |
} |
|
219 |
||
220 |
// end Receiver::StopApplication |
|
221 |
} |
|
222 |
||
223 |
void |
|
224 |
Receiver::SetCounter(Ptr<CounterCalculator<> > calc) |
|
225 |
{ |
|
226 |
m_calc = calc; |
|
227 |
// end Receiver::SetCounter |
|
228 |
} |
|
229 |
void |
|
230 |
Receiver::SetDelayTracker(Ptr<TimeMinMaxAvgTotalCalculator> delay) |
|
231 |
{ |
|
232 |
m_delay = delay; |
|
233 |
// end Receiver::SetDelayTracker |
|
234 |
} |
|
235 |
||
236 |
void |
|
237 |
Receiver::Receive(Ptr<Socket> socket) |
|
238 |
{ |
|
239 |
// NS_LOG_FUNCTION (this << socket << packet << from); |
|
240 |
||
241 |
Ptr<Packet> packet; |
|
242 |
Address from; |
|
243 |
while (packet = socket->RecvFrom(from)) { |
|
244 |
if (InetSocketAddress::IsMatchingType (from)) { |
|
245 |
InetSocketAddress address = InetSocketAddress::ConvertFrom (from); |
|
246 |
NS_LOG_INFO ("Received " << packet->GetSize() << " bytes from " << |
|
247 |
address.GetIpv4()); |
|
248 |
} |
|
249 |
||
250 |
TimestampTag timestamp; |
|
4582 | 251 |
// Should never not be found since the sender is adding it, but |
252 |
// you never know. |
|
253 |
if (packet->FindFirstMatchingByteTag(timestamp)) { |
|
254 |
Time tx = timestamp.GetTimestamp(); |
|
4487
0d67f380c764
Backed out changeset b7faa7398d1e
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
4483
diff
changeset
|
255 |
|
4582 | 256 |
if (m_delay != 0) { |
257 |
m_delay->Update(Simulator::Now() - tx); |
|
258 |
} |
|
3570 | 259 |
} |
4487
0d67f380c764
Backed out changeset b7faa7398d1e
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
4483
diff
changeset
|
260 |
|
3570 | 261 |
if (m_calc != 0) { |
262 |
m_calc->Update(); |
|
263 |
} |
|
264 |
||
265 |
// end receiving packets |
|
266 |
} |
|
267 |
||
268 |
// end Receiver::Receive |
|
269 |
} |
|
270 |
||
271 |
||
272 |
||
273 |
||
274 |
//---------------------------------------------------------------------- |
|
275 |
//-- TimestampTag |
|
276 |
//------------------------------------------------------ |
|
277 |
TypeId |
|
278 |
TimestampTag::GetTypeId(void) |
|
279 |
{ |
|
280 |
static TypeId tid = TypeId ("TimestampTag") |
|
281 |
.SetParent<Tag> () |
|
282 |
.AddConstructor<TimestampTag> () |
|
283 |
.AddAttribute ("Timestamp", |
|
284 |
"Some momentous point in time!", |
|
285 |
EmptyAttributeValue(), |
|
286 |
MakeTimeAccessor(&TimestampTag::GetTimestamp), |
|
287 |
MakeTimeChecker()) |
|
288 |
; |
|
289 |
return tid; |
|
290 |
} |
|
291 |
TypeId |
|
292 |
TimestampTag::GetInstanceTypeId(void) const |
|
293 |
{ |
|
294 |
return GetTypeId (); |
|
295 |
} |
|
296 |
||
297 |
uint32_t |
|
298 |
TimestampTag::GetSerializedSize (void) const |
|
299 |
{ |
|
300 |
return 8; |
|
301 |
} |
|
302 |
void |
|
303 |
TimestampTag::Serialize (TagBuffer i) const |
|
304 |
{ |
|
305 |
int64_t t = m_timestamp.GetNanoSeconds(); |
|
306 |
i.Write((const uint8_t *)&t, 8); |
|
307 |
} |
|
308 |
void |
|
309 |
TimestampTag::Deserialize (TagBuffer i) |
|
310 |
{ |
|
311 |
int64_t t; |
|
312 |
i.Read((uint8_t *)&t, 8); |
|
313 |
m_timestamp = NanoSeconds(t); |
|
314 |
} |
|
315 |
||
316 |
void |
|
317 |
TimestampTag::SetTimestamp(Time time) |
|
318 |
{ |
|
319 |
m_timestamp = time; |
|
320 |
} |
|
321 |
Time |
|
322 |
TimestampTag::GetTimestamp(void) const |
|
323 |
{ |
|
324 |
return m_timestamp; |
|
325 |
} |
|
326 |
||
327 |
void |
|
328 |
TimestampTag::Print(std::ostream &os) const |
|
329 |
{ |
|
330 |
os << "t=" << m_timestamp; |
|
331 |
} |