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
38namespace ns3 {
39
40NS_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");
48}
49
50void
51CsmaHelper::SetQueue (std::string type,
52 std::string n1, const AttributeValue &v1,
53 std::string n2, const AttributeValue &v2,
54 std::string n3, const AttributeValue &v3,
55 std::string n4, const AttributeValue &v4)
56{
58
60 m_queueFactory.Set (n1, v1);
61 m_queueFactory.Set (n2, v2);
62 m_queueFactory.Set (n3, v3);
63 m_queueFactory.Set (n4, v4);
64}
65
66void
68{
69 m_deviceFactory.Set (n1, v1);
70}
71
72void
74{
75 m_channelFactory.Set (n1, v1);
76}
77
78void
80{
81 m_enableFlowControl = false;
82}
83
84void
85CsmaHelper::EnablePcapInternal (std::string prefix, Ptr<NetDevice> nd, bool promiscuous, bool explicitFilename)
86{
87 //
88 // All of the Pcap enable functions vector through here including the ones
89 // that are wandering through all of devices on perhaps all of the nodes in
90 // the system. We can only deal with devices of type CsmaNetDevice.
91 //
93 if (device == 0)
94 {
95 NS_LOG_INFO ("CsmaHelper::EnablePcapInternal(): Device " << device << " not of type ns3::CsmaNetDevice");
96 return;
97 }
98
99 PcapHelper pcapHelper;
100
101 std::string filename;
102 if (explicitFilename)
103 {
104 filename = prefix;
105 }
106 else
107 {
108 filename = pcapHelper.GetFilenameFromDevice (prefix, device);
109 }
110
111 Ptr<PcapFileWrapper> file = pcapHelper.CreateFile (filename, std::ios::out,
113 if (promiscuous)
114 {
115 pcapHelper.HookDefaultSink<CsmaNetDevice> (device, "PromiscSniffer", file);
116 }
117 else
118 {
119 pcapHelper.HookDefaultSink<CsmaNetDevice> (device, "Sniffer", file);
120 }
121}
122
123void
126 std::string prefix,
128 bool explicitFilename)
129{
130 //
131 // All of the ascii enable functions vector through here including the ones
132 // that are wandering through all of devices on perhaps all of the nodes in
133 // the system. We can only deal with devices of type CsmaNetDevice.
134 //
136 if (device == 0)
137 {
138 NS_LOG_INFO ("CsmaHelper::EnableAsciiInternal(): Device " << device << " not of type ns3::CsmaNetDevice");
139 return;
140 }
141
142 //
143 // Our default trace sinks are going to use packet printing, so we have to
144 // make sure that is turned on.
145 //
147
148 //
149 // If we are not provided an OutputStreamWrapper, we are expected to create
150 // one using the usual trace filename conventions and do a Hook*WithoutContext
151 // since there will be one file per context and therefore the context would
152 // be redundant.
153 //
154 if (stream == 0)
155 {
156 //
157 // Set up an output stream object to deal with private ofstream copy
158 // constructor and lifetime issues. Let the helper decide the actual
159 // name of the file given the prefix.
160 //
161 AsciiTraceHelper asciiTraceHelper;
162
163 std::string filename;
164 if (explicitFilename)
165 {
166 filename = prefix;
167 }
168 else
169 {
170 filename = asciiTraceHelper.GetFilenameFromDevice (prefix, device);
171 }
172
173 Ptr<OutputStreamWrapper> theStream = asciiTraceHelper.CreateFileStream (filename);
174
175 //
176 // The MacRx trace source provides our "r" event.
177 //
178 asciiTraceHelper.HookDefaultReceiveSinkWithoutContext<CsmaNetDevice> (device, "MacRx", theStream);
179
180 //
181 // The "+", '-', and 'd' events are driven by trace sources actually in the
182 // transmit queue.
183 //
184 Ptr<Queue<Packet> > queue = device->GetQueue ();
185 asciiTraceHelper.HookDefaultEnqueueSinkWithoutContext<Queue<Packet> > (queue, "Enqueue", theStream);
186 asciiTraceHelper.HookDefaultDropSinkWithoutContext<Queue<Packet> > (queue, "Drop", theStream);
187 asciiTraceHelper.HookDefaultDequeueSinkWithoutContext<Queue<Packet> > (queue, "Dequeue", theStream);
188
189 return;
190 }
191
192 //
193 // If we are provided an OutputStreamWrapper, we are expected to use it, and
194 // to providd a context. We are free to come up with our own context if we
195 // want, and use the AsciiTraceHelper Hook*WithContext functions, but for
196 // compatibility and simplicity, we just use Config::Connect and let it deal
197 // with the context.
198 //
199 // Note that we are going to use the default trace sinks provided by the
200 // ascii trace helper. There is actually no AsciiTraceHelper in sight here,
201 // but the default trace sinks are actually publicly available static
202 // functions that are always there waiting for just such a case.
203 //
204 uint32_t nodeid = nd->GetNode ()->GetId ();
205 uint32_t deviceid = nd->GetIfIndex ();
206 std::ostringstream oss;
207
208 oss << "/NodeList/" << nd->GetNode ()->GetId () << "/DeviceList/" << deviceid << "/$ns3::CsmaNetDevice/MacRx";
210
211 oss.str ("");
212 oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::CsmaNetDevice/TxQueue/Enqueue";
214
215 oss.str ("");
216 oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::CsmaNetDevice/TxQueue/Dequeue";
218
219 oss.str ("");
220 oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::CsmaNetDevice/TxQueue/Drop";
222}
223
226{
228 return Install (node, channel);
229}
230
232CsmaHelper::Install (std::string nodeName) const
233{
234 Ptr<Node> node = Names::Find<Node> (nodeName);
235 return Install (node);
236}
237
240{
241 return NetDeviceContainer (InstallPriv (node, channel));
242}
243
245CsmaHelper::Install (Ptr<Node> node, std::string channelName) const
246{
247 Ptr<CsmaChannel> channel = Names::Find<CsmaChannel> (channelName);
248 return NetDeviceContainer (InstallPriv (node, channel));
249}
250
252CsmaHelper::Install (std::string nodeName, Ptr<CsmaChannel> channel) const
253{
254 Ptr<Node> node = Names::Find<Node> (nodeName);
255 return NetDeviceContainer (InstallPriv (node, channel));
256}
257
259CsmaHelper::Install (std::string nodeName, std::string channelName) const
260{
261 Ptr<Node> node = Names::Find<Node> (nodeName);
262 Ptr<CsmaChannel> channel = Names::Find<CsmaChannel> (channelName);
263 return NetDeviceContainer (InstallPriv (node, channel));
264}
265
268{
270
271 return Install (c, channel);
272}
273
276{
278
279 for (NodeContainer::Iterator i = c.Begin (); i != c.End (); i++)
280 {
281 devs.Add (InstallPriv (*i, channel));
282 }
283
284 return devs;
285}
286
288CsmaHelper::Install (const NodeContainer &c, std::string channelName) const
289{
290 Ptr<CsmaChannel> channel = Names::Find<CsmaChannel> (channelName);
291 return Install (c, channel);
292}
293
294int64_t
296{
297 int64_t currentStream = stream;
298 Ptr<NetDevice> netDevice;
299 for (NetDeviceContainer::Iterator i = c.Begin (); i != c.End (); ++i)
300 {
301 netDevice = (*i);
302 Ptr<CsmaNetDevice> csma = DynamicCast<CsmaNetDevice> (netDevice);
303 if (csma)
304 {
305 currentStream += csma->AssignStreams (currentStream);
306 }
307 }
308 return (currentStream - stream);
309}
310
313{
315 device->SetAddress (Mac48Address::Allocate ());
316 node->AddDevice (device);
318 device->SetQueue (queue);
319 device->Attach (channel);
321 {
322 // Aggregate a NetDeviceQueueInterface object
323 Ptr<NetDeviceQueueInterface> ndqi = CreateObject<NetDeviceQueueInterface> ();
324 ndqi->GetTxQueue (0)->ConnectQueueTraces (queue);
325 device->AggregateObject (ndqi);
326 }
327 return device;
328}
329
330} // namespace ns3
Manage ASCII trace files for device models.
Definition: trace-helper.h:163
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
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...
static void DefaultDropSinkWithContext(Ptr< OutputStreamWrapper > file, std::string context, Ptr< const Packet > p)
Basic Drop default trace sink.
static void DefaultReceiveSinkWithContext(Ptr< OutputStreamWrapper > file, std::string context, Ptr< const Packet > p)
Basic Receive default trace sink.
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:462
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
static void DefaultEnqueueSinkWithContext(Ptr< OutputStreamWrapper > file, std::string context, Ptr< const Packet > p)
Basic Enqueue default trace sink.
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
static void DefaultDequeueSinkWithContext(Ptr< OutputStreamWrapper > file, std::string context, Ptr< const Packet > p)
Basic Dequeue default trace sink.
Hold a value for an Attribute.
Definition: attribute.h:69
Csma Channel.
Definition: csma-channel.h:91
void DisableFlowControl(void)
Disable flow control only if you know what you are doing.
Definition: csma-helper.cc:79
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:85
ObjectFactory m_channelFactory
factory for the channel
Definition: csma-helper.h:262
void SetDeviceAttribute(std::string n1, const AttributeValue &v1)
Definition: csma-helper.cc:67
bool m_enableFlowControl
whether to enable flow control
Definition: csma-helper.h:263
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:312
void SetChannelAttribute(std::string n1, const AttributeValue &v1)
Definition: csma-helper.cc:73
CsmaHelper()
Construct a CsmaHelper.
Definition: csma-helper.cc:42
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:295
NetDeviceContainer Install(Ptr< Node > node) const
This method creates an ns3::CsmaChannel with the attributes configured by CsmaHelper::SetChannelAttri...
Definition: csma-helper.cc:225
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:51
ObjectFactory m_queueFactory
factory for the queues
Definition: csma-helper.h:260
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:124
ObjectFactory m_deviceFactory
factory for the NetDevices
Definition: csma-helper.h:261
A Device for a Csma Network Link.
Ptr< Queue< Packet > > GetQueue(void) const
Get a copy of the attached Queue.
static Mac48Address Allocate(void)
Allocate a new Mac48Address.
holds a vector of ns3::NetDevice pointers
std::vector< Ptr< NetDevice > >::const_iterator Iterator
NetDevice container iterator.
Iterator End(void) const
Get an iterator which indicates past-the-last NetDevice in the container.
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.
virtual uint32_t GetIfIndex(void) const =0
virtual Ptr< Node > GetNode(void) const =0
keep track of a set of node pointers.
std::vector< Ptr< Node > >::const_iterator Iterator
Node container iterator.
Iterator Begin(void) const
Get an iterator which refers to the first Node in the container.
Iterator End(void) const
Get an iterator which indicates past-the-last Node in the container.
uint32_t GetId(void) const
Definition: node.cc:109
uint32_t AddDevice(Ptr< NetDevice > device)
Associate a NetDevice to this node.
Definition: node.cc:130
void Set(const std::string &name, const AttributeValue &value, Args &&... args)
Set an attribute to be set during construction.
Ptr< Object > Create(void) const
Create an Object instance of the configured TypeId.
void SetTypeId(TypeId tid)
Set the TypeId of the Objects to be created by this factory.
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:470
static void EnablePrinting(void)
Enable printing packets metadata.
Definition: packet.cc:572
Manage pcap files for device models.
Definition: trace-helper.h:39
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
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
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
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
void Connect(std::string path, const CallbackBase &cb)
Definition: config.cc:920
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:281
Callback< R > MakeBoundCallback(R(*fnPtr)(TX), ARG a1)
Make Callbacks with one bound argument.
Definition: callback.h:1709
Every class exported by the ns3 library is enclosed in the ns3 namespace.
csma
Definition: second.py:63
channel
Definition: third.py:92