A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 NS_LOG_COMPONENT_DEFINE ("CsmaHelper");
38 
39 namespace ns3 {
40 
42 {
43  m_queueFactory.SetTypeId ("ns3::DropTailQueue");
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  m_queueFactory.Set (n1, v1);
57  m_queueFactory.Set (n2, v2);
58  m_queueFactory.Set (n3, v3);
59  m_queueFactory.Set (n4, v4);
60 }
61 
62 void
64 {
65  m_deviceFactory.Set (n1, v1);
66 }
67 
68 void
70 {
71  m_channelFactory.Set (n1, v1);
72 }
73 
74 void
75 CsmaHelper::EnablePcapInternal (std::string prefix, Ptr<NetDevice> nd, bool promiscuous, bool explicitFilename)
76 {
77  //
78  // All of the Pcap enable functions vector through here including the ones
79  // that are wandering through all of devices on perhaps all of the nodes in
80  // the system. We can only deal with devices of type CsmaNetDevice.
81  //
82  Ptr<CsmaNetDevice> device = nd->GetObject<CsmaNetDevice> ();
83  if (device == 0)
84  {
85  NS_LOG_INFO ("CsmaHelper::EnablePcapInternal(): Device " << device << " not of type ns3::CsmaNetDevice");
86  return;
87  }
88 
89  PcapHelper pcapHelper;
90 
91  std::string filename;
92  if (explicitFilename)
93  {
94  filename = prefix;
95  }
96  else
97  {
98  filename = pcapHelper.GetFilenameFromDevice (prefix, device);
99  }
100 
101  Ptr<PcapFileWrapper> file = pcapHelper.CreateFile (filename, std::ios::out,
103  if (promiscuous)
104  {
105  pcapHelper.HookDefaultSink<CsmaNetDevice> (device, "PromiscSniffer", file);
106  }
107  else
108  {
109  pcapHelper.HookDefaultSink<CsmaNetDevice> (device, "Sniffer", file);
110  }
111 }
112 
113 void
115  Ptr<OutputStreamWrapper> stream,
116  std::string prefix,
117  Ptr<NetDevice> nd,
118  bool explicitFilename)
119 {
120  //
121  // All of the ascii enable functions vector through here including the ones
122  // that are wandering through all of devices on perhaps all of the nodes in
123  // the system. We can only deal with devices of type CsmaNetDevice.
124  //
125  Ptr<CsmaNetDevice> device = nd->GetObject<CsmaNetDevice> ();
126  if (device == 0)
127  {
128  NS_LOG_INFO ("CsmaHelper::EnableAsciiInternal(): Device " << device << " not of type ns3::CsmaNetDevice");
129  return;
130  }
131 
132  //
133  // Our default trace sinks are going to use packet printing, so we have to
134  // make sure that is turned on.
135  //
137 
138  //
139  // If we are not provided an OutputStreamWrapper, we are expected to create
140  // one using the usual trace filename conventions and do a Hook*WithoutContext
141  // since there will be one file per context and therefore the context would
142  // be redundant.
143  //
144  if (stream == 0)
145  {
146  //
147  // Set up an output stream object to deal with private ofstream copy
148  // constructor and lifetime issues. Let the helper decide the actual
149  // name of the file given the prefix.
150  //
151  AsciiTraceHelper asciiTraceHelper;
152 
153  std::string filename;
154  if (explicitFilename)
155  {
156  filename = prefix;
157  }
158  else
159  {
160  filename = asciiTraceHelper.GetFilenameFromDevice (prefix, device);
161  }
162 
163  Ptr<OutputStreamWrapper> theStream = asciiTraceHelper.CreateFileStream (filename);
164 
165  //
166  // The MacRx trace source provides our "r" event.
167  //
168  asciiTraceHelper.HookDefaultReceiveSinkWithoutContext<CsmaNetDevice> (device, "MacRx", theStream);
169 
170  //
171  // The "+", '-', and 'd' events are driven by trace sources actually in the
172  // transmit queue.
173  //
174  Ptr<Queue> queue = device->GetQueue ();
175  asciiTraceHelper.HookDefaultEnqueueSinkWithoutContext<Queue> (queue, "Enqueue", theStream);
176  asciiTraceHelper.HookDefaultDropSinkWithoutContext<Queue> (queue, "Drop", theStream);
177  asciiTraceHelper.HookDefaultDequeueSinkWithoutContext<Queue> (queue, "Dequeue", theStream);
178 
179  return;
180  }
181 
182  //
183  // If we are provided an OutputStreamWrapper, we are expected to use it, and
184  // to providd a context. We are free to come up with our own context if we
185  // want, and use the AsciiTraceHelper Hook*WithContext functions, but for
186  // compatibility and simplicity, we just use Config::Connect and let it deal
187  // with the context.
188  //
189  // Note that we are going to use the default trace sinks provided by the
190  // ascii trace helper. There is actually no AsciiTraceHelper in sight here,
191  // but the default trace sinks are actually publicly available static
192  // functions that are always there waiting for just such a case.
193  //
194  uint32_t nodeid = nd->GetNode ()->GetId ();
195  uint32_t deviceid = nd->GetIfIndex ();
196  std::ostringstream oss;
197 
198  oss << "/NodeList/" << nd->GetNode ()->GetId () << "/DeviceList/" << deviceid << "/$ns3::CsmaNetDevice/MacRx";
200 
201  oss.str ("");
202  oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::CsmaNetDevice/TxQueue/Enqueue";
204 
205  oss.str ("");
206  oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::CsmaNetDevice/TxQueue/Dequeue";
208 
209  oss.str ("");
210  oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::CsmaNetDevice/TxQueue/Drop";
212 }
213 
216 {
218  return Install (node, channel);
219 }
220 
222 CsmaHelper::Install (std::string nodeName) const
223 {
224  Ptr<Node> node = Names::Find<Node> (nodeName);
225  return Install (node);
226 }
227 
230 {
231  return NetDeviceContainer (InstallPriv (node, channel));
232 }
233 
235 CsmaHelper::Install (Ptr<Node> node, std::string channelName) const
236 {
237  Ptr<CsmaChannel> channel = Names::Find<CsmaChannel> (channelName);
238  return NetDeviceContainer (InstallPriv (node, channel));
239 }
240 
242 CsmaHelper::Install (std::string nodeName, Ptr<CsmaChannel> channel) const
243 {
244  Ptr<Node> node = Names::Find<Node> (nodeName);
245  return NetDeviceContainer (InstallPriv (node, channel));
246 }
247 
249 CsmaHelper::Install (std::string nodeName, std::string channelName) const
250 {
251  Ptr<Node> node = Names::Find<Node> (nodeName);
252  Ptr<CsmaChannel> channel = Names::Find<CsmaChannel> (channelName);
253  return NetDeviceContainer (InstallPriv (node, channel));
254 }
255 
258 {
260 
261  return Install (c, channel);
262 }
263 
266 {
267  NetDeviceContainer devs;
268 
269  for (NodeContainer::Iterator i = c.Begin (); i != c.End (); i++)
270  {
271  devs.Add (InstallPriv (*i, channel));
272  }
273 
274  return devs;
275 }
276 
278 CsmaHelper::Install (const NodeContainer &c, std::string channelName) const
279 {
280  Ptr<CsmaChannel> channel = Names::Find<CsmaChannel> (channelName);
281  return Install (c, channel);
282 }
283 
284 int64_t
286 {
287  int64_t currentStream = stream;
288  Ptr<NetDevice> netDevice;
289  for (NetDeviceContainer::Iterator i = c.Begin (); i != c.End (); ++i)
290  {
291  netDevice = (*i);
292  Ptr<CsmaNetDevice> csma = DynamicCast<CsmaNetDevice> (netDevice);
293  if (csma)
294  {
295  currentStream += csma->AssignStreams (currentStream);
296  }
297  }
298  return (currentStream - stream);
299 }
300 
303 {
305  device->SetAddress (Mac48Address::Allocate ());
306  node->AddDevice (device);
308  device->SetQueue (queue);
309  device->Attach (channel);
310 
311  return device;
312 }
313 
314 } // namespace ns3
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:128
smart pointer class similar to boost::intrusive_ptr
Definition: ptr.h:59
void SetChannelAttribute(std::string n1, const AttributeValue &v1)
Definition: csma-helper.cc:69
static void DefaultEnqueueSinkWithContext(Ptr< OutputStreamWrapper > file, std::string context, Ptr< const Packet > p)
Ptr< PcapFileWrapper > CreateFile(std::string filename, std::ios::openmode filemode, uint32_t dataLinkType, uint32_t snapLen=65535, int32_t tzCorrection=0)
Create and initialize a pcap file.
Definition: trace-helper.cc:49
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:321
std::vector< Ptr< Node > >::const_iterator Iterator
Hold a value for an Attribute.
Definition: attribute.h:56
Manage pcap files for device models.
Definition: trace-helper.h:38
Callback< R > MakeBoundCallback(R(*fnPtr)(TX), ARG a1)
Build bound Callbacks which take varying numbers of arguments, and potentially returning a value...
Definition: callback.h:1463
static void DefaultDropSinkWithContext(Ptr< OutputStreamWrapper > file, std::string context, Ptr< const Packet > p)
void SetTypeId(TypeId tid)
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:75
NetDeviceContainer Install(Ptr< Node > node) const
This method creates an ns3::CsmaChannel with the attributes configured by CsmaHelper::SetChannelAttri...
Definition: csma-helper.cc:215
Iterator End(void) const
Get an iterator which indicates past-the-last Node in the container.
#define NS_LOG_INFO(msg)
Definition: log.h:298
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 Connect(std::string path, const CallbackBase &cb)
Definition: config.cc:728
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:299
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...
Abstract base class for packet Queues.
Definition: queue.h:45
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)
By default, packets do not keep around enough metadata to perform the operations requested by the Pri...
Definition: packet.cc:552
Ptr< Object > Create(void) const
void Add(NetDeviceContainer other)
Append the contents of another NetDeviceContainer to the end of this container.
ObjectFactory m_deviceFactory
Definition: csma-helper.h:246
holds a vector of ns3::NetDevice pointers
ObjectFactory m_channelFactory
Definition: csma-helper.h:247
Ptr< Queue > GetQueue(void) const
Get a copy of the attached Queue.
static void DefaultReceiveSinkWithContext(Ptr< OutputStreamWrapper > file, std::string context, Ptr< const Packet > p)
Csma Channel.
Definition: csma-channel.h:76
static void DefaultDequeueSinkWithContext(Ptr< OutputStreamWrapper > file, std::string context, Ptr< const Packet > p)
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.
NS_LOG_COMPONENT_DEFINE("CsmaHelper")
void Set(std::string name, const AttributeValue &value)
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:365
ObjectFactory m_queueFactory
Definition: csma-helper.h:245
void SetDeviceAttribute(std::string n1, const AttributeValue &v1)
Definition: csma-helper.cc:63
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:114
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:285
uint32_t AddDevice(Ptr< NetDevice > device)
Definition: node.cc:118
std::vector< Ptr< NetDevice > >::const_iterator Iterator
Ptr< NetDevice > InstallPriv(Ptr< Node > node, Ptr< CsmaChannel > channel) const
Definition: csma-helper.cc:302
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:114
Iterator End(void) const
Get an iterator which indicates past-the-last NetDevice in the container.
Ptr< T > GetObject(void) const
Definition: object.h:361
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:343