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/csma-net-device.h"
27 #include "ns3/csma-channel.h"
28 #include "ns3/config.h"
29 #include "ns3/packet.h"
30 #include "ns3/names.h"
31 
32 #include "ns3/trace-helper.h"
33 #include "csma-helper.h"
34 
35 #include <string>
36 
37 namespace ns3 {
38 
39 NS_LOG_COMPONENT_DEFINE ("CsmaHelper");
40 
42 {
43  m_queueFactory.SetTypeId ("ns3::DropTailQueue<Packet>");
44  m_deviceFactory.SetTypeId ("ns3::CsmaNetDevice");
45  m_channelFactory.SetTypeId ("ns3::CsmaChannel");
46 }
47 
48 void
49 CsmaHelper::SetQueue (std::string type,
50  std::string n1, const AttributeValue &v1,
51  std::string n2, const AttributeValue &v2,
52  std::string n3, const AttributeValue &v3,
53  std::string n4, const AttributeValue &v4)
54 {
56 
58  m_queueFactory.Set (n1, v1);
59  m_queueFactory.Set (n2, v2);
60  m_queueFactory.Set (n3, v3);
61  m_queueFactory.Set (n4, v4);
62 }
63 
64 void
66 {
67  m_deviceFactory.Set (n1, v1);
68 }
69 
70 void
72 {
73  m_channelFactory.Set (n1, v1);
74 }
75 
76 void
77 CsmaHelper::EnablePcapInternal (std::string prefix, Ptr<NetDevice> nd, bool promiscuous, bool explicitFilename)
78 {
79  //
80  // All of the Pcap enable functions vector through here including the ones
81  // that are wandering through all of devices on perhaps all of the nodes in
82  // the system. We can only deal with devices of type CsmaNetDevice.
83  //
84  Ptr<CsmaNetDevice> device = nd->GetObject<CsmaNetDevice> ();
85  if (device == 0)
86  {
87  NS_LOG_INFO ("CsmaHelper::EnablePcapInternal(): Device " << device << " not of type ns3::CsmaNetDevice");
88  return;
89  }
90 
91  PcapHelper pcapHelper;
92 
93  std::string filename;
94  if (explicitFilename)
95  {
96  filename = prefix;
97  }
98  else
99  {
100  filename = pcapHelper.GetFilenameFromDevice (prefix, device);
101  }
102 
103  Ptr<PcapFileWrapper> file = pcapHelper.CreateFile (filename, std::ios::out,
105  if (promiscuous)
106  {
107  pcapHelper.HookDefaultSink<CsmaNetDevice> (device, "PromiscSniffer", file);
108  }
109  else
110  {
111  pcapHelper.HookDefaultSink<CsmaNetDevice> (device, "Sniffer", file);
112  }
113 }
114 
115 void
117  Ptr<OutputStreamWrapper> stream,
118  std::string prefix,
119  Ptr<NetDevice> nd,
120  bool explicitFilename)
121 {
122  //
123  // All of the ascii enable functions vector through here including the ones
124  // that are wandering through all of devices on perhaps all of the nodes in
125  // the system. We can only deal with devices of type CsmaNetDevice.
126  //
127  Ptr<CsmaNetDevice> device = nd->GetObject<CsmaNetDevice> ();
128  if (device == 0)
129  {
130  NS_LOG_INFO ("CsmaHelper::EnableAsciiInternal(): Device " << device << " not of type ns3::CsmaNetDevice");
131  return;
132  }
133 
134  //
135  // Our default trace sinks are going to use packet printing, so we have to
136  // make sure that is turned on.
137  //
139 
140  //
141  // If we are not provided an OutputStreamWrapper, we are expected to create
142  // one using the usual trace filename conventions and do a Hook*WithoutContext
143  // since there will be one file per context and therefore the context would
144  // be redundant.
145  //
146  if (stream == 0)
147  {
148  //
149  // Set up an output stream object to deal with private ofstream copy
150  // constructor and lifetime issues. Let the helper decide the actual
151  // name of the file given the prefix.
152  //
153  AsciiTraceHelper asciiTraceHelper;
154 
155  std::string filename;
156  if (explicitFilename)
157  {
158  filename = prefix;
159  }
160  else
161  {
162  filename = asciiTraceHelper.GetFilenameFromDevice (prefix, device);
163  }
164 
165  Ptr<OutputStreamWrapper> theStream = asciiTraceHelper.CreateFileStream (filename);
166 
167  //
168  // The MacRx trace source provides our "r" event.
169  //
170  asciiTraceHelper.HookDefaultReceiveSinkWithoutContext<CsmaNetDevice> (device, "MacRx", theStream);
171 
172  //
173  // The "+", '-', and 'd' events are driven by trace sources actually in the
174  // transmit queue.
175  //
176  Ptr<Queue<Packet> > queue = device->GetQueue ();
177  asciiTraceHelper.HookDefaultEnqueueSinkWithoutContext<Queue<Packet> > (queue, "Enqueue", theStream);
178  asciiTraceHelper.HookDefaultDropSinkWithoutContext<Queue<Packet> > (queue, "Drop", theStream);
179  asciiTraceHelper.HookDefaultDequeueSinkWithoutContext<Queue<Packet> > (queue, "Dequeue", theStream);
180 
181  return;
182  }
183 
184  //
185  // If we are provided an OutputStreamWrapper, we are expected to use it, and
186  // to providd a context. We are free to come up with our own context if we
187  // want, and use the AsciiTraceHelper Hook*WithContext functions, but for
188  // compatibility and simplicity, we just use Config::Connect and let it deal
189  // with the context.
190  //
191  // Note that we are going to use the default trace sinks provided by the
192  // ascii trace helper. There is actually no AsciiTraceHelper in sight here,
193  // but the default trace sinks are actually publicly available static
194  // functions that are always there waiting for just such a case.
195  //
196  uint32_t nodeid = nd->GetNode ()->GetId ();
197  uint32_t deviceid = nd->GetIfIndex ();
198  std::ostringstream oss;
199 
200  oss << "/NodeList/" << nd->GetNode ()->GetId () << "/DeviceList/" << deviceid << "/$ns3::CsmaNetDevice/MacRx";
202 
203  oss.str ("");
204  oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::CsmaNetDevice/TxQueue/Enqueue";
206 
207  oss.str ("");
208  oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::CsmaNetDevice/TxQueue/Dequeue";
210 
211  oss.str ("");
212  oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::CsmaNetDevice/TxQueue/Drop";
214 }
215 
218 {
220  return Install (node, channel);
221 }
222 
224 CsmaHelper::Install (std::string nodeName) const
225 {
226  Ptr<Node> node = Names::Find<Node> (nodeName);
227  return Install (node);
228 }
229 
232 {
233  return NetDeviceContainer (InstallPriv (node, channel));
234 }
235 
237 CsmaHelper::Install (Ptr<Node> node, std::string channelName) const
238 {
239  Ptr<CsmaChannel> channel = Names::Find<CsmaChannel> (channelName);
240  return NetDeviceContainer (InstallPriv (node, channel));
241 }
242 
244 CsmaHelper::Install (std::string nodeName, Ptr<CsmaChannel> channel) const
245 {
246  Ptr<Node> node = Names::Find<Node> (nodeName);
247  return NetDeviceContainer (InstallPriv (node, channel));
248 }
249 
251 CsmaHelper::Install (std::string nodeName, std::string channelName) const
252 {
253  Ptr<Node> node = Names::Find<Node> (nodeName);
254  Ptr<CsmaChannel> channel = Names::Find<CsmaChannel> (channelName);
255  return NetDeviceContainer (InstallPriv (node, channel));
256 }
257 
260 {
262 
263  return Install (c, channel);
264 }
265 
268 {
269  NetDeviceContainer devs;
270 
271  for (NodeContainer::Iterator i = c.Begin (); i != c.End (); i++)
272  {
273  devs.Add (InstallPriv (*i, channel));
274  }
275 
276  return devs;
277 }
278 
280 CsmaHelper::Install (const NodeContainer &c, std::string channelName) const
281 {
282  Ptr<CsmaChannel> channel = Names::Find<CsmaChannel> (channelName);
283  return Install (c, channel);
284 }
285 
286 int64_t
288 {
289  int64_t currentStream = stream;
290  Ptr<NetDevice> netDevice;
291  for (NetDeviceContainer::Iterator i = c.Begin (); i != c.End (); ++i)
292  {
293  netDevice = (*i);
294  Ptr<CsmaNetDevice> csma = DynamicCast<CsmaNetDevice> (netDevice);
295  if (csma)
296  {
297  currentStream += csma->AssignStreams (currentStream);
298  }
299  }
300  return (currentStream - stream);
301 }
302 
305 {
307  device->SetAddress (Mac48Address::Allocate ());
308  node->AddDevice (device);
310  device->SetQueue (queue);
311  device->Attach (channel);
312 
313  return device;
314 }
315 
316 } // namespace ns3
tuple channel
Definition: third.py:85
Iterator Begin(void) const
Get an iterator which refers to the first NetDevice in the container.
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:71
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.
Ptr< Queue< Packet > > GetQueue(void) const
Get a copy of the attached Queue.
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:459
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:1686
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:201
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:77
NetDeviceContainer Install(Ptr< Node > node) const
This method creates an ns3::CsmaChannel with the attributes configured by CsmaHelper::SetChannelAttri...
Definition: csma-helper.cc:217
Iterator End(void) const
Get an iterator which indicates past-the-last Node in the container.
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:277
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. ...
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 '>'. ...
Definition: queue.cc:104
CsmaHelper()
Construct a CsmaHelper.
Definition: csma-helper.cc:41
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.
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.
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
void Connect(std::string path, const CallbackBase &cb)
Definition: config.cc:843
static void DefaultReceiveSinkWithContext(Ptr< OutputStreamWrapper > file, std::string context, Ptr< const Packet > p)
Basic Receive default trace sink.
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.
A Device for a Csma Network Link.
Iterator Begin(void) const
Get an iterator which refers to the first Node in the container.
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:65
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:116
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:287
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.
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:304
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:49
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 End(void) const
Get an iterator which indicates past-the-last NetDevice 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
tuple csma
Definition: second.py:63