1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
3 * Copyright (c) 2008 University of Washington
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
22 #include "ns3/simulator.h"
23 #include "ns3/object-factory.h"
24 #include "ns3/names.h"
25 #include "ns3/queue.h"
26 #include "ns3/emu-net-device.h"
27 #include "ns3/pcap-writer.h"
28 #include "ns3/config.h"
29 #include "ns3/packet.h"
31 #include "emu-helper.h"
33 NS_LOG_COMPONENT_DEFINE ("EmuHelper");
37 EmuHelper::EmuHelper ()
39 NS_LOG_FUNCTION_NOARGS ();
40 m_queueFactory.SetTypeId ("ns3::DropTailQueue");
41 m_deviceFactory.SetTypeId ("ns3::EmuNetDevice");
47 std::string n1, const AttributeValue &v1,
48 std::string n2, const AttributeValue &v2,
49 std::string n3, const AttributeValue &v3,
50 std::string n4, const AttributeValue &v4)
52 NS_LOG_FUNCTION_NOARGS ();
53 m_queueFactory.SetTypeId (type);
54 m_queueFactory.Set (n1, v1);
55 m_queueFactory.Set (n2, v2);
56 m_queueFactory.Set (n3, v3);
57 m_queueFactory.Set (n4, v4);
61 EmuHelper::SetAttribute (std::string n1, const AttributeValue &v1)
63 NS_LOG_FUNCTION_NOARGS ();
64 m_deviceFactory.Set (n1, v1);
68 EmuHelper::EnablePcap (std::string filename, uint32_t nodeid, uint32_t deviceid, bool promiscuous)
70 NS_LOG_FUNCTION (filename << nodeid << deviceid << promiscuous);
71 std::ostringstream oss;
72 oss << filename << "-" << nodeid << "-" << deviceid << ".pcap";
73 Ptr<PcapWriter> pcap = CreateObject<PcapWriter> ();
74 pcap->Open (oss.str ());
75 pcap->WriteEthernetHeader ();
78 oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid;
81 oss << "/$ns3::EmuNetDevice/PromiscSniffer";
85 oss << "/$ns3::EmuNetDevice/Sniffer";
87 Config::ConnectWithoutContext (oss.str (), MakeBoundCallback (&EmuHelper::SniffEvent, pcap));
91 EmuHelper::EnablePcap (std::string filename, Ptr<NetDevice> nd, bool promiscuous)
93 NS_LOG_FUNCTION (filename << &nd << promiscuous);
94 EnablePcap (filename, nd->GetNode ()->GetId (), nd->GetIfIndex (), promiscuous);
98 EmuHelper::EnablePcap (std::string filename, std::string ndName, bool promiscuous)
100 NS_LOG_FUNCTION (filename << ndName << promiscuous);
101 Ptr<NetDevice> nd = Names::Find<NetDevice> (ndName);
102 EnablePcap (filename, nd->GetNode ()->GetId (), nd->GetIfIndex (), promiscuous);
106 EmuHelper::EnablePcap (std::string filename, NetDeviceContainer d, bool promiscuous)
108 NS_LOG_FUNCTION (filename << &d << promiscuous);
109 for (NetDeviceContainer::Iterator i = d.Begin (); i != d.End (); ++i)
111 Ptr<NetDevice> dev = *i;
112 EnablePcap (filename, dev->GetNode ()->GetId (), dev->GetIfIndex (), promiscuous);
117 EmuHelper::EnablePcap (std::string filename, NodeContainer n, bool promiscuous)
119 NS_LOG_FUNCTION (filename << &n << promiscuous);
120 NetDeviceContainer devs;
121 for (NodeContainer::Iterator i = n.Begin (); i != n.End (); ++i)
124 for (uint32_t j = 0; j < node->GetNDevices (); ++j)
126 devs.Add (node->GetDevice (j));
129 EnablePcap (filename, devs, promiscuous);
133 EmuHelper::EnablePcapAll (std::string filename, bool promiscuous)
135 NS_LOG_FUNCTION (filename << promiscuous);
136 EnablePcap (filename, NodeContainer::GetGlobal (), promiscuous);
140 EmuHelper::EnableAscii (std::ostream &os, uint32_t nodeid, uint32_t deviceid)
142 NS_LOG_FUNCTION (&os << nodeid << deviceid);
143 Packet::EnablePrinting ();
144 std::ostringstream oss;
146 oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::EmuNetDevice/MacRx";
147 Config::Connect (oss.str (), MakeBoundCallback (&EmuHelper::AsciiRxEvent, &os));
150 oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::EmuNetDevice/TxQueue/Enqueue";
151 Config::Connect (oss.str (), MakeBoundCallback (&EmuHelper::AsciiEnqueueEvent, &os));
154 oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::EmuNetDevice/TxQueue/Dequeue";
155 Config::Connect (oss.str (), MakeBoundCallback (&EmuHelper::AsciiDequeueEvent, &os));
158 oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::EmuNetDevice/TxQueue/Drop";
159 Config::Connect (oss.str (), MakeBoundCallback (&EmuHelper::AsciiDropEvent, &os));
163 EmuHelper::EnableAscii (std::ostream &os, NetDeviceContainer d)
165 NS_LOG_FUNCTION (&os << &d);
166 for (NetDeviceContainer::Iterator i = d.Begin (); i != d.End (); ++i)
168 Ptr<NetDevice> dev = *i;
169 EnableAscii (os, dev->GetNode ()->GetId (), dev->GetIfIndex ());
174 EmuHelper::EnableAscii (std::ostream &os, NodeContainer n)
176 NS_LOG_FUNCTION (&os << &n);
177 NetDeviceContainer devs;
178 for (NodeContainer::Iterator i = n.Begin (); i != n.End (); ++i)
181 for (uint32_t j = 0; j < node->GetNDevices (); ++j)
183 devs.Add (node->GetDevice (j));
186 EnableAscii (os, devs);
190 EmuHelper::EnableAsciiAll (std::ostream &os)
192 NS_LOG_FUNCTION (&os);
193 EnableAscii (os, NodeContainer::GetGlobal ());
197 EmuHelper::Install (Ptr<Node> node) const
199 return NetDeviceContainer (InstallPriv (node));
203 EmuHelper::Install (std::string nodeName) const
205 Ptr<Node> node = Names::Find<Node> (nodeName);
206 return NetDeviceContainer (InstallPriv (node));
210 EmuHelper::Install (const NodeContainer &c) const
212 NetDeviceContainer devs;
214 for (NodeContainer::Iterator i = c.Begin (); i != c.End (); i++)
216 devs.Add (InstallPriv (*i));
223 EmuHelper::InstallPriv (Ptr<Node> node) const
225 Ptr<EmuNetDevice> device = m_deviceFactory.Create<EmuNetDevice> ();
226 device->SetAddress (Mac48Address::Allocate ());
227 node->AddDevice (device);
228 Ptr<Queue> queue = m_queueFactory.Create<Queue> ();
229 device->SetQueue (queue);
235 EmuHelper::SniffEvent (Ptr<PcapWriter> writer, Ptr<const Packet> packet)
237 NS_LOG_FUNCTION (writer << packet);
238 writer->WritePacket (packet);
242 EmuHelper::AsciiEnqueueEvent (
245 Ptr<const Packet> packet)
247 NS_LOG_FUNCTION (&os << path << packet);
248 *os << "+ " << Simulator::Now ().GetSeconds () << " ";
249 *os << path << " " << *packet << std::endl;
253 EmuHelper::AsciiDequeueEvent (
256 Ptr<const Packet> packet)
258 NS_LOG_FUNCTION (&os << path << packet);
259 *os << "- " << Simulator::Now ().GetSeconds () << " ";
260 *os << path << " " << *packet << std::endl;
264 EmuHelper::AsciiDropEvent (
267 Ptr<const Packet> packet)
269 NS_LOG_FUNCTION (&os << path << packet);
270 *os << "d " << Simulator::Now ().GetSeconds () << " ";
271 *os << path << " " << *packet << std::endl;
275 EmuHelper::AsciiRxEvent (
278 Ptr<const Packet> packet)
280 NS_LOG_FUNCTION (&os << path << packet);
281 *os << "r " << Simulator::Now ().GetSeconds () << " ";
282 *os << path << " " << *packet << std::endl;