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 <stdio.h>
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 
121  static bool IsInitialized (void);
122 
134  bool SetOutputFile (const std::string& fn);
135 
143  void SetXMLOutput ();
144 
152  void SetStartTime (Time t);
153 
161  void SetStopTime (Time t);
162 
174  void StartAnimation (bool restart = false);
175 
180  void StopAnimation ();
181 
190  void SetMobilityPollInterval (Time t);
191 
199  void SetRandomPosition (bool setRandPos);
200 
206  typedef void (*AnimWriteCallback) (const char * str);
207 
215 
220  void ResetAnimWriteCallback ();
221 
230  static void SetConstantPosition (Ptr <Node> n, double x, double y, double z=0);
231 
238  static void SetNodeDescription (Ptr <Node> n, std::string descr);
239 
246  void UpdateNodeDescription (Ptr <Node> n, std::string descr);
247 
254  void UpdateNodeDescription (uint32_t nodeId, std::string descr);
255 
262  void ShowNode (uint32_t nodeId, bool show = true);
263 
270  void ShowNode (Ptr <Node> n, bool show = true);
271 
278  static void SetNodeDescription (NodeContainer nc, std::string descr);
279 
288  static void SetNodeColor (Ptr <Node> n, uint8_t r, uint8_t g, uint8_t b);
289 
290 
299  void UpdateNodeColor (Ptr <Node> n, uint8_t r, uint8_t g, uint8_t b);
300 
309  void UpdateNodeColor (uint32_t nodeId, uint8_t r, uint8_t g, uint8_t b);
310 
311 
320  static void SetNodeColor (NodeContainer nc, uint8_t r, uint8_t g, uint8_t b);
321 
331  static void SetLinkDescription (uint32_t fromNode, uint32_t toNode,
332  std::string linkDescription,
333  std::string fromNodeDescription = "",
334  std::string toNodeDescription = "");
335 
345  static void SetLinkDescription (Ptr <Node> fromNode, Ptr <Node> toNode,
346  std::string linkDescription,
347  std::string fromNodeDescription = "",
348  std::string toNodeDescription = "");
349 
350 
358  void UpdateLinkDescription (uint32_t fromNode, uint32_t toNode,
359  std::string linkDescription);
360 
368  void UpdateLinkDescription (Ptr <Node> fromNode, Ptr <Node> toNode,
369  std::string linkDescription);
370 
371 
377  bool IsStarted (void);
378 
385  void ShowAll802_11 (bool showAll);
386 
393  void EnablePacketMetadata (bool enable);
394 
395 
403  uint64_t GetTracePktCount ();
404 
413  int64_t AssignStreams (int64_t stream);
414 
415 private:
416  FILE * m_f; // File handle for output (-1 if none)
417  // Write specified amount of data to the specified handle
418  int WriteN (const char*, uint32_t);
419  bool m_xml; // True if xml format desired
421  std::string m_outputFileName;
423  uint64_t gAnimUid ; // Packet unique identifier used by Animtion
426  bool m_started;
431  std::string m_originalFileName;
432 
433  void DevTxTrace (std::string context,
435  Ptr<NetDevice> tx,
436  Ptr<NetDevice> rx,
437  Time txTime,
438  Time rxTime);
439  void WifiPhyTxBeginTrace (std::string context,
441  void WifiPhyTxEndTrace (std::string context,
443  void WifiPhyTxDropTrace (std::string context,
445  void WifiPhyRxBeginTrace (std::string context,
447  void WifiPhyRxEndTrace (std::string context,
449  void WifiMacRxTrace (std::string context,
451  void WifiPhyRxDropTrace (std::string context,
453  void WimaxTxTrace (std::string context,
455  const Mac48Address &);
456  void WimaxRxTrace (std::string context,
458  const Mac48Address &);
459  void CsmaPhyTxBeginTrace (std::string context,
461  void CsmaPhyTxEndTrace (std::string context,
463  void CsmaPhyRxEndTrace (std::string context,
465  void CsmaMacRxTrace (std::string context,
467 
468  void LteTxTrace (std::string context,
470  const Mac48Address &);
471 
472  void LteRxTrace (std::string context,
474  const Mac48Address &);
475 
476  void LteSpectrumPhyTxStart (std::string context,
478  void LteSpectrumPhyRxStart (std::string context,
480 
481  void UanPhyGenTxTrace (std::string context,
483  void UanPhyGenRxTrace (std::string context,
485 
487 
488  // Write a string to the specified handle;
489  int WriteN (const std::string&);
490 
491  void OutputWirelessPacket (Ptr<const Packet> p, AnimPacketInfo& pktInfo, AnimRxInfo pktrxInfo);
492  void OutputCsmaPacket (Ptr<const Packet> p, AnimPacketInfo& pktInfo, AnimRxInfo pktrxInfo);
493  void MobilityAutoCheck ();
494 
495 
496  std::map<uint64_t, AnimPacketInfo> m_pendingWifiPackets;
497  void AddPendingWifiPacket (uint64_t AnimUid, AnimPacketInfo&);
498  bool WifiPacketIsPending (uint64_t AnimUid);
499 
500  std::map<uint64_t, AnimPacketInfo> m_pendingWimaxPackets;
501  void AddPendingWimaxPacket (uint64_t AnimUid, AnimPacketInfo&);
502  bool WimaxPacketIsPending (uint64_t AnimUid);
503 
504  std::map<uint64_t, AnimPacketInfo> m_pendingLtePackets;
505  void AddPendingLtePacket (uint64_t AnimUid, AnimPacketInfo&);
506  bool LtePacketIsPending (uint64_t AnimUid);
507 
508  std::map<uint64_t, AnimPacketInfo> m_pendingCsmaPackets;
509  void AddPendingCsmaPacket (uint64_t AnimUid, AnimPacketInfo&);
510  bool CsmaPacketIsPending (uint64_t AnimUid);
511 
512  std::map<uint64_t, AnimPacketInfo> m_pendingUanPackets;
513  void AddPendingUanPacket (uint64_t AnimUid, AnimPacketInfo&);
514  bool UanPacketIsPending (uint64_t AnimUid);
515 
517 
518  std::map<uint32_t, Vector> m_nodeLocation;
522  void WriteDummyPacket ();
523  bool NodeHasMoved (Ptr <Node> n, Vector newLocation);
524  void AddMargin ();
525 
526  void PurgePendingWifi ();
527  void PurgePendingWimax ();
528  void PurgePendingLte ();
529  void PurgePendingCsma ();
530 
531  // Recalculate topology bounds
532  void RecalcTopoBounds (Vector v);
533  std::vector < Ptr <Node> > RecalcTopoBounds ();
534 
535  void ConnectCallbacks ();
536  void ConnectLte ();
537  void ConnectLteUe (Ptr <Node> n, Ptr <LteUeNetDevice> nd, uint32_t devIndex);
538  void ConnectLteEnb (Ptr <Node> n, Ptr <LteEnbNetDevice> nd, uint32_t devIndex);
539 
540 
541  std::map <std::string, uint32_t> m_macToNodeIdMap;
542  bool IsInTimeWindow ();
543 
544  // Path helper
545  std::vector<std::string> GetElementsFromContext (std::string context);
546  Ptr <NetDevice> GetNetDeviceFromContext (std::string context);
547 
548  static std::map <uint32_t, Rgb> nodeColors;
549  static std::map <uint32_t, std::string> nodeDescriptions;
550  static std::map <P2pLinkNodeIdPair, LinkProperties, LinkPairCompare> linkProperties;
552 
553  void StartNewTraceFile();
554 
555  std::string GetIpv4Address (Ptr <NetDevice> nd);
556  std::string GetNetAnimVersion ();
557 
558  // XML helpers
559  std::string GetPreamble (void);
560  // Topology element dimensions
561  double m_topoMinX;
562  double m_topoMinY;
563  double m_topoMaxX;
564  double m_topoMaxY;
565 
566  std::string GetPacketMetadata (Ptr<const Packet> p);
567 
568  std::string GetXMLOpen_anim (uint32_t lp);
569  std::string GetXMLOpen_topology (double minX, double minY, double maxX, double maxY);
570  std::string GetXMLOpenClose_node (uint32_t lp, uint32_t id, double locX, double locY);
571  std::string GetXMLOpenClose_node (uint32_t lp, uint32_t id, double locX, double locY, struct Rgb rgb);
572  std::string GetXMLOpenClose_nodeupdate (uint32_t id, bool visible = true);
573  std::string GetXMLOpenClose_link (uint32_t fromLp, uint32_t fromId, uint32_t toLp, uint32_t toId);
574  std::string GetXMLOpenClose_linkupdate (uint32_t fromId, uint32_t toId, std::string);
575  std::string GetXMLOpen_packet (uint32_t fromLp, uint32_t fromId, double fbTx, double lbTx, std::string auxInfo = "");
576  std::string GetXMLOpenClose_rx (uint32_t toLp, uint32_t toId, double fbRx, double lbRx);
577  std::string GetXMLOpen_wpacket (uint32_t fromLp, uint32_t fromId, double fbTx, double lbTx, double range);
578  std::string GetXMLClose (std::string name) {return "</" + name + ">\n"; }
579  std::string GetXMLOpenClose_meta (std::string metaInfo);
580 
583 };
584 
590 struct Rgb
591 {
592  uint8_t r;
593  uint8_t g;
594  uint8_t b;
595 };
596 
608 class AnimByteTag : public Tag
609 {
610 public:
611 
617  static TypeId GetTypeId (void);
618 
624  virtual TypeId GetInstanceTypeId (void) const;
625 
631  virtual uint32_t GetSerializedSize (void) const;
632 
638  virtual void Serialize (TagBuffer i) const;
639 
645  virtual void Deserialize (TagBuffer i);
646 
652  virtual void Print (std::ostream &os) const;
653 
659  void Set (uint64_t AnimUid);
660 
666  uint64_t Get (void) const;
667 
668 private:
669  uint64_t m_AnimUid;
670 };
671 
672 }
673 #endif
674