|
|
| 14 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
14 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 15 |
* |
15 |
* |
| 16 |
* Author: George F. Riley<riley@ece.gatech.edu> |
16 |
* Author: George F. Riley<riley@ece.gatech.edu> |
|
|
17 |
* Modified by: John Abraham <john.abraham@gatech.edu> |
| 17 |
*/ |
18 |
*/ |
| 18 |
|
19 |
|
| 19 |
// Interface between ns3 and the network animator |
20 |
// Interface between ns3 and the network animator |
| 20 |
|
21 |
|
| 21 |
#include <stdio.h> |
22 |
#include <stdio.h> |
| 22 |
#include <sstream> |
23 |
#include <sstream> |
|
|
24 |
#include <string> |
| 25 |
#include <iomanip> |
| 26 |
#include <map> |
| 23 |
|
27 |
|
| 24 |
// Socket related includes |
28 |
// Socket related includes |
| 25 |
#if defined(HAVE_SYS_SOCKET_H) && defined(HAVE_NETINET_IN_H) |
29 |
#if defined(HAVE_SYS_SOCKET_H) && defined(HAVE_NETINET_IN_H) |
|
|
| 37 |
#include "ns3/mobility-model.h" |
41 |
#include "ns3/mobility-model.h" |
| 38 |
#include "ns3/packet.h" |
42 |
#include "ns3/packet.h" |
| 39 |
#include "ns3/simulator.h" |
43 |
#include "ns3/simulator.h" |
|
|
44 |
#include "ns3/animation-interface-helper.h" |
| 40 |
|
45 |
|
| 41 |
using namespace std; |
46 |
using namespace std; |
| 42 |
|
47 |
|
|
|
| 45 |
namespace ns3 { |
50 |
namespace ns3 { |
| 46 |
|
51 |
|
| 47 |
AnimationInterface::AnimationInterface () |
52 |
AnimationInterface::AnimationInterface () |
| 48 |
: m_fHandle (STDOUT_FILENO), m_model (0) |
53 |
: m_fHandle (STDOUT_FILENO), m_xml (false), m_model (0), mobilitypollinterval (Seconds(0.25)) |
| 49 |
{ |
54 |
{ |
| 50 |
} |
55 |
} |
| 51 |
|
56 |
|
| 52 |
AnimationInterface::~AnimationInterface () |
57 |
AnimationInterface::~AnimationInterface () |
| 53 |
{ |
58 |
{ |
|
|
59 |
StopAnimation (); |
| 60 |
} |
| 61 |
|
| 62 |
void AnimationInterface::SetXMLOutput () |
| 63 |
{ |
| 64 |
NS_LOG_INFO ("XML output set"); |
| 65 |
m_xml = true; |
| 54 |
} |
66 |
} |
| 55 |
|
67 |
|
| 56 |
bool AnimationInterface::SetOutputFile (const std::string& fn) |
68 |
bool AnimationInterface::SetOutputFile (const std::string& fn) |
|
|
| 58 |
FILE* f = fopen (fn.c_str (), "w"); |
70 |
FILE* f = fopen (fn.c_str (), "w"); |
| 59 |
if (!f) |
71 |
if (!f) |
| 60 |
{ |
72 |
{ |
|
|
73 |
NS_FATAL_ERROR ("Unable to output Animation output file"); |
| 61 |
return false; // Can't open |
74 |
return false; // Can't open |
| 62 |
} |
75 |
} |
| 63 |
m_fHandle = fileno (f); // Set the file handle |
76 |
m_fHandle = fileno (f); // Set the file handle |
|
|
| 91 |
// which is done to support a platform like MinGW |
104 |
// which is done to support a platform like MinGW |
| 92 |
} |
105 |
} |
| 93 |
|
106 |
|
|
|
107 |
Vector AnimationInterface::GetPosition (Ptr <Node> n) |
| 108 |
{ |
| 109 |
Ptr<MobilityModel> loc = n->GetObject<MobilityModel> (); |
| 110 |
Vector v(0,0,0); |
| 111 |
if (!loc) |
| 112 |
{ |
| 113 |
return v; |
| 114 |
} |
| 115 |
if (loc) |
| 116 |
{ |
| 117 |
v = loc->GetPosition (); |
| 118 |
} |
| 119 |
return v; |
| 120 |
} |
| 94 |
void AnimationInterface::StartAnimation () |
121 |
void AnimationInterface::StartAnimation () |
| 95 |
{ |
122 |
{ |
|
|
123 |
// Find the min/max x/y for the xml topology element |
| 124 |
topo_minX = 0; |
| 125 |
topo_minY = 0; |
| 126 |
topo_maxX = 1000; |
| 127 |
topo_maxY = 1000; |
| 128 |
bool first = true; |
| 129 |
for (NodeList::Iterator i = NodeList::Begin (); i != NodeList::End (); ++i) |
| 130 |
{ |
| 131 |
Ptr<Node> n = *i; |
| 132 |
Vector v = GetPosition (n); |
| 133 |
if (first) |
| 134 |
{ |
| 135 |
topo_minX = v.x; |
| 136 |
topo_minY = v.y; |
| 137 |
topo_maxX = v.x; |
| 138 |
topo_maxY = v.y; |
| 139 |
first = false; |
| 140 |
} |
| 141 |
else |
| 142 |
{ |
| 143 |
topo_minX = min (topo_minX, v.x); |
| 144 |
topo_minY = min (topo_minY, v.y); |
| 145 |
topo_maxX = max (topo_maxX, v.x); |
| 146 |
topo_maxY = max (topo_maxY, v.y); |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
if (m_xml) |
| 151 |
{ // output the xml headers |
| 152 |
ostringstream oss; |
| 153 |
oss << GetXMLOpen_anim (0); |
| 154 |
oss << GetXMLOpen_topology (topo_minX,topo_minY,topo_maxX,topo_maxY); |
| 155 |
WriteN (m_fHandle, oss.str ()); |
| 156 |
} |
| 96 |
// Dump the topology |
157 |
// Dump the topology |
| 97 |
for (NodeList::Iterator i = NodeList::Begin (); i != NodeList::End (); ++i) |
158 |
for (NodeList::Iterator i = NodeList::Begin (); i != NodeList::End (); ++i) |
| 98 |
{ |
159 |
{ |
| 99 |
Ptr<Node> n = *i; |
160 |
Ptr<Node> n = *i; |
| 100 |
Ptr<MobilityModel> loc = n->GetObject<MobilityModel> (); |
161 |
ostringstream oss; |
| 101 |
if (loc) |
162 |
if (m_xml) |
|
|
163 |
{ |
| 164 |
Vector v = GetPosition (n); |
| 165 |
oss << GetXMLOpenClose_node (0,n->GetId (),v.x,v.y); |
| 166 |
} |
| 167 |
else |
| 102 |
{ |
168 |
{ |
| 103 |
// Location exists, dump it |
169 |
// Location exists, dump it |
| 104 |
Vector v = loc->GetPosition (); |
170 |
Vector v = GetPosition (n); |
| 105 |
ostringstream oss; |
|
|
| 106 |
oss << "0.0 N " << n->GetId () |
171 |
oss << "0.0 N " << n->GetId () |
| 107 |
<< " " << v.x << " " << v.y << endl; |
172 |
<< " " << v.x << " " << v.y << endl; |
| 108 |
WriteN (m_fHandle, oss.str ().c_str (), oss.str ().length ()); |
|
|
| 109 |
} |
173 |
} |
|
|
174 |
|
| 175 |
WriteN (m_fHandle, oss.str ().c_str (), oss.str ().length ()); |
| 110 |
} |
176 |
} |
| 111 |
// Now dump the p2p links |
177 |
// Now dump the p2p links |
| 112 |
for (NodeList::Iterator i = NodeList::Begin (); i != NodeList::End (); ++i) |
178 |
for (NodeList::Iterator i = NodeList::Begin (); i != NodeList::End (); ++i) |
|
|
| 117 |
for (uint32_t i = 0; i < nDev; ++i) |
183 |
for (uint32_t i = 0; i < nDev; ++i) |
| 118 |
{ |
184 |
{ |
| 119 |
Ptr<NetDevice> dev = n->GetDevice (i); |
185 |
Ptr<NetDevice> dev = n->GetDevice (i); |
|
|
186 |
NS_ASSERT (dev); |
| 120 |
Ptr<Channel> ch = dev->GetChannel (); |
187 |
Ptr<Channel> ch = dev->GetChannel (); |
| 121 |
if (!ch) |
188 |
if (!ch) |
| 122 |
{ |
189 |
{ |
| 123 |
continue; // No channel, can't be p2p device |
190 |
NS_LOG_DEBUG ("No channel can't be a p2p device"); |
|
|
191 |
continue; |
| 124 |
} |
192 |
} |
| 125 |
string channelType = ch->GetInstanceTypeId ().GetName (); |
193 |
string channelType = ch->GetInstanceTypeId ().GetName (); |
| 126 |
if (channelType == string ("ns3::PointToPointChannel")) |
194 |
if (channelType == string ("ns3::PointToPointChannel")) |
|
|
| 134 |
if (n1Id < n2Id) |
202 |
if (n1Id < n2Id) |
| 135 |
{ // ouptut the p2p link |
203 |
{ // ouptut the p2p link |
| 136 |
ostringstream oss; |
204 |
ostringstream oss; |
| 137 |
oss << "0.0 L " << n1Id << " " << n2Id << endl; |
205 |
if (m_xml) |
| 138 |
WriteN (m_fHandle, oss.str ().c_str (), |
206 |
{ |
| 139 |
oss.str ().length ()); |
207 |
oss << GetXMLOpenClose_link (0,n1Id,0,n2Id); |
|
|
208 |
} |
| 209 |
else |
| 210 |
{ |
| 211 |
oss << "0.0 L " << n1Id << " " << n2Id << endl; |
| 212 |
} |
| 213 |
WriteN (m_fHandle, oss.str ()); |
| 214 |
|
| 140 |
} |
215 |
} |
| 141 |
} |
216 |
} |
| 142 |
} |
217 |
} |
| 143 |
else |
218 |
else |
| 144 |
{ |
219 |
{ |
| 145 |
NS_FATAL_ERROR ("Net animation currently only supports point-to-point links."); |
220 |
//NS_FATAL_ERROR ("Net animation currently only supports point-to-point links."); |
| 146 |
} |
221 |
} |
| 147 |
} |
222 |
} |
| 148 |
} |
223 |
} |
|
|
224 |
if (m_xml) |
| 225 |
{ |
| 226 |
WriteN (m_fHandle, GetXMLClose ("topology")); |
| 227 |
Simulator::Schedule (mobilitypollinterval, &AnimationInterface::MobilityAutoCheck, this); |
| 228 |
} |
| 149 |
|
229 |
|
| 150 |
// Connect the callback for packet tx events |
230 |
// Connect the callbacks |
| 151 |
Config::Connect ("/ChannelList/*/TxRxPointToPoint", |
231 |
Config::Connect ("/ChannelList/*/TxRxPointToPoint", |
| 152 |
MakeCallback (&AnimationInterface::DevTxTrace, this)); |
232 |
MakeCallback (&AnimationInterface::DevTxTrace, this)); |
|
|
233 |
Config::Connect ("NodeList/*/DeviceList/*/$ns3::WifiNetDevice/Phy/PhyTxBegin", |
| 234 |
MakeCallback (&AnimationInterface::WifiPhyTxBeginTrace, this)); |
| 235 |
Config::Connect ("NodeList/*/DeviceList/*/$ns3::WifiNetDevice/Phy/PhyTxEnd", |
| 236 |
MakeCallback (&AnimationInterface::WifiPhyTxEndTrace, this)); |
| 237 |
Config::Connect ("NodeList/*/DeviceList/*/$ns3::WifiNetDevice/Phy/PhyRxBegin", |
| 238 |
MakeCallback (&AnimationInterface::WifiPhyRxBeginTrace, this)); |
| 239 |
Config::Connect ("NodeList/*/DeviceList/*/$ns3::WifiNetDevice/Phy/PhyRxEnd", |
| 240 |
MakeCallback (&AnimationInterface::WifiPhyRxEndTrace, this)); |
| 241 |
Config::Connect ("NodeList/*/DeviceList/*/$ns3::WifiNetDevice/Phy/PhyRxDrop", |
| 242 |
MakeCallback (&AnimationInterface::WifiPhyRxDropTrace, this)); |
| 243 |
Config::ConnectWithoutContext ("/NodeList/*/$ns3::MobilityModel/CourseChange", |
| 244 |
MakeCallback (&AnimationInterface::MobilityCourseChangeTrace, this)); |
| 245 |
Config::Connect ("/NodeList/*/DeviceList/*/$ns3::WimaxNetDevice/Tx", |
| 246 |
MakeCallback (&AnimationInterface::WimaxTxTrace, this)); |
| 247 |
Config::Connect ("/NodeList/*/DeviceList/*/$ns3::WimaxNetDevice/Rx", |
| 248 |
MakeCallback (&AnimationInterface::WimaxRxTrace, this)); |
| 249 |
|
| 250 |
|
| 251 |
|
| 153 |
} |
252 |
} |
| 154 |
|
253 |
|
| 155 |
void AnimationInterface::StopAnimation () |
254 |
void AnimationInterface::StopAnimation () |
| 156 |
{ |
255 |
{ |
|
|
256 |
NS_LOG_INFO ("Stopping Animation"); |
| 157 |
if (m_fHandle > 0) |
257 |
if (m_fHandle > 0) |
| 158 |
{ |
258 |
{ |
|
|
259 |
if (m_xml) |
| 260 |
{ // Terminate the anim element |
| 261 |
WriteN (m_fHandle, GetXMLClose ("anim")); |
| 262 |
} |
| 159 |
close (m_fHandle); |
263 |
close (m_fHandle); |
|
|
264 |
m_fHandle = 0; |
| 160 |
} |
265 |
} |
| 161 |
} |
266 |
} |
| 162 |
|
267 |
|
|
|
268 |
int AnimationInterface::WriteN (int h, const string& st) |
| 269 |
{ |
| 270 |
return WriteN (h, st.c_str (), st.length ()); |
| 271 |
} |
| 272 |
|
| 163 |
|
273 |
|
| 164 |
// Private methods |
274 |
// Private methods |
|
|
275 |
void AnimationInterface::RecalcTopoBounds (Vector v) |
| 276 |
{ |
| 277 |
topo_minX = min (topo_minX, v.x); |
| 278 |
topo_minY = min (topo_minY, v.y); |
| 279 |
topo_maxX = max (topo_maxX, v.x); |
| 280 |
topo_maxY = max (topo_maxY, v.y); |
| 281 |
|
| 282 |
} |
| 283 |
|
| 165 |
int AnimationInterface::WriteN (int h, const char* data, uint32_t count) |
284 |
int AnimationInterface::WriteN (int h, const char* data, uint32_t count) |
| 166 |
{ // Write count bytes to h from data |
285 |
{ // Write count bytes to h from data |
| 167 |
uint32_t nLeft = count; |
286 |
uint32_t nLeft = count; |
| 168 |
const char* p = data; |
287 |
const char* p = data; |
| 169 |
uint32_t written = 0; |
288 |
uint32_t written = 0; |
| 170 |
|
|
|
| 171 |
while (nLeft) |
289 |
while (nLeft) |
| 172 |
{ |
290 |
{ |
| 173 |
int n = write (h, p, nLeft); |
291 |
int n = write (h, p, nLeft); |
|
|
| 186 |
Ptr<NetDevice> tx, Ptr<NetDevice> rx, |
304 |
Ptr<NetDevice> tx, Ptr<NetDevice> rx, |
| 187 |
Time txTime, Time rxTime) |
305 |
Time txTime, Time rxTime) |
| 188 |
{ |
306 |
{ |
|
|
307 |
NS_ASSERT (tx); |
| 308 |
NS_ASSERT (rx); |
| 189 |
Time now = Simulator::Now (); |
309 |
Time now = Simulator::Now (); |
| 190 |
ostringstream oss; |
310 |
ostringstream oss; |
| 191 |
oss << now.GetSeconds () << " P " |
311 |
double fbTx = now.GetSeconds (); |
| 192 |
<< tx->GetNode ()->GetId () << " " |
312 |
double lbTx = (now + txTime).GetSeconds (); |
| 193 |
<< rx->GetNode ()->GetId () << " " |
313 |
double fbRx = (now + rxTime - txTime).GetSeconds (); |
| 194 |
<< (now + txTime).GetSeconds () << " " // last bit tx time |
314 |
double lbRx = (now + rxTime).GetSeconds (); |
| 195 |
<< (now + rxTime - txTime).GetSeconds () << " " // first bit rx time |
315 |
if (m_xml) |
| 196 |
<< (now + rxTime).GetSeconds () << endl; // last bit rx time |
316 |
{ |
| 197 |
WriteN (m_fHandle, oss.str ().c_str (), oss.str ().length ()); |
317 |
oss << GetXMLOpen_packet (0,tx->GetNode ()->GetId (),fbTx,lbTx); |
|
|
318 |
oss << GetXMLOpenClose_rx (0,rx->GetNode ()->GetId (),fbRx,lbRx); |
| 319 |
oss << GetXMLClose ("packet"); |
| 320 |
} |
| 321 |
else |
| 322 |
{ |
| 323 |
oss << setprecision (10); |
| 324 |
oss << now.GetSeconds () << " P " |
| 325 |
<< tx->GetNode ()->GetId () << " " |
| 326 |
<< rx->GetNode ()->GetId () << " " |
| 327 |
<< (now + txTime).GetSeconds () << " " // last bit tx time |
| 328 |
<< (now + rxTime - txTime).GetSeconds () << " " // first bit rx time |
| 329 |
<< (now + rxTime).GetSeconds () << endl; // last bit rx time |
| 330 |
} |
| 331 |
WriteN (m_fHandle, oss.str ()); |
| 198 |
} |
332 |
} |
| 199 |
|
333 |
|
|
|
334 |
|
| 335 |
Ptr <NetDevice> |
| 336 |
AnimationInterface::GetNetDeviceFromContext (std::string context) |
| 337 |
{ |
| 338 |
// Use "NodeList/*/DeviceList/*/ as reference |
| 339 |
// where element [1] is the Node Id |
| 340 |
// element [2] is the NetDevice Id |
| 341 |
|
| 342 |
vector <std::string> elements = GetElementsFromContext (context); |
| 343 |
Ptr <Node> n = NodeList::GetNode (atoi (elements[1].c_str ())); |
| 344 |
NS_ASSERT (n); |
| 345 |
return n->GetDevice (atoi (elements[3].c_str ())); |
| 346 |
} |
| 347 |
|
| 348 |
void AnimationInterface::WifiPhyTxBeginTrace (std::string context, |
| 349 |
Ptr<const Packet> p) |
| 350 |
{ |
| 351 |
Ptr <NetDevice> ndev = GetNetDeviceFromContext (context); |
| 352 |
NS_ASSERT (ndev); |
| 353 |
Ptr <Node> n = ndev->GetNode (); |
| 354 |
NS_ASSERT (n); |
| 355 |
// Add a new pending wireless |
| 356 |
NS_LOG_INFO ("TxBeginTrace for packet:" << p->GetUid ()); |
| 357 |
pendingWifiPackets[p->GetUid ()] = |
| 358 |
AnimPacketInfo (ndev, Simulator::Now (), Simulator::Now ()+Seconds(0.5), GetPosition (n)); |
| 359 |
} |
| 360 |
|
| 361 |
void AnimationInterface::WifiPhyTxEndTrace (std::string context, |
| 362 |
Ptr<const Packet> p) |
| 363 |
{ |
| 364 |
} |
| 365 |
|
| 366 |
void AnimationInterface::WifiPhyTxDropTrace (std::string context, |
| 367 |
Ptr<const Packet> p) |
| 368 |
{ |
| 369 |
Ptr <NetDevice> ndev = GetNetDeviceFromContext (context); |
| 370 |
NS_ASSERT (ndev); |
| 371 |
// Erase pending wifi |
| 372 |
NS_LOG_INFO ("TxDropTrace for packet:" << p->GetUid ()); |
| 373 |
pendingWifiPackets.erase (pendingWifiPackets.find (p->GetUid ())); |
| 374 |
} |
| 375 |
|
| 376 |
|
| 377 |
void AnimationInterface::WifiPhyRxBeginTrace (std::string context, |
| 378 |
Ptr<const Packet> p) |
| 379 |
{ |
| 380 |
Ptr <NetDevice> ndev = GetNetDeviceFromContext (context); |
| 381 |
NS_ASSERT (ndev); |
| 382 |
Ptr <Node> n = ndev->GetNode (); |
| 383 |
NS_ASSERT (n); |
| 384 |
NS_LOG_INFO ("RxBeginTrace for packet:" << p->GetUid ()); |
| 385 |
pendingWifiPackets[p->GetUid ()].AddRxBegin (ndev, Simulator::Now (), GetPosition (n)); |
| 386 |
} |
| 387 |
|
| 388 |
|
| 389 |
void AnimationInterface::WifiPhyRxEndTrace (std::string context, |
| 390 |
Ptr<const Packet> p) |
| 391 |
{ |
| 392 |
Ptr <NetDevice> ndev = GetNetDeviceFromContext (context); |
| 393 |
NS_ASSERT (ndev); |
| 394 |
uint32_t uid = p->GetUid (); |
| 395 |
AnimPacketInfo& pkt = pendingWifiPackets[uid]; |
| 396 |
if (pkt.AddRxEnd (ndev, Simulator::Now ())) |
| 397 |
{ |
| 398 |
NS_LOG_INFO ("RxEndTrace for packet:" << uid << " complete"); |
| 399 |
OutputWirelessPacket (uid, pkt); |
| 400 |
pendingWifiPackets.erase (pendingWifiPackets.find (uid));; |
| 401 |
} |
| 402 |
} |
| 403 |
|
| 404 |
|
| 405 |
void AnimationInterface::WifiPhyRxDropTrace (std::string context, |
| 406 |
Ptr<const Packet> p) |
| 407 |
{ |
| 408 |
Ptr <NetDevice> ndev = GetNetDeviceFromContext (context); |
| 409 |
NS_ASSERT (ndev); |
| 410 |
NS_LOG_INFO ("RxDropTrace for packet:"<< p->GetUid ()); |
| 411 |
pendingWifiPackets[p->GetUid ()].AddRxDrop (ndev); |
| 412 |
} |
| 413 |
|
| 414 |
void AnimationInterface::WimaxTxTrace (std::string context, Ptr<const Packet> p, const Mac48Address & m) |
| 415 |
{ |
| 416 |
Ptr <NetDevice> ndev = GetNetDeviceFromContext (context); |
| 417 |
NS_ASSERT (ndev); |
| 418 |
Ptr <Node> n = ndev->GetNode (); |
| 419 |
NS_ASSERT (n); |
| 420 |
NS_LOG_INFO ("WimaxTxTrace for packet:" << p->GetUid ()); |
| 421 |
pendingWimaxPackets[p->GetUid ()] = |
| 422 |
AnimPacketInfo (ndev, Simulator::Now (), Simulator::Now ()+Seconds(0.5), GetPosition (n)); |
| 423 |
} |
| 424 |
|
| 425 |
|
| 426 |
void AnimationInterface::WimaxRxTrace (std::string context, Ptr<const Packet> p, const Mac48Address & m) |
| 427 |
{ |
| 428 |
Ptr <NetDevice> ndev = GetNetDeviceFromContext (context); |
| 429 |
NS_ASSERT (ndev); |
| 430 |
Ptr <Node> n = ndev->GetNode (); |
| 431 |
NS_ASSERT (n); |
| 432 |
uint32_t uid = p->GetUid (); |
| 433 |
AnimPacketInfo& pktinfo = pendingWimaxPackets[uid]; |
| 434 |
pktinfo.AddRxBegin (ndev, Simulator::Now (), GetPosition (n)); |
| 435 |
pktinfo.AddRxEnd (ndev, Simulator::Now ()+ Seconds (0.0001)); |
| 436 |
NS_LOG_INFO ("WimaxRxTrace for packet:" << p->GetUid ()); |
| 437 |
OutputWirelessPacket (p->GetUid (), pktinfo); |
| 438 |
|
| 439 |
} |
| 440 |
|
| 441 |
void AnimationInterface::MobilityCourseChangeTrace (Ptr <const MobilityModel> mobility) |
| 442 |
|
| 443 |
{ |
| 444 |
Ptr <Node> n = mobility->GetObject <Node> (); |
| 445 |
if (!n) return; |
| 446 |
if (!mobility) return; |
| 447 |
Vector v ; |
| 448 |
v = mobility->GetPosition (); |
| 449 |
RecalcTopoBounds (v); |
| 450 |
ostringstream oss; |
| 451 |
oss << GetXMLOpen_topology (topo_minX,topo_minY,topo_maxX,topo_maxY); |
| 452 |
oss << GetXMLOpenClose_node (0,n->GetId (),v.x,v.y); |
| 453 |
oss << GetXMLClose ("topology"); |
| 454 |
WriteN (m_fHandle, oss.str ()); |
| 455 |
} |
| 456 |
|
| 457 |
void AnimationInterface::MobilityAutoCheck () |
| 458 |
{ |
| 459 |
Simulator::Schedule (mobilitypollinterval, &AnimationInterface::MobilityAutoCheck, this); |
| 460 |
ostringstream oss; |
| 461 |
oss << GetXMLOpen_topology (topo_minX,topo_minY,topo_maxX,topo_maxY); |
| 462 |
for (NodeList::Iterator i = NodeList::Begin (); i != NodeList::End (); ++i) |
| 463 |
{ |
| 464 |
Ptr <Node> n = *i; |
| 465 |
if (!n) continue; |
| 466 |
Ptr <MobilityModel> mobility = n->GetObject <MobilityModel> (); |
| 467 |
if (!mobility) continue; |
| 468 |
Vector v ; |
| 469 |
v = mobility->GetPosition (); |
| 470 |
RecalcTopoBounds (v); |
| 471 |
oss << GetXMLOpenClose_node (0,n->GetId (),v.x,v.y); |
| 472 |
WriteN (m_fHandle, oss.str ()); |
| 473 |
} |
| 474 |
oss << GetXMLClose ("topology"); |
| 475 |
WriteN (m_fHandle, oss.str ()); |
| 476 |
} |
| 477 |
|
| 478 |
|
| 479 |
// Helper to output a wireless packet. |
| 480 |
// For now, only the XML interface is supported |
| 481 |
|
| 482 |
void AnimationInterface::OutputWirelessPacket (uint32_t uid, AnimPacketInfo& pktInfo) |
| 483 |
|
| 484 |
{ |
| 485 |
if (!m_xml) return; |
| 486 |
ostringstream oss; |
| 487 |
uint32_t nodeId = pktInfo.m_txnd->GetNode ()->GetId (); |
| 488 |
double lbTx = pktInfo.m_lbTx; |
| 489 |
|
| 490 |
// Oops, need to figure out about range |
| 491 |
oss << GetXMLOpen_wpacket (0,nodeId,pktInfo.m_fbTx,lbTx,pktInfo.reception_range); |
| 492 |
// Now add each rx |
| 493 |
map <uint32_t, AnimRxInfo>::iterator p; |
| 494 |
for (p = pktInfo.m_rx.begin (); p != pktInfo.m_rx.end (); p++) |
| 495 |
{ |
| 496 |
AnimRxInfo& rx = p->second; |
| 497 |
uint32_t rxId = rx.m_rxnd->GetNode ()->GetId (); |
| 498 |
if(rx.m_lbRx) |
| 499 |
oss << GetXMLOpenClose_rx (0,rxId,rx.m_fbRx,rx.m_lbRx); |
| 500 |
} |
| 501 |
oss << GetXMLClose ("wpacket"); |
| 502 |
WriteN (m_fHandle, oss.str ()); |
| 503 |
} |
| 504 |
|
| 505 |
|
| 506 |
// XML Private Helpers |
| 507 |
|
| 508 |
std::string AnimationInterface::GetXMLOpen_anim (uint32_t lp) |
| 509 |
{ |
| 510 |
ostringstream oss; |
| 511 |
oss <<"<anim lp = \"" << lp << "\" >\n"; |
| 512 |
return oss.str (); |
| 513 |
} |
| 514 |
std::string AnimationInterface::GetXMLOpen_topology (double minX,double minY,double maxX,double maxY) |
| 515 |
{ |
| 516 |
ostringstream oss; |
| 517 |
oss <<"<topology minX = \"" << minX << "\" minY = \"" << minY |
| 518 |
<< "\" maxX = \"" << maxX << "\" maxY = \"" << maxY |
| 519 |
<< "\">" << endl; |
| 520 |
return oss.str (); |
| 521 |
|
| 522 |
} |
| 523 |
|
| 524 |
std::string AnimationInterface::GetXMLOpenClose_node (uint32_t lp,uint32_t id,double locX,double locY) |
| 525 |
{ |
| 526 |
ostringstream oss; |
| 527 |
oss <<"<node lp = \"" << lp << "\" id = \"" << id << "\"" << " locX = \"" |
| 528 |
<< locX << "\" " << "locY = \"" << locY << "\" />\n"; |
| 529 |
return oss.str (); |
| 530 |
} |
| 531 |
std::string AnimationInterface::GetXMLOpenClose_link (uint32_t fromLp,uint32_t fromId, uint32_t toLp, uint32_t toId) |
| 532 |
{ |
| 533 |
ostringstream oss; |
| 534 |
oss << "<link fromLP = \"0\" fromId = \"" << fromId |
| 535 |
<< "\" toLp = \"0\" toId = \"" << toId |
| 536 |
<< "\"/>" << endl; |
| 537 |
return oss.str (); |
| 538 |
} |
| 539 |
|
| 540 |
|
| 541 |
std::string AnimationInterface::GetXMLOpen_packet (uint32_t fromLp,uint32_t fromId, double fbTx, double lbTx) |
| 542 |
{ |
| 543 |
ostringstream oss; |
| 544 |
oss << setprecision (10); |
| 545 |
oss << "<packet fromLp = \"" << fromLp << "\" fromId = \"" << fromId |
| 546 |
<< "\" fbTx = \"" << fbTx |
| 547 |
<< "\" lbTx = \"" << lbTx |
| 548 |
<< "\">" << endl; |
| 549 |
return oss.str (); |
| 550 |
} |
| 551 |
|
| 552 |
std::string AnimationInterface::GetXMLOpen_wpacket (uint32_t fromLp,uint32_t fromId, double fbTx, double lbTx, double range) |
| 553 |
{ |
| 554 |
ostringstream oss; |
| 555 |
oss << setprecision (10); |
| 556 |
oss << "<wpacket fromLp = \"" << fromLp << "\" fromId = \"" << fromId |
| 557 |
<< "\" fbTx = \"" << fbTx |
| 558 |
<< "\" lbTx = \"" << lbTx |
| 559 |
<< "\" range = \"" << range << "\">" << endl; |
| 560 |
return oss.str (); |
| 561 |
|
| 562 |
} |
| 563 |
|
| 564 |
std::string AnimationInterface::GetXMLOpenClose_rx (uint32_t toLp, uint32_t toId, double fbRx, double lbRx) |
| 565 |
{ |
| 566 |
ostringstream oss; |
| 567 |
oss << setprecision (10); |
| 568 |
oss << "<rx toLp = \"" << toLp <<"\" toId = \"" << toId |
| 569 |
<< "\" fbRx = \"" << fbRx |
| 570 |
<< "\" lbRx = \"" << lbRx |
| 571 |
<< "\"/>" << endl; |
| 572 |
return oss.str (); |
| 573 |
} |
| 574 |
|
| 575 |
std::vector<std::string> AnimationInterface::GetElementsFromContext (std::string context) |
| 576 |
{ |
| 577 |
vector <std::string> elements; |
| 578 |
size_t pos1=0, pos2; |
| 579 |
while (pos1 != context.npos) |
| 580 |
{ |
| 581 |
pos1 = context.find ("/",pos1); |
| 582 |
pos2 = context.find ("/",pos1+1); |
| 583 |
elements.push_back (context.substr (pos1+1,pos2-(pos1+1))); |
| 584 |
pos1 = pos2; |
| 585 |
pos2 = context.npos; |
| 586 |
} |
| 587 |
return elements; |
| 588 |
} |
| 589 |
|
| 590 |
|
| 200 |
} // namespace ns3 |
591 |
} // namespace ns3 |