src/contrib/pyviz.cc
author Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
Wed Sep 03 14:26:49 2008 +0100 (2008-09-03)
changeset 3595 6eccb090137c
parent 3530 57af5f5bb3fa
child 3615 c9d147a4de9c
permissions -rw-r--r--
PyViz: interface to sample packet transmissions (using ns-3 tracing).
     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  * Author: Gustavo Carneiro  <gjc@inescporto.pt>
    19  */
    20 
    21 #include "pyviz.h"
    22 #include "ns3/simulator.h"
    23 #include "ns3/config.h"
    24 #include "ns3/node-list.h"
    25 
    26 static
    27 std::vector<std::string>
    28 PathSplit (std::string str)
    29 {
    30   std::vector<std::string> results;
    31   size_t cutAt;
    32   while ((cutAt = str.find_first_of('/')) != str.npos)
    33     {
    34       if(cutAt > 0)
    35         {
    36           results.push_back(str.substr(0,cutAt));
    37         }
    38       str = str.substr(cutAt+1);
    39     }
    40   if (str.length() > 0)
    41     {
    42       results.push_back(str);
    43     }
    44   return results;
    45 }
    46 
    47 
    48 namespace ns3 {
    49 
    50 PyViz::PyViz ()
    51 {
    52   Config::Connect ("/NodeList/*/DeviceList/*/Tx", MakeCallback (&PyViz::TraceNetDevTx, this));
    53   Config::Connect ("/NodeList/*/DeviceList/*/Rx", MakeCallback (&PyViz::TraceNetDevRx, this));
    54 }
    55 
    56 PyViz::~PyViz ()
    57 {
    58 }
    59     
    60 void
    61 PyViz::SimulatorRunUntil (Time time)
    62 {
    63   m_transmissionSamples.clear ();
    64   while (!Simulator::IsFinished () && Simulator::Now () < time)
    65     {
    66       Simulator::RunOneEvent ();
    67     }
    68 }
    69 
    70 bool PyViz::TransmissionSampleKey::operator < (PyViz::TransmissionSampleKey const &other) const
    71 {
    72   if (transmitter < other.transmitter)
    73     return true;
    74   if (transmitter > other.transmitter)
    75     return false;
    76   if (receiver < other.receiver)
    77     return true;
    78   if (receiver > other.receiver)
    79     return false;
    80   return (channel < other.channel);
    81 }
    82 
    83 void
    84 PyViz::TraceNetDevTx (std::string context, Ptr<const Packet> packet, Mac48Address address)
    85 {
    86   std::vector<std::string> splitPath = PathSplit (context);
    87   int nodeIndex = atoi (splitPath[1].c_str ());
    88   int devIndex = atoi (splitPath[3].c_str ());
    89   Ptr<Node> node = NodeList::GetNode (nodeIndex);
    90   Ptr<NetDevice> device = node->GetDevice (devIndex);
    91   TxRecord record = { node };
    92   if (address == device->GetBroadcast ())
    93     {
    94       TransmissionSampleKey key = { node, NULL, device->GetChannel() };
    95       TransmissionSampleValue &sample = m_transmissionSamples[key];
    96       sample.bytes += packet->GetSize ();
    97     }
    98   else
    99     {
   100       m_txRecords[packet->GetUid ()] = record;
   101     }
   102 }
   103 
   104 void
   105 PyViz::TraceNetDevRx (std::string context, Ptr<const Packet> packet, Mac48Address address)
   106 {
   107   std::vector<std::string> splitPath = PathSplit (context);
   108   int nodeIndex = atoi (splitPath[1].c_str ());
   109   int devIndex = atoi (splitPath[3].c_str ());
   110   Ptr<Node> node = NodeList::GetNode (nodeIndex);
   111   Ptr<NetDevice> device = node->GetDevice (devIndex);
   112   Ptr<Channel> channel = device->GetChannel ();
   113 
   114   std::map<uint32_t, TxRecord>::iterator recordIter = m_txRecords.find (packet->GetUid ());
   115   if (recordIter == m_txRecords.end ())
   116     {
   117       return;
   118     }
   119   
   120   TxRecord &record = recordIter->second;
   121   
   122   TransmissionSampleKey key = { record.srcNode, node, channel };
   123   TransmissionSampleValue &sample = m_transmissionSamples[key];
   124   sample.bytes += packet->GetSize ();
   125   m_txRecords.erase (recordIter);
   126 }
   127 
   128 PyViz::TransmissionSampleList
   129 PyViz::GetTransmissionSamples () const
   130 {
   131   TransmissionSampleList list;
   132   for (std::map<TransmissionSampleKey, TransmissionSampleValue>::const_iterator
   133          iter = m_transmissionSamples.begin ();
   134        iter !=  m_transmissionSamples.end ();
   135        iter++)
   136     {
   137       TransmissionSample sample;
   138       sample.transmitter = iter->first.transmitter;
   139       sample.receiver = iter->first.receiver;
   140       sample.channel = iter->first.channel;
   141       sample.bytes = iter->second.bytes;
   142       list.push_back (sample);
   143     }
   144   return list;
   145 }
   146 
   147 }
   148