A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
node.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2006 Georgia Tech Research Corporation, INRIA
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Authors: George F. Riley<riley@ece.gatech.edu>
18 * Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19 */
20
21#include "node.h"
22
23#include "application.h"
24#include "net-device.h"
25#include "node-list.h"
26#include "packet.h"
27
28#include "ns3/assert.h"
29#include "ns3/boolean.h"
30#include "ns3/global-value.h"
31#include "ns3/log.h"
32#include "ns3/object-vector.h"
33#include "ns3/simulator.h"
34#include "ns3/uinteger.h"
35
36namespace ns3
37{
38
40
42
43/**
44 * \relates Node
45 * \anchor GlobalValueChecksumEnabled
46 * \brief A global switch to enable all checksums for all protocols.
47 */
48static GlobalValue g_checksumEnabled =
49 GlobalValue("ChecksumEnabled",
50 "A global switch to enable all checksums for all protocols",
51 BooleanValue(false),
53
56{
57 static TypeId tid =
58 TypeId("ns3::Node")
60 .SetGroupName("Network")
61 .AddConstructor<Node>()
62 .AddAttribute("DeviceList",
63 "The list of devices associated to this Node.",
66 MakeObjectVectorChecker<NetDevice>())
67 .AddAttribute("ApplicationList",
68 "The list of applications associated to this Node.",
71 MakeObjectVectorChecker<Application>())
72 .AddAttribute("Id",
73 "The id (unique integer) of this Node.",
74 TypeId::ATTR_GET, // allow only getting it.
77 MakeUintegerChecker<uint32_t>())
78 .AddAttribute(
79 "SystemId",
80 "The systemId of this node: a unique integer used for parallel simulations.",
84 MakeUintegerChecker<uint32_t>());
85 return tid;
86}
87
89 : m_id(0),
90 m_sid(0)
91{
92 NS_LOG_FUNCTION(this);
93 Construct();
94}
95
97 : m_id(0),
98 m_sid(sid)
99{
100 NS_LOG_FUNCTION(this << sid);
101 Construct();
102}
103
104void
106{
107 NS_LOG_FUNCTION(this);
108 m_id = NodeList::Add(this);
109}
110
112{
113 NS_LOG_FUNCTION(this);
114}
115
118{
119 return m_id;
120}
121
122Time
124{
125 return Simulator::Now();
126}
127
130{
131 return m_sid;
132}
133
136{
137 NS_LOG_FUNCTION(this << device);
138 uint32_t index = m_devices.size();
139 m_devices.push_back(device);
140 device->SetNode(this);
141 device->SetIfIndex(index);
142 device->SetReceiveCallback(MakeCallback(&Node::NonPromiscReceiveFromDevice, this));
144 NotifyDeviceAdded(device);
145 return index;
146}
147
150{
151 NS_ASSERT_MSG(index < m_devices.size(),
152 "Device index " << index << " is out of range (only have " << m_devices.size()
153 << " devices).");
154 return m_devices[index];
155}
156
159{
160 return m_devices.size();
161}
162
165{
166 NS_LOG_FUNCTION(this << application);
167 uint32_t index = m_applications.size();
168 m_applications.push_back(application);
169 application->SetNode(this);
171 return index;
172}
173
176{
177 NS_ASSERT_MSG(index < m_applications.size(),
178 "Application index " << index << " is out of range (only have "
179 << m_applications.size() << " applications).");
180 return m_applications[index];
181}
182
185{
186 return m_applications.size();
187}
188
189void
191{
192 NS_LOG_FUNCTION(this);
194 m_handlers.clear();
195 for (auto i = m_devices.begin(); i != m_devices.end(); i++)
196 {
197 Ptr<NetDevice> device = *i;
198 device->Dispose();
199 *i = nullptr;
200 }
201 m_devices.clear();
202 for (auto i = m_applications.begin(); i != m_applications.end(); i++)
203 {
204 Ptr<Application> application = *i;
205 application->Dispose();
206 *i = nullptr;
207 }
208 m_applications.clear();
210}
211
212void
214{
215 NS_LOG_FUNCTION(this);
216 for (auto i = m_devices.begin(); i != m_devices.end(); i++)
217 {
218 Ptr<NetDevice> device = *i;
219 device->Initialize();
220 }
221 for (auto i = m_applications.begin(); i != m_applications.end(); i++)
222 {
223 Ptr<Application> application = *i;
224 application->Initialize();
225 }
226
228}
229
230void
232 uint16_t protocolType,
233 Ptr<NetDevice> device,
234 bool promiscuous)
235{
236 NS_LOG_FUNCTION(this << &handler << protocolType << device << promiscuous);
238 entry.handler = handler;
239 entry.protocol = protocolType;
240 entry.device = device;
241 entry.promiscuous = promiscuous;
242
243 // On demand enable promiscuous mode in netdevices
244 if (promiscuous)
245 {
246 if (!device)
247 {
248 for (auto i = m_devices.begin(); i != m_devices.end(); i++)
249 {
250 Ptr<NetDevice> dev = *i;
251 dev->SetPromiscReceiveCallback(MakeCallback(&Node::PromiscReceiveFromDevice, this));
252 }
253 }
254 else
255 {
256 device->SetPromiscReceiveCallback(MakeCallback(&Node::PromiscReceiveFromDevice, this));
257 }
258 }
259
260 m_handlers.push_back(entry);
261}
262
263void
265{
266 NS_LOG_FUNCTION(this << &handler);
267 for (auto i = m_handlers.begin(); i != m_handlers.end(); i++)
268 {
269 if (i->handler.IsEqual(handler))
270 {
271 m_handlers.erase(i);
272 break;
273 }
274 }
275}
276
277bool
279{
280 BooleanValue val;
282 return val.Get();
283}
284
285bool
287 Ptr<const Packet> packet,
288 uint16_t protocol,
289 const Address& from,
290 const Address& to,
291 NetDevice::PacketType packetType)
292{
293 NS_LOG_FUNCTION(this << device << packet << protocol << &from << &to << packetType);
294 return ReceiveFromDevice(device, packet, protocol, from, to, packetType, true);
295}
296
297bool
299 Ptr<const Packet> packet,
300 uint16_t protocol,
301 const Address& from)
302{
303 NS_LOG_FUNCTION(this << device << packet << protocol << &from);
304 return ReceiveFromDevice(device,
305 packet,
306 protocol,
307 from,
308 device->GetAddress(),
310 false);
311}
312
313bool
315 Ptr<const Packet> packet,
316 uint16_t protocol,
317 const Address& from,
318 const Address& to,
319 NetDevice::PacketType packetType,
320 bool promiscuous)
321{
322 NS_LOG_FUNCTION(this << device << packet << protocol << &from << &to << packetType
323 << promiscuous);
325 "Received packet with erroneous context ; "
326 << "make sure the channels in use are correctly updating events context "
327 << "when transferring events from one node to another.");
328 bool found = false;
329
330 for (auto i = m_handlers.begin(); i != m_handlers.end(); i++)
331 {
332 if (!i->device || (i->device == device))
333 {
334 if (i->protocol == 0 || i->protocol == protocol)
335 {
336 if (promiscuous == i->promiscuous)
337 {
338 i->handler(device, packet, protocol, from, to, packetType);
339 found = true;
340 }
341 }
342 }
343 }
344 NS_LOG_DEBUG("Node " << GetId() << " ReceiveFromDevice: dev " << device->GetIfIndex()
345 << " (type=" << device->GetInstanceTypeId().GetName() << ") Packet UID "
346 << packet->GetUid() << " handler found: " << found);
347 return found;
348}
349
350void
352{
353 NS_LOG_FUNCTION(this << &listener);
354 m_deviceAdditionListeners.push_back(listener);
355 // and, then, notify the new listener about all existing devices.
356 for (auto i = m_devices.begin(); i != m_devices.end(); ++i)
357 {
358 listener(*i);
359 }
360}
361
362void
364{
365 NS_LOG_FUNCTION(this << &listener);
366 for (auto i = m_deviceAdditionListeners.begin(); i != m_deviceAdditionListeners.end(); i++)
367 {
368 if ((*i).IsEqual(listener))
369 {
371 break;
372 }
373 }
374}
375
376void
378{
379 NS_LOG_FUNCTION(this << device);
380 for (auto i = m_deviceAdditionListeners.begin(); i != m_deviceAdditionListeners.end(); i++)
381 {
382 (*i)(device);
383 }
384}
385
386} // namespace ns3
a polymophic address class
Definition: address.h:101
AttributeValue implementation for Boolean.
Definition: boolean.h:37
bool Get() const
Definition: boolean.cc:55
Hold a so-called 'global value'.
Definition: global-value.h:76
void GetValue(AttributeValue &value) const
Get the value.
Definition: global-value.cc:98
PacketType
Packet types are used as they are in Linux.
Definition: net-device.h:300
A network Node.
Definition: node.h:57
void UnregisterProtocolHandler(ProtocolHandler handler)
Definition: node.cc:264
uint32_t GetSystemId() const
Definition: node.cc:129
void DoDispose() override
The dispose method.
Definition: node.cc:190
bool PromiscReceiveFromDevice(Ptr< NetDevice > device, Ptr< const Packet > packet, uint16_t protocol, const Address &from, const Address &to, NetDevice::PacketType packetType)
Receive a packet from a device in promiscuous mode.
Definition: node.cc:286
static GlobalValue g_checksumEnabled
Definition: node.cc:48
DeviceAdditionListenerList m_deviceAdditionListeners
Device addition listeners in the node.
Definition: node.h:306
std::vector< Ptr< Application > > m_applications
Applications associated to this node.
Definition: node.h:304
uint32_t AddDevice(Ptr< NetDevice > device)
Associate a NetDevice to this node.
Definition: node.cc:135
uint32_t m_id
Node id for this node.
Definition: node.h:301
uint32_t GetNDevices() const
Definition: node.cc:158
uint32_t GetNApplications() const
Definition: node.cc:184
Ptr< Application > GetApplication(uint32_t index) const
Retrieve the index-th Application associated to this node.
Definition: node.cc:175
bool ReceiveFromDevice(Ptr< NetDevice > device, Ptr< const Packet >, uint16_t protocol, const Address &from, const Address &to, NetDevice::PacketType packetType, bool promisc)
Receive a packet from a device.
Definition: node.cc:314
bool NonPromiscReceiveFromDevice(Ptr< NetDevice > device, Ptr< const Packet > packet, uint16_t protocol, const Address &from)
Receive a packet from a device in non-promiscuous mode.
Definition: node.cc:298
uint32_t m_sid
System id for this node.
Definition: node.h:302
ProtocolHandlerList m_handlers
Protocol handlers in the node.
Definition: node.h:305
uint32_t AddApplication(Ptr< Application > application)
Associate an Application to this Node.
Definition: node.cc:164
void Construct()
Finish node's construction by setting the correct node ID.
Definition: node.cc:105
static TypeId GetTypeId()
Get the type ID.
Definition: node.cc:55
uint32_t GetId() const
Definition: node.cc:117
Node()
Definition: node.cc:88
Time GetLocalTime() const
In the future, ns3 nodes may have clock that returned a local time different from the virtual time Si...
Definition: node.cc:123
void RegisterDeviceAdditionListener(DeviceAdditionListener listener)
Definition: node.cc:351
void DoInitialize() override
Initialize() implementation.
Definition: node.cc:213
Ptr< NetDevice > GetDevice(uint32_t index) const
Retrieve the index-th NetDevice associated to this node.
Definition: node.cc:149
~Node() override
Definition: node.cc:111
void NotifyDeviceAdded(Ptr< NetDevice > device)
Notifies all the DeviceAdditionListener about the new device added.
Definition: node.cc:377
static bool ChecksumEnabled()
Definition: node.cc:278
void UnregisterDeviceAdditionListener(DeviceAdditionListener listener)
Definition: node.cc:363
std::vector< Ptr< NetDevice > > m_devices
Devices associated to this node.
Definition: node.h:303
void RegisterProtocolHandler(ProtocolHandler handler, uint16_t protocolType, Ptr< NetDevice > device, bool promiscuous=false)
Definition: node.cc:231
static uint32_t Add(Ptr< Node > node)
Definition: node-list.cc:230
A base class which provides memory management and object aggregation.
Definition: object.h:89
void Initialize()
Invoke DoInitialize on all Objects aggregated to this one.
Definition: object.cc:214
virtual void DoInitialize()
Initialize() implementation.
Definition: object.cc:451
virtual void DoDispose()
Destructor implementation.
Definition: object.cc:444
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
static void ScheduleWithContext(uint32_t context, const Time &delay, FUNC f, Ts &&... args)
Schedule an event with the given context.
Definition: simulator.h:588
static Time Now()
Return the current simulation virtual time.
Definition: simulator.cc:208
static uint32_t GetContext()
Get the current simulation context.
Definition: simulator.cc:318
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
a unique identifier for an interface.
Definition: type-id.h:59
@ ATTR_GET
The attribute can be read.
Definition: type-id.h:64
@ ATTR_SET
The attribute can be written.
Definition: type-id.h:65
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:932
Hold an unsigned integer type.
Definition: uinteger.h:45
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
Definition: assert.h:86
Ptr< const AttributeChecker > MakeBooleanChecker()
Definition: boolean.cc:124
ObjectPtrContainerValue ObjectVectorValue
ObjectVectorValue is an alias for ObjectPtrContainerValue.
Definition: object-vector.h:40
Ptr< const AttributeAccessor > MakeObjectVectorAccessor(U T::*memberVariable)
MakeAccessorHelper implementation for ObjectVector.
Definition: object-vector.h:76
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
Definition: uinteger.h:46
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:268
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:46
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:704
Protocol handler entry.
Definition: node.h:289
bool promiscuous
true if it is a promiscuous handler
Definition: node.h:293
uint16_t protocol
the protocol number
Definition: node.h:292
Ptr< NetDevice > device
the NetDevice
Definition: node.h:291
ProtocolHandler handler
the protocol handler
Definition: node.h:290