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
node.cc
Go to the documentation of this file.
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
* Copyright (c) 2006 Georgia Tech Research Corporation, 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
* Authors: George F. Riley<riley@ece.gatech.edu>
19
* Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
20
*/
21
#include "
node.h
"
22
#include "
node-list.h
"
23
#include "
net-device.h
"
24
#include "
application.h
"
25
#include "ns3/packet.h"
26
#include "ns3/simulator.h"
27
#include "ns3/object-vector.h"
28
#include "ns3/uinteger.h"
29
#include "ns3/log.h"
30
#include "ns3/assert.h"
31
#include "ns3/global-value.h"
32
#include "ns3/boolean.h"
33
#include "ns3/simulator.h"
34
35
NS_LOG_COMPONENT_DEFINE
(
"Node"
);
36
37
namespace
ns3 {
38
39
NS_OBJECT_ENSURE_REGISTERED
(Node);
40
41
GlobalValue
g_checksumEnabled
=
GlobalValue
(
"ChecksumEnabled"
,
42
"A global switch to enable all checksums for all protocols"
,
43
BooleanValue
(
false
),
44
MakeBooleanChecker ());
45
46
TypeId
47
Node::GetTypeId
(
void
)
48
{
49
static
TypeId
tid =
TypeId
(
"ns3::Node"
)
50
.
SetParent
<
Object
> ()
51
.AddConstructor<Node> ()
52
.AddAttribute (
"DeviceList"
,
"The list of devices associated to this Node."
,
53
ObjectVectorValue
(),
54
MakeObjectVectorAccessor
(&
Node::m_devices
),
55
MakeObjectVectorChecker<NetDevice> ())
56
.AddAttribute (
"ApplicationList"
,
"The list of applications associated to this Node."
,
57
ObjectVectorValue
(),
58
MakeObjectVectorAccessor
(&
Node::m_applications
),
59
MakeObjectVectorChecker<Application> ())
60
.AddAttribute (
"Id"
,
"The id (unique integer) of this Node."
,
61
TypeId::ATTR_GET
,
// allow only getting it.
62
UintegerValue
(0),
63
MakeUintegerAccessor (&
Node::m_id
),
64
MakeUintegerChecker<uint32_t> ())
65
.AddAttribute (
"SystemId"
,
"The systemId of this node: a unique integer used for parallel simulations."
,
66
TypeId::ATTR_GET
|
TypeId::ATTR_SET
,
67
UintegerValue
(0),
68
MakeUintegerAccessor (&
Node::m_sid
),
69
MakeUintegerChecker<uint32_t> ())
70
;
71
return
tid;
72
}
73
74
Node::Node
()
75
: m_id (0),
76
m_sid (0)
77
{
78
NS_LOG_FUNCTION
(
this
);
79
Construct
();
80
}
81
82
Node::Node
(uint32_t sid)
83
: m_id (0),
84
m_sid (sid)
85
{
86
NS_LOG_FUNCTION
(
this
<< sid);
87
Construct
();
88
}
89
90
void
91
Node::Construct
(
void
)
92
{
93
NS_LOG_FUNCTION
(
this
);
94
m_id
=
NodeList::Add
(
this
);
95
}
96
97
Node::~Node
()
98
{
99
NS_LOG_FUNCTION
(
this
);
100
}
101
102
uint32_t
103
Node::GetId
(
void
)
const
104
{
105
NS_LOG_FUNCTION
(
this
);
106
return
m_id
;
107
}
108
109
uint32_t
110
Node::GetSystemId
(
void
)
const
111
{
112
NS_LOG_FUNCTION
(
this
);
113
return
m_sid
;
114
}
115
116
uint32_t
117
Node::AddDevice
(
Ptr<NetDevice>
device)
118
{
119
NS_LOG_FUNCTION
(
this
<< device);
120
uint32_t index =
m_devices
.size ();
121
m_devices
.push_back (device);
122
device->
SetNode
(
this
);
123
device->
SetIfIndex
(index);
124
device->
SetReceiveCallback
(
MakeCallback
(&
Node::NonPromiscReceiveFromDevice
,
this
));
125
Simulator::ScheduleWithContext
(
GetId
(),
Seconds
(0.0),
126
&
NetDevice::Initialize
, device);
127
NotifyDeviceAdded
(device);
128
return
index;
129
}
130
Ptr<NetDevice>
131
Node::GetDevice
(uint32_t index)
const
132
{
133
NS_LOG_FUNCTION
(
this
<< index);
134
NS_ASSERT_MSG
(index <
m_devices
.size (),
"Device index "
<< index <<
135
" is out of range (only have "
<<
m_devices
.size () <<
" devices)."
);
136
return
m_devices
[index];
137
}
138
uint32_t
139
Node::GetNDevices
(
void
)
const
140
{
141
NS_LOG_FUNCTION
(
this
);
142
return
m_devices
.size ();
143
}
144
145
uint32_t
146
Node::AddApplication
(
Ptr<Application>
application)
147
{
148
NS_LOG_FUNCTION
(
this
<< application);
149
uint32_t index =
m_applications
.size ();
150
m_applications
.push_back (application);
151
application->
SetNode
(
this
);
152
Simulator::ScheduleWithContext
(
GetId
(),
Seconds
(0.0),
153
&
Application::Initialize
, application);
154
return
index;
155
}
156
Ptr<Application>
157
Node::GetApplication
(uint32_t index)
const
158
{
159
NS_LOG_FUNCTION
(
this
<< index);
160
NS_ASSERT_MSG
(index <
m_applications
.size (),
"Application index "
<< index <<
161
" is out of range (only have "
<<
m_applications
.size () <<
" applications)."
);
162
return
m_applications
[index];
163
}
164
uint32_t
165
Node::GetNApplications
(
void
)
const
166
{
167
NS_LOG_FUNCTION
(
this
);
168
return
m_applications
.size ();
169
}
170
171
void
172
Node::DoDispose
()
173
{
174
NS_LOG_FUNCTION
(
this
);
175
m_deviceAdditionListeners
.clear ();
176
m_handlers
.clear ();
177
for
(std::vector<
Ptr<NetDevice>
>::iterator i =
m_devices
.begin ();
178
i !=
m_devices
.end (); i++)
179
{
180
Ptr<NetDevice>
device = *i;
181
device->
Dispose
();
182
*i = 0;
183
}
184
m_devices
.clear ();
185
for
(std::vector<
Ptr<Application>
>::iterator i =
m_applications
.begin ();
186
i !=
m_applications
.end (); i++)
187
{
188
Ptr<Application>
application = *i;
189
application->
Dispose
();
190
*i = 0;
191
}
192
m_applications
.clear ();
193
Object::DoDispose
();
194
}
195
void
196
Node::DoInitialize
(
void
)
197
{
198
NS_LOG_FUNCTION
(
this
);
199
for
(std::vector<
Ptr<NetDevice>
>::iterator i =
m_devices
.begin ();
200
i !=
m_devices
.end (); i++)
201
{
202
Ptr<NetDevice>
device = *i;
203
device->
Initialize
();
204
}
205
for
(std::vector<
Ptr<Application>
>::iterator i =
m_applications
.begin ();
206
i !=
m_applications
.end (); i++)
207
{
208
Ptr<Application>
application = *i;
209
application->
Initialize
();
210
}
211
212
Object::DoInitialize
();
213
}
214
215
void
216
Node::RegisterProtocolHandler
(
ProtocolHandler
handler,
217
uint16_t protocolType,
218
Ptr<NetDevice>
device,
219
bool
promiscuous)
220
{
221
NS_LOG_FUNCTION
(
this
<< &handler << protocolType << device << promiscuous);
222
struct
Node::ProtocolHandlerEntry
entry;
223
entry.
handler
= handler;
224
entry.
protocol
= protocolType;
225
entry.
device
= device;
226
entry.
promiscuous
= promiscuous;
227
228
// On demand enable promiscuous mode in netdevices
229
if
(promiscuous)
230
{
231
if
(device == 0)
232
{
233
for
(std::vector<
Ptr<NetDevice>
>::iterator i =
m_devices
.begin ();
234
i !=
m_devices
.end (); i++)
235
{
236
Ptr<NetDevice>
dev = *i;
237
dev->
SetPromiscReceiveCallback
(
MakeCallback
(&
Node::PromiscReceiveFromDevice
,
this
));
238
}
239
}
240
else
241
{
242
device->
SetPromiscReceiveCallback
(
MakeCallback
(&
Node::PromiscReceiveFromDevice
,
this
));
243
}
244
}
245
246
m_handlers
.push_back (entry);
247
}
248
249
void
250
Node::UnregisterProtocolHandler
(
ProtocolHandler
handler)
251
{
252
NS_LOG_FUNCTION
(
this
<< &handler);
253
for
(ProtocolHandlerList::iterator i =
m_handlers
.begin ();
254
i !=
m_handlers
.end (); i++)
255
{
256
if
(i->handler.IsEqual (handler))
257
{
258
m_handlers
.erase (i);
259
break
;
260
}
261
}
262
}
263
264
bool
265
Node::ChecksumEnabled
(
void
)
266
{
267
NS_LOG_FUNCTION_NOARGS
();
268
BooleanValue
val;
269
g_checksumEnabled
.
GetValue
(val);
270
return
val.
Get
();
271
}
272
273
bool
274
Node::PromiscReceiveFromDevice
(
Ptr<NetDevice>
device,
Ptr<const Packet>
packet, uint16_t protocol,
275
const
Address
&from,
const
Address
&to,
NetDevice::PacketType
packetType)
276
{
277
NS_LOG_FUNCTION
(
this
<< device << packet << protocol << &from << &to << packetType);
278
return
ReceiveFromDevice
(device, packet, protocol, from, to, packetType,
true
);
279
}
280
281
bool
282
Node::NonPromiscReceiveFromDevice
(
Ptr<NetDevice>
device,
Ptr<const Packet>
packet, uint16_t protocol,
283
const
Address
&from)
284
{
285
NS_LOG_FUNCTION
(
this
<< device << packet << protocol << &from);
286
return
ReceiveFromDevice
(device, packet, protocol, from, device->
GetAddress
(),
NetDevice::PacketType
(0),
false
);
287
}
288
289
bool
290
Node::ReceiveFromDevice
(
Ptr<NetDevice>
device,
Ptr<const Packet>
packet, uint16_t protocol,
291
const
Address
&from,
const
Address
&to,
NetDevice::PacketType
packetType,
bool
promiscuous)
292
{
293
NS_LOG_FUNCTION
(
this
<< device << packet << protocol << &from << &to << packetType << promiscuous);
294
NS_ASSERT_MSG
(
Simulator::GetContext
() ==
GetId
(),
"Received packet with erroneous context ; "
<<
295
"make sure the channels in use are correctly updating events context "
<<
296
"when transfering events from one node to another."
);
297
NS_LOG_DEBUG
(
"Node "
<<
GetId
() <<
" ReceiveFromDevice: dev "
298
<< device->
GetIfIndex
() <<
" (type="
<< device->
GetInstanceTypeId
().
GetName
()
299
<<
") Packet UID "
<< packet->
GetUid
());
300
bool
found =
false
;
301
302
for
(ProtocolHandlerList::iterator i =
m_handlers
.begin ();
303
i !=
m_handlers
.end (); i++)
304
{
305
if
(i->device == 0 ||
306
(i->device != 0 && i->device == device))
307
{
308
if
(i->protocol == 0 ||
309
i->protocol == protocol)
310
{
311
if
(promiscuous == i->promiscuous)
312
{
313
i->handler (device, packet, protocol, from, to, packetType);
314
found =
true
;
315
}
316
}
317
}
318
}
319
return
found;
320
}
321
void
322
Node::RegisterDeviceAdditionListener
(
DeviceAdditionListener
listener)
323
{
324
NS_LOG_FUNCTION
(
this
<< &listener);
325
m_deviceAdditionListeners
.push_back (listener);
326
// and, then, notify the new listener about all existing devices.
327
for
(std::vector<
Ptr<NetDevice>
>::const_iterator i =
m_devices
.begin ();
328
i !=
m_devices
.end (); ++i)
329
{
330
listener (*i);
331
}
332
}
333
void
334
Node::UnregisterDeviceAdditionListener
(
DeviceAdditionListener
listener)
335
{
336
NS_LOG_FUNCTION
(
this
<< &listener);
337
for
(DeviceAdditionListenerList::iterator i =
m_deviceAdditionListeners
.begin ();
338
i !=
m_deviceAdditionListeners
.end (); i++)
339
{
340
if
((*i).IsEqual (listener))
341
{
342
m_deviceAdditionListeners
.erase (i);
343
break
;
344
}
345
}
346
}
347
348
void
349
Node::NotifyDeviceAdded
(
Ptr<NetDevice>
device)
350
{
351
NS_LOG_FUNCTION
(
this
<< device);
352
for
(DeviceAdditionListenerList::iterator i =
m_deviceAdditionListeners
.begin ();
353
i !=
m_deviceAdditionListeners
.end (); i++)
354
{
355
(*i) (device);
356
}
357
}
358
359
360
}
// namespace ns3
src
network
model
node.cc
Generated on Tue May 14 2013 11:08:30 for ns-3 by
1.8.1.2