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
emu-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 University of Washington
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
19
#include <string>
20
21
#include "ns3/log.h"
22
#include "ns3/simulator.h"
23
#include "ns3/object-factory.h"
24
#include "ns3/names.h"
25
#include "ns3/queue.h"
26
#include "ns3/emu-net-device.h"
27
#include "ns3/config.h"
28
#include "ns3/packet.h"
29
30
#include "ns3/trace-helper.h"
31
#include "
emu-helper.h
"
32
33
NS_LOG_COMPONENT_DEFINE
(
"EmuHelper"
);
34
35
namespace
ns3 {
36
37
EmuHelper::EmuHelper
()
38
{
39
NS_LOG_FUNCTION_NOARGS
();
40
m_queueFactory
.
SetTypeId
(
"ns3::DropTailQueue"
);
41
m_deviceFactory
.
SetTypeId
(
"ns3::EmuNetDevice"
);
42
}
43
44
void
45
EmuHelper::SetQueue
(
46
std::string type,
47
std::string n1,
const
AttributeValue
&v1,
48
std::string n2,
const
AttributeValue
&v2,
49
std::string n3,
const
AttributeValue
&v3,
50
std::string n4,
const
AttributeValue
&v4)
51
{
52
NS_LOG_FUNCTION_NOARGS
();
53
m_queueFactory
.
SetTypeId
(type);
54
m_queueFactory
.
Set
(n1, v1);
55
m_queueFactory
.
Set
(n2, v2);
56
m_queueFactory
.
Set
(n3, v3);
57
m_queueFactory
.
Set
(n4, v4);
58
}
59
60
void
61
EmuHelper::SetAttribute
(std::string n1,
const
AttributeValue
&v1)
62
{
63
NS_LOG_FUNCTION_NOARGS
();
64
m_deviceFactory
.
Set
(n1, v1);
65
}
66
67
void
68
EmuHelper::EnablePcapInternal
(std::string prefix,
Ptr<NetDevice>
nd,
bool
promiscuous,
bool
explicitFilename)
69
{
70
//
71
// All of the Pcap enable functions vector through here including the ones
72
// that are wandering through all of devices on perhaps all of the nodes in
73
// the system. We can only deal with devices of type EmuNetDevice.
74
//
75
Ptr<EmuNetDevice>
device = nd->
GetObject
<
EmuNetDevice
> ();
76
if
(device == 0)
77
{
78
NS_LOG_INFO
(
"EmuHelper::EnablePcapInternal(): Device "
<< device <<
" not of type ns3::EmuNetDevice"
);
79
return
;
80
}
81
82
PcapHelper
pcapHelper;
83
84
std::string filename;
85
if
(explicitFilename)
86
{
87
filename = prefix;
88
}
89
else
90
{
91
filename = pcapHelper.
GetFilenameFromDevice
(prefix, device);
92
}
93
94
Ptr<PcapFileWrapper>
file = pcapHelper.
CreateFile
(filename, std::ios::out,
PcapHelper::DLT_EN10MB
);
95
if
(promiscuous)
96
{
97
pcapHelper.
HookDefaultSink
<
EmuNetDevice
> (device,
"PromiscSniffer"
, file);
98
}
99
else
100
{
101
pcapHelper.
HookDefaultSink
<
EmuNetDevice
> (device,
"Sniffer"
, file);
102
}
103
}
104
105
void
106
EmuHelper::EnableAsciiInternal
(
107
Ptr<OutputStreamWrapper>
stream,
108
std::string prefix,
109
Ptr<NetDevice>
nd,
110
bool
explicitFilename)
111
{
112
//
113
// All of the ascii enable functions vector through here including the ones
114
// that are wandering through all of devices on perhaps all of the nodes in
115
// the system. We can only deal with devices of type EmuNetDevice.
116
//
117
Ptr<EmuNetDevice>
device = nd->
GetObject
<
EmuNetDevice
> ();
118
if
(device == 0)
119
{
120
NS_LOG_INFO
(
"EmuHelper::EnableAsciiInternal(): Device "
<< device <<
" not of type ns3::EmuNetDevice"
);
121
return
;
122
}
123
124
//
125
// Our default trace sinks are going to use packet printing, so we have to
126
// make sure that is turned on.
127
//
128
Packet::EnablePrinting
();
129
130
//
131
// If we are not provided an OutputStreamWrapper, we are expected to create
132
// one using the usual trace filename conventions and do a Hook*WithoutContext
133
// since there will be one file per context and therefore the context would
134
// be redundant.
135
//
136
if
(stream == 0)
137
{
138
//
139
// Set up an output stream object to deal with private ofstream copy
140
// constructor and lifetime issues. Let the helper decide the actual
141
// name of the file given the prefix.
142
//
143
AsciiTraceHelper
asciiTraceHelper;
144
145
std::string filename;
146
if
(explicitFilename)
147
{
148
filename = prefix;
149
}
150
else
151
{
152
filename = asciiTraceHelper.
GetFilenameFromDevice
(prefix, device);
153
}
154
155
Ptr<OutputStreamWrapper>
theStream = asciiTraceHelper.
CreateFileStream
(filename);
156
157
//
158
// The MacRx trace source provides our "r" event.
159
//
160
asciiTraceHelper.
HookDefaultReceiveSinkWithoutContext
<
EmuNetDevice
> (device,
"MacRx"
, theStream);
161
162
//
163
// The "+", '-', and 'd' events are driven by trace sources actually in the
164
// transmit queue.
165
//
166
Ptr<Queue>
queue = device->
GetQueue
();
167
asciiTraceHelper.
HookDefaultEnqueueSinkWithoutContext
<
Queue
> (queue,
"Enqueue"
, theStream);
168
asciiTraceHelper.
HookDefaultDropSinkWithoutContext
<
Queue
> (queue,
"Drop"
, theStream);
169
asciiTraceHelper.
HookDefaultDequeueSinkWithoutContext
<
Queue
> (queue,
"Dequeue"
, theStream);
170
171
return
;
172
}
173
174
//
175
// If we are provided an OutputStreamWrapper, we are expected to use it, and
176
// to providd a context. We are free to come up with our own context if we
177
// want, and use the AsciiTraceHelper Hook*WithContext functions, but for
178
// compatibility and simplicity, we just use Config::Connect and let it deal
179
// with the context.
180
//
181
// Note that we are going to use the default trace sinks provided by the
182
// ascii trace helper. There is actually no AsciiTraceHelper in sight here,
183
// but the default trace sinks are actually publicly available static
184
// functions that are always there waiting for just such a case.
185
//
186
uint32_t nodeid = nd->
GetNode
()->
GetId
();
187
uint32_t deviceid = nd->
GetIfIndex
();
188
std::ostringstream oss;
189
190
oss <<
"/NodeList/"
<< nd->
GetNode
()->
GetId
() <<
"/DeviceList/"
<< deviceid <<
"/$ns3::EmuNetDevice/MacRx"
;
191
Config::Connect
(oss.str (),
MakeBoundCallback
(&
AsciiTraceHelper::DefaultReceiveSinkWithContext
, stream));
192
193
oss.str (
""
);
194
oss <<
"/NodeList/"
<< nodeid <<
"/DeviceList/"
<< deviceid <<
"/$ns3::EmuNetDevice/TxQueue/Enqueue"
;
195
Config::Connect
(oss.str (),
MakeBoundCallback
(&
AsciiTraceHelper::DefaultEnqueueSinkWithContext
, stream));
196
197
oss.str (
""
);
198
oss <<
"/NodeList/"
<< nodeid <<
"/DeviceList/"
<< deviceid <<
"/$ns3::EmuNetDevice/TxQueue/Dequeue"
;
199
Config::Connect
(oss.str (),
MakeBoundCallback
(&
AsciiTraceHelper::DefaultDequeueSinkWithContext
, stream));
200
201
oss.str (
""
);
202
oss <<
"/NodeList/"
<< nodeid <<
"/DeviceList/"
<< deviceid <<
"/$ns3::EmuNetDevice/TxQueue/Drop"
;
203
Config::Connect
(oss.str (),
MakeBoundCallback
(&
AsciiTraceHelper::DefaultDropSinkWithContext
, stream));
204
}
205
206
NetDeviceContainer
207
EmuHelper::Install
(
Ptr<Node>
node)
const
208
{
209
return
NetDeviceContainer
(
InstallPriv
(node));
210
}
211
212
NetDeviceContainer
213
EmuHelper::Install
(std::string nodeName)
const
214
{
215
Ptr<Node>
node = Names::Find<Node> (nodeName);
216
return
NetDeviceContainer
(
InstallPriv
(node));
217
}
218
219
NetDeviceContainer
220
EmuHelper::Install
(
const
NodeContainer
&c)
const
221
{
222
NetDeviceContainer
devs;
223
224
for
(
NodeContainer::Iterator
i = c.
Begin
(); i != c.
End
(); i++)
225
{
226
devs.
Add
(
InstallPriv
(*i));
227
}
228
229
return
devs;
230
}
231
232
Ptr<NetDevice>
233
EmuHelper::InstallPriv
(
Ptr<Node>
node)
const
234
{
235
Ptr<EmuNetDevice>
device =
m_deviceFactory
.
Create
<
EmuNetDevice
> ();
236
device->SetAddress (
Mac48Address::Allocate
());
237
node->
AddDevice
(device);
238
Ptr<Queue>
queue =
m_queueFactory
.
Create
<
Queue
> ();
239
device->SetQueue (queue);
240
241
return
device;
242
}
243
244
}
// namespace ns3
src
emu
helper
emu-helper.cc
Generated on Tue Oct 9 2012 16:45:37 for ns-3 by
1.8.1.2