A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
animation-interface.h
Go to the documentation of this file.
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  * Author: 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 <cstdio>
26 #include <map>
27 #include "ns3/ptr.h"
28 #include "ns3/net-device.h"
29 #include "ns3/node-container.h"
30 #include "ns3/nstime.h"
31 #include "ns3/log.h"
32 #include "ns3/node-list.h"
33 #include "ns3/random-variable-stream.h"
34 #include "ns3/simulator.h"
35 #include "ns3/config.h"
36 #include "ns3/animation-interface-helper.h"
37 #include "ns3/mac48-address.h"
38 #include "ns3/lte-ue-net-device.h"
39 #include "ns3/lte-enb-net-device.h"
40 #include "ns3/uan-phy-gen.h"
41 
42 namespace ns3 {
43 
44 #define MAX_PKTS_PER_TRACE_FILE 100000
45 struct Rgb;
46 typedef struct
47 {
48  uint32_t fromNode;
49  uint32_t toNode;
51 
52 typedef struct
53 {
54  std::string fromNodeDescription;
55  std::string toNodeDescription;
56  std::string linkDescription;
58 
60 {
62  {
63  //Check if they are the same node pairs but flipped
64  if ( ((first.fromNode == second.fromNode) && (first.toNode == second.toNode)) ||
65  ((first.fromNode == second.toNode) && (first.toNode == second.fromNode)) )
66  {
67  return false;
68  }
69  std::ostringstream oss1;
70  oss1 << first.fromNode << first.toNode;
71  std::ostringstream oss2;
72  oss2 << second.fromNode << second.toNode;
73  return oss1.str () < oss2.str ();
74  }
75 
76 };
77 
93 {
94 public:
95 
106  AnimationInterface (const std::string filename,
107  uint64_t maxPktsPerFile = MAX_PKTS_PER_TRACE_FILE,
108  bool usingXML = true);
109 
115 
127  void EnableIpv4RouteTracking (std::string fileName, Time startTime, Time stopTime, Time pollInterval = Seconds(5));
128 
141  void EnableIpv4RouteTracking (std::string fileName, Time startTime, Time stopTime, NodeContainer nc, Time pollInterval = Seconds(5));
142 
148  static bool IsInitialized (void);
149 
157  void SetStartTime (Time t);
158 
166  void SetStopTime (Time t);
167 
176  void SetMobilityPollInterval (Time t);
177 
185  void SetRandomPosition (bool setRandPos);
186 
192  typedef void (*AnimWriteCallback) (const char * str);
193 
201 
206  void ResetAnimWriteCallback ();
207 
216  static void SetConstantPosition (Ptr <Node> n, double x, double y, double z=0);
217 
224  static void SetNodeDescription (Ptr <Node> n, std::string descr);
225 
232  void UpdateNodeDescription (Ptr <Node> n, std::string descr);
233 
240  void UpdateNodeDescription (uint32_t nodeId, std::string descr);
241 
248  void ShowNode (uint32_t nodeId, bool show = true);
249 
256  void ShowNode (Ptr <Node> n, bool show = true);
257 
264  static void SetNodeDescription (NodeContainer nc, std::string descr);
265 
274  static void SetNodeColor (Ptr <Node> n, uint8_t r, uint8_t g, uint8_t b);
275 
276 
285  void UpdateNodeColor (Ptr <Node> n, uint8_t r, uint8_t g, uint8_t b);
286 
295  void UpdateNodeColor (uint32_t nodeId, uint8_t r, uint8_t g, uint8_t b);
296 
297 
306  static void SetNodeColor (NodeContainer nc, uint8_t r, uint8_t g, uint8_t b);
307 
317  static void SetLinkDescription (uint32_t fromNode, uint32_t toNode,
318  std::string linkDescription,
319  std::string fromNodeDescription = "",
320  std::string toNodeDescription = "");
321 
331  static void SetLinkDescription (Ptr <Node> fromNode, Ptr <Node> toNode,
332  std::string linkDescription,
333  std::string fromNodeDescription = "",
334  std::string toNodeDescription = "");
335 
336 
344  void UpdateLinkDescription (uint32_t fromNode, uint32_t toNode,
345  std::string linkDescription);
346 
354  void UpdateLinkDescription (Ptr <Node> fromNode, Ptr <Node> toNode,
355  std::string linkDescription);
356 
357 
363  bool IsStarted (void);
364 
371  void EnablePacketMetadata (bool enable);
372 
380  uint64_t GetTracePktCount ();
381 
390  int64_t AssignStreams (int64_t stream);
391 
392 private:
393  FILE * m_f; // File handle for output (0 if none)
394  FILE * m_routingF; // File handle for routing table output (0 if None);
395  // Write specified amount of data to the specified handle
396  int WriteN (const char*, uint32_t, FILE * f);
397  bool m_xml; // True if xml format desired
399  std::string m_outputFileName;
401  uint64_t gAnimUid ; // Packet unique identifier used by Animtion
404  bool m_started;
409  std::string m_originalFileName;
411  std::string m_routingFileName;
414 
415  void TrackIpv4Route ();
416  std::string GetIpv4RoutingTable (Ptr <Node> n);
417 
429  bool SetOutputFile (const std::string& fn);
430  bool SetRoutingOutputFile (const std::string& fn);
431 
439  void SetXMLOutput ();
440 
452  void StartAnimation (bool restart = false);
453 
458  void StopAnimation ();
459 
460  void DevTxTrace (std::string context,
462  Ptr<NetDevice> tx,
463  Ptr<NetDevice> rx,
464  Time txTime,
465  Time rxTime);
466  void WifiPhyTxBeginTrace (std::string context,
468  void WifiPhyTxEndTrace (std::string context,
470  void WifiPhyTxDropTrace (std::string context,
472  void WifiPhyRxBeginTrace (std::string context,
474  void WifiPhyRxEndTrace (std::string context,
476  void WifiMacRxTrace (std::string context,
478  void WifiPhyRxDropTrace (std::string context,
480  void WimaxTxTrace (std::string context,
482  const Mac48Address &);
483  void WimaxRxTrace (std::string context,
485  const Mac48Address &);
486  void CsmaPhyTxBeginTrace (std::string context,
488  void CsmaPhyTxEndTrace (std::string context,
490  void CsmaPhyRxEndTrace (std::string context,
492  void CsmaMacRxTrace (std::string context,
494 
495  void LteTxTrace (std::string context,
497  const Mac48Address &);
498 
499  void LteRxTrace (std::string context,
501  const Mac48Address &);
502 
503  void LteSpectrumPhyTxStart (std::string context,
505  void LteSpectrumPhyRxStart (std::string context,
507 
508  void UanPhyGenTxTrace (std::string context,
510  void UanPhyGenRxTrace (std::string context,
512 
514 
515  // Write a string to the specified handle;
516  int WriteN (const std::string&, FILE * f);
517 
518  void OutputWirelessPacket (Ptr<const Packet> p, AnimPacketInfo& pktInfo, AnimRxInfo pktrxInfo);
519  void OutputCsmaPacket (Ptr<const Packet> p, AnimPacketInfo& pktInfo, AnimRxInfo pktrxInfo);
520  void MobilityAutoCheck ();
521 
522 
523  std::map<uint64_t, AnimPacketInfo> m_pendingWifiPackets;
524  void AddPendingWifiPacket (uint64_t AnimUid, AnimPacketInfo&);
525  bool WifiPacketIsPending (uint64_t AnimUid);
526 
527  std::map<uint64_t, AnimPacketInfo> m_pendingWimaxPackets;
528  void AddPendingWimaxPacket (uint64_t AnimUid, AnimPacketInfo&);
529  bool WimaxPacketIsPending (uint64_t AnimUid);
530 
531  std::map<uint64_t, AnimPacketInfo> m_pendingLtePackets;
532  void AddPendingLtePacket (uint64_t AnimUid, AnimPacketInfo&);
533  bool LtePacketIsPending (uint64_t AnimUid);
534 
535  std::map<uint64_t, AnimPacketInfo> m_pendingCsmaPackets;
536  void AddPendingCsmaPacket (uint64_t AnimUid, AnimPacketInfo&);
537  bool CsmaPacketIsPending (uint64_t AnimUid);
538 
539  std::map<uint64_t, AnimPacketInfo> m_pendingUanPackets;
540  void AddPendingUanPacket (uint64_t AnimUid, AnimPacketInfo&);
541  bool UanPacketIsPending (uint64_t AnimUid);
542 
544 
545  std::map<uint32_t, Vector> m_nodeLocation;
549  void WriteDummyPacket ();
550  bool NodeHasMoved (Ptr <Node> n, Vector newLocation);
551 
552  void PurgePendingWifi ();
553  void PurgePendingWimax ();
554  void PurgePendingLte ();
555  void PurgePendingCsma ();
556 
557  // Recalculate topology bounds
558  void RecalcTopoBounds (Vector v);
559  std::vector < Ptr <Node> > RecalcTopoBounds ();
560 
561  void ConnectCallbacks ();
562  void ConnectLte ();
563  void ConnectLteUe (Ptr <Node> n, Ptr <LteUeNetDevice> nd, uint32_t devIndex);
564  void ConnectLteEnb (Ptr <Node> n, Ptr <LteEnbNetDevice> nd, uint32_t devIndex);
565 
566 
567  std::map <std::string, uint32_t> m_macToNodeIdMap;
568  bool IsInTimeWindow ();
569 
570  // Path helper
571  std::vector<std::string> GetElementsFromContext (std::string context);
572  Ptr <NetDevice> GetNetDeviceFromContext (std::string context);
573 
574  static std::map <uint32_t, Rgb> nodeColors;
575  static std::map <uint32_t, std::string> nodeDescriptions;
576  static std::map <P2pLinkNodeIdPair, LinkProperties, LinkPairCompare> linkProperties;
578 
579  void StartNewTraceFile();
580 
581  std::string GetMacAddress (Ptr <NetDevice> nd);
582  std::string GetIpv4Address (Ptr <NetDevice> nd);
583  void WriteNonP2pLinkProperties (uint32_t id, std::string ipv4Address, std::string channelType);
584 
585  std::string GetNetAnimVersion ();
586 
587  // XML helpers
588  std::string GetPreamble (void);
589  // Topology element dimensions
590  double m_topoMinX;
591  double m_topoMinY;
592  double m_topoMaxX;
593  double m_topoMaxY;
594 
595  std::string GetPacketMetadata (Ptr<const Packet> p);
596 
597  std::string GetXMLOpen_anim (uint32_t lp);
598  std::string GetXMLOpen_topology (double minX, double minY, double maxX, double maxY);
599  std::string GetXMLOpenClose_node (uint32_t lp, uint32_t id, double locX, double locY);
600  std::string GetXMLOpenClose_node (uint32_t lp, uint32_t id, double locX, double locY, struct Rgb rgb);
601  std::string GetXMLOpenClose_nodeupdate (uint32_t id, bool visible = true);
602  std::string GetXMLOpenClose_link (uint32_t fromLp, uint32_t fromId, uint32_t toLp, uint32_t toId);
603  std::string GetXMLOpenClose_linkupdate (uint32_t fromId, uint32_t toId, std::string);
604  std::string GetXMLOpen_packet (uint32_t fromLp, uint32_t fromId, double fbTx, double lbTx, std::string auxInfo = "");
605  std::string GetXMLOpenClose_p (std::string pktType, uint32_t fId, double fbTx, double lbTx, uint32_t tId, double fbRx, double lbRx,
606  std::string metaInfo = "", std::string auxInfo = "");
607  std::string GetXMLOpenClose_rx (uint32_t toLp, uint32_t toId, double fbRx, double lbRx);
608  std::string GetXMLOpen_wpacket (uint32_t fromLp, uint32_t fromId, double fbTx, double lbTx, double range);
609  std::string GetXMLClose (std::string name) {return "</" + name + ">\n"; }
610  std::string GetXMLOpenClose_meta (std::string metaInfo);
611  std::string GetXMLOpenClose_NonP2pLinkProperties (uint32_t id, std::string ipv4Address, std::string channelType);
612  std::string GetXMLOpenClose_routing (uint32_t id, std::string routingInfo);
613 
614 
617 };
618 
624 struct Rgb
625 {
626  uint8_t r;
627  uint8_t g;
628  uint8_t b;
629 };
630 
642 class AnimByteTag : public Tag
643 {
644 public:
645 
651  static TypeId GetTypeId (void);
652 
658  virtual TypeId GetInstanceTypeId (void) const;
659 
665  virtual uint32_t GetSerializedSize (void) const;
666 
672  virtual void Serialize (TagBuffer i) const;
673 
679  virtual void Deserialize (TagBuffer i);
680 
686  virtual void Print (std::ostream &os) const;
687 
693  void Set (uint64_t AnimUid);
694 
700  uint64_t Get (void) const;
701 
702 private:
703  uint64_t m_AnimUid;
704 };
705 
706 }
707 #endif
708