src/node/node.cc
changeset 1176 4894ea885c0f
parent 729 b5e744285e92
child 1186 909e9eb2124e
equal deleted inserted replaced
1175:00ad4ec69939 1176:4894ea885c0f
     1 // -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*-
     1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
     2 //
     2 /*
     3 // Copyright (c) 2006 Georgia Tech Research Corporation
     3  * Copyright (c) 2006 Georgia Tech Research Corporation, INRIA
     4 // All rights reserved.
     4  *
     5 //
     5  * This program is free software; you can redistribute it and/or modify
     6 // 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 // it under the terms of the GNU General Public License version 2 as
     7  * published by the Free Software Foundation;
     8 // published by the Free Software Foundation;
     8  *
     9 //
     9  * This program is distributed in the hope that it will be useful,
    10 // This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13 // GNU General Public License for more details.
    13  *
    14 //
    14  * You should have received a copy of the GNU General Public License
    15 // 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 // 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 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    17  *
    18 //
    18  * Authors: George F. Riley<riley@ece.gatech.edu>
    19 // Author: George F. Riley<riley@ece.gatech.edu>
    19  *          Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
    20 //
    20  */
    21 
       
    22 // Implement the basic Node object for ns3.
       
    23 // George F. Riley, Georgia Tech, Fall 2006
       
    24 
       
    25 #include "node.h"
    21 #include "node.h"
    26 #include "node-list.h"
    22 #include "node-list.h"
    27 #include "net-device.h"
    23 #include "net-device.h"
    28 #include "application.h"
    24 #include "application.h"
    29 #include "ns3/simulator.h"
    25 #include "ns3/simulator.h"
       
    26 #include "ns3/empty-trace-resolver.h"
    30 
    27 
    31 namespace ns3{
    28 namespace ns3{
    32 
    29 
    33 const InterfaceId Node::iid = MakeInterfaceId ("Node", Object::iid);
    30 const InterfaceId Node::iid = MakeInterfaceId ("Node", Object::iid);
    34 
    31 
    72 uint32_t 
    69 uint32_t 
    73 Node::AddDevice (Ptr<NetDevice> device)
    70 Node::AddDevice (Ptr<NetDevice> device)
    74 {
    71 {
    75   uint32_t index = m_devices.size ();
    72   uint32_t index = m_devices.size ();
    76   m_devices.push_back (device);
    73   m_devices.push_back (device);
    77   DoAddDevice (device);
       
    78   device->SetIfIndex(index);
    74   device->SetIfIndex(index);
       
    75   device->SetReceiveCallback (MakeCallback (&Node::ReceiveFromDevice, this));
       
    76   NotifyDeviceAdded (device);
    79   return index;
    77   return index;
    80 }
    78 }
    81 Ptr<NetDevice>
    79 Ptr<NetDevice>
    82 Node::GetDevice (uint32_t index) const
    80 Node::GetDevice (uint32_t index) const
    83 {
    81 {
   127     }
   125     }
   128   m_applications.clear ();
   126   m_applications.clear ();
   129   Object::DoDispose ();
   127   Object::DoDispose ();
   130 }
   128 }
   131 
   129 
       
   130 TraceResolver *
       
   131 Node::DoCreateTraceResolver (TraceContext const &context)
       
   132 {
       
   133   return new EmptyTraceResolver (context);
       
   134 }
       
   135 void 
       
   136 Node::NotifyDeviceAdded (Ptr<NetDevice> device)
       
   137 {}
       
   138 
       
   139 void
       
   140 Node::RegisterProtocolHandler (ProtocolHandler handler, 
       
   141                                uint16_t protocolType,
       
   142                                Ptr<NetDevice> device)
       
   143 {
       
   144   struct Node::ProtocolHandlerEntry entry;
       
   145   entry.handler = handler;
       
   146   entry.isSpecificProtocol = true;
       
   147   entry.protocol = protocolType;
       
   148   entry.device = device;
       
   149   m_handlers.push_back (entry);
       
   150 }
       
   151 
       
   152 void 
       
   153 Node::RegisterProtocolHandler (ProtocolHandler handler,
       
   154                                Ptr<NetDevice> device)
       
   155 {
       
   156   struct Node::ProtocolHandlerEntry entry;
       
   157   entry.handler = handler;
       
   158   entry.isSpecificProtocol = false;
       
   159   entry.protocol = 0;
       
   160   entry.device = device;
       
   161   m_handlers.push_back (entry);
       
   162 }
       
   163 
       
   164 void
       
   165 Node::UnregisterProtocolHandler (ProtocolHandler handler)
       
   166 {
       
   167   for (ProtocolHandlerList::iterator i = m_handlers.begin ();
       
   168        i != m_handlers.end (); i++)
       
   169     {
       
   170       if (i->handler.IsEqual (handler))
       
   171         {
       
   172           m_handlers.erase (i);
       
   173           break;
       
   174         }
       
   175     }
       
   176 }
       
   177 
       
   178 bool
       
   179 Node::ReceiveFromDevice (Ptr<NetDevice> device, const Packet &packet, uint16_t protocol)
       
   180 {
       
   181   bool found = false;
       
   182   for (ProtocolHandlerList::iterator i = m_handlers.begin ();
       
   183        i != m_handlers.end (); i++)
       
   184     {
       
   185       if (i->device == 0 ||
       
   186           (i->device != 0 && i->device == device))
       
   187         {
       
   188           if (!i->isSpecificProtocol || 
       
   189               (i->isSpecificProtocol && i->protocol == protocol))
       
   190             {
       
   191               i->handler (packet, protocol, device);
       
   192               found = true;
       
   193             }
       
   194         }
       
   195     }
       
   196   return found;
       
   197 }
       
   198 
   132 }//namespace ns3
   199 }//namespace ns3