1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
3 * Copyright (c) 2008 INRIA
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: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
20 #include "point-to-point-helper.h"
21 #include "ns3/simulator.h"
22 #include "ns3/point-to-point-net-device.h"
23 #include "ns3/point-to-point-channel.h"
24 #include "ns3/queue.h"
25 #include "ns3/pcap-writer.h"
26 #include "ns3/config.h"
27 #include "ns3/packet.h"
28 #include "ns3/names.h"
33 PointToPointHelper::PointToPointHelper ()
35 m_queueFactory.SetTypeId ("ns3::DropTailQueue");
36 m_deviceFactory.SetTypeId ("ns3::PointToPointNetDevice");
37 m_channelFactory.SetTypeId ("ns3::PointToPointChannel");
41 PointToPointHelper::SetQueue (std::string type,
42 std::string n1, const AttributeValue &v1,
43 std::string n2, const AttributeValue &v2,
44 std::string n3, const AttributeValue &v3,
45 std::string n4, const AttributeValue &v4)
47 m_queueFactory.SetTypeId (type);
48 m_queueFactory.Set (n1, v1);
49 m_queueFactory.Set (n2, v2);
50 m_queueFactory.Set (n3, v3);
51 m_queueFactory.Set (n4, v4);
55 PointToPointHelper::SetDeviceAttribute (std::string n1, const AttributeValue &v1)
57 m_deviceFactory.Set (n1, v1);
61 PointToPointHelper::SetChannelAttribute (std::string n1, const AttributeValue &v1)
63 m_channelFactory.Set (n1, v1);
67 PointToPointHelper::EnablePcap (std::string filename, uint32_t nodeid, uint32_t deviceid)
69 std::ostringstream oss;
70 oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::PointToPointNetDevice/";
71 Config::MatchContainer matches = Config::LookupMatches (oss.str ());
72 if (matches.GetN () == 0)
77 oss << filename << "-" << nodeid << "-" << deviceid << ".pcap";
78 Ptr<PcapWriter> pcap = CreateObject<PcapWriter> ();
79 pcap->Open (oss.str ());
80 pcap->WritePppHeader ();
82 oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid;
83 oss << "/$ns3::PointToPointNetDevice/PromiscSniffer";
84 Config::ConnectWithoutContext (oss.str (), MakeBoundCallback (&PointToPointHelper::SniffEvent, pcap));
88 PointToPointHelper::EnablePcap (std::string filename, Ptr<NetDevice> nd)
90 EnablePcap (filename, nd->GetNode ()->GetId (), nd->GetIfIndex ());
94 PointToPointHelper::EnablePcap (std::string filename, std::string ndName)
96 Ptr<NetDevice> nd = Names::Find<NetDevice> (ndName);
97 EnablePcap (filename, nd->GetNode ()->GetId (), nd->GetIfIndex ());
101 PointToPointHelper::EnablePcap (std::string filename, NetDeviceContainer d)
103 for (NetDeviceContainer::Iterator i = d.Begin (); i != d.End (); ++i)
105 Ptr<NetDevice> dev = *i;
106 EnablePcap (filename, dev->GetNode ()->GetId (), dev->GetIfIndex ());
111 PointToPointHelper::EnablePcap (std::string filename, NodeContainer n)
113 NetDeviceContainer devs;
114 for (NodeContainer::Iterator i = n.Begin (); i != n.End (); ++i)
117 for (uint32_t j = 0; j < node->GetNDevices (); ++j)
119 devs.Add (node->GetDevice (j));
122 EnablePcap (filename, devs);
126 PointToPointHelper::EnablePcapAll (std::string filename)
128 EnablePcap (filename, NodeContainer::GetGlobal ());
132 PointToPointHelper::EnableAscii (std::ostream &os, uint32_t nodeid, uint32_t deviceid)
134 Packet::EnablePrinting ();
135 std::ostringstream oss;
136 oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::PointToPointNetDevice/MacRx";
137 Config::Connect (oss.str (), MakeBoundCallback (&PointToPointHelper::AsciiRxEvent, &os));
139 oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::PointToPointNetDevice/TxQueue/Enqueue";
140 Config::Connect (oss.str (), MakeBoundCallback (&PointToPointHelper::AsciiEnqueueEvent, &os));
142 oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::PointToPointNetDevice/TxQueue/Dequeue";
143 Config::Connect (oss.str (), MakeBoundCallback (&PointToPointHelper::AsciiDequeueEvent, &os));
145 oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::PointToPointNetDevice/TxQueue/Drop";
146 Config::Connect (oss.str (), MakeBoundCallback (&PointToPointHelper::AsciiDropEvent, &os));
150 PointToPointHelper::EnableAscii (std::ostream &os, NetDeviceContainer d)
152 for (NetDeviceContainer::Iterator i = d.Begin (); i != d.End (); ++i)
154 Ptr<NetDevice> dev = *i;
155 EnableAscii (os, dev->GetNode ()->GetId (), dev->GetIfIndex ());
160 PointToPointHelper::EnableAscii (std::ostream &os, NodeContainer n)
162 NetDeviceContainer devs;
163 for (NodeContainer::Iterator i = n.Begin (); i != n.End (); ++i)
166 for (uint32_t j = 0; j < node->GetNDevices (); ++j)
168 devs.Add (node->GetDevice (j));
171 EnableAscii (os, devs);
175 PointToPointHelper::EnableAsciiAll (std::ostream &os)
177 EnableAscii (os, NodeContainer::GetGlobal ());
181 PointToPointHelper::Install (NodeContainer c)
183 NS_ASSERT (c.GetN () == 2);
184 return Install (c.Get (0), c.Get (1));
188 PointToPointHelper::Install (Ptr<Node> a, Ptr<Node> b)
190 NetDeviceContainer container;
192 Ptr<PointToPointNetDevice> devA = m_deviceFactory.Create<PointToPointNetDevice> ();
193 devA->SetAddress (Mac48Address::Allocate ());
195 Ptr<Queue> queueA = m_queueFactory.Create<Queue> ();
196 devA->SetQueue (queueA);
197 Ptr<PointToPointNetDevice> devB = m_deviceFactory.Create<PointToPointNetDevice> ();
198 devB->SetAddress (Mac48Address::Allocate ());
200 Ptr<Queue> queueB = m_queueFactory.Create<Queue> ();
201 devB->SetQueue (queueB);
202 Ptr<PointToPointChannel> channel = m_channelFactory.Create<PointToPointChannel> ();
203 devA->Attach (channel);
204 devB->Attach (channel);
205 container.Add (devA);
206 container.Add (devB);
212 PointToPointHelper::Install (Ptr<Node> a, std::string bName)
214 Ptr<Node> b = Names::Find<Node> (bName);
215 return Install (a, b);
219 PointToPointHelper::Install (std::string aName, Ptr<Node> b)
221 Ptr<Node> a = Names::Find<Node> (aName);
222 return Install (a, b);
226 PointToPointHelper::Install (std::string aName, std::string bName)
228 Ptr<Node> a = Names::Find<Node> (aName);
229 Ptr<Node> b = Names::Find<Node> (bName);
230 return Install (a, b);
234 PointToPointHelper::InstallStar (Ptr<Node> hub, NodeContainer spokes,
235 NetDeviceContainer& hubDevices, NetDeviceContainer& spokeDevices)
237 for (uint32_t i = 0; i < spokes.GetN (); ++i)
239 NetDeviceContainer nd = Install (hub, spokes.Get (i));
240 hubDevices.Add (nd.Get (0));
241 spokeDevices.Add (nd.Get (1));
246 PointToPointHelper::InstallStar (std::string hubName, NodeContainer spokes,
247 NetDeviceContainer& hubDevices, NetDeviceContainer& spokeDevices)
249 Ptr<Node> hub = Names::Find<Node> (hubName);
250 InstallStar (hub, spokes, hubDevices, spokeDevices);
254 PointToPointHelper::SniffEvent (Ptr<PcapWriter> writer, Ptr<const Packet> packet)
256 writer->WritePacket (packet);
260 PointToPointHelper::AsciiEnqueueEvent (std::ostream *os, std::string path, Ptr<const Packet> packet)
262 *os << "+ " << Simulator::Now ().GetSeconds () << " ";
263 *os << path << " " << *packet << std::endl;
267 PointToPointHelper::AsciiDequeueEvent (std::ostream *os, std::string path, Ptr<const Packet> packet)
269 *os << "- " << Simulator::Now ().GetSeconds () << " ";
270 *os << path << " " << *packet << std::endl;
274 PointToPointHelper::AsciiDropEvent (std::ostream *os, std::string path, Ptr<const Packet> packet)
276 *os << "d " << Simulator::Now ().GetSeconds () << " ";
277 *os << path << " " << *packet << std::endl;
281 PointToPointHelper::AsciiRxEvent (std::ostream *os, std::string path, Ptr<const Packet> packet)
283 *os << "r " << Simulator::Now ().GetSeconds () << " ";
284 *os << path << " " << *packet << std::endl;