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
;
66
return
tid;
67
}
68
69
Node::Node
()
70
: m_id (0),
71
m_sid (0)
72
{
73
NS_LOG_FUNCTION
(
this
);
74
Construct
();
75
}
76
77
Node::Node
(uint32_t sid)
78
: m_id (0),
79
m_sid (sid)
80
{
81
NS_LOG_FUNCTION
(
this
<< sid);
82
Construct
();
83
}
84
85
void
86
Node::Construct
(
void
)
87
{
88
NS_LOG_FUNCTION
(
this
);
89
m_id
=
NodeList::Add
(
this
);
90
}
91
92
Node::~Node
()
93
{
94
NS_LOG_FUNCTION
(
this
);
95
}
96
97
uint32_t
98
Node::GetId
(
void
)
const
99
{
100
NS_LOG_FUNCTION
(
this
);
101
return
m_id
;
102
}
103
104
uint32_t
105
Node::GetSystemId
(
void
)
const
106
{
107
NS_LOG_FUNCTION
(
this
);
108
return
m_sid
;
109
}
110
111
uint32_t
112
Node::AddDevice
(
Ptr<NetDevice>
device)
113
{
114
NS_LOG_FUNCTION
(
this
<< device);
115
uint32_t index =
m_devices
.size ();
116
m_devices
.push_back (device);
117
device->
SetNode
(
this
);
118
device->
SetIfIndex
(index);
119
device->
SetReceiveCallback
(
MakeCallback
(&
Node::NonPromiscReceiveFromDevice
,
this
));
120
Simulator::ScheduleWithContext
(
GetId
(),
Seconds
(0.0),
121
&
NetDevice::Start
, device);
122
NotifyDeviceAdded
(device);
123
return
index;
124
}
125
Ptr<NetDevice>
126
Node::GetDevice
(uint32_t index)
const
127
{
128
NS_LOG_FUNCTION
(
this
<< index);
129
NS_ASSERT_MSG
(index <
m_devices
.size (),
"Device index "
<< index <<
130
" is out of range (only have "
<<
m_devices
.size () <<
" devices)."
);
131
return
m_devices
[index];
132
}
133
uint32_t
134
Node::GetNDevices
(
void
)
const
135
{
136
NS_LOG_FUNCTION
(
this
);
137
return
m_devices
.size ();
138
}
139
140
uint32_t
141
Node::AddApplication
(
Ptr<Application>
application)
142
{
143
NS_LOG_FUNCTION
(
this
<< application);
144
uint32_t index =
m_applications
.size ();
145
m_applications
.push_back (application);
146
application->
SetNode
(
this
);
147
Simulator::ScheduleWithContext
(
GetId
(),
Seconds
(0.0),
148
&
Application::Start
, application);
149
return
index;
150
}
151
Ptr<Application>
152
Node::GetApplication
(uint32_t index)
const
153
{
154
NS_LOG_FUNCTION
(
this
<< index);
155
NS_ASSERT_MSG
(index <
m_applications
.size (),
"Application index "
<< index <<
156
" is out of range (only have "
<<
m_applications
.size () <<
" applications)."
);
157
return
m_applications
[index];
158
}
159
uint32_t
160
Node::GetNApplications
(
void
)
const
161
{
162
NS_LOG_FUNCTION
(
this
);
163
return
m_applications
.size ();
164
}
165
166
void
167
Node::DoDispose
()
168
{
169
NS_LOG_FUNCTION
(
this
);
170
m_deviceAdditionListeners
.clear ();
171
m_handlers
.clear ();
172
for
(std::vector<
Ptr<NetDevice>
>::iterator i =
m_devices
.begin ();
173
i !=
m_devices
.end (); i++)
174
{
175
Ptr<NetDevice>
device = *i;
176
device->
Dispose
();
177
*i = 0;
178
}
179
m_devices
.clear ();
180
for
(std::vector<
Ptr<Application>
>::iterator i =
m_applications
.begin ();
181
i !=
m_applications
.end (); i++)
182
{
183
Ptr<Application>
application = *i;
184
application->
Dispose
();
185
*i = 0;
186
}
187
m_applications
.clear ();
188
Object::DoDispose
();
189
}
190
void
191
Node::DoStart
(
void
)
192
{
193
NS_LOG_FUNCTION
(
this
);
194
for
(std::vector<
Ptr<NetDevice>
>::iterator i =
m_devices
.begin ();
195
i !=
m_devices
.end (); i++)
196
{
197
Ptr<NetDevice>
device = *i;
198
device->
Start
();
199
}
200
for
(std::vector<
Ptr<Application>
>::iterator i =
m_applications
.begin ();
201
i !=
m_applications
.end (); i++)
202
{
203
Ptr<Application>
application = *i;
204
application->
Start
();
205
}
206
207
Object::DoStart
();
208
}
209
210
void
211
Node::RegisterProtocolHandler
(
ProtocolHandler
handler,
212
uint16_t protocolType,
213
Ptr<NetDevice>
device,
214
bool
promiscuous)
215
{
216
NS_LOG_FUNCTION
(
this
<< &handler << protocolType << device << promiscuous);
217
struct
Node::ProtocolHandlerEntry
entry;
218
entry.
handler
= handler;
219
entry.
protocol
= protocolType;
220
entry.
device
= device;
221
entry.
promiscuous
= promiscuous;
222
223
// On demand enable promiscuous mode in netdevices
224
if
(promiscuous)
225
{
226
if
(device == 0)
227
{
228
for
(std::vector<
Ptr<NetDevice>
>::iterator i =
m_devices
.begin ();
229
i !=
m_devices
.end (); i++)
230
{
231
Ptr<NetDevice>
dev = *i;
232
dev->
SetPromiscReceiveCallback
(
MakeCallback
(&
Node::PromiscReceiveFromDevice
,
this
));
233
}
234
}
235
else
236
{
237
device->
SetPromiscReceiveCallback
(
MakeCallback
(&
Node::PromiscReceiveFromDevice
,
this
));
238
}
239
}
240
241
m_handlers
.push_back (entry);
242
}
243
244
void
245
Node::UnregisterProtocolHandler
(
ProtocolHandler
handler)
246
{
247
NS_LOG_FUNCTION
(
this
<< &handler);
248
for
(ProtocolHandlerList::iterator i =
m_handlers
.begin ();
249
i !=
m_handlers
.end (); i++)
250
{
251
if
(i->handler.IsEqual (handler))
252
{
253
m_handlers
.erase (i);
254
break
;
255
}
256
}
257
}
258
259
bool
260
Node::ChecksumEnabled
(
void
)
261
{
262
NS_LOG_FUNCTION_NOARGS
();
263
BooleanValue
val;
264
g_checksumEnabled
.
GetValue
(val);
265
return
val.
Get
();
266
}
267
268
bool
269
Node::PromiscReceiveFromDevice
(
Ptr<NetDevice>
device,
Ptr<const Packet>
packet, uint16_t protocol,
270
const
Address
&from,
const
Address
&to,
NetDevice::PacketType
packetType)
271
{
272
NS_LOG_FUNCTION
(
this
<< device << packet << protocol << &from << &to << packetType);
273
return
ReceiveFromDevice
(device, packet, protocol, from, to, packetType,
true
);
274
}
275
276
bool
277
Node::NonPromiscReceiveFromDevice
(
Ptr<NetDevice>
device,
Ptr<const Packet>
packet, uint16_t protocol,
278
const
Address
&from)
279
{
280
NS_LOG_FUNCTION
(
this
<< device << packet << protocol << &from);
281
return
ReceiveFromDevice
(device, packet, protocol, from, device->
GetAddress
(),
NetDevice::PacketType
(0),
false
);
282
}
283
284
bool
285
Node::ReceiveFromDevice
(
Ptr<NetDevice>
device,
Ptr<const Packet>
packet, uint16_t protocol,
286
const
Address
&from,
const
Address
&to,
NetDevice::PacketType
packetType,
bool
promiscuous)
287
{
288
NS_LOG_FUNCTION
(
this
<< device << packet << protocol << &from << &to << packetType << promiscuous);
289
NS_ASSERT_MSG
(
Simulator::GetContext
() ==
GetId
(),
"Received packet with erroneous context ; "
<<
290
"make sure the channels in use are correctly updating events context "
<<
291
"when transfering events from one node to another."
);
292
NS_LOG_DEBUG
(
"Node "
<<
GetId
() <<
" ReceiveFromDevice: dev "
293
<< device->
GetIfIndex
() <<
" (type="
<< device->
GetInstanceTypeId
().
GetName
()
294
<<
") Packet UID "
<< packet->
GetUid
());
295
bool
found =
false
;
296
297
for
(ProtocolHandlerList::iterator i =
m_handlers
.begin ();
298
i !=
m_handlers
.end (); i++)
299
{
300
if
(i->device == 0 ||
301
(i->device != 0 && i->device == device))
302
{
303
if
(i->protocol == 0 ||
304
i->protocol == protocol)
305
{
306
if
(promiscuous == i->promiscuous)
307
{
308
i->handler (device, packet, protocol, from, to, packetType);
309
found =
true
;
310
}
311
}
312
}
313
}
314
return
found;
315
}
316
void
317
Node::RegisterDeviceAdditionListener
(
DeviceAdditionListener
listener)
318
{
319
NS_LOG_FUNCTION
(
this
<< &listener);
320
m_deviceAdditionListeners
.push_back (listener);
321
// and, then, notify the new listener about all existing devices.
322
for
(std::vector<
Ptr<NetDevice>
>::const_iterator i =
m_devices
.begin ();
323
i !=
m_devices
.end (); ++i)
324
{
325
listener (*i);
326
}
327
}
328
void
329
Node::UnregisterDeviceAdditionListener
(
DeviceAdditionListener
listener)
330
{
331
NS_LOG_FUNCTION
(
this
<< &listener);
332
for
(DeviceAdditionListenerList::iterator i =
m_deviceAdditionListeners
.begin ();
333
i !=
m_deviceAdditionListeners
.end (); i++)
334
{
335
if
((*i).IsEqual (listener))
336
{
337
m_deviceAdditionListeners
.erase (i);
338
break
;
339
}
340
}
341
}
342
343
void
344
Node::NotifyDeviceAdded
(
Ptr<NetDevice>
device)
345
{
346
NS_LOG_FUNCTION
(
this
<< device);
347
for
(DeviceAdditionListenerList::iterator i =
m_deviceAdditionListeners
.begin ();
348
i !=
m_deviceAdditionListeners
.end (); i++)
349
{
350
(*i) (device);
351
}
352
}
353
354
355
}
// namespace ns3
src
network
model
node.cc
Generated on Fri Dec 21 2012 19:00:43 for ns-3 by
1.8.1.2