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 "csma-helper.h"
21 #include "ns3/simulator.h"
22 #include "ns3/object-factory.h"
23 #include "ns3/queue.h"
24 #include "ns3/csma-net-device.h"
25 #include "ns3/csma-channel.h"
26 #include "ns3/pcap-writer.h"
27 #include "ns3/config.h"
28 #include "ns3/packet.h"
29 #include "ns3/names.h"
34 CsmaHelper::CsmaHelper ()
36 m_queueFactory.SetTypeId ("ns3::DropTailQueue");
37 m_deviceFactory.SetTypeId ("ns3::CsmaNetDevice");
38 m_channelFactory.SetTypeId ("ns3::CsmaChannel");
42 CsmaHelper::SetQueue (std::string type,
43 std::string n1, const AttributeValue &v1,
44 std::string n2, const AttributeValue &v2,
45 std::string n3, const AttributeValue &v3,
46 std::string n4, const AttributeValue &v4)
48 m_queueFactory.SetTypeId (type);
49 m_queueFactory.Set (n1, v1);
50 m_queueFactory.Set (n2, v2);
51 m_queueFactory.Set (n3, v3);
52 m_queueFactory.Set (n4, v4);
56 CsmaHelper::SetDeviceAttribute (std::string n1, const AttributeValue &v1)
58 m_deviceFactory.Set (n1, v1);
62 CsmaHelper::SetChannelAttribute (std::string n1, const AttributeValue &v1)
64 m_channelFactory.Set (n1, v1);
68 CsmaHelper::EnablePcap (std::string filename, uint32_t nodeid, uint32_t deviceid, bool promiscuous)
70 std::ostringstream oss;
71 oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::CsmaNetDevice/";
72 Config::MatchContainer matches = Config::LookupMatches (oss.str ());
73 if (matches.GetN () == 0)
78 oss << filename << "-" << nodeid << "-" << deviceid << ".pcap";
79 Ptr<PcapWriter> pcap = CreateObject<PcapWriter> ();
80 pcap->Open (oss.str ());
81 pcap->WriteEthernetHeader ();
83 oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid;
86 oss << "/$ns3::CsmaNetDevice/PromiscSniffer";
90 oss << "/$ns3::CsmaNetDevice/Sniffer";
92 Config::ConnectWithoutContext (oss.str (), MakeBoundCallback (&CsmaHelper::SniffEvent, pcap));
96 CsmaHelper::EnablePcap (std::string filename, NetDeviceContainer d, bool promiscuous)
98 for (NetDeviceContainer::Iterator i = d.Begin (); i != d.End (); ++i)
100 Ptr<NetDevice> dev = *i;
101 EnablePcap (filename, dev->GetNode ()->GetId (), dev->GetIfIndex (), promiscuous);
106 CsmaHelper::EnablePcap (std::string filename, Ptr<NetDevice> nd, bool promiscuous)
108 EnablePcap (filename, nd->GetNode ()->GetId (), nd->GetIfIndex (), promiscuous);
112 CsmaHelper::EnablePcap (std::string filename, std::string ndName, bool promiscuous)
114 Ptr<NetDevice> nd = Names::Find<NetDevice> (ndName);
115 EnablePcap (filename, nd->GetNode ()->GetId (), nd->GetIfIndex (), promiscuous);
119 CsmaHelper::EnablePcap (std::string filename, NodeContainer n, bool promiscuous)
121 NetDeviceContainer devs;
122 for (NodeContainer::Iterator i = n.Begin (); i != n.End (); ++i)
125 for (uint32_t j = 0; j < node->GetNDevices (); ++j)
127 devs.Add (node->GetDevice (j));
130 EnablePcap (filename, devs, promiscuous);
134 CsmaHelper::EnablePcapAll (std::string filename, bool promiscuous)
136 EnablePcap (filename, NodeContainer::GetGlobal (), promiscuous);
140 CsmaHelper::EnableAscii (std::ostream &os, uint32_t nodeid, uint32_t deviceid)
142 Packet::EnablePrinting ();
143 std::ostringstream oss;
144 oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::CsmaNetDevice/MacRx";
145 Config::Connect (oss.str (), MakeBoundCallback (&CsmaHelper::AsciiRxEvent, &os));
147 oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::CsmaNetDevice/TxQueue/Enqueue";
148 Config::Connect (oss.str (), MakeBoundCallback (&CsmaHelper::AsciiEnqueueEvent, &os));
150 oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::CsmaNetDevice/TxQueue/Dequeue";
151 Config::Connect (oss.str (), MakeBoundCallback (&CsmaHelper::AsciiDequeueEvent, &os));
153 oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::CsmaNetDevice/TxQueue/Drop";
154 Config::Connect (oss.str (), MakeBoundCallback (&CsmaHelper::AsciiDropEvent, &os));
157 CsmaHelper::EnableAscii (std::ostream &os, NetDeviceContainer d)
159 for (NetDeviceContainer::Iterator i = d.Begin (); i != d.End (); ++i)
161 Ptr<NetDevice> dev = *i;
162 EnableAscii (os, dev->GetNode ()->GetId (), dev->GetIfIndex ());
166 CsmaHelper::EnableAscii (std::ostream &os, NodeContainer n)
168 NetDeviceContainer devs;
169 for (NodeContainer::Iterator i = n.Begin (); i != n.End (); ++i)
172 for (uint32_t j = 0; j < node->GetNDevices (); ++j)
174 devs.Add (node->GetDevice (j));
177 EnableAscii (os, devs);
181 CsmaHelper::EnableAsciiAll (std::ostream &os)
183 EnableAscii (os, NodeContainer::GetGlobal ());
187 CsmaHelper::Install (Ptr<Node> node) const
189 Ptr<CsmaChannel> channel = m_channelFactory.Create ()->GetObject<CsmaChannel> ();
190 return Install (node, channel);
194 CsmaHelper::Install (std::string nodeName) const
196 Ptr<Node> node = Names::Find<Node> (nodeName);
197 return Install (node);
201 CsmaHelper::Install (Ptr<Node> node, Ptr<CsmaChannel> channel) const
203 return NetDeviceContainer (InstallPriv (node, channel));
207 CsmaHelper::Install (Ptr<Node> node, std::string channelName) const
209 Ptr<CsmaChannel> channel = Names::Find<CsmaChannel> (channelName);
210 return NetDeviceContainer (InstallPriv (node, channel));
214 CsmaHelper::Install (std::string nodeName, Ptr<CsmaChannel> channel) const
216 Ptr<Node> node = Names::Find<Node> (nodeName);
217 return NetDeviceContainer (InstallPriv (node, channel));
221 CsmaHelper::Install (std::string nodeName, std::string channelName) const
223 Ptr<Node> node = Names::Find<Node> (nodeName);
224 Ptr<CsmaChannel> channel = Names::Find<CsmaChannel> (channelName);
225 return NetDeviceContainer (InstallPriv (node, channel));
229 CsmaHelper::Install (const NodeContainer &c) const
231 Ptr<CsmaChannel> channel = m_channelFactory.Create ()->GetObject<CsmaChannel> ();
233 return Install (c, channel);
237 CsmaHelper::Install (const NodeContainer &c, Ptr<CsmaChannel> channel) const
239 NetDeviceContainer devs;
241 for (NodeContainer::Iterator i = c.Begin (); i != c.End (); i++)
243 devs.Add (InstallPriv (*i, channel));
250 CsmaHelper::Install (const NodeContainer &c, std::string channelName) const
252 Ptr<CsmaChannel> channel = Names::Find<CsmaChannel> (channelName);
253 return Install (c, channel);
257 CsmaHelper::InstallPriv (Ptr<Node> node, Ptr<CsmaChannel> channel) const
259 Ptr<CsmaNetDevice> device = m_deviceFactory.Create<CsmaNetDevice> ();
260 device->SetAddress (Mac48Address::Allocate ());
261 node->AddDevice (device);
262 Ptr<Queue> queue = m_queueFactory.Create<Queue> ();
263 device->SetQueue (queue);
264 device->Attach (channel);
270 CsmaHelper::InstallStar (Ptr<Node> hub, NodeContainer spokes,
271 NetDeviceContainer& hubDevices, NetDeviceContainer& spokeDevices)
273 for (uint32_t i = 0; i < spokes.GetN (); ++i)
275 NodeContainer nodes (hub, spokes.Get (i));
276 NetDeviceContainer nd = Install (nodes);
277 hubDevices.Add (nd.Get (0));
278 spokeDevices.Add (nd.Get (1));
283 CsmaHelper::InstallStar (std::string hubName, NodeContainer spokes,
284 NetDeviceContainer& hubDevices, NetDeviceContainer& spokeDevices)
286 Ptr<Node> hub = Names::Find<Node> (hubName);
287 InstallStar (hub, spokes, hubDevices, spokeDevices);
291 CsmaHelper::SniffEvent (Ptr<PcapWriter> writer, Ptr<const Packet> packet)
293 writer->WritePacket (packet);
297 CsmaHelper::AsciiEnqueueEvent (std::ostream *os, std::string path, Ptr<const Packet> packet)
299 *os << "+ " << Simulator::Now ().GetSeconds () << " ";
300 *os << path << " " << *packet << std::endl;
304 CsmaHelper::AsciiDequeueEvent (std::ostream *os, std::string path, Ptr<const Packet> packet)
306 *os << "- " << Simulator::Now ().GetSeconds () << " ";
307 *os << path << " " << *packet << std::endl;
311 CsmaHelper::AsciiDropEvent (std::ostream *os, std::string path, Ptr<const Packet> packet)
313 *os << "d " << Simulator::Now ().GetSeconds () << " ";
314 *os << path << " " << *packet << std::endl;
318 CsmaHelper::AsciiRxEvent (std::ostream *os, std::string path, Ptr<const Packet> packet)
320 *os << "r " << Simulator::Now ().GetSeconds () << " ";
321 *os << path << " " << *packet << std::endl;