|
20 |
|
20 |
|
21 |
#include <stdio.h> |
21 |
#include <stdio.h> |
22 |
#include <sstream> |
22 |
#include <sstream> |
|
|
23 |
#include <string> |
24 |
#include <iomanip> |
25 |
#include <map> |
23 |
|
26 |
|
24 |
// Socket related includes |
27 |
// Socket related includes |
25 |
#if defined(HAVE_SYS_SOCKET_H) && defined(HAVE_NETINET_IN_H) |
28 |
#if defined(HAVE_SYS_SOCKET_H) && defined(HAVE_NETINET_IN_H) |
|
37 |
#include "ns3/mobility-model.h" |
40 |
#include "ns3/mobility-model.h" |
38 |
#include "ns3/packet.h" |
41 |
#include "ns3/packet.h" |
39 |
#include "ns3/simulator.h" |
42 |
#include "ns3/simulator.h" |
|
|
43 |
#include "ns3/animation-interface-helper.h" |
40 |
|
44 |
|
41 |
using namespace std; |
45 |
using namespace std; |
42 |
|
46 |
|
|
45 |
namespace ns3 { |
49 |
namespace ns3 { |
46 |
|
50 |
|
47 |
AnimationInterface::AnimationInterface () |
51 |
AnimationInterface::AnimationInterface () |
48 |
: m_fHandle (STDOUT_FILENO), m_model (0) |
52 |
: m_fHandle (STDOUT_FILENO), m_xml (false), m_model (0), mobilitypollinterval (Seconds(0.25)) |
49 |
{ |
53 |
{ |
50 |
} |
54 |
} |
51 |
|
55 |
|
52 |
AnimationInterface::~AnimationInterface () |
56 |
AnimationInterface::~AnimationInterface () |
53 |
{ |
57 |
{ |
|
|
58 |
StopAnimation (); |
59 |
} |
60 |
|
61 |
void AnimationInterface::SetXMLOutput () |
62 |
{ |
63 |
m_xml = true; |
54 |
} |
64 |
} |
55 |
|
65 |
|
56 |
bool AnimationInterface::SetOutputFile (const std::string& fn) |
66 |
bool AnimationInterface::SetOutputFile (const std::string& fn) |
|
93 |
|
103 |
|
94 |
void AnimationInterface::StartAnimation () |
104 |
void AnimationInterface::StartAnimation () |
95 |
{ |
105 |
{ |
|
|
106 |
// Find the min/max x/y for the xml topology element |
107 |
topo_minX = 0; |
108 |
topo_minY = 0; |
109 |
topo_maxX = 0; |
110 |
topo_maxY = 0; |
111 |
bool first = true; |
112 |
for (NodeList::Iterator i = NodeList::Begin (); i != NodeList::End (); ++i) |
113 |
{ |
114 |
Ptr<Node> n = *i; |
115 |
Ptr<MobilityModel> loc = n->GetObject<MobilityModel> (); |
116 |
Vector v; |
117 |
if (!loc) continue; |
118 |
if (loc) |
119 |
{ |
120 |
v = loc->GetPosition (); |
121 |
} |
122 |
if (first) |
123 |
{ |
124 |
topo_minX = v.x; |
125 |
topo_minY = v.y; |
126 |
topo_maxX = v.x; |
127 |
topo_maxY = v.y; |
128 |
first = false; |
129 |
} |
130 |
else |
131 |
{ |
132 |
topo_minX = min (topo_minX, v.x); |
133 |
topo_minY = min (topo_minY, v.y); |
134 |
topo_maxX = max (topo_maxX, v.x); |
135 |
topo_maxY = max (topo_maxY, v.y); |
136 |
} |
137 |
} |
138 |
|
139 |
if (m_xml) |
140 |
{ // output the xml headers |
141 |
// Compute width/height, and add a small margin |
142 |
double w = topo_maxX - topo_minX; |
143 |
double h = topo_maxY - topo_minY; |
144 |
topo_minX -= w * 0.05; |
145 |
topo_minY -= h * 0.05; |
146 |
topo_maxX = topo_minX + w * 1.10; |
147 |
topo_maxY = topo_minY + h * 1.10; |
148 |
ostringstream oss; |
149 |
oss << GetXMLOpen_anim (0); |
150 |
oss << GetXMLOpen_topology (topo_minX,topo_minY,topo_maxX,topo_maxY); |
151 |
WriteN (m_fHandle, oss.str ()); |
152 |
} |
96 |
// Dump the topology |
153 |
// Dump the topology |
97 |
for (NodeList::Iterator i = NodeList::Begin (); i != NodeList::End (); ++i) |
154 |
for (NodeList::Iterator i = NodeList::Begin (); i != NodeList::End (); ++i) |
98 |
{ |
155 |
{ |
99 |
Ptr<Node> n = *i; |
156 |
Ptr<Node> n = *i; |
100 |
Ptr<MobilityModel> loc = n->GetObject<MobilityModel> (); |
157 |
Ptr<MobilityModel> loc = n->GetObject<MobilityModel> (); |
101 |
if (loc) |
158 |
if (!loc) continue; // Can't find a position |
|
|
159 |
ostringstream oss; |
160 |
if (m_xml) |
161 |
{ |
162 |
Vector v = loc->GetPosition (); |
163 |
oss << GetXMLOpenClose_node (0,n->GetId (),v.x,v.y); |
164 |
} |
165 |
else |
102 |
{ |
166 |
{ |
103 |
// Location exists, dump it |
167 |
// Location exists, dump it |
104 |
Vector v = loc->GetPosition (); |
168 |
Vector v = loc->GetPosition (); |
105 |
ostringstream oss; |
|
|
106 |
oss << "0.0 N " << n->GetId () |
169 |
oss << "0.0 N " << n->GetId () |
107 |
<< " " << v.x << " " << v.y << endl; |
170 |
<< " " << v.x << " " << v.y << endl; |
108 |
WriteN (m_fHandle, oss.str ().c_str (), oss.str ().length ()); |
|
|
109 |
} |
171 |
} |
|
|
172 |
|
173 |
WriteN (m_fHandle, oss.str ().c_str (), oss.str ().length ()); |
110 |
} |
174 |
} |
111 |
// Now dump the p2p links |
175 |
// Now dump the p2p links |
112 |
for (NodeList::Iterator i = NodeList::Begin (); i != NodeList::End (); ++i) |
176 |
for (NodeList::Iterator i = NodeList::Begin (); i != NodeList::End (); ++i) |
|
134 |
if (n1Id < n2Id) |
198 |
if (n1Id < n2Id) |
135 |
{ // ouptut the p2p link |
199 |
{ // ouptut the p2p link |
136 |
ostringstream oss; |
200 |
ostringstream oss; |
137 |
oss << "0.0 L " << n1Id << " " << n2Id << endl; |
201 |
if (m_xml) |
138 |
WriteN (m_fHandle, oss.str ().c_str (), |
202 |
{ |
139 |
oss.str ().length ()); |
203 |
oss << GetXMLOpenClose_link (0,n1Id,0,n2Id); |
|
|
204 |
} |
205 |
else |
206 |
{ |
207 |
oss << "0.0 L " << n1Id << " " << n2Id << endl; |
208 |
} |
209 |
WriteN (m_fHandle, oss.str ()); |
210 |
|
140 |
} |
211 |
} |
141 |
} |
212 |
} |
142 |
} |
213 |
} |
143 |
else |
214 |
else |
144 |
{ |
215 |
{ |
145 |
NS_FATAL_ERROR ("Net animation currently only supports point-to-point links."); |
216 |
//NS_FATAL_ERROR ("Net animation currently only supports point-to-point links."); |
146 |
} |
217 |
} |
147 |
} |
218 |
} |
148 |
} |
219 |
} |
|
|
220 |
if (m_xml) |
221 |
{ |
222 |
WriteN (m_fHandle, GetXMLClose ("topology")); |
223 |
Simulator::Schedule (mobilitypollinterval, &AnimationInterface::MobilityAutoCheck, this); |
224 |
} |
149 |
|
225 |
|
150 |
// Connect the callback for packet tx events |
226 |
// Connect the callback for packet tx events |
151 |
Config::Connect ("/ChannelList/*/TxRxPointToPoint", |
227 |
Config::Connect ("/ChannelList/*/TxRxPointToPoint", |
152 |
MakeCallback (&AnimationInterface::DevTxTrace, this)); |
228 |
MakeCallback (&AnimationInterface::DevTxTrace, this)); |
|
|
229 |
//#if 0 // To be implemented after trace sources are installed |
230 |
Config::Connect ("NodeList/*/DeviceList/*/$ns3::WifiNetDevice/Phy/PhyTxBegin", |
231 |
MakeCallback (&AnimationInterface::PhyTxBeginTrace, this)); |
232 |
Config::Connect ("NodeList/*/DeviceList/*/$ns3::WifiNetDevice/Phy/PhyTxEnd", |
233 |
MakeCallback (&AnimationInterface::PhyTxEndTrace, this)); |
234 |
Config::Connect ("NodeList/*/DeviceList/*/$ns3::WifiNetDevice/Phy/PhyRxBegin", |
235 |
MakeCallback (&AnimationInterface::PhyRxBeginTrace, this)); |
236 |
Config::Connect ("NodeList/*/DeviceList/*/$ns3::WifiNetDevice/Phy/PhyRxEnd", |
237 |
MakeCallback (&AnimationInterface::PhyRxEndTrace, this)); |
238 |
Config::Connect ("NodeList/*/DeviceList/*/$ns3::WifiNetDevice/Phy/PhyRxDrop", |
239 |
MakeCallback (&AnimationInterface::PhyRxDropTrace, this)); |
240 |
Config::ConnectWithoutContext ("/NodeList/1/$ns3::MobilityModel/CourseChange", |
241 |
MakeCallback (&AnimationInterface::MobilityCourseChangeTrace, this)); |
242 |
|
243 |
//#endif |
244 |
|
153 |
} |
245 |
} |
154 |
|
246 |
|
155 |
void AnimationInterface::StopAnimation () |
247 |
void AnimationInterface::StopAnimation () |
156 |
{ |
248 |
{ |
157 |
if (m_fHandle > 0) |
249 |
if (m_fHandle > 0) |
158 |
{ |
250 |
{ |
|
|
251 |
if (m_xml) |
252 |
{ // Terminate the anim element |
253 |
WriteN (m_fHandle, GetXMLClose ("anim")); |
254 |
} |
159 |
close (m_fHandle); |
255 |
close (m_fHandle); |
|
|
256 |
m_fHandle = 0; |
160 |
} |
257 |
} |
161 |
} |
258 |
} |
162 |
|
259 |
|
|
|
260 |
int AnimationInterface::WriteN (int h, const string& st) |
261 |
{ |
262 |
return WriteN (h, st.c_str (), st.length ()); |
263 |
} |
264 |
|
163 |
|
265 |
|
164 |
// Private methods |
266 |
// Private methods |
165 |
int AnimationInterface::WriteN (int h, const char* data, uint32_t count) |
267 |
int AnimationInterface::WriteN (int h, const char* data, uint32_t count) |
|
167 |
uint32_t nLeft = count; |
269 |
uint32_t nLeft = count; |
168 |
const char* p = data; |
270 |
const char* p = data; |
169 |
uint32_t written = 0; |
271 |
uint32_t written = 0; |
170 |
|
|
|
171 |
while (nLeft) |
272 |
while (nLeft) |
172 |
{ |
273 |
{ |
173 |
int n = write (h, p, nLeft); |
274 |
int n = write (h, p, nLeft); |
|
188 |
{ |
289 |
{ |
189 |
Time now = Simulator::Now (); |
290 |
Time now = Simulator::Now (); |
190 |
ostringstream oss; |
291 |
ostringstream oss; |
191 |
oss << now.GetSeconds () << " P " |
292 |
double fbTx = now.GetSeconds (); |
192 |
<< tx->GetNode ()->GetId () << " " |
293 |
double lbTx = (now + txTime).GetSeconds (); |
193 |
<< rx->GetNode ()->GetId () << " " |
294 |
double fbRx = (now + rxTime - txTime).GetSeconds (); |
194 |
<< (now + txTime).GetSeconds () << " " // last bit tx time |
295 |
double lbRx = (now + rxTime).GetSeconds (); |
195 |
<< (now + rxTime - txTime).GetSeconds () << " " // first bit rx time |
296 |
if (m_xml) |
196 |
<< (now + rxTime).GetSeconds () << endl; // last bit rx time |
297 |
{ |
197 |
WriteN (m_fHandle, oss.str ().c_str (), oss.str ().length ()); |
298 |
oss << GetXMLOpen_packet (0,tx->GetNode ()->GetId (),fbTx,lbTx); |
|
|
299 |
oss << GetXMLOpenClose_rx (0,rx->GetNode ()->GetId (),fbRx,lbRx); |
300 |
oss << GetXMLClose ("packet"); |
301 |
} |
302 |
else |
303 |
{ |
304 |
oss << setprecision (10); |
305 |
oss << now.GetSeconds () << " P " |
306 |
<< tx->GetNode ()->GetId () << " " |
307 |
<< rx->GetNode ()->GetId () << " " |
308 |
<< (now + txTime).GetSeconds () << " " // last bit tx time |
309 |
<< (now + rxTime - txTime).GetSeconds () << " " // first bit rx time |
310 |
<< (now + rxTime).GetSeconds () << endl; // last bit rx time |
311 |
} |
312 |
WriteN (m_fHandle, oss.str ()); |
198 |
} |
313 |
} |
199 |
|
314 |
|
|
|
315 |
|
316 |
void AnimationInterface::PhyTxBeginTrace (std::string context, |
317 |
Ptr<const Packet> p, |
318 |
Ptr<const Object> nd, |
319 |
const Time& txTime, |
320 |
uint32_t nReceivers) |
321 |
{ |
322 |
// Add a new pending wireless |
323 |
Ptr<const NetDevice> ndev = DynamicCast<const NetDevice>(nd); |
324 |
NS_ASSERT (ndev); |
325 |
pendingWirelessPackets[p->GetUid ()] = |
326 |
AnimPacketInfo (ndev, nReceivers, Simulator::Now (), Simulator::Now () + txTime); |
327 |
} |
328 |
|
329 |
|
330 |
void AnimationInterface::PhyTxEndTrace (std::string context, |
331 |
Ptr<const Packet> p, |
332 |
Ptr<const Object> nd) |
333 |
{ |
334 |
} |
335 |
|
336 |
|
337 |
void AnimationInterface::PhyRxBeginTrace (std::string context, |
338 |
Ptr<const Packet> p, |
339 |
Ptr<const Object> nd) |
340 |
{ |
341 |
Ptr<const NetDevice> ndev = DynamicCast<const NetDevice>(nd); |
342 |
NS_ASSERT (ndev); |
343 |
pendingWirelessPackets[p->GetUid ()].AddRxBegin (ndev, Simulator::Now ()); |
344 |
} |
345 |
|
346 |
|
347 |
void AnimationInterface::PhyRxEndTrace (std::string context, |
348 |
Ptr<const Packet> p, |
349 |
Ptr<const Object> nd) |
350 |
{ |
351 |
Ptr<const NetDevice> ndev = DynamicCast<const NetDevice>(nd); |
352 |
NS_ASSERT (ndev); |
353 |
uint32_t uid = p->GetUid (); |
354 |
AnimPacketInfo& pkt = pendingWirelessPackets[uid]; |
355 |
if (pkt.AddRxEnd (ndev, Simulator::Now ())) |
356 |
{ |
357 |
//cout << "Packet " << uid << " complete" << endl; |
358 |
OutputWirelessPacket (uid, pkt); |
359 |
pendingWirelessPackets.erase (pendingWirelessPackets.find (uid));; |
360 |
} |
361 |
} |
362 |
|
363 |
|
364 |
void AnimationInterface::PhyRxDropTrace (std::string context, |
365 |
Ptr<const Packet> p, |
366 |
Ptr<const Object> nd) |
367 |
{ |
368 |
pendingWirelessPackets[p->GetUid ()].AddRxDrop (); |
369 |
} |
370 |
|
371 |
|
372 |
void AnimationInterface::MobilityCourseChangeTrace (Ptr <const MobilityModel> mobility) |
373 |
|
374 |
{ |
375 |
Ptr <Node> n = mobility->GetObject <Node> (); |
376 |
if (!n) return; |
377 |
if (!mobility) return; |
378 |
Vector v ; |
379 |
v = mobility->GetPosition (); |
380 |
ostringstream oss; |
381 |
oss << GetXMLOpen_topology (topo_minX,topo_minY,topo_maxX,topo_maxY); |
382 |
oss << GetXMLOpenClose_node (0,n->GetId (),v.x,v.y); |
383 |
oss << GetXMLClose ("topology"); |
384 |
WriteN (m_fHandle, oss.str ()); |
385 |
} |
386 |
|
387 |
void AnimationInterface::MobilityAutoCheck () |
388 |
{ |
389 |
Simulator::Schedule (mobilitypollinterval, &AnimationInterface::MobilityAutoCheck, this); |
390 |
for (NodeList::Iterator i = NodeList::Begin (); i != NodeList::End (); ++i) |
391 |
{ |
392 |
Ptr <Node> n = *i; |
393 |
if (!n) continue; |
394 |
Ptr <MobilityModel> mobility = n->GetObject <MobilityModel> (); |
395 |
if (!mobility) continue; |
396 |
Vector v ; |
397 |
v = mobility->GetPosition (); |
398 |
ostringstream oss; |
399 |
oss << GetXMLOpen_topology (topo_minX,topo_minY,topo_maxX,topo_maxY); |
400 |
oss << GetXMLOpenClose_node (0,n->GetId (),v.x,v.y); |
401 |
oss << GetXMLClose ("topology"); |
402 |
WriteN (m_fHandle, oss.str ()); |
403 |
} |
404 |
} |
405 |
|
406 |
|
407 |
// Helper to output a wireless packet. |
408 |
// For now, only the XML interface is supported |
409 |
|
410 |
void AnimationInterface::OutputWirelessPacket (uint32_t uid, AnimPacketInfo& pktInfo) |
411 |
|
412 |
{ |
413 |
if (!m_xml) return; |
414 |
ostringstream oss; |
415 |
uint32_t nodeId = pktInfo.m_nd->GetNode ()->GetId (); |
416 |
double lbTx = pktInfo.m_lbTx; |
417 |
|
418 |
#if 0 // To be tested |
419 |
|
420 |
// This is a hack until the notify tx end is working |
421 |
if (lbTx == 0) |
422 |
{ |
423 |
if (pktInfo.m_rx.empty ()) |
424 |
{ |
425 |
lbTx = pktInfo.m_fbTx + 100E-6; // 100 microsec |
426 |
} |
427 |
else |
428 |
{ |
429 |
lbTx = pktInfo.m_fbTx + |
430 |
pktInfo.m_rx[0].m_lbRx - pktInfo.m_rx[0].m_fbRx; |
431 |
} |
432 |
} |
433 |
|
434 |
#endif |
435 |
|
436 |
// Oops, need to figure out about range |
437 |
oss << GetXMLOpen_wpacket (0,nodeId,pktInfo.m_fbTx,lbTx,500); |
438 |
// Now add each rx |
439 |
for (uint32_t i = 0; i < pktInfo.m_rx.size (); ++i) |
440 |
{ |
441 |
AnimRxInfo& rx = pktInfo.m_rx[i]; |
442 |
uint32_t rxId = rx.m_nd->GetNode ()->GetId (); |
443 |
oss << GetXMLOpenClose_rx (0,rxId,rx.m_fbRx,rx.m_lbRx); |
444 |
} |
445 |
oss << GetXMLClose ("wpacket"); |
446 |
WriteN (m_fHandle, oss.str ()); |
447 |
} |
448 |
|
449 |
|
450 |
// XML Private Helpers |
451 |
|
452 |
std::string AnimationInterface::GetXMLOpen_anim (uint32_t lp) |
453 |
{ |
454 |
ostringstream oss; |
455 |
oss <<"<anim lp = \"" << lp << "\" >\n"; |
456 |
return oss.str (); |
457 |
} |
458 |
std::string AnimationInterface::GetXMLOpen_topology (double minX,double minY,double maxX,double maxY) |
459 |
{ |
460 |
ostringstream oss; |
461 |
oss <<"<topology minX = \"" << minX << "\" minY = \"" << minY |
462 |
<< "\" maxX = \"" << maxX << "\" maxY = \"" << maxY |
463 |
<< "\">" << endl; |
464 |
return oss.str (); |
465 |
|
466 |
} |
467 |
|
468 |
std::string AnimationInterface::GetXMLOpenClose_node (uint32_t lp,uint32_t id,double locX,double locY) |
469 |
{ |
470 |
ostringstream oss; |
471 |
oss <<"<node lp = \"" << lp << "\" id = \"" << id << "\"" << " locX = \"" |
472 |
<< locX << "\" " << "locY = \"" << locY << "\" />\n"; |
473 |
return oss.str (); |
474 |
} |
475 |
std::string AnimationInterface::GetXMLOpenClose_link (uint32_t fromLp,uint32_t fromId, uint32_t toLp, uint32_t toId) |
476 |
{ |
477 |
ostringstream oss; |
478 |
oss << "<link fromLP = \"0\" fromId = \"" << fromId |
479 |
<< "\" toLp = \"0\" toId = \"" << toId |
480 |
<< "\"/>" << endl; |
481 |
return oss.str (); |
482 |
} |
483 |
|
484 |
|
485 |
std::string AnimationInterface::GetXMLOpen_packet (uint32_t fromLp,uint32_t fromId, double fbTx, double lbTx) |
486 |
{ |
487 |
ostringstream oss; |
488 |
oss << setprecision (10); |
489 |
oss << "<packet fromLp = \"" << fromLp << "\" fromId = \"" << fromId |
490 |
<< "\" fbTx = \"" << fbTx |
491 |
<< "\" lbTx = \"" << lbTx |
492 |
<< "\">" << endl; |
493 |
return oss.str (); |
494 |
} |
495 |
|
496 |
std::string AnimationInterface::GetXMLOpen_wpacket (uint32_t fromLp,uint32_t fromId, double fbTx, double lbTx, double range) |
497 |
{ |
498 |
ostringstream oss; |
499 |
oss << setprecision (10); |
500 |
oss << "<wpacket fromLp = \"" << fromLp << "\" fromId = \"" << fromId |
501 |
<< "\" fbTx = \"" << fbTx |
502 |
<< "\" lbTx = \"" << lbTx |
503 |
<< "\" range = \"" << range << "\">" << endl; |
504 |
return oss.str (); |
505 |
|
506 |
} |
507 |
|
508 |
std::string AnimationInterface::GetXMLOpenClose_rx (uint32_t toLp, uint32_t toId, double fbRx, double lbRx) |
509 |
{ |
510 |
ostringstream oss; |
511 |
oss << setprecision (10); |
512 |
oss << "<rx toLp = \"" << toLp <<"\" toId = \"" << toId |
513 |
<< "\" fbRx = \"" << fbRx |
514 |
<< "\" lbRx = \"" << lbRx |
515 |
<< "\"/>" << endl; |
516 |
return oss.str (); |
517 |
} |
518 |
|
519 |
|
200 |
} // namespace ns3 |
520 |
} // namespace ns3 |