PyViz: interface to sample packet transmissions (using ns-3 tracing).
authorGustavo J. A. M. Carneiro <gjc@inescporto.pt>
Wed Sep 03 14:26:49 2008 +0100 (17 months ago)
changeset 35956eccb090137c
parent 3594 e965ed757e92
child 3611 ae00f2970d3b
PyViz: interface to sample packet transmissions (using ns-3 tracing).
src/contrib/pyviz.cc
src/contrib/pyviz.h
     1.1 --- a/src/contrib/pyviz.cc	Tue Sep 02 15:12:00 2008 +0100
     1.2 +++ b/src/contrib/pyviz.cc	Wed Sep 03 14:26:49 2008 +0100
     1.3 @@ -20,11 +20,37 @@
     1.4  
     1.5  #include "pyviz.h"
     1.6  #include "ns3/simulator.h"
     1.7 +#include "ns3/config.h"
     1.8 +#include "ns3/node-list.h"
     1.9 +
    1.10 +static
    1.11 +std::vector<std::string>
    1.12 +PathSplit (std::string str)
    1.13 +{
    1.14 +  std::vector<std::string> results;
    1.15 +  size_t cutAt;
    1.16 +  while ((cutAt = str.find_first_of('/')) != str.npos)
    1.17 +    {
    1.18 +      if(cutAt > 0)
    1.19 +        {
    1.20 +          results.push_back(str.substr(0,cutAt));
    1.21 +        }
    1.22 +      str = str.substr(cutAt+1);
    1.23 +    }
    1.24 +  if (str.length() > 0)
    1.25 +    {
    1.26 +      results.push_back(str);
    1.27 +    }
    1.28 +  return results;
    1.29 +}
    1.30 +
    1.31  
    1.32  namespace ns3 {
    1.33  
    1.34  PyViz::PyViz ()
    1.35  {
    1.36 +  Config::Connect ("/NodeList/*/DeviceList/*/Tx", MakeCallback (&PyViz::TraceNetDevTx, this));
    1.37 +  Config::Connect ("/NodeList/*/DeviceList/*/Rx", MakeCallback (&PyViz::TraceNetDevRx, this));
    1.38  }
    1.39  
    1.40  PyViz::~PyViz ()
    1.41 @@ -34,11 +60,89 @@
    1.42  void
    1.43  PyViz::SimulatorRunUntil (Time time)
    1.44  {
    1.45 +  m_transmissionSamples.clear ();
    1.46    while (!Simulator::IsFinished () && Simulator::Now () < time)
    1.47      {
    1.48        Simulator::RunOneEvent ();
    1.49      }
    1.50  }
    1.51  
    1.52 +bool PyViz::TransmissionSampleKey::operator < (PyViz::TransmissionSampleKey const &other) const
    1.53 +{
    1.54 +  if (transmitter < other.transmitter)
    1.55 +    return true;
    1.56 +  if (transmitter > other.transmitter)
    1.57 +    return false;
    1.58 +  if (receiver < other.receiver)
    1.59 +    return true;
    1.60 +  if (receiver > other.receiver)
    1.61 +    return false;
    1.62 +  return (channel < other.channel);
    1.63  }
    1.64  
    1.65 +void
    1.66 +PyViz::TraceNetDevTx (std::string context, Ptr<const Packet> packet, Mac48Address address)
    1.67 +{
    1.68 +  std::vector<std::string> splitPath = PathSplit (context);
    1.69 +  int nodeIndex = atoi (splitPath[1].c_str ());
    1.70 +  int devIndex = atoi (splitPath[3].c_str ());
    1.71 +  Ptr<Node> node = NodeList::GetNode (nodeIndex);
    1.72 +  Ptr<NetDevice> device = node->GetDevice (devIndex);
    1.73 +  TxRecord record = { node };
    1.74 +  if (address == device->GetBroadcast ())
    1.75 +    {
    1.76 +      TransmissionSampleKey key = { node, NULL, device->GetChannel() };
    1.77 +      TransmissionSampleValue &sample = m_transmissionSamples[key];
    1.78 +      sample.bytes += packet->GetSize ();
    1.79 +    }
    1.80 +  else
    1.81 +    {
    1.82 +      m_txRecords[packet->GetUid ()] = record;
    1.83 +    }
    1.84 +}
    1.85 +
    1.86 +void
    1.87 +PyViz::TraceNetDevRx (std::string context, Ptr<const Packet> packet, Mac48Address address)
    1.88 +{
    1.89 +  std::vector<std::string> splitPath = PathSplit (context);
    1.90 +  int nodeIndex = atoi (splitPath[1].c_str ());
    1.91 +  int devIndex = atoi (splitPath[3].c_str ());
    1.92 +  Ptr<Node> node = NodeList::GetNode (nodeIndex);
    1.93 +  Ptr<NetDevice> device = node->GetDevice (devIndex);
    1.94 +  Ptr<Channel> channel = device->GetChannel ();
    1.95 +
    1.96 +  std::map<uint32_t, TxRecord>::iterator recordIter = m_txRecords.find (packet->GetUid ());
    1.97 +  if (recordIter == m_txRecords.end ())
    1.98 +    {
    1.99 +      return;
   1.100 +    }
   1.101 +  
   1.102 +  TxRecord &record = recordIter->second;
   1.103 +  
   1.104 +  TransmissionSampleKey key = { record.srcNode, node, channel };
   1.105 +  TransmissionSampleValue &sample = m_transmissionSamples[key];
   1.106 +  sample.bytes += packet->GetSize ();
   1.107 +  m_txRecords.erase (recordIter);
   1.108 +}
   1.109 +
   1.110 +PyViz::TransmissionSampleList
   1.111 +PyViz::GetTransmissionSamples () const
   1.112 +{
   1.113 +  TransmissionSampleList list;
   1.114 +  for (std::map<TransmissionSampleKey, TransmissionSampleValue>::const_iterator
   1.115 +         iter = m_transmissionSamples.begin ();
   1.116 +       iter !=  m_transmissionSamples.end ();
   1.117 +       iter++)
   1.118 +    {
   1.119 +      TransmissionSample sample;
   1.120 +      sample.transmitter = iter->first.transmitter;
   1.121 +      sample.receiver = iter->first.receiver;
   1.122 +      sample.channel = iter->first.channel;
   1.123 +      sample.bytes = iter->second.bytes;
   1.124 +      list.push_back (sample);
   1.125 +    }
   1.126 +  return list;
   1.127 +}
   1.128 +
   1.129 +}
   1.130 +
     2.1 --- a/src/contrib/pyviz.h	Tue Sep 02 15:12:00 2008 +0100
     2.2 +++ b/src/contrib/pyviz.h	Wed Sep 03 14:26:49 2008 +0100
     2.3 @@ -24,6 +24,11 @@
     2.4  #define NS3_PYVIZ_H
     2.5  
     2.6  #include "ns3/nstime.h"
     2.7 +#include "ns3/node.h"
     2.8 +#include "ns3/channel.h"
     2.9 +#include "ns3/packet.h"
    2.10 +#include "ns3/mac48-address.h"
    2.11 +#include <map>
    2.12  
    2.13  namespace ns3 {
    2.14  
    2.15 @@ -35,7 +40,46 @@
    2.16    
    2.17    // Run simulation until a given (simulated, absolute) time is reached
    2.18    void SimulatorRunUntil (Time time);
    2.19 -  
    2.20 +
    2.21 +  struct TransmissionSample
    2.22 +  {
    2.23 +    Ptr<Node> transmitter;
    2.24 +    Ptr<Node> receiver; // NULL if broadcast
    2.25 +    Ptr<Channel> channel;
    2.26 +    uint32_t bytes;
    2.27 +  };
    2.28 +
    2.29 +  typedef std::vector<TransmissionSample> TransmissionSampleList;
    2.30 +
    2.31 +  TransmissionSampleList GetTransmissionSamples () const;
    2.32 +
    2.33 +private:
    2.34 +
    2.35 +  struct TxRecord
    2.36 +  {
    2.37 +    Ptr<Node> srcNode;
    2.38 +  };
    2.39 +  std::map<uint32_t, TxRecord> m_txRecords;
    2.40 +
    2.41 +  struct TransmissionSampleKey
    2.42 +  {
    2.43 +    bool operator < (TransmissionSampleKey const &other) const;
    2.44 +    Ptr<Node> transmitter;
    2.45 +    Ptr<Node> receiver; // NULL if broadcast
    2.46 +    Ptr<Channel> channel;
    2.47 +  };
    2.48 +
    2.49 +  struct TransmissionSampleValue
    2.50 +  {
    2.51 +    TransmissionSampleValue () : bytes (0) {}
    2.52 +    uint32_t bytes;
    2.53 +  };
    2.54 +
    2.55 +  std::map<TransmissionSampleKey, TransmissionSampleValue> m_transmissionSamples;
    2.56 +
    2.57 +  void TraceNetDevTx (std::string context, Ptr<const Packet> packet, Mac48Address address);
    2.58 +  void TraceNetDevRx (std::string context, Ptr<const Packet> packet, Mac48Address address);
    2.59 +
    2.60  };
    2.61  
    2.62