View | Details | Raw Unified | Return to bug 1172
Collapse All | Expand All

(-)a/src/netanim/examples/wscript (+1 lines)
 Lines 12-14    Link Here 
12
    obj = bld.create_ns3_program('star-animation',
12
    obj = bld.create_ns3_program('star-animation',
13
                                 ['netanim', 'applications', 'point-to-point-layout'])
13
                                 ['netanim', 'applications', 'point-to-point-layout'])
14
    obj.source = 'star-animation.cc'
14
    obj.source = 'star-animation.cc'
15
(-)2a75f23944d6 (+80 lines)
Added Link Here 
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
 * Author: John Abraham<john.abraham@gatech.edu>
17
 */
18
19
// Animation Interface helpers
20
21
#include "ns3/animation-interface-helper.h"
22
#include <iostream>
23
namespace ns3 {
24
AnimPacketInfo::AnimPacketInfo()
25
  : m_txnd (0), m_nRx (0), m_nRxEnd (0), m_fbTx (0), m_lbTx (0), 
26
    transmitter_loc (Vector (0,0,0))
27
{
28
}
29
30
AnimPacketInfo::AnimPacketInfo(Ptr<const NetDevice> nd, const Time& fbTx, 
31
  const Time& lbTx, Vector txLoc)
32
  : m_txnd (nd), m_nRx (0), m_nRxEnd (0),
33
    m_fbTx (fbTx.GetSeconds ()), m_lbTx (lbTx.GetSeconds ()), transmitter_loc (txLoc), 
34
    reception_range (0) 
35
{
36
}
37
38
void AnimPacketInfo::AddRxBegin (Ptr<const NetDevice> nd, const Time& fbRx,
39
                                 Vector rxLoc)
40
{
41
  m_rx[nd->GetNode ()->GetId ()] = AnimRxInfo (fbRx,nd);
42
  m_nRx++;
43
  reception_range = std::max (reception_range,CalculateDistance (transmitter_loc,rxLoc));
44
}
45
46
bool AnimPacketInfo::AddRxEnd (Ptr<const NetDevice> nd, const Time& lbRx)
47
48
{
49
  uint32_t NodeId = nd->GetNode ()->GetId (); 
50
  // Find the RxInfo
51
  if (m_rx.find(NodeId) != m_rx.end ())
52
    { 
53
      if (m_rx[NodeId].m_rxnd != nd) 
54
        {
55
          return false; 
56
        }
57
      m_rx[NodeId].m_lbRx = lbRx.GetSeconds ();
58
      m_nRxEnd++;
59
      if (m_nRxEnd == m_nRx) 
60
        {
61
          return true;
62
        }
63
      return false; // Still more rxEnd expected
64
    }
65
  return false; // Still more rxEnd expected
66
67
}
68
69
void AnimPacketInfo::AddRxDrop (Ptr<const NetDevice> nd)
70
{
71
  if (m_rx.find (nd->GetNode ()->GetId ()) == m_rx.end()) 
72
    {
73
      return;
74
    }
75
  m_rx.erase (m_rx.find (nd->GetNode ()->GetId ()));
76
  m_nRx--;
77
}
78
79
80
} // namespace ns3
(-)2a75f23944d6 (+66 lines)
Added Link Here 
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
 * Author: John Abraham <john.abraham@gatech.edu>
17
 */
18
19
// Animation Interface helpers
20
21
#ifndef _ANIMATION_INTERFACE_HELPER_H_
22
#define _ANIMATION_INTERFACE_HELPER_H_
23
24
#include "ns3/node.h"
25
#include "ns3/mobility-model.h"
26
#include "ns3/packet.h"
27
#include "ns3/simulator.h"
28
#include <vector>
29
#include <map>
30
31
namespace ns3 {
32
// We use an AnimPacketInfo structure for wireless traces to keep up
33
// when each receiver has received packets.  Also an AnimPacketRxInfo
34
// to keep up with each receiver and the time it received the packet
35
struct AnimRxInfo
36
{
37
  AnimRxInfo () {};
38
  AnimRxInfo (const Time& fbRx, Ptr <const NetDevice> nd)
39
    : m_fbRx (fbRx.GetSeconds ()), m_lbRx (0), m_rxnd (nd) {}
40
  double m_fbRx;             // First bit rx time
41
  double m_lbRx;             // Last bit rx time
42
  Ptr <const NetDevice> m_rxnd; // The receiving net device
43
};
44
45
struct AnimPacketInfo
46
47
{
48
  AnimPacketInfo ();
49
  AnimPacketInfo(Ptr<const NetDevice> nd,
50
                 const Time& fbTx, const Time& lbTxi,Vector txLoc);
51
  void AddRxBegin (Ptr <const NetDevice> nd, const Time& fbRx,
52
                   Vector rxLoc);
53
  bool AddRxEnd (Ptr <const NetDevice> nd, const Time& fbRx);
54
  void AddRxDrop (Ptr <const NetDevice> nd);
55
  Ptr<const NetDevice> m_txnd; //Transmitter NetDevice
56
  uint32_t m_nRx;       // Number of receivers
57
  uint32_t m_nRxEnd;    // Number of rxEnd callbacks
58
  double   m_fbTx;      // Time of first bit tx
59
  double   m_lbTx;      // Time of last bit tx
60
  Vector   transmitter_loc;
61
  double reception_range;
62
  std::map<uint32_t,AnimRxInfo> m_rx; //collection of receivers
63
};
64
} // namespace ns3
65
66
#endif
(-)a/src/netanim/model/animation-interface.cc (-19 / +396 lines)
 Lines 20-25    Link Here 
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)
 Lines 37-42    Link Here 
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
 Lines 45-56    Link Here 
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)
 Lines 58-63    Link Here 
58
  FILE* f = fopen (fn.c_str (), "w");
68
  FILE* f = fopen (fn.c_str (), "w");
59
  if (!f)
69
  if (!f)
60
    {
70
    {
71
      NS_FATAL_ERROR ("Unable to output Animation output file");
61
      return false; // Can't open
72
      return false; // Can't open
62
    }
73
    }
63
  m_fHandle = fileno (f); // Set the file handle
74
  m_fHandle = fileno (f); // Set the file handle
 Lines 91-112    Link Here 
91
                // which is done to support a platform like MinGW
102
                // which is done to support a platform like MinGW
92
}
103
}
93
104
105
Vector AnimationInterface::GetPosition (Ptr <Node> n)
106
{
107
  Ptr<MobilityModel> loc = n->GetObject<MobilityModel> ();
108
  Vector v(0,0,0);
109
  if (!loc)
110
    {
111
      return v;
112
    }
113
  if (loc)
114
    {
115
      v = loc->GetPosition ();
116
    }
117
  return v;
118
}
94
void AnimationInterface::StartAnimation ()
119
void AnimationInterface::StartAnimation ()
95
{
120
{
121
  // Find the min/max x/y for the xml topology element
122
  topo_minX = 0;
123
  topo_minY = 0;
124
  topo_maxX = 1000;
125
  topo_maxY = 1000;
126
  bool   first = true;
127
  for (NodeList::Iterator i = NodeList::Begin (); i != NodeList::End (); ++i)
128
    {
129
      Ptr<Node> n = *i;
130
      Vector v = GetPosition (n); 
131
      if (first)
132
        {
133
          topo_minX = v.x;
134
          topo_minY = v.y;
135
          topo_maxX = v.x;
136
          topo_maxY = v.y;
137
          first = false;
138
        }
139
      else
140
        {
141
          topo_minX = min (topo_minX, v.x);
142
          topo_minY = min (topo_minY, v.y);
143
          topo_maxX = max (topo_maxX, v.x);
144
          topo_maxY = max (topo_maxY, v.y);
145
        }
146
    }
147
148
  if (m_xml)
149
    { // output the xml headers
150
      ostringstream oss;
151
      oss << GetXMLOpen_anim (0);
152
      oss << GetXMLOpen_topology (topo_minX,topo_minY,topo_maxX,topo_maxY);
153
      WriteN (m_fHandle, oss.str ());
154
    }
96
  // Dump the topology
155
  // Dump the topology
97
  for (NodeList::Iterator i = NodeList::Begin (); i != NodeList::End (); ++i)
156
  for (NodeList::Iterator i = NodeList::Begin (); i != NodeList::End (); ++i)
98
    {
157
    {
99
      Ptr<Node> n = *i;
158
      Ptr<Node> n = *i;
100
      Ptr<MobilityModel> loc = n->GetObject<MobilityModel> ();
159
      ostringstream oss;
101
      if (loc)
160
      if (m_xml)
161
        {
162
          Vector v = GetPosition (n);
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 = GetPosition (n);
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)
 Lines 134-173    Link Here 
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 callbacks
151
  Config::Connect ("/ChannelList/*/TxRxPointToPoint",
227
  Config::Connect ("/ChannelList/*/TxRxPointToPoint",
152
                   MakeCallback (&AnimationInterface::DevTxTrace, this));
228
                   MakeCallback (&AnimationInterface::DevTxTrace, this));
229
  Config::Connect ("NodeList/*/DeviceList/*/$ns3::WifiNetDevice/Phy/PhyTxBegin",
230
                   MakeCallback (&AnimationInterface::WifiPhyTxBeginTrace, this));
231
  Config::Connect ("NodeList/*/DeviceList/*/$ns3::WifiNetDevice/Phy/PhyTxEnd",
232
                   MakeCallback (&AnimationInterface::WifiPhyTxEndTrace, this));
233
  Config::Connect ("NodeList/*/DeviceList/*/$ns3::WifiNetDevice/Phy/PhyRxBegin",
234
                   MakeCallback (&AnimationInterface::WifiPhyRxBeginTrace, this));
235
  Config::Connect ("NodeList/*/DeviceList/*/$ns3::WifiNetDevice/Phy/PhyRxEnd",
236
                   MakeCallback (&AnimationInterface::WifiPhyRxEndTrace, this));
237
  Config::Connect ("NodeList/*/DeviceList/*/$ns3::WifiNetDevice/Phy/PhyRxDrop",
238
                   MakeCallback (&AnimationInterface::WifiPhyRxDropTrace, this));
239
  Config::ConnectWithoutContext ("/NodeList/*/$ns3::MobilityModel/CourseChange",
240
                   MakeCallback (&AnimationInterface::MobilityCourseChangeTrace, this));
241
  Config::Connect ("/NodeList/*/DeviceList/*/$ns3::WimaxNetDevice/Tx",
242
                   MakeCallback (&AnimationInterface::WimaxTxTrace, this));
243
  Config::Connect ("/NodeList/*/DeviceList/*/$ns3::WimaxNetDevice/Rx",
244
                   MakeCallback (&AnimationInterface::WimaxRxTrace, this));
245
246
247
153
}
248
}
154
249
155
void AnimationInterface::StopAnimation ()
250
void AnimationInterface::StopAnimation ()
156
{
251
{
157
  if (m_fHandle > 0) 
252
  if (m_fHandle > 0) 
158
    {
253
    {
254
      if (m_xml)
255
        { // Terminate the anim element
256
          WriteN (m_fHandle, GetXMLClose ("anim"));
257
        }
159
      close (m_fHandle);
258
      close (m_fHandle);
259
      m_fHandle = 0;
160
    }
260
    }
161
}
261
}
162
262
263
int AnimationInterface::WriteN (int h, const string& st)
264
{
265
  return WriteN (h, st.c_str (), st.length ());
266
}
267
163
268
164
// Private methods
269
// Private methods
270
void AnimationInterface::RecalcTopoBounds (Vector v)
271
{
272
  topo_minX = min (topo_minX, v.x);
273
  topo_minY = min (topo_minY, v.y);
274
  topo_maxX = max (topo_maxX, v.x);
275
  topo_maxY = max (topo_maxY, v.y);
276
277
}
278
165
int AnimationInterface::WriteN (int h, const char* data, uint32_t count)
279
int AnimationInterface::WriteN (int h, const char* data, uint32_t count)
166
{ // Write count bytes to h from data
280
{ // Write count bytes to h from data
167
  uint32_t    nLeft   = count;
281
  uint32_t    nLeft   = count;
168
  const char* p       = data;
282
  const char* p       = data;
169
  uint32_t    written = 0;
283
  uint32_t    written = 0;
170
171
  while (nLeft)
284
  while (nLeft)
172
    {
285
    {
173
      int n = write (h, p, nLeft);
286
      int n = write (h, p, nLeft);
 Lines 188-200    Link Here 
188
{
301
{
189
  Time now = Simulator::Now ();
302
  Time now = Simulator::Now ();
190
  ostringstream oss;
303
  ostringstream oss;
191
  oss << now.GetSeconds () << " P "
304
  double fbTx = now.GetSeconds ();
192
      << tx->GetNode ()->GetId () << " "
305
  double lbTx = (now + txTime).GetSeconds ();
193
      << rx->GetNode ()->GetId () << " "
306
  double fbRx = (now + rxTime - txTime).GetSeconds ();
194
      << (now + txTime).GetSeconds () << " " // last bit tx time
307
  double lbRx = (now + rxTime).GetSeconds ();
195
      << (now + rxTime - txTime).GetSeconds () << " " // first bit rx time
308
  if (m_xml)
196
      << (now + rxTime).GetSeconds () << endl;         // last bit rx time
309
    {
197
  WriteN (m_fHandle, oss.str ().c_str (), oss.str ().length ());
310
      oss << GetXMLOpen_packet (0,tx->GetNode ()->GetId (),fbTx,lbTx);
311
      oss << GetXMLOpenClose_rx (0,rx->GetNode ()->GetId (),fbRx,lbRx); 
312
      oss << GetXMLClose ("packet");
313
    }
314
  else
315
    {
316
      oss << setprecision (10);
317
      oss << now.GetSeconds () << " P "
318
          << tx->GetNode ()->GetId () << " "
319
          << rx->GetNode ()->GetId () << " "
320
          << (now + txTime).GetSeconds () << " " // last bit tx time
321
          << (now + rxTime - txTime).GetSeconds () << " " // first bit rx time
322
          << (now + rxTime).GetSeconds () << endl;         // last bit rx time
323
    }
324
  WriteN (m_fHandle, oss.str ());
198
}
325
}
199
326
327
328
Ptr <NetDevice>
329
AnimationInterface::GetNetDeviceFromContext (std::string context)
330
{
331
  // Use "NodeList/*/DeviceList/*/ as reference
332
  // where element [1] is the Node Id
333
  // element [2] is the NetDevice Id
334
335
  vector <std::string> elements = GetElementsFromContext (context);
336
  Ptr <Node> n = NodeList::GetNode (atoi (elements[1].c_str ()));
337
  NS_ASSERT (n);
338
  return n->GetDevice (atoi (elements[3].c_str ()));
339
}
340
                                  
341
void AnimationInterface::WifiPhyTxBeginTrace (std::string context,
342
                                          Ptr<const Packet> p)
343
{
344
  Ptr <NetDevice> ndev = GetNetDeviceFromContext (context); 
345
  NS_ASSERT (ndev);
346
  Ptr <Node> n = ndev->GetNode ();
347
  NS_ASSERT (n);
348
  // Add a new pending wireless
349
  pendingWifiPackets[p->GetUid ()] =
350
  AnimPacketInfo (ndev, Simulator::Now (), Simulator::Now ()+Seconds(0.5), GetPosition (n));
351
}
352
353
void AnimationInterface::WifiPhyTxEndTrace (std::string context,
354
                                            Ptr<const Packet> p)
355
{
356
}
357
358
void AnimationInterface::WifiPhyTxDropTrace (std::string context,
359
                                          Ptr<const Packet> p)
360
{
361
  Ptr <NetDevice> ndev = GetNetDeviceFromContext (context);
362
  NS_ASSERT (ndev);
363
  // Erase pending wifi
364
  pendingWifiPackets.erase (pendingWifiPackets.find (p->GetUid ()));
365
}
366
367
368
void AnimationInterface::WifiPhyRxBeginTrace (std::string context,
369
                                          Ptr<const Packet> p)
370
{
371
  Ptr <NetDevice> ndev = GetNetDeviceFromContext (context);
372
  NS_ASSERT (ndev);
373
  Ptr <Node> n = ndev->GetNode ();
374
  NS_ASSERT (n);
375
  pendingWifiPackets[p->GetUid ()].AddRxBegin (ndev, Simulator::Now (), GetPosition (n));
376
}
377
378
379
void AnimationInterface::WifiPhyRxEndTrace (std::string context,
380
                                        Ptr<const Packet> p)
381
{
382
  Ptr <NetDevice> ndev = GetNetDeviceFromContext (context);
383
  NS_ASSERT (ndev);
384
  uint32_t uid = p->GetUid ();
385
  AnimPacketInfo& pkt = pendingWifiPackets[uid];
386
  if (pkt.AddRxEnd (ndev, Simulator::Now ()))
387
    {
388
      //cout << "Packet " << uid << " complete" << endl;
389
      OutputWirelessPacket (uid, pkt);
390
      pendingWifiPackets.erase (pendingWifiPackets.find (uid));;
391
    }
392
}
393
394
395
void AnimationInterface::WifiPhyRxDropTrace (std::string context,
396
                                         Ptr<const Packet> p)
397
{
398
  Ptr <NetDevice> ndev = GetNetDeviceFromContext (context);
399
  NS_ASSERT (ndev);
400
  pendingWifiPackets[p->GetUid ()].AddRxDrop (ndev);
401
}
402
403
void AnimationInterface::WimaxTxTrace (std::string context, Ptr<const Packet> p, const Mac48Address & m)
404
{
405
  Ptr <NetDevice> ndev = GetNetDeviceFromContext (context);
406
  NS_ASSERT (ndev);
407
  Ptr <Node> n = ndev->GetNode ();
408
  NS_ASSERT (n);
409
  pendingWimaxPackets[p->GetUid ()] =
410
  AnimPacketInfo (ndev, Simulator::Now (), Simulator::Now ()+Seconds(0.5), GetPosition (n));
411
}
412
413
414
void AnimationInterface::WimaxRxTrace (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
  uint32_t uid = p->GetUid ();
421
  AnimPacketInfo& pktinfo = pendingWimaxPackets[uid];
422
  pktinfo.AddRxBegin (ndev, Simulator::Now (), GetPosition (n));
423
  pktinfo.AddRxEnd (ndev, Simulator::Now ()+ Seconds (0.0001));
424
  OutputWirelessPacket (p->GetUid (), pktinfo);
425
426
}
427
428
void AnimationInterface::MobilityCourseChangeTrace (Ptr <const MobilityModel> mobility)
429
430
{
431
  Ptr <Node> n = mobility->GetObject <Node> ();
432
  if (!n) return;
433
  if (!mobility) return; 
434
  Vector v ;
435
  v = mobility->GetPosition ();
436
  RecalcTopoBounds (v);
437
  ostringstream oss; 
438
  oss << GetXMLOpen_topology (topo_minX,topo_minY,topo_maxX,topo_maxY);
439
  oss << GetXMLOpenClose_node (0,n->GetId (),v.x,v.y);
440
  oss << GetXMLClose ("topology");
441
  WriteN (m_fHandle, oss.str ());
442
}
443
444
void AnimationInterface::MobilityAutoCheck ()
445
{
446
  Simulator::Schedule (mobilitypollinterval, &AnimationInterface::MobilityAutoCheck, this);
447
  for (NodeList::Iterator i = NodeList::Begin (); i != NodeList::End (); ++i)
448
    {
449
      Ptr <Node> n = *i;
450
      if (!n) continue;
451
      Ptr <MobilityModel> mobility = n->GetObject <MobilityModel> ();
452
      if (!mobility) continue;
453
      Vector v ;
454
      v = mobility->GetPosition ();
455
      RecalcTopoBounds (v);
456
      ostringstream oss;
457
      oss << GetXMLOpen_topology (topo_minX,topo_minY,topo_maxX,topo_maxY);
458
      oss << GetXMLOpenClose_node (0,n->GetId (),v.x,v.y);
459
      oss << GetXMLClose ("topology");
460
      WriteN (m_fHandle, oss.str ());
461
    }
462
}
463
464
465
// Helper to output a wireless packet.
466
// For now, only the XML interface is supported
467
468
void AnimationInterface::OutputWirelessPacket (uint32_t uid, AnimPacketInfo& pktInfo)
469
470
{
471
  if (!m_xml) return;
472
  ostringstream oss;
473
  uint32_t nodeId = pktInfo.m_txnd->GetNode ()->GetId ();
474
  double lbTx = pktInfo.m_lbTx;
475
476
  // Oops, need to figure out about range
477
  oss << GetXMLOpen_wpacket (0,nodeId,pktInfo.m_fbTx,lbTx,pktInfo.reception_range);
478
  // Now add each rx
479
  map <uint32_t, AnimRxInfo>::iterator p;
480
  for (p = pktInfo.m_rx.begin (); p != pktInfo.m_rx.end (); p++)
481
    {
482
      AnimRxInfo& rx = p->second;
483
      uint32_t rxId = rx.m_rxnd->GetNode ()->GetId ();
484
      if(rx.m_lbRx)
485
      oss << GetXMLOpenClose_rx (0,rxId,rx.m_fbRx,rx.m_lbRx);
486
    }
487
  oss << GetXMLClose ("wpacket");
488
  WriteN (m_fHandle, oss.str ());
489
}
490
491
492
// XML Private Helpers
493
494
std::string AnimationInterface::GetXMLOpen_anim (uint32_t lp)
495
{
496
  ostringstream oss;
497
  oss <<"<anim lp = \"" << lp << "\" >\n";
498
  return oss.str ();
499
}
500
std::string AnimationInterface::GetXMLOpen_topology (double minX,double minY,double maxX,double maxY)
501
{
502
  ostringstream oss;
503
  oss <<"<topology minX = \"" << minX << "\" minY = \"" << minY
504
      << "\" maxX = \"" << maxX << "\" maxY = \"" << maxY
505
      << "\">" << endl;
506
  return oss.str ();
507
508
}
509
510
std::string AnimationInterface::GetXMLOpenClose_node (uint32_t lp,uint32_t id,double locX,double locY)
511
{
512
  ostringstream oss;
513
  oss <<"<node lp = \"" << lp << "\" id = \"" << id << "\"" << " locX = \"" 
514
      << locX << "\" " << "locY = \"" << locY << "\" />\n";
515
  return oss.str ();
516
}
517
std::string AnimationInterface::GetXMLOpenClose_link (uint32_t fromLp,uint32_t fromId, uint32_t toLp, uint32_t toId)
518
{
519
  ostringstream oss;
520
  oss << "<link fromLP = \"0\" fromId = \"" << fromId
521
      << "\" toLp = \"0\" toId = \"" << toId
522
      << "\"/>" << endl;
523
  return oss.str ();
524
}
525
526
527
std::string AnimationInterface::GetXMLOpen_packet (uint32_t fromLp,uint32_t fromId, double fbTx, double lbTx)
528
{
529
  ostringstream oss;
530
  oss << setprecision (10);
531
  oss << "<packet fromLp = \"" << fromLp << "\" fromId = \"" << fromId
532
      << "\" fbTx = \"" << fbTx
533
      << "\" lbTx = \"" << lbTx
534
      << "\">" << endl;
535
  return oss.str ();
536
}
537
538
std::string AnimationInterface::GetXMLOpen_wpacket (uint32_t fromLp,uint32_t fromId, double fbTx, double lbTx, double range)
539
{
540
  ostringstream oss;
541
  oss << setprecision (10);
542
  oss << "<wpacket fromLp = \"" << fromLp << "\" fromId = \"" << fromId
543
      << "\" fbTx = \"" << fbTx
544
      << "\" lbTx = \"" << lbTx
545
      << "\" range = \"" << range << "\">" << endl;
546
  return oss.str ();
547
548
}
549
550
std::string AnimationInterface::GetXMLOpenClose_rx (uint32_t toLp, uint32_t toId, double fbRx, double lbRx)
551
{
552
  ostringstream oss;
553
  oss << setprecision (10);
554
  oss << "<rx toLp = \"" << toLp <<"\" toId = \"" << toId
555
      << "\" fbRx = \"" << fbRx
556
      << "\" lbRx = \"" << lbRx
557
      << "\"/>" << endl;
558
  return oss.str ();
559
}
560
561
std::vector<std::string> AnimationInterface::GetElementsFromContext (std::string context)
562
{
563
  vector <std::string> elements;
564
  size_t pos1=0, pos2;
565
  while (pos1 != context.npos)
566
  {
567
    pos1 = context.find ("/",pos1);
568
    pos2 = context.find ("/",pos1+1);
569
    elements.push_back (context.substr (pos1+1,pos2-(pos1+1)));
570
    pos1 = pos2; 
571
    pos2 = context.npos;
572
  }
573
  return elements;
574
}
575
576
200
} // namespace ns3
577
} // namespace ns3
(-)a/src/netanim/model/animation-interface.h (-9 / +81 lines)
 Lines 1-4    Link Here 
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
1
2
/*
2
/*
3
 * This program is free software; you can redistribute it and/or modify
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
4
 * it under the terms of the GNU General Public License version 2 as
 Lines 14-19    Link Here 
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
 Lines 22-39    Link Here 
22
#define ANIMATION_INTERFACE__H
23
#define ANIMATION_INTERFACE__H
23
24
24
#include <string>
25
#include <string>
25
26
#include <stdio.h>
27
#include <map>
26
#include "ns3/ptr.h"
28
#include "ns3/ptr.h"
27
#include "ns3/net-device.h"
29
#include "ns3/net-device.h"
28
#include "ns3/nstime.h"
30
#include "ns3/nstime.h"
29
#include "ns3/log.h"
31
#include "ns3/log.h"
30
#include "ns3/node-list.h"
32
#include "ns3/node-list.h"
31
33
#include "ns3/simulator.h"
34
#include "ns3/config.h"
35
#include "ns3/animation-interface-helper.h"
36
#include "ns3/mac48-address.h"
32
37
33
namespace ns3 {
38
namespace ns3 {
34
39
35
class NetModel;
40
class NetModel;
36
37
/**
41
/**
38
 * \defgroup netanim Netanim
42
 * \defgroup netanim Netanim
39
 *
43
 *
 Lines 72-77    Link Here 
72
  bool SetOutputFile (const std::string& fn);
76
  bool SetOutputFile (const std::string& fn);
73
77
74
/**
78
/**
79
 * @brief Specify that animation commands are to be written
80
 * in XML format.
81
 *
82
 * @returns none
83
 */
84
85
  void SetXMLOutput ();
86
87
/**
75
 * @brief Specify that animation commands are to be written to
88
 * @brief Specify that animation commands are to be written to
76
 * a socket.
89
 * a socket.
77
 *
90
 *
 Lines 103-120    Link Here 
103
 */
116
 */
104
  void StopAnimation ();
117
  void StopAnimation ();
105
118
119
private:
120
  void DevTxTrace (std::string context,
121
                   Ptr<const Packet> p,
122
                   Ptr<NetDevice> tx,
123
                   Ptr<NetDevice> rx,
124
                   Time txTime,
125
                   Time rxTime);
126
  void WifiPhyTxBeginTrace (std::string context,
127
                        Ptr<const Packet> p);
128
  void WifiPhyTxEndTrace (std::string context,
129
                          Ptr<const Packet> p);
130
  void WifiPhyTxDropTrace (std::string context,
131
                           Ptr<const Packet> p);
132
  void WifiPhyRxBeginTrace (std::string context,
133
                            Ptr<const Packet> p);
134
  void WifiPhyRxEndTrace (std::string context,
135
                          Ptr<const Packet> p);
136
  void WifiPhyRxDropTrace (std::string context,
137
                           Ptr<const Packet> p);
106
138
107
private:
139
  void WimaxTxTrace (std::string context,
108
  // Packet tx animation callback
140
                     Ptr<const Packet> p,
109
  void DevTxTrace (std::string context, Ptr<const Packet> p,
141
		     const Mac48Address &);
110
                   Ptr<NetDevice> tx, Ptr<NetDevice> rx,
142
  void WimaxRxTrace (std::string context,
111
                   Time txTime, Time rxTime);
143
                     Ptr<const Packet> p,
144
                     const Mac48Address &);
145
  void MobilityCourseChangeTrace (Ptr <const MobilityModel> mob);
146
112
  // Write specified amount of data to the specified handle
147
  // Write specified amount of data to the specified handle
113
  int  WriteN (int, const char*, uint32_t);
148
  int  WriteN (int, const char*, uint32_t);
149
  // Write a string to the specified handle;
150
  int  WriteN (int, const std::string&);
151
  //Helper to output xml wireless packet
152
  void OutputWirelessPacket (uint32_t, AnimPacketInfo&);
153
  void MobilityAutoCheck ();
154
  void SetMobilityPollInterval (Time t) {mobilitypollinterval = t;}
114
private:
155
private:
115
  int       m_fHandle;  // File handle for output (-1 if none)
156
  int       m_fHandle;  // File handle for output (-1 if none)
157
  bool      m_xml;      // True if xml format desired
116
  NetModel* m_model;    // If non nil, points to the internal network model
158
  NetModel* m_model;    // If non nil, points to the internal network model
117
                        // for the interlan animator
159
                        // for the interlan animator
160
  std::map<uint32_t, AnimPacketInfo> pendingWifiPackets;
161
  std::map<uint32_t, AnimPacketInfo> pendingWimaxPackets;
162
  
163
  Vector GetPosition (Ptr <Node> n);
164
165
  // Recalculate topology bounds
166
  void RecalcTopoBounds (Vector v);
167
168
  // Path helper
169
  std::vector<std::string> GetElementsFromContext (std::string context);
170
  Ptr <NetDevice> GetNetDeviceFromContext (std::string context);
171
172
  // XML helpers
173
174
  // Topology element dimensions
175
  double topo_minX;
176
  double topo_minY;
177
  double topo_maxX;
178
  double topo_maxY;
179
180
  std::string GetXMLOpen_anim (uint32_t lp);
181
  std::string GetXMLOpen_topology (double minX,double minY,double maxX,double maxY);
182
  std::string GetXMLOpenClose_node (uint32_t lp,uint32_t id,double locX,double locY);
183
  std::string GetXMLOpenClose_link (uint32_t fromLp,uint32_t fromId, uint32_t toLp, uint32_t toId);
184
  std::string GetXMLOpen_packet (uint32_t fromLp,uint32_t fromId, double fbTx, double lbTx);
185
  std::string GetXMLOpenClose_rx (uint32_t toLp, uint32_t toId, double fbRx, double lbRx);
186
  std::string GetXMLOpen_wpacket (uint32_t fromLp,uint32_t fromId, double fbTx, double lbTx, double range);
187
  std::string GetXMLClose (std::string name) {return "</" + name + ">\n"; }
188
  Time mobilitypollinterval;
189
118
};
190
};
119
}
191
}
120
#endif
192
#endif
(-)a/src/netanim/wscript (-19 / +20 lines)
 Lines 1-25    Link Here 
1
## -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*-
1
## -*-Mode : python; py-indent-offset : 4; indent-tabs-mode : nil; coding : utf-8; -*-
2
2
3
def build(bld):
3
def build (bld) :
4
    module = bld.create_ns3_module('netanim', ['internet', 'mobility'])
4
	module = bld.create_ns3_module ('netanim', ['internet', 'mobility', 'wimax', 'wifi'])
5
    module.includes = '.'
5
	module.includes = '.'
6
    module.source = [
6
	module.source = [
7
        'model/animation-interface.cc',
7
			  'model/animation-interface.cc',
8
        ]
8
			  'helper/animation-interface-helper.cc',
9
		        ]
9
10
10
    headers = bld.new_task_gen('ns3header')
11
	headers = bld.new_task_gen ('ns3header')
11
    headers.module = 'netanim'
12
	headers.module = 'netanim'
12
    headers.source = [
13
	headers.source = [
13
        'model/animation-interface.h',
14
			  'model/animation-interface.h',
14
        ]
15
			  'helper/animation-interface-helper.h',
16
  			 ]
15
17
16
    if (bld.env['ENABLE_EXAMPLES']):
18
	if (bld.env['ENABLE_EXAMPLES']) :
17
        bld.add_subdirs('examples')
19
		bld.add_subdirs ('examples')
18
20
19
    bld.ns3_python_bindings()
21
	bld.ns3_python_bindings ()
20
22
23
def configure (conf) :
24
	conf.check (header_name='sys/socket.h', define_name='HAVE_SYS_SOCKET_H')
25
	conf.check (header_name='netinet/in.h', define_name='HAVE_NETINET_IN_H')
21
26
22
def configure(conf):
23
    conf.check(header_name='sys/socket.h', define_name='HAVE_SYS_SOCKET_H')
24
    conf.check(header_name='netinet/in.h', define_name='HAVE_NETINET_IN_H')
25

Return to bug 1172