PyViz: interface to sample packet transmissions (using ns-3 tracing).
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
3 * Copyright (c) 2008 INESC Porto
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;
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.
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
18 * Author: Gustavo Carneiro <gjc@inescporto.pt>
22 #include "ns3/simulator.h"
23 #include "ns3/config.h"
24 #include "ns3/node-list.h"
27 std::vector<std::string>
28 PathSplit (std::string str)
30 std::vector<std::string> results;
32 while ((cutAt = str.find_first_of('/')) != str.npos)
36 results.push_back(str.substr(0,cutAt));
38 str = str.substr(cutAt+1);
42 results.push_back(str);
52 Config::Connect ("/NodeList/*/DeviceList/*/Tx", MakeCallback (&PyViz::TraceNetDevTx, this));
53 Config::Connect ("/NodeList/*/DeviceList/*/Rx", MakeCallback (&PyViz::TraceNetDevRx, this));
61 PyViz::SimulatorRunUntil (Time time)
63 m_transmissionSamples.clear ();
64 while (!Simulator::IsFinished () && Simulator::Now () < time)
66 Simulator::RunOneEvent ();
70 bool PyViz::TransmissionSampleKey::operator < (PyViz::TransmissionSampleKey const &other) const
72 if (transmitter < other.transmitter)
74 if (transmitter > other.transmitter)
76 if (receiver < other.receiver)
78 if (receiver > other.receiver)
80 return (channel < other.channel);
84 PyViz::TraceNetDevTx (std::string context, Ptr<const Packet> packet, Mac48Address address)
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 ())
94 TransmissionSampleKey key = { node, NULL, device->GetChannel() };
95 TransmissionSampleValue &sample = m_transmissionSamples[key];
96 sample.bytes += packet->GetSize ();
100 m_txRecords[packet->GetUid ()] = record;
105 PyViz::TraceNetDevRx (std::string context, Ptr<const Packet> packet, Mac48Address address)
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 ();
114 std::map<uint32_t, TxRecord>::iterator recordIter = m_txRecords.find (packet->GetUid ());
115 if (recordIter == m_txRecords.end ())
120 TxRecord &record = recordIter->second;
122 TransmissionSampleKey key = { record.srcNode, node, channel };
123 TransmissionSampleValue &sample = m_transmissionSamples[key];
124 sample.bytes += packet->GetSize ();
125 m_txRecords.erase (recordIter);
128 PyViz::TransmissionSampleList
129 PyViz::GetTransmissionSamples () const
131 TransmissionSampleList list;
132 for (std::map<TransmissionSampleKey, TransmissionSampleValue>::const_iterator
133 iter = m_transmissionSamples.begin ();
134 iter != m_transmissionSamples.end ();
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);