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).
gjc@3519
     1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
gjc@3519
     2
/*
gjc@3519
     3
 * Copyright (c) 2008 INESC Porto
gjc@3519
     4
 *
gjc@3519
     5
 * This program is free software; you can redistribute it and/or modify
gjc@3519
     6
 * it under the terms of the GNU General Public License version 2 as
gjc@3519
     7
 * published by the Free Software Foundation;
gjc@3519
     8
 *
gjc@3519
     9
 * This program is distributed in the hope that it will be useful,
gjc@3519
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
gjc@3519
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
gjc@3519
    12
 * GNU General Public License for more details.
gjc@3519
    13
 *
gjc@3519
    14
 * You should have received a copy of the GNU General Public License
gjc@3519
    15
 * along with this program; if not, write to the Free Software
gjc@3519
    16
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
gjc@3519
    17
 *
gjc@3519
    18
 * Author: Gustavo Carneiro  <gjc@inescporto.pt>
gjc@3519
    19
 */
gjc@3519
    20
gjc@3519
    21
#include "pyviz.h"
gjc@3519
    22
#include "ns3/simulator.h"
gjc@3595
    23
#include "ns3/config.h"
gjc@3595
    24
#include "ns3/node-list.h"
gjc@3595
    25
gjc@3595
    26
static
gjc@3595
    27
std::vector<std::string>
gjc@3595
    28
PathSplit (std::string str)
gjc@3595
    29
{
gjc@3595
    30
  std::vector<std::string> results;
gjc@3595
    31
  size_t cutAt;
gjc@3595
    32
  while ((cutAt = str.find_first_of('/')) != str.npos)
gjc@3595
    33
    {
gjc@3595
    34
      if(cutAt > 0)
gjc@3595
    35
        {
gjc@3595
    36
          results.push_back(str.substr(0,cutAt));
gjc@3595
    37
        }
gjc@3595
    38
      str = str.substr(cutAt+1);
gjc@3595
    39
    }
gjc@3595
    40
  if (str.length() > 0)
gjc@3595
    41
    {
gjc@3595
    42
      results.push_back(str);
gjc@3595
    43
    }
gjc@3595
    44
  return results;
gjc@3595
    45
}
gjc@3595
    46
gjc@3519
    47
gjc@3530
    48
namespace ns3 {
gjc@3530
    49
gjc@3530
    50
PyViz::PyViz ()
gjc@3530
    51
{
gjc@3595
    52
  Config::Connect ("/NodeList/*/DeviceList/*/Tx", MakeCallback (&PyViz::TraceNetDevTx, this));
gjc@3595
    53
  Config::Connect ("/NodeList/*/DeviceList/*/Rx", MakeCallback (&PyViz::TraceNetDevRx, this));
gjc@3530
    54
}
gjc@3530
    55
gjc@3530
    56
PyViz::~PyViz ()
gjc@3530
    57
{
gjc@3530
    58
}
gjc@3519
    59
    
gjc@3519
    60
void
gjc@3530
    61
PyViz::SimulatorRunUntil (Time time)
gjc@3519
    62
{
gjc@3595
    63
  m_transmissionSamples.clear ();
gjc@3521
    64
  while (!Simulator::IsFinished () && Simulator::Now () < time)
gjc@3519
    65
    {
gjc@3519
    66
      Simulator::RunOneEvent ();
gjc@3519
    67
    }
gjc@3519
    68
}
gjc@3519
    69
gjc@3595
    70
bool PyViz::TransmissionSampleKey::operator < (PyViz::TransmissionSampleKey const &other) const
gjc@3595
    71
{
gjc@3595
    72
  if (transmitter < other.transmitter)
gjc@3595
    73
    return true;
gjc@3595
    74
  if (transmitter > other.transmitter)
gjc@3595
    75
    return false;
gjc@3595
    76
  if (receiver < other.receiver)
gjc@3595
    77
    return true;
gjc@3595
    78
  if (receiver > other.receiver)
gjc@3595
    79
    return false;
gjc@3595
    80
  return (channel < other.channel);
gjc@3530
    81
}
gjc@3519
    82
gjc@3595
    83
void
gjc@3595
    84
PyViz::TraceNetDevTx (std::string context, Ptr<const Packet> packet, Mac48Address address)
gjc@3595
    85
{
gjc@3595
    86
  std::vector<std::string> splitPath = PathSplit (context);
gjc@3595
    87
  int nodeIndex = atoi (splitPath[1].c_str ());
gjc@3595
    88
  int devIndex = atoi (splitPath[3].c_str ());
gjc@3595
    89
  Ptr<Node> node = NodeList::GetNode (nodeIndex);
gjc@3595
    90
  Ptr<NetDevice> device = node->GetDevice (devIndex);
gjc@3595
    91
  TxRecord record = { node };
gjc@3595
    92
  if (address == device->GetBroadcast ())
gjc@3595
    93
    {
gjc@3595
    94
      TransmissionSampleKey key = { node, NULL, device->GetChannel() };
gjc@3595
    95
      TransmissionSampleValue &sample = m_transmissionSamples[key];
gjc@3595
    96
      sample.bytes += packet->GetSize ();
gjc@3595
    97
    }
gjc@3595
    98
  else
gjc@3595
    99
    {
gjc@3595
   100
      m_txRecords[packet->GetUid ()] = record;
gjc@3595
   101
    }
gjc@3595
   102
}
gjc@3595
   103
gjc@3595
   104
void
gjc@3595
   105
PyViz::TraceNetDevRx (std::string context, Ptr<const Packet> packet, Mac48Address address)
gjc@3595
   106
{
gjc@3595
   107
  std::vector<std::string> splitPath = PathSplit (context);
gjc@3595
   108
  int nodeIndex = atoi (splitPath[1].c_str ());
gjc@3595
   109
  int devIndex = atoi (splitPath[3].c_str ());
gjc@3595
   110
  Ptr<Node> node = NodeList::GetNode (nodeIndex);
gjc@3595
   111
  Ptr<NetDevice> device = node->GetDevice (devIndex);
gjc@3595
   112
  Ptr<Channel> channel = device->GetChannel ();
gjc@3595
   113
gjc@3595
   114
  std::map<uint32_t, TxRecord>::iterator recordIter = m_txRecords.find (packet->GetUid ());
gjc@3595
   115
  if (recordIter == m_txRecords.end ())
gjc@3595
   116
    {
gjc@3595
   117
      return;
gjc@3595
   118
    }
gjc@3595
   119
  
gjc@3595
   120
  TxRecord &record = recordIter->second;
gjc@3595
   121
  
gjc@3595
   122
  TransmissionSampleKey key = { record.srcNode, node, channel };
gjc@3595
   123
  TransmissionSampleValue &sample = m_transmissionSamples[key];
gjc@3595
   124
  sample.bytes += packet->GetSize ();
gjc@3595
   125
  m_txRecords.erase (recordIter);
gjc@3595
   126
}
gjc@3595
   127
gjc@3595
   128
PyViz::TransmissionSampleList
gjc@3595
   129
PyViz::GetTransmissionSamples () const
gjc@3595
   130
{
gjc@3595
   131
  TransmissionSampleList list;
gjc@3595
   132
  for (std::map<TransmissionSampleKey, TransmissionSampleValue>::const_iterator
gjc@3595
   133
         iter = m_transmissionSamples.begin ();
gjc@3595
   134
       iter !=  m_transmissionSamples.end ();
gjc@3595
   135
       iter++)
gjc@3595
   136
    {
gjc@3595
   137
      TransmissionSample sample;
gjc@3595
   138
      sample.transmitter = iter->first.transmitter;
gjc@3595
   139
      sample.receiver = iter->first.receiver;
gjc@3595
   140
      sample.channel = iter->first.channel;
gjc@3595
   141
      sample.bytes = iter->second.bytes;
gjc@3595
   142
      list.push_back (sample);
gjc@3595
   143
    }
gjc@3595
   144
  return list;
gjc@3595
   145
}
gjc@3595
   146
gjc@3595
   147
}
gjc@3595
   148