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 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Authors: George F. Riley<riley@ece.gatech.edu>
7 * Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
8 */
9
10#include "node.h"
11
12#include "application.h"
13#include "net-device.h"
14#include "node-list.h"
15#include "packet.h"
16
17#include "ns3/assert.h"
18#include "ns3/boolean.h"
19#include "ns3/global-value.h"
20#include "ns3/log.h"
21#include "ns3/object-vector.h"
22#include "ns3/simulator.h"
23#include "ns3/uinteger.h"
24
25namespace ns3
26{
27
29
31
32/**
33 * \relates Node
34 * \anchor GlobalValueChecksumEnabled
35 * \brief A global switch to enable all checksums for all protocols.
36 */
37static GlobalValue g_checksumEnabled =
38 GlobalValue("ChecksumEnabled",
39 "A global switch to enable all checksums for all protocols",
40 BooleanValue(false),
42
45{
46 static TypeId tid =
47 TypeId("ns3::Node")
49 .SetGroupName("Network")
50 .AddConstructor<Node>()
51 .AddAttribute("DeviceList",
52 "The list of devices associated to this Node.",
56 .AddAttribute("ApplicationList",
57 "The list of applications associated to this Node.",
61 .AddAttribute("Id",
62 "The id (unique integer) of this Node.",
63 TypeId::ATTR_GET, // allow only getting it.
67 .AddAttribute(
68 "SystemId",
69 "The systemId of this node: a unique integer used for parallel simulations.",
74 return tid;
75}
76
78 : m_id(0),
79 m_sid(0)
80{
81 NS_LOG_FUNCTION(this);
82 Construct();
83}
84
86 : m_id(0),
87 m_sid(sid)
88{
89 NS_LOG_FUNCTION(this << sid);
90 Construct();
91}
92
93void
95{
96 NS_LOG_FUNCTION(this);
97 m_id = NodeList::Add(this);
98}
99
101{
102 NS_LOG_FUNCTION(this);
103}
104
107{
108 return m_id;
109}
110
111Time
113{
114 return Simulator::Now();
115}
116
119{
120 return m_sid;
121}
122
125{
126 NS_LOG_FUNCTION(this << device);
127 uint32_t index = m_devices.size();
128 m_devices.push_back(device);
129 device->SetNode(this);
130 device->SetIfIndex(index);
131 device->SetReceiveCallback(MakeCallback(&Node::NonPromiscReceiveFromDevice, this));
133 NotifyDeviceAdded(device);
134 return index;
135}
136
139{
140 NS_ASSERT_MSG(index < m_devices.size(),
141 "Device index " << index << " is out of range (only have " << m_devices.size()
142 << " devices).");
143 return m_devices[index];
144}
145
148{
149 return m_devices.size();
150}
151
154{
155 NS_LOG_FUNCTION(this << application);
156 uint32_t index = m_applications.size();
157 m_applications.push_back(application);
158 application->SetNode(this);
160 return index;
161}
162
165{
166 NS_ASSERT_MSG(index < m_applications.size(),
167 "Application index " << index << " is out of range (only have "
168 << m_applications.size() << " applications).");
169 return m_applications[index];
170}
171
174{
175 return m_applications.size();
176}
177
178void
180{
181 NS_LOG_FUNCTION(this);
183 m_handlers.clear();
184 for (auto i = m_devices.begin(); i != m_devices.end(); i++)
185 {
186 Ptr<NetDevice> device = *i;
187 device->Dispose();
188 *i = nullptr;
189 }
190 m_devices.clear();
191 for (auto i = m_applications.begin(); i != m_applications.end(); i++)
192 {
193 Ptr<Application> application = *i;
194 application->Dispose();
195 *i = nullptr;
196 }
197 m_applications.clear();
199}
200
201void
203{
204 NS_LOG_FUNCTION(this);
205 for (auto i = m_devices.begin(); i != m_devices.end(); i++)
206 {
207 Ptr<NetDevice> device = *i;
208 device->Initialize();
209 }
210 for (auto i = m_applications.begin(); i != m_applications.end(); i++)
211 {
212 Ptr<Application> application = *i;
213 application->Initialize();
214 }
215
217}
218
219void
221 uint16_t protocolType,
222 Ptr<NetDevice> device,
223 bool promiscuous)
224{
225 NS_LOG_FUNCTION(this << &handler << protocolType << device << promiscuous);
227 entry.handler = handler;
228 entry.protocol = protocolType;
229 entry.device = device;
230 entry.promiscuous = promiscuous;
231
232 // On demand enable promiscuous mode in netdevices
233 if (promiscuous)
234 {
235 if (!device)
236 {
237 for (auto i = m_devices.begin(); i != m_devices.end(); i++)
238 {
239 Ptr<NetDevice> dev = *i;
240 dev->SetPromiscReceiveCallback(MakeCallback(&Node::PromiscReceiveFromDevice, this));
241 }
242 }
243 else
244 {
245 device->SetPromiscReceiveCallback(MakeCallback(&Node::PromiscReceiveFromDevice, this));
246 }
247 }
248
249 m_handlers.push_back(entry);
250}
251
252void
254{
255 NS_LOG_FUNCTION(this << &handler);
256 for (auto i = m_handlers.begin(); i != m_handlers.end(); i++)
257 {
258 if (i->handler.IsEqual(handler))
259 {
260 m_handlers.erase(i);
261 break;
262 }
263 }
264}
265
266bool
268{
269 BooleanValue val;
271 return val.Get();
272}
273
274bool
276 Ptr<const Packet> packet,
277 uint16_t protocol,
278 const Address& from,
279 const Address& to,
280 NetDevice::PacketType packetType)
281{
282 NS_LOG_FUNCTION(this << device << packet << protocol << &from << &to << packetType);
283 return ReceiveFromDevice(device, packet, protocol, from, to, packetType, true);
284}
285
286bool
288 Ptr<const Packet> packet,
289 uint16_t protocol,
290 const Address& from)
291{
292 NS_LOG_FUNCTION(this << device << packet << protocol << &from);
293 return ReceiveFromDevice(device,
294 packet,
295 protocol,
296 from,
297 device->GetAddress(),
299 false);
300}
301
302bool
304 Ptr<const Packet> packet,
305 uint16_t protocol,
306 const Address& from,
307 const Address& to,
308 NetDevice::PacketType packetType,
309 bool promiscuous)
310{
311 NS_LOG_FUNCTION(this << device << packet << protocol << &from << &to << packetType
312 << promiscuous);
314 "Received packet with erroneous context ; "
315 << "make sure the channels in use are correctly updating events context "
316 << "when transferring events from one node to another.");
317 bool found = false;
318
319 for (auto i = m_handlers.begin(); i != m_handlers.end(); i++)
320 {
321 if (!i->device || (i->device == device))
322 {
323 if (i->protocol == 0 || i->protocol == protocol)
324 {
325 if (promiscuous == i->promiscuous)
326 {
327 i->handler(device, packet, protocol, from, to, packetType);
328 found = true;
329 }
330 }
331 }
332 }
333 NS_LOG_DEBUG("Node " << GetId() << " ReceiveFromDevice: dev " << device->GetIfIndex()
334 << " (type=" << device->GetInstanceTypeId().GetName() << ") Packet UID "
335 << packet->GetUid() << " handler found: " << found);
336 return found;
337}
338
339void
341{
342 NS_LOG_FUNCTION(this << &listener);
343 m_deviceAdditionListeners.push_back(listener);
344 // and, then, notify the new listener about all existing devices.
345 for (auto i = m_devices.begin(); i != m_devices.end(); ++i)
346 {
347 listener(*i);
348 }
349}
350
351void
353{
354 NS_LOG_FUNCTION(this << &listener);
355 for (auto i = m_deviceAdditionListeners.begin(); i != m_deviceAdditionListeners.end(); i++)
356 {
357 if ((*i).IsEqual(listener))
358 {
360 break;
361 }
362 }
363}
364
365void
367{
368 NS_LOG_FUNCTION(this << device);
369 for (auto i = m_deviceAdditionListeners.begin(); i != m_deviceAdditionListeners.end(); i++)
370 {
371 (*i)(device);
372 }
373}
374
375} // namespace ns3
a polymophic address class
Definition address.h:90
bool Get() const
Definition boolean.cc:44
Hold a so-called 'global value'.
void GetValue(AttributeValue &value) const
Get the value.
PacketType
Packet types are used as they are in Linux.
Definition net-device.h:289
A network Node.
Definition node.h:46
void UnregisterProtocolHandler(ProtocolHandler handler)
Definition node.cc:253
uint32_t GetSystemId() const
Definition node.cc:118
void DoDispose() override
The dispose method.
Definition node.cc:179
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:275
static GlobalValue g_checksumEnabled
Definition node.cc:37
DeviceAdditionListenerList m_deviceAdditionListeners
Device addition listeners in the node.
Definition node.h:295
std::vector< Ptr< Application > > m_applications
Applications associated to this node.
Definition node.h:293
uint32_t AddDevice(Ptr< NetDevice > device)
Associate a NetDevice to this node.
Definition node.cc:124
uint32_t m_id
Node id for this node.
Definition node.h:290
uint32_t GetNDevices() const
Definition node.cc:147
uint32_t GetNApplications() const
Definition node.cc:173
Ptr< Application > GetApplication(uint32_t index) const
Retrieve the index-th Application associated to this node.
Definition node.cc:164
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:303
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:287
uint32_t m_sid
System id for this node.
Definition node.h:291
ProtocolHandlerList m_handlers
Protocol handlers in the node.
Definition node.h:294
uint32_t AddApplication(Ptr< Application > application)
Associate an Application to this Node.
Definition node.cc:153
void Construct()
Finish node's construction by setting the correct node ID.
Definition node.cc:94
static TypeId GetTypeId()
Get the type ID.
Definition node.cc:44
uint32_t GetId() const
Definition node.cc:106
Node()
Definition node.cc:77
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:112
void RegisterDeviceAdditionListener(DeviceAdditionListener listener)
Definition node.cc:340
void DoInitialize() override
Initialize() implementation.
Definition node.cc:202
Ptr< NetDevice > GetDevice(uint32_t index) const
Retrieve the index-th NetDevice associated to this node.
Definition node.cc:138
~Node() override
Definition node.cc:100
void NotifyDeviceAdded(Ptr< NetDevice > device)
Notifies all the DeviceAdditionListener about the new device added.
Definition node.cc:366
static bool ChecksumEnabled()
Definition node.cc:267
void UnregisterDeviceAdditionListener(DeviceAdditionListener listener)
Definition node.cc:352
std::vector< Ptr< NetDevice > > m_devices
Devices associated to this node.
Definition node.h:292
void RegisterProtocolHandler(ProtocolHandler handler, uint16_t protocolType, Ptr< NetDevice > device, bool promiscuous=false)
Definition node.cc:220
static uint32_t Add(Ptr< Node > node)
Definition node-list.cc:219
A base class which provides memory management and object aggregation.
Definition object.h:78
void Initialize()
Invoke DoInitialize on all Objects aggregated to this one.
Definition object.cc:203
virtual void DoInitialize()
Initialize() implementation.
Definition object.cc:440
virtual void DoDispose()
Destructor implementation.
Definition object.cc:433
Smart pointer class similar to boost::intrusive_ptr.
static void ScheduleWithContext(uint32_t context, const Time &delay, FUNC f, Ts &&... args)
Schedule an event with the given context.
Definition simulator.h:577
static Time Now()
Return the current simulation virtual time.
Definition simulator.cc:197
static uint32_t GetContext()
Get the current simulation context.
Definition simulator.cc:307
Simulation virtual time values and global simulation resolution.
Definition nstime.h:94
a unique identifier for an interface.
Definition type-id.h:48
@ ATTR_GET
The attribute can be read.
Definition type-id.h:53
@ ATTR_SET
The attribute can be written.
Definition type-id.h:54
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1001
Hold an unsigned integer type.
Definition uinteger.h:34
#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:75
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition log.h:257
#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:35
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< const AttributeChecker > MakeBooleanChecker()
Definition boolean.cc:113
Ptr< const AttributeChecker > MakeUintegerChecker()
Definition uinteger.h:85
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:684
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
Definition uinteger.h:35
Ptr< const AttributeChecker > MakeObjectVectorChecker()
ObjectPtrContainerValue ObjectVectorValue
ObjectVectorValue is an alias for ObjectPtrContainerValue.
Ptr< const AttributeAccessor > MakeObjectVectorAccessor(U T::*memberVariable)
MakeAccessorHelper implementation for ObjectVector.
Protocol handler entry.
Definition node.h:278
bool promiscuous
true if it is a promiscuous handler
Definition node.h:282
uint16_t protocol
the protocol number
Definition node.h:281
Ptr< NetDevice > device
the NetDevice
Definition node.h:280
ProtocolHandler handler
the protocol handler
Definition node.h:279