A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
animation-interface.h
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 {
61  bool operator () (P2pLinkNodeIdPair first, P2pLinkNodeIdPair second)
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 
79  std::string destination;
80  uint32_t fromNodeId;
81 };
82 
83 
84 typedef struct {
85  uint32_t nodeId;
86  std::string nextHop;
87 
89 
105 {
106 public:
107 
118  AnimationInterface (const std::string filename,
119  uint64_t maxPktsPerFile = MAX_PKTS_PER_TRACE_FILE,
120  bool usingXML = true);
121 
127 
139  AnimationInterface & EnableIpv4RouteTracking (std::string fileName, Time startTime, Time stopTime, Time pollInterval = Seconds(5));
140 
153  AnimationInterface & EnableIpv4RouteTracking (std::string fileName, Time startTime, Time stopTime, NodeContainer nc, Time pollInterval = Seconds(5));
154 
160  static bool IsInitialized (void);
161 
169  void SetStartTime (Time t);
170 
178  void SetStopTime (Time t);
179 
188  void SetMobilityPollInterval (Time t);
189 
197  void SetRandomPosition (bool setRandPos);
198 
204  typedef void (*AnimWriteCallback) (const char * str);
205 
213 
218  void ResetAnimWriteCallback ();
219 
228  static void SetConstantPosition (Ptr <Node> n, double x, double y, double z=0);
229 
236  static void SetNodeDescription (Ptr <Node> n, std::string descr);
237 
244  void UpdateNodeDescription (Ptr <Node> n, std::string descr);
245 
252  void UpdateNodeDescription (uint32_t nodeId, std::string descr);
253 
260  void ShowNode (uint32_t nodeId, bool show = true);
261 
268  void ShowNode (Ptr <Node> n, bool show = true);
269 
276  static void SetNodeDescription (NodeContainer nc, std::string descr);
277 
286  static void SetNodeColor (Ptr <Node> n, uint8_t r, uint8_t g, uint8_t b);
287 
288 
297  void UpdateNodeColor (Ptr <Node> n, uint8_t r, uint8_t g, uint8_t b);
298 
307  void UpdateNodeColor (uint32_t nodeId, uint8_t r, uint8_t g, uint8_t b);
308 
309 
318  static void SetNodeColor (NodeContainer nc, uint8_t r, uint8_t g, uint8_t b);
319 
329  static void SetLinkDescription (uint32_t fromNode, uint32_t toNode,
330  std::string linkDescription,
331  std::string fromNodeDescription = "",
332  std::string toNodeDescription = "");
333 
343  static void SetLinkDescription (Ptr <Node> fromNode, Ptr <Node> toNode,
344  std::string linkDescription,
345  std::string fromNodeDescription = "",
346  std::string toNodeDescription = "");
347 
348 
356  void UpdateLinkDescription (uint32_t fromNode, uint32_t toNode,
357  std::string linkDescription);
358 
366  void UpdateLinkDescription (Ptr <Node> fromNode, Ptr <Node> toNode,
367  std::string linkDescription);
368 
376  AnimationInterface & AddSourceDestination (uint32_t fromNodeId, std::string destinationIpv4Address);
377 
383  bool IsStarted (void);
384 
391  void EnablePacketMetadata (bool enable);
392 
400  uint64_t GetTracePktCount ();
401 
410  int64_t AssignStreams (int64_t stream);
411 
412 private:
413  FILE * m_f; // File handle for output (0 if none)
414  FILE * m_routingF; // File handle for routing table output (0 if None);
415  // Write specified amount of data to the specified handle
416  int WriteN (const char*, uint32_t, FILE * f);
417  bool m_xml; // True if xml format desired
418  Time m_mobilityPollInterval;
419  std::string m_outputFileName;
420  bool m_outputFileSet;
421  uint64_t gAnimUid ; // Packet unique identifier used by Animtion
422  bool m_randomPosition;
423  AnimWriteCallback m_writeCallback;
424  bool m_started;
425  bool m_enablePacketMetadata;
426  Time m_startTime;
427  Time m_stopTime;
428  uint64_t m_maxPktsPerFile;
429  std::string m_originalFileName;
430  Time m_routingStopTime;
431  std::string m_routingFileName;
432  Time m_routingPollInterval;
433  NodeContainer m_routingNc;
434 
435  void TrackIpv4Route ();
436  void TrackIpv4RoutePaths ();
437  std::string GetIpv4RoutingTable (Ptr <Node> n);
438 
450  bool SetOutputFile (const std::string& fn);
451  bool SetRoutingOutputFile (const std::string& fn);
452 
460  void SetXMLOutput ();
461 
473  void StartAnimation (bool restart = false);
474 
479  void StopAnimation (bool onlyAnimation = false);
480 
481  void DevTxTrace (std::string context,
483  Ptr<NetDevice> tx,
484  Ptr<NetDevice> rx,
485  Time txTime,
486  Time rxTime);
487  void WifiPhyTxBeginTrace (std::string context,
489  void WifiPhyTxEndTrace (std::string context,
491  void WifiPhyTxDropTrace (std::string context,
493  void WifiPhyRxBeginTrace (std::string context,
495  void WifiPhyRxEndTrace (std::string context,
497  void WifiMacRxTrace (std::string context,
499  void WifiPhyRxDropTrace (std::string context,
501  void WimaxTxTrace (std::string context,
503  const Mac48Address &);
504  void WimaxRxTrace (std::string context,
506  const Mac48Address &);
507  void CsmaPhyTxBeginTrace (std::string context,
509  void CsmaPhyTxEndTrace (std::string context,
511  void CsmaPhyRxEndTrace (std::string context,
513  void CsmaMacRxTrace (std::string context,
515 
516  void LteTxTrace (std::string context,
518  const Mac48Address &);
519 
520  void LteRxTrace (std::string context,
522  const Mac48Address &);
523 
524  void LteSpectrumPhyTxStart (std::string context,
526  void LteSpectrumPhyRxStart (std::string context,
528 
529  void UanPhyGenTxTrace (std::string context,
531  void UanPhyGenRxTrace (std::string context,
533 
534  void MobilityCourseChangeTrace (Ptr <const MobilityModel> mob);
535 
536  // Write a string to the specified handle;
537  int WriteN (const std::string&, FILE * f);
538 
539  void OutputWirelessPacket (Ptr<const Packet> p, AnimPacketInfo& pktInfo, AnimRxInfo pktrxInfo);
540  void OutputCsmaPacket (Ptr<const Packet> p, AnimPacketInfo& pktInfo, AnimRxInfo pktrxInfo);
541  void MobilityAutoCheck ();
542 
543 
544  std::map<uint64_t, AnimPacketInfo> m_pendingWifiPackets;
545  void AddPendingWifiPacket (uint64_t AnimUid, AnimPacketInfo&);
546  bool WifiPacketIsPending (uint64_t AnimUid);
547 
548  std::map<uint64_t, AnimPacketInfo> m_pendingWimaxPackets;
549  void AddPendingWimaxPacket (uint64_t AnimUid, AnimPacketInfo&);
550  bool WimaxPacketIsPending (uint64_t AnimUid);
551 
552  std::map<uint64_t, AnimPacketInfo> m_pendingLtePackets;
553  void AddPendingLtePacket (uint64_t AnimUid, AnimPacketInfo&);
554  bool LtePacketIsPending (uint64_t AnimUid);
555 
556  std::map<uint64_t, AnimPacketInfo> m_pendingCsmaPackets;
557  void AddPendingCsmaPacket (uint64_t AnimUid, AnimPacketInfo&);
558  bool CsmaPacketIsPending (uint64_t AnimUid);
559 
560  std::map<uint64_t, AnimPacketInfo> m_pendingUanPackets;
561  void AddPendingUanPacket (uint64_t AnimUid, AnimPacketInfo&);
562  bool UanPacketIsPending (uint64_t AnimUid);
563 
564  uint64_t GetAnimUidFromPacket (Ptr <const Packet>);
565 
566  std::map<uint32_t, Vector> m_nodeLocation;
567  Vector GetPosition (Ptr <Node> n);
568  Vector UpdatePosition (Ptr <Node> n);
569  Vector UpdatePosition (Ptr <Node> n, Vector v);
570  void WriteDummyPacket ();
571  bool NodeHasMoved (Ptr <Node> n, Vector newLocation);
572 
573  void PurgePendingWifi ();
574  void PurgePendingWimax ();
575  void PurgePendingLte ();
576  void PurgePendingCsma ();
577 
578  // Recalculate topology bounds
579  void RecalcTopoBounds (Vector v);
580  std::vector < Ptr <Node> > RecalcTopoBounds ();
581 
582  void ConnectCallbacks ();
583  void ConnectLte ();
584  void ConnectLteUe (Ptr <Node> n, Ptr <LteUeNetDevice> nd, uint32_t devIndex);
585  void ConnectLteEnb (Ptr <Node> n, Ptr <LteEnbNetDevice> nd, uint32_t devIndex);
586 
587 
588  std::map <std::string, uint32_t> m_macToNodeIdMap;
589  std::map <std::string, uint32_t> m_ipv4ToNodeIdMap;
590  void AddToIpv4AddressNodeIdTable (std::string, uint32_t);
591  std::vector <Ipv4RouteTrackElement> m_ipv4RouteTrackElements;
592  typedef std::vector <Ipv4RoutePathElement> Ipv4RoutePathElements;
593  void RecursiveIpv4RoutePathSearch (std::string fromIpv4, std::string toIpv4, Ipv4RoutePathElements &);
594  void WriteRoutePath (uint32_t nodeId, std::string destination, Ipv4RoutePathElements rpElements);
595  bool IsInTimeWindow ();
596 
597  // Path helper
598  std::vector<std::string> GetElementsFromContext (std::string context);
599  Ptr <NetDevice> GetNetDeviceFromContext (std::string context);
600 
601  static std::map <uint32_t, Rgb> nodeColors;
602  static std::map <uint32_t, std::string> nodeDescriptions;
603  static std::map <P2pLinkNodeIdPair, LinkProperties, LinkPairCompare> linkProperties;
604  uint64_t m_currentPktCount;
605 
606  void StartNewTraceFile();
607 
608  std::string GetMacAddress (Ptr <NetDevice> nd);
609  std::string GetIpv4Address (Ptr <NetDevice> nd);
610  void WriteNonP2pLinkProperties (uint32_t id, std::string ipv4Address, std::string channelType);
611 
612  std::string GetNetAnimVersion ();
613 
614  // XML helpers
615  std::string GetPreamble (void);
616  // Topology element dimensions
617  double m_topoMinX;
618  double m_topoMinY;
619  double m_topoMaxX;
620  double m_topoMaxY;
621 
622  std::string GetPacketMetadata (Ptr<const Packet> p);
623 
624  std::string GetXMLOpen_anim (uint32_t lp);
625  std::string GetXMLOpen_topology (double minX, double minY, double maxX, double maxY);
626  std::string GetXMLOpenClose_node (uint32_t lp, uint32_t id, double locX, double locY);
627  std::string GetXMLOpenClose_node (uint32_t lp, uint32_t id, double locX, double locY, struct Rgb rgb);
628  std::string GetXMLOpenClose_nodeupdate (uint32_t id, bool visible = true);
629  std::string GetXMLOpenClose_link (uint32_t fromLp, uint32_t fromId, uint32_t toLp, uint32_t toId);
630  std::string GetXMLOpenClose_linkupdate (uint32_t fromId, uint32_t toId, std::string);
631  std::string GetXMLOpen_packet (uint32_t fromLp, uint32_t fromId, double fbTx, double lbTx, std::string auxInfo = "");
632  std::string GetXMLOpenClose_p (std::string pktType, uint32_t fId, double fbTx, double lbTx, uint32_t tId, double fbRx, double lbRx,
633  std::string metaInfo = "", std::string auxInfo = "");
634  std::string GetXMLOpenClose_rx (uint32_t toLp, uint32_t toId, double fbRx, double lbRx);
635  std::string GetXMLOpen_wpacket (uint32_t fromLp, uint32_t fromId, double fbTx, double lbTx, double range);
636  std::string GetXMLClose (std::string name) {return "</" + name + ">\n"; }
637  std::string GetXMLOpenClose_meta (std::string metaInfo);
638  std::string GetXMLOpenClose_NonP2pLinkProperties (uint32_t id, std::string ipv4Address, std::string channelType);
639  std::string GetXMLOpenClose_routing (uint32_t id, std::string routingInfo);
640  std::string GetXMLOpenClose_rp (uint32_t nodeId, std::string destination, Ipv4RoutePathElements rpElements);
641 
642 
643 
646 };
647 
653 struct Rgb
654 {
655  uint8_t r;
656  uint8_t g;
657  uint8_t b;
658 };
659 
671 class AnimByteTag : public Tag
672 {
673 public:
674 
680  static TypeId GetTypeId (void);
681 
687  virtual TypeId GetInstanceTypeId (void) const;
688 
694  virtual uint32_t GetSerializedSize (void) const;
695 
701  virtual void Serialize (TagBuffer i) const;
702 
708  virtual void Deserialize (TagBuffer i);
709 
715  virtual void Print (std::ostream &os) const;
716 
722  void Set (uint64_t AnimUid);
723 
729  uint64_t Get (void) const;
730 
731 private:
732  uint64_t m_AnimUid;
733 };
734 
735 }
736 #endif
737 
void Set(uint64_t AnimUid)
Set global Uid in tag.
uint64_t GetTracePktCount()
Get trace file packet count (This used only for testing)
keep track of time unit.
Definition: nstime.h:149
AnimationInterface & AddSourceDestination(uint32_t fromNodeId, std::string destinationIpv4Address)
Helper function to print the routing path from a source node to destination IP.
AnimRxInfo helper class.
void EnablePacketMetadata(bool enable)
Enable Packet metadata.
static TypeId GetTypeId(void)
Get Type Id.
static void SetNodeColor(Ptr< Node > n, uint8_t r, uint8_t g, uint8_t b)
Helper function to set the node color.
static bool IsInitialized(void)
Check if AnimationInterface is initialized.
virtual void Deserialize(TagBuffer i)
Deserialize function.
void SetStartTime(Time t)
Specify the time at which capture should start.
void SetStopTime(Time t)
Specify the time at which capture should stop.
a 3d vector
Definition: vector.h:31
void UpdateNodeColor(Ptr< Node > n, uint8_t r, uint8_t g, uint8_t b)
Helper function to update the node color.
void ResetAnimWriteCallback()
Reset the write callback function.
static void SetConstantPosition(Ptr< Node > n, double x, double y, double z=0)
Helper function to set Constant Position for a given node.
virtual void Print(std::ostream &os) const
Print tag info.
static void SetLinkDescription(uint32_t fromNode, uint32_t toNode, std::string linkDescription, std::string fromNodeDescription="", std::string toNodeDescription="")
Helper function to set the description for a link.
void SetXMLOutput()
Specify that animation commands are to be written in XML format.
void SetRandomPosition(bool setRandPos)
Set random position if a Mobility Model does not exists for the node.
~AnimationInterface()
Destructor for the animator interface.
Ptr< UniformRandomVariable > m_uniformRandomVariable
Provides uniform random variables.
static void SetNodeDescription(Ptr< Node > n, std::string descr)
Helper function to set a brief description for a given node.
void StartAnimation(bool restart=false)
Writes the topology information and sets up the appropriate animation packet tx callback.
int64_t AssignStreams(int64_t stream)
AnimationInterface & EnableIpv4RouteTracking(std::string fileName, Time startTime, Time stopTime, Time pollInterval=Seconds(5))
Enable tracking of the Ipv4 routing table for all Nodes.
tag a set of bytes in a packet
Definition: tag.h:36
keep track of a set of node pointers.
void UpdateLinkDescription(uint32_t fromNode, uint32_t toNode, std::string linkDescription)
Helper function to update the description for a link.
an EUI-48 address
Definition: mac48-address.h:41
virtual void Serialize(TagBuffer i) const
Serialize function.
bool SetOutputFile(const std::string &fn)
Specify that animation commands are to be written to the specified output file.
Byte tag using by Anim to uniquely identify packets.
virtual TypeId GetInstanceTypeId(void) const
Get Instance Type Id.
void StopAnimation(bool onlyAnimation=false)
Closes the interface to the animator.
uint64_t Get(void) const
Get Uid in tag.
read and write tag data
Definition: tag-buffer.h:51
Time Seconds(double seconds)
create ns3::Time instances in units of seconds.
Definition: nstime.h:586
AnimationInterface(const std::string filename, uint64_t maxPktsPerFile=MAX_PKTS_PER_TRACE_FILE, bool usingXML=true)
Constructor.
virtual uint32_t GetSerializedSize(void) const
Get Serialized Size.
Interface to network animator.
void SetAnimWriteCallback(AnimWriteCallback cb)
Set a callback function to listen to AnimationInterface write events.
void SetMobilityPollInterval(Time t)
Set mobility poll interval:WARNING: setting a low interval can cause slowness.
A structure to store red, blue and green components for entities such as nodes.
void ShowNode(uint32_t nodeId, bool show=true)
Helper function to show/hide a node.
bool IsStarted(void)
Is AnimationInterface started.
a unique identifier for an interface.
Definition: type-id.h:44
void(* AnimWriteCallback)(const char *str)
typedef for WriteCallBack used for listening to AnimationInterface write messages ...
void UpdateNodeDescription(Ptr< Node > n, std::string descr)
Helper function to update the description for a given node.
AnimPacketInfo helper class.