00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "node.h"
00022 #include "node-list.h"
00023 #include "net-device.h"
00024 #include "application.h"
00025 #include "ns3/packet.h"
00026 #include "ns3/simulator.h"
00027 #include "ns3/object-vector.h"
00028 #include "ns3/uinteger.h"
00029 #include "ns3/log.h"
00030 #include "ns3/assert.h"
00031
00032 NS_LOG_COMPONENT_DEFINE ("Node");
00033
00034 namespace ns3{
00035
00036 NS_OBJECT_ENSURE_REGISTERED (Node);
00037
00038 TypeId
00039 Node::GetTypeId (void)
00040 {
00041 static TypeId tid = TypeId ("ns3::Node")
00042 .SetParent<Object> ()
00043 .AddAttribute ("DeviceList", "The list of devices associated to this Node.",
00044 ObjectVectorValue (),
00045 MakeObjectVectorAccessor (&Node::m_devices),
00046 MakeObjectVectorChecker<NetDevice> ())
00047 .AddAttribute ("ApplicationList", "The list of applications associated to this Node.",
00048 ObjectVectorValue (),
00049 MakeObjectVectorAccessor (&Node::m_applications),
00050 MakeObjectVectorChecker<Application> ())
00051 .AddAttribute ("Id", "The id (unique integer) of this Node.",
00052 TypeId::ATTR_GET,
00053 UintegerValue (0),
00054 MakeUintegerAccessor (&Node::m_id),
00055 MakeUintegerChecker<uint32_t> ())
00056 ;
00057 return tid;
00058 }
00059
00060 Node::Node()
00061 : m_id(0),
00062 m_sid(0)
00063 {
00064 Construct ();
00065 }
00066
00067 Node::Node(uint32_t sid)
00068 : m_id(0),
00069 m_sid(sid)
00070 {
00071 Construct ();
00072 }
00073
00074 void
00075 Node::Construct (void)
00076 {
00077 m_id = NodeList::Add (this);
00078 }
00079
00080 Node::~Node ()
00081 {}
00082
00083 uint32_t
00084 Node::GetId (void) const
00085 {
00086 return m_id;
00087 }
00088
00089 uint32_t
00090 Node::GetSystemId (void) const
00091 {
00092 return m_sid;
00093 }
00094
00095 uint32_t
00096 Node::AddDevice (Ptr<NetDevice> device)
00097 {
00098 uint32_t index = m_devices.size ();
00099 m_devices.push_back (device);
00100 device->SetNode (this);
00101 device->SetIfIndex(index);
00102 device->SetReceiveCallback (MakeCallback (&Node::NonPromiscReceiveFromDevice, this));
00103 NotifyDeviceAdded (device);
00104 return index;
00105 }
00106 Ptr<NetDevice>
00107 Node::GetDevice (uint32_t index) const
00108 {
00109 NS_ASSERT_MSG (index < m_devices.size (), "Device index " << index <<
00110 " is out of range (only have " << m_devices.size () << " devices).");
00111 return m_devices[index];
00112 }
00113 uint32_t
00114 Node::GetNDevices (void) const
00115 {
00116 return m_devices.size ();
00117 }
00118
00119 uint32_t
00120 Node::AddApplication (Ptr<Application> application)
00121 {
00122 uint32_t index = m_applications.size ();
00123 m_applications.push_back (application);
00124 application->SetNode (this);
00125 return index;
00126 }
00127 Ptr<Application>
00128 Node::GetApplication (uint32_t index) const
00129 {
00130 NS_ASSERT_MSG (index < m_applications.size (), "Application index " << index <<
00131 " is out of range (only have " << m_applications.size () << " applications).");
00132 return m_applications[index];
00133 }
00134 uint32_t
00135 Node::GetNApplications (void) const
00136 {
00137 return m_applications.size ();
00138 }
00139
00140 void
00141 Node::DoDispose()
00142 {
00143 for (std::vector<Ptr<NetDevice> >::iterator i = m_devices.begin ();
00144 i != m_devices.end (); i++)
00145 {
00146 Ptr<NetDevice> device = *i;
00147 device->Dispose ();
00148 *i = 0;
00149 }
00150 m_devices.clear ();
00151 for (std::vector<Ptr<Application> >::iterator i = m_applications.begin ();
00152 i != m_applications.end (); i++)
00153 {
00154 Ptr<Application> application = *i;
00155 application->Dispose ();
00156 *i = 0;
00157 }
00158 m_applications.clear ();
00159 Object::DoDispose ();
00160 }
00161
00162 void
00163 Node::NotifyDeviceAdded (Ptr<NetDevice> device)
00164 {}
00165
00166 void
00167 Node::RegisterProtocolHandler (ProtocolHandler handler,
00168 uint16_t protocolType,
00169 Ptr<NetDevice> device,
00170 bool promiscuous)
00171 {
00172 struct Node::ProtocolHandlerEntry entry;
00173 entry.handler = handler;
00174 entry.protocol = protocolType;
00175 entry.device = device;
00176 entry.promiscuous = promiscuous;
00177
00178
00179 if (promiscuous)
00180 {
00181 if (device == 0)
00182 {
00183 for (std::vector<Ptr<NetDevice> >::iterator i = m_devices.begin ();
00184 i != m_devices.end (); i++)
00185 {
00186 Ptr<NetDevice> dev = *i;
00187 if (dev->SupportsSendFrom ())
00188 {
00189 dev->SetPromiscReceiveCallback (MakeCallback (&Node::PromiscReceiveFromDevice, this));
00190 }
00191 }
00192 }
00193 else
00194 {
00195 if (device->SupportsSendFrom ())
00196 {
00197 device->SetPromiscReceiveCallback (MakeCallback (&Node::PromiscReceiveFromDevice, this));
00198 }
00199 else
00200 {
00201 NS_LOG_WARN ("Protocol handler request promiscuous mode for a specific netdevice,"
00202 " but netdevice does not support promiscuous mode.");
00203 }
00204 }
00205 }
00206
00207 m_handlers.push_back (entry);
00208 }
00209
00210 void
00211 Node::UnregisterProtocolHandler (ProtocolHandler handler)
00212 {
00213 for (ProtocolHandlerList::iterator i = m_handlers.begin ();
00214 i != m_handlers.end (); i++)
00215 {
00216 if (i->handler.IsEqual (handler))
00217 {
00218 m_handlers.erase (i);
00219 break;
00220 }
00221 }
00222 }
00223
00224 bool
00225 Node::PromiscReceiveFromDevice (Ptr<NetDevice> device, Ptr<const Packet> packet, uint16_t protocol,
00226 const Address &from, const Address &to, NetDevice::PacketType packetType)
00227 {
00228 NS_LOG_FUNCTION(device->GetName ());
00229 return ReceiveFromDevice (device, packet, protocol, from, to, packetType, true);
00230 }
00231
00232 bool
00233 Node::NonPromiscReceiveFromDevice (Ptr<NetDevice> device, Ptr<const Packet> packet, uint16_t protocol,
00234 const Address &from)
00235 {
00236 NS_LOG_FUNCTION(device->GetName ());
00237 return ReceiveFromDevice (device, packet, protocol, from, from, NetDevice::PacketType (0), false);
00238 }
00239
00240 bool
00241 Node::ReceiveFromDevice (Ptr<NetDevice> device, Ptr<const Packet> packet, uint16_t protocol,
00242 const Address &from, const Address &to, NetDevice::PacketType packetType, bool promiscuous)
00243 {
00244 NS_LOG_DEBUG("Node " << GetId () << " ReceiveFromDevice: dev "
00245 << device->GetIfIndex () << " ("
00246 << device->GetName () << " type " << device->GetInstanceTypeId ().GetName ()
00247 << ") Packet UID " << packet->GetUid ());
00248 bool found = false;
00249
00250 for (ProtocolHandlerList::iterator i = m_handlers.begin ();
00251 i != m_handlers.end (); i++)
00252 {
00253 if (i->device == 0 ||
00254 (i->device != 0 && i->device == device))
00255 {
00256 if (i->protocol == 0 ||
00257 i->protocol == protocol)
00258 {
00259 if (promiscuous == i->promiscuous)
00260 {
00261 i->handler (device, packet, protocol, from, to, packetType);
00262 found = true;
00263 }
00264 }
00265 }
00266 }
00267 return found;
00268 }
00269
00270 }