A Discrete-Event Network Simulator
API
 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 
36 
37 namespace ns3 {
38 
40  ;
41 
43  "A global switch to enable all checksums for all protocols",
44  BooleanValue (false),
45  MakeBooleanChecker ());
46 
47 TypeId
49 {
50  static TypeId tid = TypeId ("ns3::Node")
51  .SetParent<Object> ()
52  .AddConstructor<Node> ()
53  .AddAttribute ("DeviceList", "The list of devices associated to this Node.",
56  MakeObjectVectorChecker<NetDevice> ())
57  .AddAttribute ("ApplicationList", "The list of applications associated to this Node.",
60  MakeObjectVectorChecker<Application> ())
61  .AddAttribute ("Id", "The id (unique integer) of this Node.",
62  TypeId::ATTR_GET, // allow only getting it.
63  UintegerValue (0),
64  MakeUintegerAccessor (&Node::m_id),
65  MakeUintegerChecker<uint32_t> ())
66  .AddAttribute ("SystemId", "The systemId of this node: a unique integer used for parallel simulations.",
68  UintegerValue (0),
69  MakeUintegerAccessor (&Node::m_sid),
70  MakeUintegerChecker<uint32_t> ())
71  ;
72  return tid;
73 }
74 
76  : m_id (0),
77  m_sid (0)
78 {
79  NS_LOG_FUNCTION (this);
80  Construct ();
81 }
82 
83 Node::Node(uint32_t sid)
84  : m_id (0),
85  m_sid (sid)
86 {
87  NS_LOG_FUNCTION (this << sid);
88  Construct ();
89 }
90 
91 void
93 {
94  NS_LOG_FUNCTION (this);
95  m_id = NodeList::Add (this);
96 }
97 
99 {
100  NS_LOG_FUNCTION (this);
101 }
102 
103 uint32_t
104 Node::GetId (void) const
105 {
106  NS_LOG_FUNCTION (this);
107  return m_id;
108 }
109 
110 uint32_t
111 Node::GetSystemId (void) const
112 {
113  NS_LOG_FUNCTION (this);
114  return m_sid;
115 }
116 
117 uint32_t
119 {
120  NS_LOG_FUNCTION (this << device);
121  uint32_t index = m_devices.size ();
122  m_devices.push_back (device);
123  device->SetNode (this);
124  device->SetIfIndex (index);
125  device->SetReceiveCallback (MakeCallback (&Node::NonPromiscReceiveFromDevice, this));
126  Simulator::ScheduleWithContext (GetId (), Seconds (0.0),
127  &NetDevice::Initialize, device);
128  NotifyDeviceAdded (device);
129  return index;
130 }
132 Node::GetDevice (uint32_t index) const
133 {
134  NS_LOG_FUNCTION (this << index);
135  NS_ASSERT_MSG (index < m_devices.size (), "Device index " << index <<
136  " is out of range (only have " << m_devices.size () << " devices).");
137  return m_devices[index];
138 }
139 uint32_t
140 Node::GetNDevices (void) const
141 {
142  NS_LOG_FUNCTION (this);
143  return m_devices.size ();
144 }
145 
146 uint32_t
148 {
149  NS_LOG_FUNCTION (this << application);
150  uint32_t index = m_applications.size ();
151  m_applications.push_back (application);
152  application->SetNode (this);
153  Simulator::ScheduleWithContext (GetId (), Seconds (0.0),
154  &Application::Initialize, application);
155  return index;
156 }
158 Node::GetApplication (uint32_t index) const
159 {
160  NS_LOG_FUNCTION (this << index);
161  NS_ASSERT_MSG (index < m_applications.size (), "Application index " << index <<
162  " is out of range (only have " << m_applications.size () << " applications).");
163  return m_applications[index];
164 }
165 uint32_t
167 {
168  NS_LOG_FUNCTION (this);
169  return m_applications.size ();
170 }
171 
172 void
174 {
175  NS_LOG_FUNCTION (this);
176  m_deviceAdditionListeners.clear ();
177  m_handlers.clear ();
178  for (std::vector<Ptr<NetDevice> >::iterator i = m_devices.begin ();
179  i != m_devices.end (); i++)
180  {
181  Ptr<NetDevice> device = *i;
182  device->Dispose ();
183  *i = 0;
184  }
185  m_devices.clear ();
186  for (std::vector<Ptr<Application> >::iterator i = m_applications.begin ();
187  i != m_applications.end (); i++)
188  {
189  Ptr<Application> application = *i;
190  application->Dispose ();
191  *i = 0;
192  }
193  m_applications.clear ();
195 }
196 void
198 {
199  NS_LOG_FUNCTION (this);
200  for (std::vector<Ptr<NetDevice> >::iterator i = m_devices.begin ();
201  i != m_devices.end (); i++)
202  {
203  Ptr<NetDevice> device = *i;
204  device->Initialize ();
205  }
206  for (std::vector<Ptr<Application> >::iterator i = m_applications.begin ();
207  i != m_applications.end (); i++)
208  {
209  Ptr<Application> application = *i;
210  application->Initialize ();
211  }
212 
214 }
215 
216 void
218  uint16_t protocolType,
219  Ptr<NetDevice> device,
220  bool promiscuous)
221 {
222  NS_LOG_FUNCTION (this << &handler << protocolType << device << promiscuous);
223  struct Node::ProtocolHandlerEntry entry;
224  entry.handler = handler;
225  entry.protocol = protocolType;
226  entry.device = device;
227  entry.promiscuous = promiscuous;
228 
229  // On demand enable promiscuous mode in netdevices
230  if (promiscuous)
231  {
232  if (device == 0)
233  {
234  for (std::vector<Ptr<NetDevice> >::iterator i = m_devices.begin ();
235  i != m_devices.end (); i++)
236  {
237  Ptr<NetDevice> dev = *i;
238  dev->SetPromiscReceiveCallback (MakeCallback (&Node::PromiscReceiveFromDevice, this));
239  }
240  }
241  else
242  {
243  device->SetPromiscReceiveCallback (MakeCallback (&Node::PromiscReceiveFromDevice, this));
244  }
245  }
246 
247  m_handlers.push_back (entry);
248 }
249 
250 void
252 {
253  NS_LOG_FUNCTION (this << &handler);
254  for (ProtocolHandlerList::iterator i = m_handlers.begin ();
255  i != m_handlers.end (); i++)
256  {
257  if (i->handler.IsEqual (handler))
258  {
259  m_handlers.erase (i);
260  break;
261  }
262  }
263 }
264 
265 bool
267 {
269  BooleanValue val;
271  return val.Get ();
272 }
273 
274 bool
276  const Address &from, const Address &to, NetDevice::PacketType packetType)
277 {
278  NS_LOG_FUNCTION (this << device << packet << protocol << &from << &to << packetType);
279  return ReceiveFromDevice (device, packet, protocol, from, to, packetType, true);
280 }
281 
282 bool
284  const Address &from)
285 {
286  NS_LOG_FUNCTION (this << device << packet << protocol << &from);
287  return ReceiveFromDevice (device, packet, protocol, from, device->GetAddress (), NetDevice::PacketType (0), false);
288 }
289 
290 bool
291 Node::ReceiveFromDevice (Ptr<NetDevice> device, Ptr<const Packet> packet, uint16_t protocol,
292  const Address &from, const Address &to, NetDevice::PacketType packetType, bool promiscuous)
293 {
294  NS_LOG_FUNCTION (this << device << packet << protocol << &from << &to << packetType << promiscuous);
295  NS_ASSERT_MSG (Simulator::GetContext () == GetId (), "Received packet with erroneous context ; " <<
296  "make sure the channels in use are correctly updating events context " <<
297  "when transfering events from one node to another.");
298  NS_LOG_DEBUG ("Node " << GetId () << " ReceiveFromDevice: dev "
299  << device->GetIfIndex () << " (type=" << device->GetInstanceTypeId ().GetName ()
300  << ") Packet UID " << packet->GetUid ());
301  bool found = false;
302 
303  for (ProtocolHandlerList::iterator i = m_handlers.begin ();
304  i != m_handlers.end (); i++)
305  {
306  if (i->device == 0 ||
307  (i->device != 0 && i->device == device))
308  {
309  if (i->protocol == 0 ||
310  i->protocol == protocol)
311  {
312  if (promiscuous == i->promiscuous)
313  {
314  i->handler (device, packet, protocol, from, to, packetType);
315  found = true;
316  }
317  }
318  }
319  }
320  return found;
321 }
322 void
324 {
325  NS_LOG_FUNCTION (this << &listener);
326  m_deviceAdditionListeners.push_back (listener);
327  // and, then, notify the new listener about all existing devices.
328  for (std::vector<Ptr<NetDevice> >::const_iterator i = m_devices.begin ();
329  i != m_devices.end (); ++i)
330  {
331  listener (*i);
332  }
333 }
334 void
336 {
337  NS_LOG_FUNCTION (this << &listener);
338  for (DeviceAdditionListenerList::iterator i = m_deviceAdditionListeners.begin ();
339  i != m_deviceAdditionListeners.end (); i++)
340  {
341  if ((*i).IsEqual (listener))
342  {
343  m_deviceAdditionListeners.erase (i);
344  break;
345  }
346  }
347 }
348 
349 void
351 {
352  NS_LOG_FUNCTION (this << device);
353  for (DeviceAdditionListenerList::iterator i = m_deviceAdditionListeners.begin ();
354  i != m_deviceAdditionListeners.end (); i++)
355  {
356  (*i) (device);
357  }
358 }
359 
360 
361 } // namespace ns3
Ptr< const AttributeAccessor > MakeObjectVectorAccessor(U T::*memberContainer)
Definition: object-vector.h:51
uint32_t AddApplication(Ptr< Application > application)
Definition: node.cc:147
bool Get(void) const
Definition: boolean.cc:45
smart pointer class similar to boost::intrusive_ptr
Definition: ptr.h:59
#define NS_LOG_FUNCTION(parameters)
Definition: log.h:345
Hold a bool native type.
Definition: boolean.h:38
uint32_t GetNApplications(void) const
Definition: node.cc:166
virtual void DoInitialize(void)
This method is called only once by Object::Initialize.
Definition: node.cc:197
DeviceAdditionListenerList m_deviceAdditionListeners
Definition: node.h:230
static bool ChecksumEnabled(void)
Definition: node.cc:266
uint64_t GetUid(void) const
A packet is allocated a new uid when it is created empty or with zero-filled payload.
Definition: packet.cc:393
void NotifyDeviceAdded(Ptr< NetDevice > device)
Definition: node.cc:350
static uint32_t GetContext(void)
Definition: simulator.cc:300
ProtocolHandler handler
Definition: node.h:217
NS_OBJECT_ENSURE_REGISTERED(NullMessageSimulatorImpl)
virtual void DoDispose(void)
This method is called by Object::Dispose or by the object's destructor, whichever comes first...
Definition: object.cc:336
std::vector< Ptr< NetDevice > > m_devices
Definition: node.h:227
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
Definition: log.h:309
hold a so-called 'global value'.
Definition: global-value.h:47
uint32_t GetSystemId(void) const
Definition: node.cc:111
void UnregisterProtocolHandler(ProtocolHandler handler)
Definition: node.cc:251
a polymophic address class
Definition: address.h:86
The attribute can be read.
Definition: type-id.h:56
uint32_t m_sid
Definition: node.h:226
GlobalValue g_checksumEnabled
Definition: node.cc:42
Ptr< Application > GetApplication(uint32_t index) const
Definition: node.cc:158
virtual void DoDispose(void)
The dispose method.
Definition: node.cc:173
Ptr< NetDevice > device
Definition: node.h:218
bool PromiscReceiveFromDevice(Ptr< NetDevice > device, Ptr< const Packet >, uint16_t protocol, const Address &from, const Address &to, NetDevice::PacketType packetType)
Definition: node.cc:275
Hold an unsigned integer type.
Definition: uinteger.h:46
static uint32_t Add(Ptr< Node > node)
Definition: node-list.cc:176
Ptr< NetDevice > GetDevice(uint32_t index) const
Definition: node.cc:132
uint16_t protocol
Definition: node.h:219
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1238
static void ScheduleWithContext(uint32_t context, Time const &time, MEM mem_ptr, OBJ obj)
Schedule an event with the given context.
Definition: simulator.h:904
uint32_t GetNDevices(void) const
Definition: node.cc:140
ProtocolHandlerList m_handlers
Definition: node.h:229
NS_LOG_COMPONENT_DEFINE("Node")
void RegisterDeviceAdditionListener(DeviceAdditionListener listener)
Definition: node.cc:323
The attribute can be written.
Definition: type-id.h:57
std::vector< Ptr< Application > > m_applications
Definition: node.h:228
Definition: node.h:216
uint32_t m_id
Definition: node.h:225
void RegisterProtocolHandler(ProtocolHandler handler, uint16_t protocolType, Ptr< NetDevice > device, bool promiscuous=false)
Definition: node.cc:217
#define NS_ASSERT_MSG(condition, message)
Definition: assert.h:86
static TypeId GetTypeId(void)
Definition: node.cc:48
bool ReceiveFromDevice(Ptr< NetDevice > device, Ptr< const Packet >, uint16_t protocol, const Address &from, const Address &to, NetDevice::PacketType packetType, bool promisc)
Definition: node.cc:291
uint32_t AddDevice(Ptr< NetDevice > device)
Definition: node.cc:118
uint32_t GetId(void) const
Definition: node.cc:104
void UnregisterDeviceAdditionListener(DeviceAdditionListener listener)
Definition: node.cc:335
virtual ~Node()
Definition: node.cc:98
#define NS_LOG_DEBUG(msg)
Definition: log.h:289
void Initialize(void)
This method calls the virtual DoInitialize method on all the objects aggregated to this object...
Definition: object.cc:180
bool NonPromiscReceiveFromDevice(Ptr< NetDevice > device, Ptr< const Packet >, uint16_t protocol, const Address &from)
Definition: node.cc:283
a base class which provides memory management and object aggregation
Definition: object.h:63
PacketType
Packet types are used as they are in Linux.
Definition: net-device.h:270
contain a set of ns3::Object pointers.
a unique identifier for an interface.
Definition: type-id.h:49
void Construct(void)
Definition: node.cc:92
TypeId SetParent(TypeId tid)
Definition: type-id.cc:611
virtual void DoInitialize(void)
This method is called only once by Object::Initialize.
Definition: object.cc:343
void GetValue(AttributeValue &value) const
bool promiscuous
Definition: node.h:220
Node()
Definition: node.cc:75