A Discrete-Event Network Simulator
API
csma-helper.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2008 INRIA
4  *
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;
8  *
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.
13  *
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
17  *
18  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19  */
20 
21 #include "ns3/abort.h"
22 #include "ns3/log.h"
23 #include "ns3/simulator.h"
24 #include "ns3/object-factory.h"
25 #include "ns3/queue.h"
26 #include "ns3/net-device-queue-interface.h"
27 #include "ns3/csma-net-device.h"
28 #include "ns3/csma-channel.h"
29 #include "ns3/config.h"
30 #include "ns3/packet.h"
31 #include "ns3/names.h"
32 
33 #include "ns3/trace-helper.h"
34 #include "csma-helper.h"
35 
36 #include <string>
37 
38 namespace ns3 {
39 
40 NS_LOG_COMPONENT_DEFINE ("CsmaHelper");
41 
43 {
44  m_queueFactory.SetTypeId ("ns3::DropTailQueue<Packet>");
45  m_deviceFactory.SetTypeId ("ns3::CsmaNetDevice");
46  m_channelFactory.SetTypeId ("ns3::CsmaChannel");
47 }
48 
49 void
50 CsmaHelper::SetQueue (std::string type,
51  std::string n1, const AttributeValue &v1,
52  std::string n2, const AttributeValue &v2,
53  std::string n3, const AttributeValue &v3,
54  std::string n4, const AttributeValue &v4)
55 {
57 
59  m_queueFactory.Set (n1, v1);
60  m_queueFactory.Set (n2, v2);
61  m_queueFactory.Set (n3, v3);
62  m_queueFactory.Set (n4, v4);
63 }
64 
65 void
67 {
68  m_deviceFactory.Set (n1, v1);
69 }
70 
71 void
73 {
74  m_channelFactory.Set (n1, v1);
75 }
76 
77 void
78 CsmaHelper::EnablePcapInternal (std::string prefix, Ptr<NetDevice> nd, bool promiscuous, bool explicitFilename)
79 {
80  //
81  // All of the Pcap enable functions vector through here including the ones
82  // that are wandering through all of devices on perhaps all of the nodes in
83  // the system. We can only deal with devices of type CsmaNetDevice.
84  //
85  Ptr<CsmaNetDevice> device = nd->GetObject<CsmaNetDevice> ();
86  if (device == 0)
87  {
88  NS_LOG_INFO ("CsmaHelper::EnablePcapInternal(): Device " << device << " not of type ns3::CsmaNetDevice");
89  return;
90  }
91 
92  PcapHelper pcapHelper;
93 
94  std::string filename;
95  if (explicitFilename)
96  {
97  filename = prefix;
98  }
99  else
100  {
101  filename = pcapHelper.GetFilenameFromDevice (prefix, device);
102  }
103 
104  Ptr<PcapFileWrapper> file = pcapHelper.CreateFile (filename, std::ios::out,
106  if (promiscuous)
107  {
108  pcapHelper.HookDefaultSink<CsmaNetDevice> (device, "PromiscSniffer", file);
109  }
110  else
111  {
112  pcapHelper.HookDefaultSink<CsmaNetDevice> (device, "Sniffer", file);
113  }
114 }
115 
116 void
118  Ptr<OutputStreamWrapper> stream,
119  std::string prefix,
120  Ptr<NetDevice> nd,
121  bool explicitFilename)
122 {
123  //
124  // All of the ascii enable functions vector through here including the ones
125  // that are wandering through all of devices on perhaps all of the nodes in
126  // the system. We can only deal with devices of type CsmaNetDevice.
127  //
128  Ptr<CsmaNetDevice> device = nd->GetObject<CsmaNetDevice> ();
129  if (device == 0)
130  {
131  NS_LOG_INFO ("CsmaHelper::EnableAsciiInternal(): Device " << device << " not of type ns3::CsmaNetDevice");
132  return;
133  }
134 
135  //
136  // Our default trace sinks are going to use packet printing, so we have to
137  // make sure that is turned on.
138  //
140 
141  //
142  // If we are not provided an OutputStreamWrapper, we are expected to create
143  // one using the usual trace filename conventions and do a Hook*WithoutContext
144  // since there will be one file per context and therefore the context would
145  // be redundant.
146  //
147  if (stream == 0)
148  {
149  //
150  // Set up an output stream object to deal with private ofstream copy
151  // constructor and lifetime issues. Let the helper decide the actual
152  // name of the file given the prefix.
153  //
154  AsciiTraceHelper asciiTraceHelper;
155 
156  std::string filename;
157  if (explicitFilename)
158  {
159  filename = prefix;
160  }
161  else
162  {
163  filename = asciiTraceHelper.GetFilenameFromDevice (prefix, device);
164  }
165 
166  Ptr<OutputStreamWrapper> theStream = asciiTraceHelper.CreateFileStream (filename);
167 
168  //
169  // The MacRx trace source provides our "r" event.
170  //
171  asciiTraceHelper.HookDefaultReceiveSinkWithoutContext<CsmaNetDevice> (device, "MacRx", theStream);
172 
173  //
174  // The "+", '-', and 'd' events are driven by trace sources actually in the
175  // transmit queue.
176  //
177  Ptr<Queue<Packet> > queue = device->GetQueue ();
178  asciiTraceHelper.HookDefaultEnqueueSinkWithoutContext<Queue<Packet> > (queue, "Enqueue", theStream);
179  asciiTraceHelper.HookDefaultDropSinkWithoutContext<Queue<Packet> > (queue, "Drop", theStream);
180  asciiTraceHelper.HookDefaultDequeueSinkWithoutContext<Queue<Packet> > (queue, "Dequeue", theStream);
181 
182  return;
183  }
184 
185  //
186  // If we are provided an OutputStreamWrapper, we are expected to use it, and
187  // to providd a context. We are free to come up with our own context if we
188  // want, and use the AsciiTraceHelper Hook*WithContext functions, but for
189  // compatibility and simplicity, we just use Config::Connect and let it deal
190  // with the context.
191  //
192  // Note that we are going to use the default trace sinks provided by the
193  // ascii trace helper. There is actually no AsciiTraceHelper in sight here,
194  // but the default trace sinks are actually publicly available static
195  // functions that are always there waiting for just such a case.
196  //
197  uint32_t nodeid = nd->GetNode ()->GetId ();
198  uint32_t deviceid = nd->GetIfIndex ();
199  std::ostringstream oss;
200 
201  oss << "/NodeList/" << nd->GetNode ()->GetId () << "/DeviceList/" << deviceid << "/$ns3::CsmaNetDevice/MacRx";
203 
204  oss.str ("");
205  oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::CsmaNetDevice/TxQueue/Enqueue";
207 
208  oss.str ("");
209  oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::CsmaNetDevice/TxQueue/Dequeue";
211 
212  oss.str ("");
213  oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::CsmaNetDevice/TxQueue/Drop";
215 }
216 
219 {
221  return Install (node, channel);
222 }
223 
225 CsmaHelper::Install (std::string nodeName) const
226 {
227  Ptr<Node> node = Names::Find<Node> (nodeName);
228  return Install (node);
229 }
230 
233 {
234  return NetDeviceContainer (InstallPriv (node, channel));
235 }
236 
238 CsmaHelper::Install (Ptr<Node> node, std::string channelName) const
239 {
240  Ptr<CsmaChannel> channel = Names::Find<CsmaChannel> (channelName);
241  return NetDeviceContainer (InstallPriv (node, channel));
242 }
243 
245 CsmaHelper::Install (std::string nodeName, Ptr<CsmaChannel> channel) const
246 {
247  Ptr<Node> node = Names::Find<Node> (nodeName);
248  return NetDeviceContainer (InstallPriv (node, channel));
249 }
250 
252 CsmaHelper::Install (std::string nodeName, std::string channelName) const
253 {
254  Ptr<Node> node = Names::Find<Node> (nodeName);
255  Ptr<CsmaChannel> channel = Names::Find<CsmaChannel> (channelName);
256  return NetDeviceContainer (InstallPriv (node, channel));
257 }
258 
261 {
263 
264  return Install (c, channel);
265 }
266 
269 {
270  NetDeviceContainer devs;
271 
272  for (NodeContainer::Iterator i = c.Begin (); i != c.End (); i++)
273  {
274  devs.Add (InstallPriv (*i, channel));
275  }
276 
277  return devs;
278 }
279 
281 CsmaHelper::Install (const NodeContainer &c, std::string channelName) const
282 {
283  Ptr<CsmaChannel> channel = Names::Find<CsmaChannel> (channelName);
284  return Install (c, channel);
285 }
286 
287 int64_t
289 {
290  int64_t currentStream = stream;
291  Ptr<NetDevice> netDevice;
292  for (NetDeviceContainer::Iterator i = c.Begin (); i != c.End (); ++i)
293  {
294  netDevice = (*i);
295  Ptr<CsmaNetDevice> csma = DynamicCast<CsmaNetDevice> (netDevice);
296  if (csma)
297  {
298  currentStream += csma->AssignStreams (currentStream);
299  }
300  }
301  return (currentStream - stream);
302 }
303 
306 {
308  device->SetAddress (Mac48Address::Allocate ());
309  node->AddDevice (device);
311  device->SetQueue (queue);
312  device->Attach (channel);
313  // Aggregate a NetDeviceQueueInterface object
314  Ptr<NetDeviceQueueInterface> ndqi = CreateObject<NetDeviceQueueInterface> ();
315  ndqi->GetTxQueue (0)->ConnectQueueTraces (queue);
316  device->AggregateObject (ndqi);
317 
318  return device;
319 }
320 
321 } // namespace ns3
ns3::NetDeviceContainer
holds a vector of ns3::NetDevice pointers
Definition: net-device-container.h:42
NS_LOG_COMPONENT_DEFINE
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
ns3::PcapHelper::CreateFile
Ptr< PcapFileWrapper > CreateFile(std::string filename, std::ios::openmode filemode, DataLinkType dataLinkType, uint32_t snapLen=std::numeric_limits< uint32_t >::max(), int32_t tzCorrection=0)
Create and initialize a pcap file.
Definition: trace-helper.cc:49
ns3::PcapHelper::DLT_EN10MB
@ DLT_EN10MB
Definition: trace-helper.h:52
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::PcapHelper::GetFilenameFromDevice
std::string GetFilenameFromDevice(std::string prefix, Ptr< NetDevice > device, bool useObjectNames=true)
Let the pcap helper figure out a reasonable filename to use for a pcap file associated with a device.
Definition: trace-helper.cc:80
ns3::CsmaChannel
Csma Channel.
Definition: csma-channel.h:91
ns3::Object::GetObject
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:470
ns3::Packet::EnablePrinting
static void EnablePrinting(void)
Enable printing packets metadata.
Definition: packet.cc:572
ns3::AttributeValue
Hold a value for an Attribute.
Definition: attribute.h:69
ns3::CsmaHelper::CsmaHelper
CsmaHelper()
Construct a CsmaHelper.
Definition: csma-helper.cc:42
third.channel
channel
Definition: third.py:92
ns3::PcapHelper::HookDefaultSink
void HookDefaultSink(Ptr< T > object, std::string traceName, Ptr< PcapFileWrapper > file)
Hook a trace source to the default trace sink.
Definition: trace-helper.h:148
ns3::AsciiTraceHelper::HookDefaultEnqueueSinkWithoutContext
void HookDefaultEnqueueSinkWithoutContext(Ptr< T > object, std::string traceName, Ptr< OutputStreamWrapper > stream)
Hook a trace source to the default enqueue operation trace sink that does not accept nor log a trace ...
Definition: trace-helper.h:462
ns3::CsmaHelper::Install
NetDeviceContainer Install(Ptr< Node > node) const
This method creates an ns3::CsmaChannel with the attributes configured by CsmaHelper::SetChannelAttri...
Definition: csma-helper.cc:218
ns3::CsmaHelper::m_channelFactory
ObjectFactory m_channelFactory
factory for the channel
Definition: csma-helper.h:252
ns3::QueueBase::AppendItemTypeIfNotPresent
static void AppendItemTypeIfNotPresent(std::string &typeId, const std::string &itemType)
Append the item type to the provided type ID if the latter does not end with '>'.
Definition: queue.cc:73
ns3::CsmaHelper::AssignStreams
int64_t AssignStreams(NetDeviceContainer c, int64_t stream)
Assign a fixed random variable stream number to the random variables used by this model.
Definition: csma-helper.cc:288
ns3::Ptr
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:74
ns3::CsmaHelper::EnableAsciiInternal
virtual void EnableAsciiInternal(Ptr< OutputStreamWrapper > stream, std::string prefix, Ptr< NetDevice > nd, bool explicitFilename)
Enable ascii trace output on the indicated net device.
Definition: csma-helper.cc:117
ns3::NetDeviceContainer::Begin
Iterator Begin(void) const
Get an iterator which refers to the first NetDevice in the container.
Definition: net-device-container.cc:46
ns3::NodeContainer::Begin
Iterator Begin(void) const
Get an iterator which refers to the first Node in the container.
Definition: node-container.cc:77
NS_LOG_INFO
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:281
ns3::AsciiTraceHelper::CreateFileStream
Ptr< OutputStreamWrapper > CreateFileStream(std::string filename, std::ios::openmode filemode=std::ios::out)
Create and initialize an output stream object we'll use to write the traced bits.
Definition: trace-helper.cc:191
ns3::Mac48Address::Allocate
static Mac48Address Allocate(void)
Allocate a new Mac48Address.
Definition: mac48-address.cc:135
ns3::CsmaHelper::SetChannelAttribute
void SetChannelAttribute(std::string n1, const AttributeValue &v1)
Definition: csma-helper.cc:72
ns3::CsmaHelper::m_queueFactory
ObjectFactory m_queueFactory
factory for the queues
Definition: csma-helper.h:250
ns3::AsciiTraceHelper::HookDefaultReceiveSinkWithoutContext
void HookDefaultReceiveSinkWithoutContext(Ptr< T > object, std::string traceName, Ptr< OutputStreamWrapper > stream)
Hook a trace source to the default receive operation trace sink that does not accept nor log a trace ...
Definition: trace-helper.h:528
ns3::Node::AddDevice
uint32_t AddDevice(Ptr< NetDevice > device)
Associate a NetDevice to this node.
Definition: node.cc:130
ns3::PcapHelper
Manage pcap files for device models.
Definition: trace-helper.h:39
ns3::NetDeviceContainer::End
Iterator End(void) const
Get an iterator which indicates past-the-last NetDevice in the container.
Definition: net-device-container.cc:51
Queue< Packet >
ns3::CsmaHelper::m_deviceFactory
ObjectFactory m_deviceFactory
factory for the NetDevices
Definition: csma-helper.h:251
ns3::CsmaHelper::InstallPriv
Ptr< NetDevice > InstallPriv(Ptr< Node > node, Ptr< CsmaChannel > channel) const
This method creates an ns3::CsmaNetDevice with the attributes configured by CsmaHelper::SetDeviceAttr...
Definition: csma-helper.cc:305
csma-helper.h
ns3::AsciiTraceHelper::DefaultReceiveSinkWithContext
static void DefaultReceiveSinkWithContext(Ptr< OutputStreamWrapper > file, std::string context, Ptr< const Packet > p)
Basic Receive default trace sink.
Definition: trace-helper.cc:394
ns3::NetDeviceContainer::Iterator
std::vector< Ptr< NetDevice > >::const_iterator Iterator
NetDevice container iterator.
Definition: net-device-container.h:45
ns3::AsciiTraceHelper
Manage ASCII trace files for device models.
Definition: trace-helper.h:163
ns3::CsmaHelper::SetQueue
void SetQueue(std::string type, std::string n1="", const AttributeValue &v1=EmptyAttributeValue(), std::string n2="", const AttributeValue &v2=EmptyAttributeValue(), std::string n3="", const AttributeValue &v3=EmptyAttributeValue(), std::string n4="", const AttributeValue &v4=EmptyAttributeValue())
Definition: csma-helper.cc:50
ns3::NodeContainer::Iterator
std::vector< Ptr< Node > >::const_iterator Iterator
Node container iterator.
Definition: node-container.h:42
ns3::Config::Connect
void Connect(std::string path, const CallbackBase &cb)
Definition: config.cc:920
ns3::CsmaNetDevice::GetQueue
Ptr< Queue< Packet > > GetQueue(void) const
Get a copy of the attached Queue.
Definition: csma-net-device.cc:824
ns3::AsciiTraceHelper::DefaultDequeueSinkWithContext
static void DefaultDequeueSinkWithContext(Ptr< OutputStreamWrapper > file, std::string context, Ptr< const Packet > p)
Basic Dequeue default trace sink.
Definition: trace-helper.cc:370
ns3::MakeBoundCallback
Callback< R > MakeBoundCallback(R(*fnPtr)(TX), ARG a1)
Make Callbacks with one bound argument.
Definition: callback.h:1703
ns3::CsmaHelper::EnablePcapInternal
virtual void EnablePcapInternal(std::string prefix, Ptr< NetDevice > nd, bool promiscuous, bool explicitFilename)
Enable pcap output on the indicated net device.
Definition: csma-helper.cc:78
ns3::ObjectFactory::Set
void Set(const std::string &name, const AttributeValue &value, Args &&... args)
Set an attribute to be set during construction.
Definition: object-factory.h:223
ns3::CsmaHelper::SetDeviceAttribute
void SetDeviceAttribute(std::string n1, const AttributeValue &v1)
Definition: csma-helper.cc:66
ns3::NodeContainer
keep track of a set of node pointers.
Definition: node-container.h:39
ns3::AsciiTraceHelper::HookDefaultDropSinkWithoutContext
void HookDefaultDropSinkWithoutContext(Ptr< T > object, std::string traceName, Ptr< OutputStreamWrapper > stream)
Hook a trace source to the default drop operation trace sink that does not accept nor log a trace con...
Definition: trace-helper.h:484
ns3::CsmaNetDevice
A Device for a Csma Network Link.
Definition: csma-net-device.h:59
ns3::NodeContainer::End
Iterator End(void) const
Get an iterator which indicates past-the-last Node in the container.
Definition: node-container.cc:82
ns3::ObjectFactory::SetTypeId
void SetTypeId(TypeId tid)
Set the TypeId of the Objects to be created by this factory.
Definition: object-factory.cc:40
ns3::AsciiTraceHelper::DefaultDropSinkWithContext
static void DefaultDropSinkWithContext(Ptr< OutputStreamWrapper > file, std::string context, Ptr< const Packet > p)
Basic Drop default trace sink.
Definition: trace-helper.cc:343
ns3::ObjectFactory::Create
Ptr< Object > Create(void) const
Create an Object instance of the configured TypeId.
Definition: object-factory.cc:98
ns3::AsciiTraceHelper::GetFilenameFromDevice
std::string GetFilenameFromDevice(std::string prefix, Ptr< NetDevice > device, bool useObjectNames=true)
Let the ascii trace helper figure out a reasonable filename to use for an ascii trace file associated...
Definition: trace-helper.cc:212
ns3::AsciiTraceHelper::DefaultEnqueueSinkWithContext
static void DefaultEnqueueSinkWithContext(Ptr< OutputStreamWrapper > file, std::string context, Ptr< const Packet > p)
Basic Enqueue default trace sink.
Definition: trace-helper.cc:317
create-module.file
file
Definition: create-module.py:634
second.csma
csma
Definition: second.py:63
ns3::NetDeviceContainer::Add
void Add(NetDeviceContainer other)
Append the contents of another NetDeviceContainer to the end of this container.
Definition: net-device-container.cc:67
ns3::AsciiTraceHelper::HookDefaultDequeueSinkWithoutContext
void HookDefaultDequeueSinkWithoutContext(Ptr< T > object, std::string traceName, Ptr< OutputStreamWrapper > stream)
Hook a trace source to the default dequeue operation trace sink that does not accept nor log a trace ...
Definition: trace-helper.h:506