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
fd-net-device-helper.cc
Go to the documentation of this file.
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
* Copyright (c) 2012 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: Alina Quereilhac <alina.quereilhac@inria.fr>
19
*
20
*/
21
22
#include "
fd-net-device-helper.h
"
23
24
#include "ns3/abort.h"
25
#include "ns3/config.h"
26
#include "ns3/fd-net-device.h"
27
#include "ns3/log.h"
28
#include "ns3/names.h"
29
#include "ns3/object-factory.h"
30
#include "ns3/packet.h"
31
#include "ns3/simulator.h"
32
#include "ns3/trace-helper.h"
33
34
#include <string>
35
36
NS_LOG_COMPONENT_DEFINE
(
"FdNetDeviceHelper"
);
37
38
namespace
ns3 {
39
40
FdNetDeviceHelper::FdNetDeviceHelper
()
41
{
42
m_deviceFactory
.
SetTypeId
(
"ns3::FdNetDevice"
);
43
}
44
45
void
46
FdNetDeviceHelper::SetAttribute
(std::string n1,
const
AttributeValue
&v1)
47
{
48
NS_LOG_FUNCTION
(
this
);
49
m_deviceFactory
.
Set
(n1, v1);
50
}
51
52
void
53
FdNetDeviceHelper::EnablePcapInternal
(std::string prefix,
Ptr<NetDevice>
nd,
bool
promiscuous,
bool
explicitFilename)
54
{
55
//
56
// All of the Pcap enable functions vector through here including the ones
57
// that are wandering through all of devices on perhaps all of the nodes in
58
// the system. We can only deal with devices of type FdNetDevice.
59
//
60
Ptr<FdNetDevice>
device = nd->
GetObject
<
FdNetDevice
> ();
61
if
(device == 0)
62
{
63
NS_LOG_INFO
(
"FdNetDeviceHelper::EnablePcapInternal(): Device "
<< device <<
" not of type ns3::FdNetDevice"
);
64
return
;
65
}
66
67
PcapHelper
pcapHelper;
68
69
std::string filename;
70
if
(explicitFilename)
71
{
72
filename = prefix;
73
}
74
else
75
{
76
filename = pcapHelper.
GetFilenameFromDevice
(prefix, device);
77
}
78
79
Ptr<PcapFileWrapper>
file = pcapHelper.
CreateFile
(filename, std::ios::out,
PcapHelper::DLT_EN10MB
);
80
if
(promiscuous)
81
{
82
pcapHelper.
HookDefaultSink
<
FdNetDevice
> (device,
"PromiscSniffer"
, file);
83
}
84
else
85
{
86
pcapHelper.
HookDefaultSink
<
FdNetDevice
> (device,
"Sniffer"
, file);
87
}
88
}
89
90
void
91
FdNetDeviceHelper::EnableAsciiInternal
(
92
Ptr<OutputStreamWrapper>
stream,
93
std::string prefix,
94
Ptr<NetDevice>
nd,
95
bool
explicitFilename)
96
{
97
//
98
// All of the ascii enable functions vector through here including the ones
99
// that are wandering through all of devices on perhaps all of the nodes in
100
// the system. We can only deal with devices of type FdNetDevice.
101
//
102
Ptr<FdNetDevice>
device = nd->
GetObject
<
FdNetDevice
> ();
103
if
(device == 0)
104
{
105
NS_LOG_INFO
(
"FdNetDeviceHelper::EnableAsciiInternal(): Device "
<< device <<
" not of type ns3::FdNetDevice"
);
106
return
;
107
}
108
109
//
110
// Our default trace sinks are going to use packet printing, so we have to
111
// make sure that is turned on.
112
//
113
Packet::EnablePrinting
();
114
115
//
116
// If we are not provided an OutputStreamWrapper, we are expected to create
117
// one using the usual trace filename conventions and do a Hook*WithoutContext
118
// since there will be one file per context and therefore the context would
119
// be redundant.
120
//
121
if
(stream == 0)
122
{
123
//
124
// Set up an output stream object to deal with private ofstream copy
125
// constructor and lifetime issues. Let the helper decide the actual
126
// name of the file given the prefix.
127
//
128
AsciiTraceHelper
asciiTraceHelper;
129
130
std::string filename;
131
if
(explicitFilename)
132
{
133
filename = prefix;
134
}
135
else
136
{
137
filename = asciiTraceHelper.
GetFilenameFromDevice
(prefix, device);
138
}
139
140
Ptr<OutputStreamWrapper>
theStream = asciiTraceHelper.
CreateFileStream
(filename);
141
142
//
143
// The MacRx trace source provides our "r" event.
144
//
145
asciiTraceHelper.
HookDefaultReceiveSinkWithoutContext
<
FdNetDevice
> (device,
"MacRx"
, theStream);
146
147
return
;
148
}
149
150
//
151
// If we are provided an OutputStreamWrapper, we are expected to use it, and
152
// to providd a context. We are free to come up with our own context if we
153
// want, and use the AsciiTraceHelper Hook*WithContext functions, but for
154
// compatibility and simplicity, we just use Config::Connect and let it deal
155
// with the context.
156
//
157
// Note that we are going to use the default trace sinks provided by the
158
// ascii trace helper. There is actually no AsciiTraceHelper in sight here,
159
// but the default trace sinks are actually publicly available static
160
// functions that are always there waiting for just such a case.
161
//
162
uint32_t deviceid = nd->
GetIfIndex
();
163
std::ostringstream oss;
164
165
oss <<
"/NodeList/"
<< nd->
GetNode
()->
GetId
() <<
"/DeviceList/"
<< deviceid <<
"/$ns3::FdNetDevice/MacRx"
;
166
Config::Connect
(oss.str (),
MakeBoundCallback
(&
AsciiTraceHelper::DefaultReceiveSinkWithContext
, stream));
167
}
168
169
NetDeviceContainer
170
FdNetDeviceHelper::Install
(
Ptr<Node>
node)
const
171
{
172
return
NetDeviceContainer
(
InstallPriv
(node));
173
}
174
175
NetDeviceContainer
176
FdNetDeviceHelper::Install
(std::string nodeName)
const
177
{
178
Ptr<Node>
node = Names::Find<Node> (nodeName);
179
return
NetDeviceContainer
(
InstallPriv
(node));
180
}
181
182
NetDeviceContainer
183
FdNetDeviceHelper::Install
(
const
NodeContainer
&c)
const
184
{
185
NetDeviceContainer
devs;
186
187
for
(
NodeContainer::Iterator
i = c.
Begin
(); i != c.
End
(); i++)
188
{
189
devs.
Add
(
InstallPriv
(*i));
190
}
191
192
return
devs;
193
}
194
195
Ptr<NetDevice>
196
FdNetDeviceHelper::InstallPriv
(
Ptr<Node>
node)
const
197
{
198
Ptr<FdNetDevice>
device =
m_deviceFactory
.
Create
<
FdNetDevice
> ();
199
device->SetAddress (
Mac48Address::Allocate
());
200
node->
AddDevice
(device);
201
return
device;
202
}
203
204
}
// namespace ns3
src
fd-net-device
helper
fd-net-device-helper.cc
Generated on Tue May 14 2013 11:08:20 for ns-3 by
1.8.1.2