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
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
Manage ASCII trace files for device models.
Definition: trace-helper.h:161
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
void SetChannelAttribute(std::string n1, const AttributeValue &v1)
Definition: csma-helper.cc:72
static void DefaultEnqueueSinkWithContext(Ptr< OutputStreamWrapper > file, std::string context, Ptr< const Packet > p)
Basic Enqueue default trace sink.
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:483
std::vector< Ptr< Node > >::const_iterator Iterator
Node container iterator.
Iterator End(void) const
Get an iterator which indicates past-the-last NetDevice in the container.
Hold a value for an Attribute.
Definition: attribute.h:68
Manage pcap files for device models.
Definition: trace-helper.h:38
Callback< R > MakeBoundCallback(R(*fnPtr)(TX), ARG a1)
Make Callbacks with one bound argument.
Definition: callback.h:1847
static void DefaultDropSinkWithContext(Ptr< OutputStreamWrapper > file, std::string context, Ptr< const Packet > p)
Basic Drop default trace sink.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
void SetTypeId(TypeId tid)
Set the TypeId of the Objects to be created by this factory.
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
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:281
Ptr< OutputStreamWrapper > CreateFileStream(std::string filename, std::ios::openmode filemode=std::ios::out)
Create and initialize an output stream object we&#39;ll use to write the traced bits. ...
Iterator End(void) const
Get an iterator which indicates past-the-last Node in the container.
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:461
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...
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
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 &#39;>&#39;. ...
Definition: queue.cc:73
channel
Definition: third.py:92
CsmaHelper()
Construct a CsmaHelper.
Definition: csma-helper.cc:42
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
static Mac48Address Allocate(void)
Allocate a new Mac48Address.
Ptr< Queue< Packet > > GetQueue(void) const
Get a copy of the attached Queue.
static void EnablePrinting(void)
Enable printing packets metadata.
Definition: packet.cc:572
Ptr< Object > Create(void) const
Create an Object instance of the configured TypeId.
Iterator Begin(void) const
Get an iterator which refers to the first NetDevice in the container.
void Add(NetDeviceContainer other)
Append the contents of another NetDeviceContainer to the end of this container.
ObjectFactory m_deviceFactory
factory for the NetDevices
Definition: csma-helper.h:251
holds a vector of ns3::NetDevice pointers
ObjectFactory m_channelFactory
factory for the channel
Definition: csma-helper.h:252
csma
Definition: second.py:63
void Connect(std::string path, const CallbackBase &cb)
Definition: config.cc:873
static void DefaultReceiveSinkWithContext(Ptr< OutputStreamWrapper > file, std::string context, Ptr< const Packet > p)
Basic Receive default trace sink.
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:461
Csma Channel.
Definition: csma-channel.h:90
static void DefaultDequeueSinkWithContext(Ptr< OutputStreamWrapper > file, std::string context, Ptr< const Packet > p)
Basic Dequeue default trace sink.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
keep track of a set of node pointers.
void Set(std::string name, const AttributeValue &value)
Set an attribute to be set during construction.
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:527
ObjectFactory m_queueFactory
factory for the queues
Definition: csma-helper.h:250
void SetDeviceAttribute(std::string n1, const AttributeValue &v1)
Definition: csma-helper.cc:66
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
NetDeviceContainer Install(Ptr< Node > node) const
This method creates an ns3::CsmaChannel with the attributes configured by CsmaHelper::SetChannelAttri...
Definition: csma-helper.cc:218
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
uint32_t AddDevice(Ptr< NetDevice > device)
Associate a NetDevice to this node.
Definition: node.cc:128
std::vector< Ptr< NetDevice > >::const_iterator Iterator
NetDevice container iterator.
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
void HookDefaultSink(Ptr< T > object, std::string traceName, Ptr< PcapFileWrapper > file)
Hook a trace source to the default trace sink.
Definition: trace-helper.h:147
Iterator Begin(void) const
Get an iterator which refers to the first Node in the container.
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:505