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 (+86 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 "ns3/log.h"
23
24
#include <iostream>
25
26
NS_LOG_COMPONENT_DEFINE ("AnimationInterfaceHelper");
27
28
namespace ns3 {
29
AnimPacketInfo::AnimPacketInfo()
30
  : m_txnd (0), m_nRx (0), m_nRxEnd (0), m_fbTx (0), m_lbTx (0), 
31
    transmitter_loc (Vector (0,0,0))
32
{
33
}
34
35
AnimPacketInfo::AnimPacketInfo(Ptr<const NetDevice> nd, const Time& fbTx, 
36
  const Time& lbTx, Vector txLoc)
37
  : m_txnd (nd), m_nRx (0), m_nRxEnd (0),
38
    m_fbTx (fbTx.GetSeconds ()), m_lbTx (lbTx.GetSeconds ()), transmitter_loc (txLoc), 
39
    reception_range (0) 
40
{
41
}
42
43
void AnimPacketInfo::AddRxBegin (Ptr<const NetDevice> nd, const Time& fbRx,
44
                                 Vector rxLoc)
45
{
46
  m_rx[nd->GetNode ()->GetId ()] = AnimRxInfo (fbRx,nd);
47
  m_nRx++;
48
  reception_range = std::max (reception_range,CalculateDistance (transmitter_loc,rxLoc));
49
}
50
51
bool AnimPacketInfo::AddRxEnd (Ptr<const NetDevice> nd, const Time& lbRx)
52
53
{
54
  uint32_t NodeId = nd->GetNode ()->GetId (); 
55
  // Find the RxInfo
56
  if (m_rx.find(NodeId) != m_rx.end ())
57
    { 
58
      if (m_rx[NodeId].m_rxnd != nd) 
59
        {
60
          return false; 
61
        }
62
      m_rx[NodeId].m_lbRx = lbRx.GetSeconds ();
63
      m_nRxEnd++;
64
      if (m_nRxEnd == m_nRx) 
65
        {
66
          return true;
67
        }
68
      return false; // Still more rxEnd expected
69
    }
70
   NS_ASSERT ("Received RxEnd notification but RxInfo never existed");
71
  return false; // Still more rxEnd expected
72
}
73
74
void AnimPacketInfo::AddRxDrop (Ptr<const NetDevice> nd)
75
{
76
  if (m_rx.find (nd->GetNode ()->GetId ()) == m_rx.end()) 
77
    {
78
      NS_LOG_DEBUG ("Received RxDrop notification"); 
79
      return;
80
    }
81
  m_rx.erase (m_rx.find (nd->GetNode ()->GetId ()));
82
  m_nRx--;
83
}
84
85
86
} // namespace ns3
(-)2a75f23944d6 (+188 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
33
34
/**
35
 * \ingroup netanim
36
 *
37
 * \brief AnimRxInfo helper class 
38
 *
39
 * This helper class keeps of wireless packets received
40
 * including info about the first bit received time,
41
 * last bit received time and NetDevice received on
42
 * It is intended only for use by the AnimationInterface
43
 * class.
44
 * 
45
 */
46
47
class AnimRxInfo
48
{
49
public:
50
51
  /**
52
   * \brief Default constructor
53
   *
54
   */
55
  AnimRxInfo () {};
56
57
  /**
58
   * \brief Constructor
59
   * \param First-bit Receive time
60
   * \param Ptr to NetDevice used for reception
61
   *
62
   */
63
64
65
  AnimRxInfo (const Time& fbRx, Ptr <const NetDevice> nd)
66
    : m_fbRx (fbRx.GetSeconds ()), m_lbRx (0), m_rxnd (nd) {}
67
  /* 
68
   * First bit receive time
69
   */
70
  double m_fbRx;            
71
  
72
  /* 
73
   * Last bit receive time
74
   */
75
  double m_lbRx;             
76
77
  /*
78
   * Ptr to receiving Net Device
79
   */
80
  Ptr <const NetDevice> m_rxnd;
81
};
82
83
/**
84
 * \ingroup netanim
85
 *
86
 * \brief AnimPacketInfo helper class
87
 *
88
 * This helper class keeps of wireless packets transmitted and
89
 * received
90
 * including info about the last bit transmit time, first bit 
91
 * transmit time, location of the transmitter and
92
 * NetDevice transmited on
93
 * It is intended only for use by the AnimationInterface
94
 * class.
95
 *
96
 */
97
98
class AnimPacketInfo
99
100
{
101
public:
102
103
  /**
104
   * \brief Default constructor
105
   */
106
  AnimPacketInfo ();
107
  
108
  /**
109
   * \brief Constructor
110
   * \param Ptr to NetDevice transmitted on
111
   * \param First bit transmit time
112
   * \param Last bit transmit time
113
   * \param Transmitter Location
114
   *
115
   */
116
  AnimPacketInfo(Ptr<const NetDevice> nd,
117
                 const Time& fbTx, const Time& lbTx,Vector txLoc);
118
119
  /**
120
   * Ptr to NetDevice used for transmission
121
   */
122
  Ptr<const NetDevice> m_txnd;
123
124
  /**
125
   * Number of receivers
126
   */
127
  uint32_t m_nRx;      
128
129
  /** 
130
   * Number of RxEnd trace callbacks
131
   */
132
  uint32_t m_nRxEnd;   
133
134
  /** 
135
   * First bit transmission time
136
   */
137
  double   m_fbTx;     
138
139
  /**
140
   * Last bit transmission time
141
   */
142
  double   m_lbTx;     
143
144
  /**
145
   * Transmitter's location
146
   */
147
  Vector   transmitter_loc;
148
149
  /** 
150
   * Receptiion range
151
   */
152
  double reception_range;
153
154
  /**
155
   * Collection of receivers
156
   */
157
  std::map<uint32_t,AnimRxInfo> m_rx;
158
159
  /**
160
   * \brief Record RxBegin notification was received
161
   * \param Ptr to NetDevice where packet was received
162
   * \param First bit receive time
163
   * \param Location of the transmitter
164
   *
165
   */
166
  void AddRxBegin (Ptr <const NetDevice> nd, const Time& fbRx,
167
                   Vector rxLoc);
168
169
  /**
170
   * \brief Record RxEnd notification was received
171
   * \param Ptr to NetDevice where packet was received
172
   * \param First bit receive time
173
   *
174
   */
175
  bool AddRxEnd (Ptr <const NetDevice> nd, const Time& fbRx);
176
177
  /**
178
   * \brief Record RxDrop notification was received
179
   * \param Ptr to NetDevice where packet was dropped on reception
180
   *
181
   */
182
  void AddRxDrop (Ptr <const NetDevice> nd);
183
184
};
185
186
} // namespace ns3
187
188
#endif
(-)a/src/netanim/model/animation-interface.cc (-20 / +411 lines)
 Lines 14-25    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
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)
 Lines 37-42    Link Here 
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
 Lines 45-56    Link Here 
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)
 Lines 58-63    Link Here 
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
 Lines 91-112    Link Here 
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)
 Lines 117-126    Link Here 
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"))
 Lines 134-173    Link Here 
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);
 Lines 186-200    Link Here 
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
(-)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