|
|
| 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) |
| 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 |
double minX = 0; |
| 108 |
double minY = 0; |
| 109 |
double maxX = 0; |
| 110 |
double 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 |
minX = v.x; |
| 125 |
minY = v.y; |
| 126 |
maxX = v.x; |
| 127 |
maxY = v.y; |
| 128 |
first = false; |
| 129 |
} |
| 130 |
else |
| 131 |
{ |
| 132 |
minX = min (minX, v.x); |
| 133 |
minY = min (minY, v.y); |
| 134 |
maxX = max (maxX, v.x); |
| 135 |
maxY = max (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 = maxX - minX; |
| 143 |
double h = maxY - minY; |
| 144 |
minX -= w * 0.05; |
| 145 |
minY -= h * 0.05; |
| 146 |
maxX = minX + w * 1.10; |
| 147 |
maxY = minY + h * 1.10; |
| 148 |
ostringstream oss; |
| 149 |
oss << GetXMLOpen_anim (0); |
| 150 |
oss << GetXMLOpen_topology (minX,minY,maxX,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 |
} |
|
|
| 146 |
} |
217 |
} |
| 147 |
} |
218 |
} |
| 148 |
} |
219 |
} |
|
|
220 |
if (m_xml) |
| 221 |
{ |
| 222 |
WriteN (m_fHandle, GetXMLClose ("topology")); |
| 223 |
} |
| 149 |
|
224 |
|
| 150 |
// Connect the callback for packet tx events |
225 |
// Connect the callback for packet tx events |
| 151 |
Config::Connect ("/ChannelList/*/TxRxPointToPoint", |
226 |
Config::Connect ("/ChannelList/*/TxRxPointToPoint", |
| 152 |
MakeCallback (&AnimationInterface::DevTxTrace, this)); |
227 |
MakeCallback (&AnimationInterface::DevTxTrace, this)); |
|
|
228 |
#if 0 // To be implemented after trace sources are installed |
| 229 |
Config::Connect ("NodeList/*/DeviceList/*/$ns3::WifiNetDevice/Phy/PhyTxBegin", |
| 230 |
MakeCallback (&AnimationInterface::PhyTxBeginTrace, this)); |
| 231 |
Config::Connect ("NodeList/*/DeviceList/*/$ns3::WifiNetDevice/Phy/PhyTxEnd", |
| 232 |
MakeCallback (&AnimationInterface::PhyTxEndTrace, this)); |
| 233 |
Config::Connect ("NodeList/*/DeviceList/*/$ns3::WifiNetDevice/Phy/PhyRxBegin", |
| 234 |
MakeCallback (&AnimationInterface::PhyRxBeginTrace, this)); |
| 235 |
Config::Connect ("NodeList/*/DeviceList/*/$ns3::WifiNetDevice/Phy/PhyRxEnd", |
| 236 |
MakeCallback (&AnimationInterface::PhyRxEndTrace, this)); |
| 237 |
Config::Connect ("NodeList/*/DeviceList/*/$ns3::WifiNetDevice/Phy/PhyRxDrop", |
| 238 |
MakeCallback (&AnimationInterface::PhyRxDropTrace, this)); |
| 239 |
#endif |
| 240 |
|
| 153 |
} |
241 |
} |
| 154 |
|
242 |
|
| 155 |
void AnimationInterface::StopAnimation () |
243 |
void AnimationInterface::StopAnimation () |
| 156 |
{ |
244 |
{ |
| 157 |
if (m_fHandle > 0) |
245 |
if (m_fHandle > 0) |
| 158 |
{ |
246 |
{ |
|
|
247 |
if (m_xml) |
| 248 |
{ // Terminate the anim element |
| 249 |
WriteN (m_fHandle, GetXMLClose ("anim")); |
| 250 |
} |
| 159 |
close (m_fHandle); |
251 |
close (m_fHandle); |
|
|
252 |
m_fHandle = 0; |
| 160 |
} |
253 |
} |
| 161 |
} |
254 |
} |
| 162 |
|
255 |
|
|
|
256 |
int AnimationInterface::WriteN (int h, const string& st) |
| 257 |
{ |
| 258 |
return WriteN (h, st.c_str (), st.length ()); |
| 259 |
} |
| 260 |
|
| 163 |
|
261 |
|
| 164 |
// Private methods |
262 |
// Private methods |
| 165 |
int AnimationInterface::WriteN (int h, const char* data, uint32_t count) |
263 |
int AnimationInterface::WriteN (int h, const char* data, uint32_t count) |
|
|
| 167 |
uint32_t nLeft = count; |
265 |
uint32_t nLeft = count; |
| 168 |
const char* p = data; |
266 |
const char* p = data; |
| 169 |
uint32_t written = 0; |
267 |
uint32_t written = 0; |
| 170 |
|
|
|
| 171 |
while (nLeft) |
268 |
while (nLeft) |
| 172 |
{ |
269 |
{ |
| 173 |
int n = write (h, p, nLeft); |
270 |
int n = write (h, p, nLeft); |
|
|
| 188 |
{ |
285 |
{ |
| 189 |
Time now = Simulator::Now (); |
286 |
Time now = Simulator::Now (); |
| 190 |
ostringstream oss; |
287 |
ostringstream oss; |
| 191 |
oss << now.GetSeconds () << " P " |
288 |
double fbTx = now.GetSeconds (); |
| 192 |
<< tx->GetNode ()->GetId () << " " |
289 |
double lbTx = (now + txTime).GetSeconds (); |
| 193 |
<< rx->GetNode ()->GetId () << " " |
290 |
double fbRx = (now + rxTime - txTime).GetSeconds (); |
| 194 |
<< (now + txTime).GetSeconds () << " " // last bit tx time |
291 |
double lbRx = (now + rxTime).GetSeconds (); |
| 195 |
<< (now + rxTime - txTime).GetSeconds () << " " // first bit rx time |
292 |
if (m_xml) |
| 196 |
<< (now + rxTime).GetSeconds () << endl; // last bit rx time |
293 |
{ |
| 197 |
WriteN (m_fHandle, oss.str ().c_str (), oss.str ().length ()); |
294 |
oss << GetXMLOpen_packet (0,tx->GetNode ()->GetId (),fbTx,lbTx); |
|
|
295 |
oss << GetXMLOpenClose_rx (0,rx->GetNode ()->GetId (),fbRx,lbRx); |
| 296 |
oss << GetXMLClose ("packet"); |
| 297 |
} |
| 298 |
else |
| 299 |
{ |
| 300 |
oss << setprecision (10); |
| 301 |
oss << now.GetSeconds () << " P " |
| 302 |
<< tx->GetNode ()->GetId () << " " |
| 303 |
<< rx->GetNode ()->GetId () << " " |
| 304 |
<< (now + txTime).GetSeconds () << " " // last bit tx time |
| 305 |
<< (now + rxTime - txTime).GetSeconds () << " " // first bit rx time |
| 306 |
<< (now + rxTime).GetSeconds () << endl; // last bit rx time |
| 307 |
} |
| 308 |
WriteN (m_fHandle, oss.str ()); |
| 198 |
} |
309 |
} |
| 199 |
|
310 |
|
|
|
311 |
|
| 312 |
void AnimationInterface::PhyTxBeginTrace (std::string context, |
| 313 |
Ptr<const Packet> p, |
| 314 |
Ptr<const NetDevice> nd, |
| 315 |
const Time& txTime, |
| 316 |
uint32_t nReceivers) |
| 317 |
{ |
| 318 |
// Add a new pending wireless |
| 319 |
pendingWirelessPackets[p->GetUid ()] = |
| 320 |
AnimPacketInfo (nd, nReceivers, Simulator::Now (), Simulator::Now () + txTime); |
| 321 |
} |
| 322 |
|
| 323 |
|
| 324 |
void AnimationInterface::PhyTxEndTrace (std::string context, |
| 325 |
Ptr<const Packet> p, |
| 326 |
Ptr<const NetDevice> nd) |
| 327 |
{ |
| 328 |
} |
| 329 |
|
| 330 |
|
| 331 |
void AnimationInterface::PhyRxBeginTrace (std::string context, |
| 332 |
Ptr<const Packet> p, |
| 333 |
Ptr<const NetDevice> nd) |
| 334 |
{ |
| 335 |
pendingWirelessPackets[p->GetUid ()].AddRxBegin (nd, Simulator::Now ()); |
| 336 |
} |
| 337 |
|
| 338 |
|
| 339 |
void AnimationInterface::PhyRxEndTrace (std::string context, |
| 340 |
Ptr<const Packet> p, |
| 341 |
Ptr<const NetDevice> nd) |
| 342 |
{ |
| 343 |
|
| 344 |
uint32_t uid = p->GetUid (); |
| 345 |
AnimPacketInfo& pkt = pendingWirelessPackets[uid]; |
| 346 |
if (pkt.AddRxEnd (nd, Simulator::Now ())) |
| 347 |
{ |
| 348 |
//cout << "Packet " << uid << " complete" << endl; |
| 349 |
OutputWirelessPacket (uid, pkt); |
| 350 |
pendingWirelessPackets.erase (pendingWirelessPackets.find (uid));; |
| 351 |
} |
| 352 |
} |
| 353 |
|
| 354 |
|
| 355 |
void AnimationInterface::PhyRxDropTrace (std::string context, |
| 356 |
Ptr<const Packet> p, |
| 357 |
Ptr<const NetDevice> nd) |
| 358 |
{ |
| 359 |
pendingWirelessPackets[p->GetUid ()].AddRxDrop (); |
| 360 |
|
| 361 |
} |
| 362 |
|
| 363 |
// Helper to output a wireless packet. |
| 364 |
// For now, only the XML interface is supported |
| 365 |
|
| 366 |
void AnimationInterface::OutputWirelessPacket (uint32_t uid, AnimPacketInfo& pktInfo) |
| 367 |
|
| 368 |
{ |
| 369 |
if (!m_xml) return; |
| 370 |
ostringstream oss; |
| 371 |
uint32_t nodeId = pktInfo.m_nd->GetNode ()->GetId (); |
| 372 |
double lbTx = pktInfo.m_lbTx; |
| 373 |
|
| 374 |
#if 0 // To be tested |
| 375 |
|
| 376 |
// This is a hack until the notify tx end is working |
| 377 |
if (lbTx == 0) |
| 378 |
{ |
| 379 |
if (pktInfo.m_rx.empty ()) |
| 380 |
{ |
| 381 |
lbTx = pktInfo.m_fbTx + 100E-6; // 100 microsec |
| 382 |
} |
| 383 |
else |
| 384 |
{ |
| 385 |
lbTx = pktInfo.m_fbTx + |
| 386 |
pktInfo.m_rx[0].m_lbRx - pktInfo.m_rx[0].m_fbRx; |
| 387 |
} |
| 388 |
} |
| 389 |
|
| 390 |
#endif |
| 391 |
|
| 392 |
// Oops, need to figure out about range |
| 393 |
oss << GetXMLOpen_wpacket (0,nodeId,pktInfo.m_fbTx,lbTx,500); |
| 394 |
// Now add each rx |
| 395 |
for (uint32_t i = 0; i < pktInfo.m_rx.size (); ++i) |
| 396 |
{ |
| 397 |
AnimRxInfo& rx = pktInfo.m_rx[i]; |
| 398 |
uint32_t rxId = rx.m_nd->GetNode ()->GetId (); |
| 399 |
oss << GetXMLOpenClose_rx (0,rxId,rx.m_fbRx,rx.m_lbRx); |
| 400 |
} |
| 401 |
oss << GetXMLClose ("wpacket"); |
| 402 |
WriteN (m_fHandle, oss.str ()); |
| 403 |
} |
| 404 |
|
| 405 |
|
| 406 |
// XML Private Helpers |
| 407 |
|
| 408 |
std::string AnimationInterface::GetXMLOpen_anim (uint32_t lp) |
| 409 |
{ |
| 410 |
ostringstream oss; |
| 411 |
oss <<"<anim lp = \"" << lp << "\" >\n"; |
| 412 |
return oss.str (); |
| 413 |
} |
| 414 |
std::string AnimationInterface::GetXMLOpen_topology (double minX,double minY,double maxX,double maxY) |
| 415 |
{ |
| 416 |
ostringstream oss; |
| 417 |
oss <<"<topology minX = \"" << minX << "\" minY = \"" << minY |
| 418 |
<< "\" maxX = \"" << maxX << "\" maxY = \"" << maxY |
| 419 |
<< "\">" << endl; |
| 420 |
return oss.str (); |
| 421 |
|
| 422 |
} |
| 423 |
|
| 424 |
std::string AnimationInterface::GetXMLOpenClose_node (uint32_t lp,uint32_t id,double locX,double locY) |
| 425 |
{ |
| 426 |
ostringstream oss; |
| 427 |
oss <<"<node lp = \"" << lp << "\" id = \"" << id << "\"" << " locX = \"" |
| 428 |
<< locX << "\" " << "locY = \"" << locY << "\" \n />"; |
| 429 |
return oss.str (); |
| 430 |
} |
| 431 |
std::string AnimationInterface::GetXMLOpenClose_link (uint32_t fromLp,uint32_t fromId, uint32_t toLp, uint32_t toId) |
| 432 |
{ |
| 433 |
ostringstream oss; |
| 434 |
oss << "<link fromLP = \"0\" fromId = \"" << fromId |
| 435 |
<< "\" toLp = \"0\" toId = \"" << toId |
| 436 |
<< "\"/>" << endl; |
| 437 |
return oss.str (); |
| 438 |
} |
| 439 |
|
| 440 |
|
| 441 |
std::string AnimationInterface::GetXMLOpen_packet (uint32_t fromLp,uint32_t fromId, double fbTx, double lbTx) |
| 442 |
{ |
| 443 |
ostringstream oss; |
| 444 |
oss << setprecision (10); |
| 445 |
oss << "<packet fromLp = \"" << fromLp << "\" fromId = \"" << fromId |
| 446 |
<< "\" fbTx = \"" << fbTx |
| 447 |
<< "\" lbTx = \"" << lbTx |
| 448 |
<< "\">" << endl; |
| 449 |
return oss.str (); |
| 450 |
} |
| 451 |
|
| 452 |
std::string AnimationInterface::GetXMLOpen_wpacket (uint32_t fromLp,uint32_t fromId, double fbTx, double lbTx, double range) |
| 453 |
{ |
| 454 |
ostringstream oss; |
| 455 |
oss << setprecision (10); |
| 456 |
oss << "<wpacket fromLp = \"" << fromLp << "\" fromId = \"" << fromId |
| 457 |
<< "\" fbTx = \"" << fbTx |
| 458 |
<< "\" lbTx = \"" << lbTx |
| 459 |
<< "\" range = \"" << range << "\">" << endl; |
| 460 |
return oss.str (); |
| 461 |
|
| 462 |
} |
| 463 |
|
| 464 |
std::string AnimationInterface::GetXMLOpenClose_rx (uint32_t toLp, uint32_t toId, double fbRx, double lbRx) |
| 465 |
{ |
| 466 |
ostringstream oss; |
| 467 |
oss << setprecision (10); |
| 468 |
oss << "<rx toLp = \"" << toLp <<"\" toId = \"" << toId |
| 469 |
<< "\" fbRx = \"" << fbRx |
| 470 |
<< "\" lbRx = \"" << lbRx |
| 471 |
<< "\"/>" << endl; |
| 472 |
return oss.str (); |
| 473 |
} |
| 474 |
|
| 475 |
|
| 200 |
} // namespace ns3 |
476 |
} // namespace ns3 |