implement the Node::ProtocolHandler support.

This commit is contained in:
Mathieu Lacage
2007-07-31 09:09:31 +02:00
parent 7af432c313
commit b4237e51dc
18 changed files with 174 additions and 524 deletions

View File

@@ -1,32 +1,29 @@
// -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*-
//
// Copyright (c) 2006 Georgia Tech Research Corporation
// All rights reserved.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2 as
// published by the Free Software Foundation;
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// Author: George F. Riley<riley@ece.gatech.edu>
//
// Implement the basic Node object for ns3.
// George F. Riley, Georgia Tech, Fall 2006
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2006 Georgia Tech Research Corporation, INRIA
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation;
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Authors: George F. Riley<riley@ece.gatech.edu>
* Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
*/
#include "node.h"
#include "node-list.h"
#include "net-device.h"
#include "application.h"
#include "ns3/simulator.h"
#include "ns3/empty-trace-resolver.h"
namespace ns3{
@@ -74,8 +71,9 @@ Node::AddDevice (Ptr<NetDevice> device)
{
uint32_t index = m_devices.size ();
m_devices.push_back (device);
DoAddDevice (device);
device->SetIfIndex(index);
device->SetReceiveCallback (MakeCallback (&Node::ReceiveFromDevice, this));
NotifyDeviceAdded (device);
return index;
}
Ptr<NetDevice>
@@ -129,4 +127,73 @@ void Node::DoDispose()
Object::DoDispose ();
}
TraceResolver *
Node::DoCreateTraceResolver (TraceContext const &context)
{
return new EmptyTraceResolver (context);
}
void
Node::NotifyDeviceAdded (Ptr<NetDevice> device)
{}
void
Node::RegisterProtocolHandler (ProtocolHandler handler,
uint16_t protocolType,
Ptr<NetDevice> device)
{
struct Node::ProtocolHandlerEntry entry;
entry.handler = handler;
entry.isSpecificProtocol = true;
entry.protocol = protocolType;
entry.device = device;
m_handlers.push_back (entry);
}
void
Node::RegisterProtocolHandler (ProtocolHandler handler,
Ptr<NetDevice> device)
{
struct Node::ProtocolHandlerEntry entry;
entry.handler = handler;
entry.isSpecificProtocol = false;
entry.protocol = 0;
entry.device = device;
m_handlers.push_back (entry);
}
void
Node::UnregisterProtocolHandler (ProtocolHandler handler)
{
for (ProtocolHandlerList::iterator i = m_handlers.begin ();
i != m_handlers.end (); i++)
{
if (i->handler.IsEqual (handler))
{
m_handlers.erase (i);
break;
}
}
}
bool
Node::ReceiveFromDevice (Ptr<NetDevice> device, const Packet &packet, uint16_t protocol)
{
bool found = false;
for (ProtocolHandlerList::iterator i = m_handlers.begin ();
i != m_handlers.end (); i++)
{
if (i->device == 0 ||
(i->device != 0 && i->device == device))
{
if (!i->isSpecificProtocol ||
(i->isSpecificProtocol && i->protocol == protocol))
{
i->handler (packet, protocol, device);
found = true;
}
}
}
return found;
}
}//namespace ns3

View File

@@ -24,6 +24,7 @@
#include <vector>
#include "ns3/object.h"
#include "ns3/callback.h"
namespace ns3 {
@@ -31,6 +32,7 @@ class TraceContext;
class TraceResolver;
class NetDevice;
class Application;
class Packet;
/**
* \brief A network Node.
@@ -123,6 +125,41 @@ public:
*/
uint32_t GetNApplications (void) const;
/**
* A protocol handler
*/
typedef Callback<void,const Packet &,uint16_t,Ptr<NetDevice> > ProtocolHandler;
/**
* \param handler the handler to register
* \param protocolType the type of protocol this handler is
* interested in.
* \param device the device attached to this handler. If the
* value is zero, the handler is attached to all
* devices on this node.
*/
void RegisterProtocolHandler (ProtocolHandler handler,
uint16_t protocolType,
Ptr<NetDevice> device);
/**
* \param handler the handler to register
* \param device the device attached to this handler. If the
* value is zero, the handler is attached to all
* devices on this node.
*
* Register a handler to receive all packets for all
* protocols.
*/
void RegisterProtocolHandler (ProtocolHandler handler,
Ptr<NetDevice> device);
/**
* \param handler the handler to unregister
*
* After this call returns, the input handler will never
* be invoked anymore.
*/
void UnregisterProtocolHandler (ProtocolHandler handler);
protected:
/**
* Must be invoked by subclasses only.
@@ -147,7 +184,7 @@ private:
*
* Subclasses must implement this method.
*/
virtual TraceResolver *DoCreateTraceResolver (TraceContext const &context) = 0;
virtual TraceResolver *DoCreateTraceResolver (TraceContext const &context);
/**
* \param device the device added to this Node.
*
@@ -156,12 +193,22 @@ private:
* at this point to setup the node's receive function for
* the NetDevice packets.
*/
virtual void DoAddDevice (Ptr<NetDevice> device) = 0;
virtual void NotifyDeviceAdded (Ptr<NetDevice> device);
bool ReceiveFromDevice (Ptr<NetDevice> device, const Packet &packet, uint16_t protocol);
struct ProtocolHandlerEntry {
ProtocolHandler handler;
bool isSpecificProtocol;
uint16_t protocol;
Ptr<NetDevice> device;
};
typedef std::vector<struct Node::ProtocolHandlerEntry> ProtocolHandlerList;
uint32_t m_id; // Node id for this node
uint32_t m_sid; // System id for this node
std::vector<Ptr<NetDevice> > m_devices;
std::vector<Ptr<Application> > m_applications;
ProtocolHandlerList m_handlers;
};
} //namespace ns3