src/contrib/pyviz.cc
changeset 3595 6eccb090137c
parent 3530 57af5f5bb3fa
child 3615 c9d147a4de9c
     1.1 --- a/src/contrib/pyviz.cc	Fri Aug 08 12:25:08 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 +