A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Portuguese
Docs ▼
Wiki
Manual
Models
Develop ▼
API
Bugs
API
Main Page
Related Pages
Modules
Namespaces
Classes
Files
File List
File Members
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
41
CsmaHelper::CsmaHelper
()
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
{
55
m_queueFactory
.
SetTypeId
(type);
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
63
CsmaHelper::SetDeviceAttribute
(std::string n1,
const
AttributeValue
&v1)
64
{
65
m_deviceFactory
.
Set
(n1, v1);
66
}
67
68
void
69
CsmaHelper::SetChannelAttribute
(std::string n1,
const
AttributeValue
&v1)
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,
102
PcapHelper::DLT_EN10MB
);
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
114
CsmaHelper::EnableAsciiInternal
(
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
//
136
Packet::EnablePrinting
();
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"
;
199
Config::Connect
(oss.str (),
MakeBoundCallback
(&
AsciiTraceHelper::DefaultReceiveSinkWithContext
, stream));
200
201
oss.str (
""
);
202
oss <<
"/NodeList/"
<< nodeid <<
"/DeviceList/"
<< deviceid <<
"/$ns3::CsmaNetDevice/TxQueue/Enqueue"
;
203
Config::Connect
(oss.str (),
MakeBoundCallback
(&
AsciiTraceHelper::DefaultEnqueueSinkWithContext
, stream));
204
205
oss.str (
""
);
206
oss <<
"/NodeList/"
<< nodeid <<
"/DeviceList/"
<< deviceid <<
"/$ns3::CsmaNetDevice/TxQueue/Dequeue"
;
207
Config::Connect
(oss.str (),
MakeBoundCallback
(&
AsciiTraceHelper::DefaultDequeueSinkWithContext
, stream));
208
209
oss.str (
""
);
210
oss <<
"/NodeList/"
<< nodeid <<
"/DeviceList/"
<< deviceid <<
"/$ns3::CsmaNetDevice/TxQueue/Drop"
;
211
Config::Connect
(oss.str (),
MakeBoundCallback
(&
AsciiTraceHelper::DefaultDropSinkWithContext
, stream));
212
}
213
214
NetDeviceContainer
215
CsmaHelper::Install
(
Ptr<Node>
node)
const
216
{
217
Ptr<CsmaChannel>
channel =
m_channelFactory
.
Create
()->
GetObject
<
CsmaChannel
> ();
218
return
Install
(node, channel);
219
}
220
221
NetDeviceContainer
222
CsmaHelper::Install
(std::string nodeName)
const
223
{
224
Ptr<Node>
node = Names::Find<Node> (nodeName);
225
return
Install
(node);
226
}
227
228
NetDeviceContainer
229
CsmaHelper::Install
(
Ptr<Node>
node,
Ptr<CsmaChannel>
channel)
const
230
{
231
return
NetDeviceContainer
(
InstallPriv
(node, channel));
232
}
233
234
NetDeviceContainer
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
241
NetDeviceContainer
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
248
NetDeviceContainer
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
256
NetDeviceContainer
257
CsmaHelper::Install
(
const
NodeContainer
&c)
const
258
{
259
Ptr<CsmaChannel>
channel =
m_channelFactory
.
Create
()->
GetObject
<
CsmaChannel
> ();
260
261
return
Install
(c, channel);
262
}
263
264
NetDeviceContainer
265
CsmaHelper::Install
(
const
NodeContainer
&c,
Ptr<CsmaChannel>
channel)
const
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
277
NetDeviceContainer
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
285
CsmaHelper::AssignStreams
(
NetDeviceContainer
c, int64_t stream)
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
301
Ptr<NetDevice>
302
CsmaHelper::InstallPriv
(
Ptr<Node>
node,
Ptr<CsmaChannel>
channel)
const
303
{
304
Ptr<CsmaNetDevice>
device =
m_deviceFactory
.
Create
<
CsmaNetDevice
> ();
305
device->
SetAddress
(
Mac48Address::Allocate
());
306
node->
AddDevice
(device);
307
Ptr<Queue>
queue =
m_queueFactory
.
Create
<
Queue
> ();
308
device->
SetQueue
(queue);
309
device->
Attach
(channel);
310
311
return
device;
312
}
313
314
}
// namespace ns3
src
csma
helper
csma-helper.cc
Generated on Tue Oct 9 2012 16:45:36 for ns-3 by
1.8.1.2