src/contrib/pyviz.h
author Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
Thu Jan 29 16:46:39 2009 +0000 (12 months ago)
changeset 3911 e070ed523ee9
parent 3887 7809ce96af0d
permissions -rw-r--r--
Visualizer.set_follow_node(node) API (makes camera follow a node)
     1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
     2 /*
     3  * Copyright (c) 2008 INESC Porto
     4  *
     5  * This program is free software; you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License version 2 as
     7  * published by the Free Software Foundation;
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program; if not, write to the Free Software
    16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    17  *
    18  * C++ helper functions for use by the python visualizer (for things
    19  * Python is too slow at).
    20  *
    21  * Author: Gustavo Carneiro  <gjc@inescporto.pt>
    22  */
    23 #ifndef NS3_PYVIZ_H
    24 #define NS3_PYVIZ_H
    25 
    26 #include "ns3/nstime.h"
    27 #include "ns3/event-id.h"
    28 #include "ns3/node.h"
    29 #include "ns3/channel.h"
    30 #include "ns3/packet.h"
    31 #include "ns3/mac48-address.h"
    32 #include <map>
    33 #include <set>
    34 
    35 namespace ns3 {
    36 
    37 class PyViz
    38 {
    39 public:
    40   PyViz ();
    41   ~PyViz ();
    42 
    43   void RegisterDropTracePath (std::string const &tracePath);
    44 
    45   void RegisterCsmaLikeDevice (std::string const &deviceTypeName);
    46   void RegisterWifiLikeDevice (std::string const &deviceTypeName);
    47   void RegisterPointToPointLikeDevice (std::string const &deviceTypeName);
    48 
    49   // Run simulation until a given (simulated, absolute) time is reached
    50   void SimulatorRunUntil (Time time);
    51 
    52   static void Pause (std::string const &message);  
    53   std::vector<std::string> GetPauseMessages () const;
    54 
    55   struct TransmissionSample
    56   {
    57     Ptr<Node> transmitter;
    58     Ptr<Node> receiver; // NULL if broadcast
    59     Ptr<Channel> channel;
    60     uint32_t bytes;
    61   };
    62   typedef std::vector<TransmissionSample> TransmissionSampleList;
    63   TransmissionSampleList GetTransmissionSamples () const;
    64 
    65   struct PacketDropSample
    66   {
    67     Ptr<Node> transmitter;
    68     uint32_t bytes;
    69   };
    70   typedef std::vector<PacketDropSample> PacketDropSampleList;
    71   PacketDropSampleList GetPacketDropSamples () const;
    72 
    73 
    74   struct PacketSample
    75   {
    76     Time time;
    77     Ptr<Packet> packet;
    78     Ptr<NetDevice> device;
    79   };
    80   struct TxPacketSample : PacketSample
    81   {
    82     Mac48Address to;
    83   };
    84   struct RxPacketSample : PacketSample
    85   {
    86     Mac48Address from;
    87   };
    88 
    89   struct LastPacketsSample
    90   {
    91     std::vector<RxPacketSample> lastReceivedPackets;
    92     std::vector<TxPacketSample> lastTransmittedPackets;
    93     std::vector<PacketSample> lastDroppedPackets;
    94   };
    95   LastPacketsSample GetLastPackets (uint32_t nodeId) const;
    96 
    97 
    98   void SetNodesOfInterest (std::set<uint32_t> nodes);
    99 
   100   struct NetDeviceStatistics
   101   {
   102     NetDeviceStatistics () : transmittedBytes (0), receivedBytes (0),
   103                              transmittedPackets (0), receivedPackets (0) {}
   104     uint64_t transmittedBytes;
   105     uint64_t receivedBytes;
   106     uint32_t transmittedPackets;
   107     uint32_t receivedPackets;
   108   };
   109 
   110   struct NodeStatistics
   111   {
   112     uint32_t nodeId;
   113     std::vector<NetDeviceStatistics> statistics;
   114   };
   115   
   116   std::vector<NodeStatistics> GetNodesStatistics () const;
   117 
   118   enum PacketCaptureMode {
   119     PACKET_CAPTURE_DISABLED=1, // packet capture is disabled
   120     PACKET_CAPTURE_FILTER_HEADERS_OR, // packet capture if any of the indicated headers is present
   121     PACKET_CAPTURE_FILTER_HEADERS_AND, // packet capture if all of the indicated headers are present
   122   };
   123 
   124   struct PacketCaptureOptions
   125   {
   126     std::set<TypeId> headers;
   127     uint32_t numLastPackets;
   128     PacketCaptureMode mode;
   129   };
   130 
   131   void SetPacketCaptureOptions (uint32_t nodeId, PacketCaptureOptions options);
   132 
   133 
   134   // Yes, I know, this is just a utility function, not really related to the class in any way.
   135 
   136   // -#- @lineX1(direction=inout); @lineY1(direction=inout); @lineX2(direction=inout); @lineY2(direction=inout) -#-
   137   static void LineClipping (double boundsX1, double boundsY1, double boundsX2, double boundsY2, double &lineX1, double &lineY1, double &lineX2, double &lineY2); // don't break this line or pybindgen will not be able to pick up the above annotation :(
   138   
   139 
   140 private:
   141 
   142   bool GetPacketCaptureOptions (uint32_t nodeId, const PacketCaptureOptions **outOptions) const;
   143   static bool FilterPacket (Ptr<const Packet> packet, const PacketCaptureOptions &options);
   144 
   145 
   146   typedef std::pair<Ptr<Channel>, uint32_t> TxRecordKey;
   147 
   148   struct TxRecordValue
   149   {
   150     Time time;
   151     Ptr<Node> srcNode;
   152     bool isBroadcast;
   153   };
   154 
   155   struct TransmissionSampleKey
   156   {
   157     bool operator < (TransmissionSampleKey const &other) const;
   158     bool operator == (TransmissionSampleKey const &other) const;
   159     Ptr<Node> transmitter;
   160     Ptr<Node> receiver; // NULL if broadcast
   161     Ptr<Channel> channel;
   162   };
   163 
   164   struct TransmissionSampleValue
   165   {
   166     uint32_t bytes;
   167   };
   168 
   169   // data
   170   std::map<uint32_t, PacketCaptureOptions> m_packetCaptureOptions;
   171   std::vector<std::string> m_pauseMessages;
   172   std::map<TxRecordKey, TxRecordValue> m_txRecords;
   173   std::map<TransmissionSampleKey, TransmissionSampleValue> m_transmissionSamples;
   174   std::map<Ptr<Node>, uint32_t> m_packetDrops;
   175   std::set<uint32_t> m_nodesOfInterest; // list of node IDs whose transmissions will be monitored
   176   std::map<uint32_t, Time> m_packetsOfInterest; // list of packet UIDs that will be monitored
   177   std::map<uint32_t, LastPacketsSample> m_lastPackets;
   178   std::map<uint32_t, std::vector<NetDeviceStatistics> > m_nodesStatistics;
   179 
   180   // Trace callbacks
   181   void TraceNetDevTxWifi (std::string context, Ptr<const Packet> packet, Mac48Address address);
   182   void TraceNetDevRxWifi (std::string context, Ptr<const Packet> packet, Mac48Address address);
   183 
   184   void TraceDrop (std::string context, Ptr<const Packet> packet);
   185 
   186   void TraceNetDevTxCsma (std::string context, Ptr<const Packet> packet);
   187   void TraceNetDevRxCsma (std::string context, Ptr<const Packet> packet);
   188   void TraceNetDevPromiscRxCsma (std::string context, Ptr<const Packet> packet, NetDevice::PacketType packetType);
   189 
   190   void TraceNetDevTxPointToPoint (std::string context, Ptr<const Packet> packet);
   191   void TraceNetDevRxPointToPoint (std::string context, Ptr<const Packet> packet);
   192 
   193   inline NetDeviceStatistics & FindNetDeviceStatistics (int node, int interface);
   194 
   195   void DoPause (std::string const &message);
   196 
   197   bool m_stop;
   198   EventId m_stopCallbackEvent;
   199   void CallbackStopSimulation ();
   200 };
   201 
   202 
   203 }
   204 
   205 #endif /* NS3_PYVIZ_H */