~ubuntu-branches/ubuntu/quantal/ns3/quantal

« back to all changes in this revision

Viewing changes to ns-3.13/src/netanim/model/animation-interface.h

  • Committer: Package Import Robot
  • Author(s): YunQiang Su, Aron Xu, YunQiang Su, Upstream
  • Date: 2012-01-06 00:35:42 UTC
  • mfrom: (10.1.5 sid)
  • Revision ID: package-import@ubuntu.com-20120106003542-vcn5g03mhapm991h
Tags: 3.13+dfsg-1
[ Aron Xu ]:
        add tag binary and binary-indep, 
  for not build doc when --binary-arch (Closes: #654493).
[ YunQiang Su ]
        add waf 1.5/1.6 source to debian directory, 
  and build waf from there (Closes: #642217).
[ Upstream ]
  Successfully link with --as-needed option (Closes: #642225).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * This program is free software; you can redistribute it and/or modify
 
3
 * it under the terms of the GNU General Public License version 2 as
 
4
 * published by the Free Software Foundation;
 
5
 *
 
6
 * This program is distributed in the hope that it will be useful,
 
7
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
8
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
9
 * GNU General Public License for more details.
 
10
 *
 
11
 * You should have received a copy of the GNU General Public License
 
12
 * along with this program; if not, write to the Free Software
 
13
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
14
 *
 
15
 * Author: George F. Riley<riley@ece.gatech.edu>
 
16
 * Modified by: John Abraham <john.abraham@gatech.edu>
 
17
 */
 
18
 
 
19
// Interface between ns3 and the network animator
 
20
 
 
21
#ifndef ANIMATION_INTERFACE__H
 
22
#define ANIMATION_INTERFACE__H
 
23
 
 
24
#include <string>
 
25
#include <stdio.h>
 
26
#include <map>
 
27
#include "ns3/ptr.h"
 
28
#include "ns3/net-device.h"
 
29
#include "ns3/nstime.h"
 
30
#include "ns3/log.h"
 
31
#include "ns3/node-list.h"
 
32
#include "ns3/simulator.h"
 
33
#include "ns3/config.h"
 
34
#include "ns3/animation-interface-helper.h"
 
35
#include "ns3/mac48-address.h"
 
36
 
 
37
#ifdef WIN32
 
38
#include <winsock2.h>
 
39
#include <io.h>
 
40
#define STDOUT_FILENO (SOCKET)GetStdHandle(STD_OUTPUT_HANDLE)
 
41
#define close _close
 
42
#define write _write
 
43
#undef GetObject
 
44
#undef min
 
45
#undef max
 
46
#define HANDLETYPE SOCKET
 
47
#else
 
48
#include "ns3/netanim-config.h"
 
49
#define HANDLETYPE int
 
50
#endif
 
51
 
 
52
namespace ns3 {
 
53
 
 
54
/**
 
55
 * \defgroup netanim Netanim
 
56
 *
 
57
 * This section documents the API of the ns-3 netanim module. For a generic functional description, please refer to the ns-3 manual.
 
58
 */
 
59
 
 
60
/**
 
61
 * \ingroup netanim
 
62
 *
 
63
 * \brief Interface to network animator
 
64
 *
 
65
 * Provides functions that facilitate communications with an
 
66
 * external or internal network animator.
 
67
 */
 
68
class AnimationInterface
 
69
{
 
70
public:
 
71
 
 
72
  /**
 
73
   * \brief Construct the animator interface. No arguments needed.
 
74
   *
 
75
   */
 
76
  AnimationInterface ();
 
77
 
 
78
  /**
 
79
   * \brief Destructor for the animator interface.
 
80
   *
 
81
   */
 
82
  ~AnimationInterface ();
 
83
 
 
84
  /**
 
85
   * \brief Constructor
 
86
   * \param filename The Filename for the trace file used by the Animator
 
87
   * \param usingXML Set to true if XML output traces are required
 
88
   *
 
89
   */
 
90
  AnimationInterface (const std::string filename, bool usingXML = true);
 
91
 
 
92
  /**
 
93
   * \brief Constructor
 
94
   * \param port Port on which ns-3 should listen to for connection from the
 
95
   *        external netanim application
 
96
   * \param usingXML Set to true if XML output traces are required
 
97
   *
 
98
   */
 
99
  AnimationInterface (uint16_t port, bool usingXML = true);
 
100
 
 
101
  /**
 
102
   * \brief Check if AnimationInterface is initialized
 
103
   * \returns true if AnimationInterface was already initialized
 
104
   *
 
105
   */
 
106
  static bool IsInitialized (void);
 
107
 
 
108
  /**
 
109
   * \brief Specify that animation commands are to be written
 
110
   * to the specified output file.
 
111
   *
 
112
   * This call is used to write the animation information to a text
 
113
   * file that can later be used as input to the network animator tool.
 
114
   *
 
115
   * \param fn The name of the output file.
 
116
   * \returns true if successful open.
 
117
   *
 
118
   */
 
119
  bool SetOutputFile (const std::string& fn);
 
120
 
 
121
  /**
 
122
   * \brief Specify that animation commands are to be written
 
123
   * in XML format.
 
124
   *
 
125
   * \returns none
 
126
   *
 
127
   */
 
128
  void SetXMLOutput ();
 
129
 
 
130
  /**
 
131
   * \brief (Deprecated) Specify that animation commands are to be written to
 
132
   * a socket.
 
133
   *
 
134
   * This call is used to set the ns3 process in server mode, waiting
 
135
   * for a TCP connection from the animator.  This call will not
 
136
   * return until the animator connects in, or if the bind to the
 
137
   * specified port fails.
 
138
   *
 
139
   * \param port Port number to bind to.
 
140
   * \returns true if connection created, false if bind failed.
 
141
   *
 
142
   */
 
143
  bool SetServerPort (uint16_t port);
 
144
 
 
145
  /**
 
146
   * \brief Writes the topology information and sets up the appropriate
 
147
   *  animation packet tx callback
 
148
   *
 
149
   * Writes the topology information to the appropriate output, depending
 
150
   * on prior calls to SetOutputFile, SetServerPort, or SetInternalAnimation.
 
151
   * Then creates the callbacks needed for the animator to start processing
 
152
   * packets.
 
153
   *
 
154
   */
 
155
  void StartAnimation ();
 
156
 
 
157
  /**
 
158
   * \brief Closes the interface to the animator.
 
159
   *
 
160
   */
 
161
  void StopAnimation ();
 
162
 
 
163
  /**
 
164
   * \brief Set mobility poll interval:WARNING: setting a low interval can 
 
165
   * cause slowness
 
166
   *
 
167
   * \param t Time interval between fetching mobility/position information
 
168
   * Default: 0.25s
 
169
   *
 
170
   */
 
171
  void SetMobilityPollInterval (Time t);
 
172
 
 
173
  /**
 
174
   * \brief Set random position if a Mobility Model does not exists for the node
 
175
   *
 
176
   * \param setRandPos True if a random position can be set for a node without a
 
177
   * Mobililty model
 
178
   *
 
179
   */
 
180
  void SetRandomPosition (bool setRandPos);
 
181
 
 
182
  /**
 
183
   * \brief typedef for WriteCallBack used for listening to AnimationInterface
 
184
   * write messages
 
185
   * 
 
186
   */
 
187
  typedef void (*AnimWriteCallback) (const char * str);
 
188
 
 
189
  /**
 
190
   * \brief Set a callback function to listen to AnimationInterface write events
 
191
   *
 
192
   * \param cb Address of callback function
 
193
   *
 
194
   */
 
195
  void SetAnimWriteCallback (AnimWriteCallback cb);
 
196
 
 
197
  /**
 
198
   * \brief Reset the write callback function
 
199
   *
 
200
   */
 
201
  void ResetAnimWriteCallback ();
 
202
 
 
203
  /**
 
204
   * \brief Helper function to set Constant Position for a given node
 
205
   * \param n Ptr to the node
 
206
   * \param x X co-ordinate of the node
 
207
   * \param y Y co-ordinate of the node
 
208
   * \param z Z co-ordinate of the node
 
209
   *
 
210
   */
 
211
  void SetConstantPosition (Ptr <Node> n, double x, double y, double z=0);
 
212
 
 
213
  /**
 
214
   * \brief Is AnimationInterface started
 
215
   * \returns true if AnimationInterface was started
 
216
   *
 
217
   */
 
218
  bool IsStarted (void);
 
219
 
 
220
private:
 
221
#ifndef WIN32
 
222
  int m_fHandle;  // File handle for output (-1 if none)
 
223
  // Write specified amount of data to the specified handle
 
224
  int WriteN (int, const char*, uint32_t);
 
225
#else
 
226
  SOCKET m_fHandle;  // File handle for output (-1 if none)
 
227
  int  WriteN (SOCKET, const char*, uint32_t);
 
228
#endif
 
229
  bool m_xml;      // True if xml format desired
 
230
  Time mobilitypollinterval;
 
231
  bool usingSockets;
 
232
  uint16_t mport;
 
233
  std::string outputfilename;
 
234
  bool OutputFileSet;
 
235
  bool ServerPortSet;
 
236
  void DevTxTrace (std::string context,
 
237
                   Ptr<const Packet> p,
 
238
                   Ptr<NetDevice> tx,
 
239
                   Ptr<NetDevice> rx,
 
240
                   Time txTime,
 
241
                   Time rxTime);
 
242
  void WifiPhyTxBeginTrace (std::string context,
 
243
                            Ptr<const Packet> p);
 
244
  void WifiPhyTxEndTrace (std::string context,
 
245
                          Ptr<const Packet> p);
 
246
  void WifiPhyTxDropTrace (std::string context,
 
247
                           Ptr<const Packet> p);
 
248
  void WifiPhyRxBeginTrace (std::string context,
 
249
                            Ptr<const Packet> p);
 
250
  void WifiPhyRxEndTrace (std::string context,
 
251
                          Ptr<const Packet> p);
 
252
  void WifiMacRxTrace (std::string context,
 
253
                       Ptr<const Packet> p);
 
254
  void WifiPhyRxDropTrace (std::string context,
 
255
                           Ptr<const Packet> p);
 
256
  void WimaxTxTrace (std::string context,
 
257
                     Ptr<const Packet> p,
 
258
                     const Mac48Address &);
 
259
  void WimaxRxTrace (std::string context,
 
260
                     Ptr<const Packet> p,
 
261
                     const Mac48Address &);
 
262
  void CsmaPhyTxBeginTrace (std::string context,
 
263
                            Ptr<const Packet> p);
 
264
  void CsmaPhyTxEndTrace (std::string context,
 
265
                            Ptr<const Packet> p);
 
266
  void CsmaPhyRxEndTrace (std::string context,
 
267
                          Ptr<const Packet> p);
 
268
  void CsmaMacRxTrace (std::string context,
 
269
                       Ptr<const Packet> p);
 
270
  void MobilityCourseChangeTrace (Ptr <const MobilityModel> mob);
 
271
 
 
272
  // Write a string to the specified handle;
 
273
  int  WriteN (int, const std::string&);
 
274
 
 
275
  void OutputWirelessPacket (AnimPacketInfo& pktInfo, AnimRxInfo pktrxInfo);
 
276
  void OutputCsmaPacket (AnimPacketInfo& pktInfo, AnimRxInfo pktrxInfo);
 
277
  void MobilityAutoCheck ();
 
278
  
 
279
  uint64_t gAnimUid ;    // Packet unique identifier used by Animtion
 
280
 
 
281
  std::map<uint64_t, AnimPacketInfo> pendingWifiPackets;
 
282
  void AddPendingWifiPacket (uint64_t AnimUid, AnimPacketInfo&);
 
283
  bool WifiPacketIsPending (uint64_t AnimUid); 
 
284
 
 
285
  std::map<uint64_t, AnimPacketInfo> pendingWimaxPackets;
 
286
  void AddPendingWimaxPacket (uint64_t AnimUid, AnimPacketInfo&);
 
287
  bool WimaxPacketIsPending (uint64_t AnimUid); 
 
288
 
 
289
  std::map<uint64_t, AnimPacketInfo> pendingCsmaPackets;
 
290
  void AddPendingCsmaPacket (uint64_t AnimUid, AnimPacketInfo&);
 
291
  bool CsmaPacketIsPending (uint64_t AnimUid);
 
292
 
 
293
  uint64_t GetAnimUidFromPacket (Ptr <const Packet>);
 
294
 
 
295
  std::map<uint32_t, Vector> nodeLocation;
 
296
  Vector GetPosition (Ptr <Node> n);
 
297
  Vector UpdatePosition (Ptr <Node> n);
 
298
  Vector UpdatePosition (Ptr <Node> n, Vector v);
 
299
  void WriteDummyPacket ();
 
300
  bool NodeHasMoved (Ptr <Node> n, Vector newLocation);
 
301
  void AddMargin ();
 
302
 
 
303
  void PurgePendingWifi ();
 
304
  void PurgePendingWimax ();
 
305
  void PurgePendingCsma ();
 
306
 
 
307
  // Recalculate topology bounds
 
308
  void RecalcTopoBounds (Vector v);
 
309
  std::vector < Ptr <Node> > RecalcTopoBounds ();
 
310
 
 
311
  bool randomPosition;
 
312
  AnimWriteCallback m_writeCallback;
 
313
  void ConnectCallbacks ();
 
314
 
 
315
  bool m_started;
 
316
 
 
317
  // Path helper
 
318
  std::vector<std::string> GetElementsFromContext (std::string context);
 
319
  Ptr <NetDevice> GetNetDeviceFromContext (std::string context);
 
320
 
 
321
  // XML helpers
 
322
  std::string GetPreamble (void);
 
323
  // Topology element dimensions
 
324
  double topo_minX;
 
325
  double topo_minY;
 
326
  double topo_maxX;
 
327
  double topo_maxY;
 
328
 
 
329
  std::string GetXMLOpen_anim (uint32_t lp);
 
330
  std::string GetXMLOpen_topology (double minX,double minY,double maxX,double maxY);
 
331
  std::string GetXMLOpenClose_node (uint32_t lp,uint32_t id,double locX,double locY);
 
332
  std::string GetXMLOpenClose_link (uint32_t fromLp,uint32_t fromId, uint32_t toLp, uint32_t toId);
 
333
  std::string GetXMLOpen_packet (uint32_t fromLp,uint32_t fromId, double fbTx, double lbTx, std::string auxInfo = "");
 
334
  std::string GetXMLOpenClose_rx (uint32_t toLp, uint32_t toId, double fbRx, double lbRx);
 
335
  std::string GetXMLOpen_wpacket (uint32_t fromLp,uint32_t fromId, double fbTx, double lbTx, double range);
 
336
  std::string GetXMLClose (std::string name) {return "</" + name + ">\n"; }
 
337
 
 
338
};
 
339
 
 
340
/**
 
341
 * \ingroup netanim
 
342
 *
 
343
 * \brief Byte tag using by Anim to uniquely identify packets
 
344
 *
 
345
 * When Anim receives a Tx Notification we tag the packet with a unique global uint64_t identifier
 
346
 * before recording Tx information
 
347
 * When Anim receives Rx notifications the tag is used to retrieve Tx information recorded earlier 
 
348
 * 
 
349
 */
 
350
 
 
351
class AnimByteTag : public Tag
 
352
{
 
353
public:
 
354
 
 
355
  /**
 
356
   * \brief Get Type Id
 
357
   * \returns Type Id
 
358
   *
 
359
   */
 
360
  static TypeId GetTypeId (void);
 
361
  
 
362
  /**
 
363
   * \brief Get Instance Type Id
 
364
   * \returns Type Id
 
365
   *
 
366
   */
 
367
  virtual TypeId GetInstanceTypeId (void) const;
 
368
 
 
369
  /**
 
370
   * \brief Get Serialized Size
 
371
   * \returns Serialized Size (i.e size of uint64_t)
 
372
   *
 
373
   */
 
374
  virtual uint32_t GetSerializedSize (void) const;
 
375
 
 
376
  /**
 
377
   * \brief Serialize function
 
378
   * \param i Tag Buffer
 
379
   *
 
380
   */
 
381
  virtual void Serialize (TagBuffer i) const;
 
382
 
 
383
  /**
 
384
   * \brief Deserialize function
 
385
   * \param i Tag Buffer
 
386
   *
 
387
   */
 
388
  virtual void Deserialize (TagBuffer i);
 
389
 
 
390
  /**
 
391
   * \brief Print tag info
 
392
   * \param os Reference of ostream object
 
393
   *
 
394
   */
 
395
  virtual void Print (std::ostream &os) const;
 
396
 
 
397
  /**
 
398
   * \brief Set global Uid in tag
 
399
   * \param AnimUid global Uid
 
400
   *
 
401
   */
 
402
  void Set (uint64_t AnimUid);
 
403
 
 
404
  /**
 
405
   * \brief Get Uid in tag
 
406
   * \returns Uid in tag
 
407
   *
 
408
   */
 
409
  uint64_t Get (void) const;
 
410
 
 
411
private:
 
412
  uint64_t m_AnimUid;
 
413
};
 
414
 
 
415
}
 
416
#endif
 
417