From f1cd4984a19a4bb2669dbcfcb28551df64e792be Mon Sep 17 00:00:00 2001 From: Tom Henderson Date: Tue, 27 Mar 2007 21:48:22 -0700 Subject: [PATCH] Add ApplicationList and Capability --- SConstruct | 5 ++ examples/simple-p2p.cc | 8 ++++ src/node/application-list.cc | 91 ++++++++++++++++++++++++++++++++++++ src/node/application-list.h | 60 ++++++++++++++++++++++++ src/node/capability.cc | 82 ++++++++++++++++++++++++++++++++ src/node/capability.h | 55 ++++++++++++++++++++++ src/node/internet-node.cc | 11 +++++ src/node/internet-node.h | 2 + src/node/node.cc | 5 ++ src/node/node.h | 10 +++- 10 files changed, 327 insertions(+), 2 deletions(-) create mode 100644 src/node/application-list.cc create mode 100644 src/node/application-list.h create mode 100644 src/node/capability.cc create mode 100644 src/node/capability.h diff --git a/SConstruct b/SConstruct index 57cbe7b12..a08cf33b7 100644 --- a/SConstruct +++ b/SConstruct @@ -204,8 +204,10 @@ node.add_sources ([ 'udp-end-point.cc', 'datagram-socket.cc', 'udp.cc', + 'capability.cc', 'arp-header.cc', 'application.cc', + 'application-list.cc', 'onoff-application.cc', 'arp-cache.cc', 'arp-ipv4-interface.cc', @@ -226,6 +228,7 @@ node.add_headers ([ 'udp.h', 'ipv4-l4-protocol.h', 'application.h', + 'application-list.h', 'onoff-application.h', 'arp-header.h', 'arp-cache-cache.h', @@ -259,7 +262,9 @@ node.add_inst_headers ([ 'udp-header.h', 'channel.h', 'node-list.h', + 'capability.h', 'application.h', + 'application-list.h', 'onoff-application.h', ]) diff --git a/examples/simple-p2p.cc b/examples/simple-p2p.cc index eef93d555..8bbc33769 100644 --- a/examples/simple-p2p.cc +++ b/examples/simple-p2p.cc @@ -68,6 +68,7 @@ #include "ns3/object-container.h" #include "ns3/p2p-topology.h" #include "ns3/onoff-application.h" +#include "ns3/application-list.h" #include "ns3/random-variable.h" using namespace ns3; @@ -265,6 +266,13 @@ int main (int argc, char *argv[]) sink1->Bind (80); #ifdef NOTYET + // The arguments to ApplicationTCPSend constructor are the IPAddress + // of the server, port number for the server, and a random variable + // specifying the amount of data to send. + OnOffApplication* ooff = n1->GetApplicationList()-> + Add(OnOffApplication(*n0, Ipv4Address("10.1.2.2"), + 80, ConstantVariable(1), ConstantVariable(0), 1000, 210)); + // This is functional and could soon replace the above DatagramSockets, // but needs tuning OnOffApplication* ooff = new OnOffApplication(*n0, Ipv4Address("10.1.2.2"), diff --git a/src/node/application-list.cc b/src/node/application-list.cc new file mode 100644 index 000000000..bdb357515 --- /dev/null +++ b/src/node/application-list.cc @@ -0,0 +1,91 @@ +// -*- Mode:NS3 -*- +// +// 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 +// +// Implement the application list capability for NS3 nodes +// George F. Riley, Georgia Tech, Spring 2007 + +#include "application.h" +#include "application-list.h" + +#define nil 0 + +namespace ns3{ + +ApplicationList::ApplicationList() + : Capability(nil) +{ +} + +ApplicationList::ApplicationList(Node* n) + : Capability(n) +{ +} + +ApplicationList::~ApplicationList() +{ // Destructor, nothing needed as the SmartSet destroys itself +} + +ApplicationList* ApplicationList::Copy(Node& n) const +{ // Copy this app list + ApplicationList* r = new ApplicationList(); + r->SetNode(n); + return r; +} + +void ApplicationList::SetNode(Node& n) +{ + Capability::SetNode(n); + // Set the node pointer in each application + for (SmartSet::const_iterator i = m_apps.Begin(); + i != m_apps.End(); ++i) + { // Set correct node pointer in each app + (*i)->SetNode(n); + } +} + +void ApplicationList::Remove(Application* a) +{ // Remove the specified application from the list + m_apps.Remove(a); +} + +SmartSet::size_type ApplicationList::Count() const +{ + return m_apps.Size(); +} + +Application* ApplicationList::Get(SmartSet::size_type i) const +{ // Get the i'th application. Note, this is linear time in N + if (m_apps.Empty()) return nil; // List is empty + SmartSet::const_iterator k = m_apps.Begin(); + while(i > 0) + { + if (k == m_apps.End()) return nil; // Not found + --i; + ++k; + } + return *k; +} + +const SmartSet& ApplicationList::GetAll() const +{ + return m_apps; +} + +}//namespace ns3 diff --git a/src/node/application-list.h b/src/node/application-list.h new file mode 100644 index 000000000..211749b3b --- /dev/null +++ b/src/node/application-list.h @@ -0,0 +1,60 @@ +// -*- Mode:NS3 -*- +// +// 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 +// +// Manages the list of applications associated with a node. +// George F. Riley, Georgia Tech, Spring 2007 + +#ifndef __APPLICATION_LIST_H__ +#define __APPLICATION_LIST_H__ + +#include "application.h" +#include "ns3/smartset.h" +#include "capability.h" + +namespace ns3 { + +class ApplicationList : public Capability { +public: + ApplicationList(); + ApplicationList(Node*); + // Copy constructor not needed, default one is correct + ~ApplicationList(); + // Inherited from Capabilty + virtual ApplicationList* Copy(Node&) const; + virtual void SetNode(Node&); // Sets the node for all apps + // Manage the list + template T* Add(const T& t) // Add a new application + { + T* a = t.Copy(); + m_apps.Add(a); + return a; + } + void Remove(Application*); // Application has finished + SmartSet::size_type Count() const; // Number of applications + Application* Get(SmartSet::size_type) const; // Get app by index + const SmartSet& GetAll() const; // Get the entire app list + +private: + SmartSet m_apps; +}; + +}//namespace ns3 +#endif + diff --git a/src/node/capability.cc b/src/node/capability.cc new file mode 100644 index 000000000..d134feee2 --- /dev/null +++ b/src/node/capability.cc @@ -0,0 +1,82 @@ +// -*- Mode:NS3 -*- +// +// 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 +// + +// Implementation of the capability base class. + +#include "capability.h" +#include "node.h" +#include "node-reference.h" + +#define nil 0 + +namespace ns3 { + +Capability::Capability() + : m_node(nil) +{ // Nothing else needed +} + +Capability::Capability(Node* n) +{ + m_node = new NodeReference(*n); +} + +// Copy constructor +Capability::Capability(const Capability& o) +{ + m_node = new NodeReference(*o.GetNode()); +} + + +// Assignment operator +Capability& Capability::operator=(const Capability& rhs) +{ + if (this == &rhs) return *this; // Self assignment + delete m_node; + m_node = new NodeReference(*rhs.GetNode()); + return *this; +} + + + +Capability::~Capability() +{ + delete m_node; +} + +// SetNode should be overridden by any capability subclass +// that manages other objects needing node information, such +// as the ApplicationList. +void Capability::SetNode(Node& n) +{ + delete m_node; + m_node = new NodeReference(n); +} + + +Node* Capability::GetNode() const +{ + if (!m_node) return nil; + return m_node->GetNode(); +} + +} // namespace ns3 + diff --git a/src/node/capability.h b/src/node/capability.h new file mode 100644 index 000000000..b60e0b227 --- /dev/null +++ b/src/node/capability.h @@ -0,0 +1,55 @@ +// -*- Mode:NS3 -*- +// +// 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 +// +// Define the base class for all node capabilities. +// George F. Riley, Georgia Tech, Fall 2006 + +#ifndef __CAPABILITY_H__ +#define __CAPABILITY_H__ + +// All capabilities must implement a copy method, to allow node subclasses +// to have a pointer to any subclass of the capability and still copy +// correctly. Capabilities also have a pointer to the owning node. +// The node pointer is not owned by the capability, and is not deleted +// by the capability destructor. + +#define nil 0 + +namespace ns3 { +class Node; +class NodeReference; + +class Capability +{ +public: + Capability(); + Capability(Node* n); + Capability(const Capability&); // Copy constructor + virtual Capability& operator=(const Capability&); // Assignment operator + virtual ~Capability(); + virtual Capability* Copy(Node&) const = 0; + virtual void SetNode(Node&); + virtual Node* GetNode() const; +private: + NodeReference* m_node; // Node on which this capability is assigned +}; + +} //namespace ns3 +#endif diff --git a/src/node/internet-node.cc b/src/node/internet-node.cc index 1c4d5a645..cec474eec 100644 --- a/src/node/internet-node.cc +++ b/src/node/internet-node.cc @@ -24,6 +24,7 @@ #include "ns3/composite-trace-resolver.h" #include "net-device-list.h" +#include "application-list.h" #include "l3-demux.h" #include "ipv4-l4-demux.h" #include "internet-node.h" @@ -45,6 +46,7 @@ InternetNode::InternetNode() { // Instantiate the capabilities m_netDevices = new NetDeviceList(); + m_applicationList = new ApplicationList(); m_l3Demux = new L3Demux(this); m_ipv4L4Demux = new Ipv4L4Demux(this); m_l3Demux->Insert (Ipv4 (this)); @@ -56,6 +58,7 @@ InternetNode::InternetNode() InternetNode::InternetNode (InternetNode const &o) { m_netDevices = new NetDeviceList (); + m_applicationList = new ApplicationList(); m_l3Demux = o.m_l3Demux->Copy (this); m_ipv4L4Demux = o.m_ipv4L4Demux->Copy (this); SetupLoopback (); @@ -64,6 +67,7 @@ InternetNode const & InternetNode::operator = (InternetNode const &o) { delete m_netDevices; + delete m_applicationList; delete m_l3Demux; delete m_ipv4L4Demux; m_netDevices = new NetDeviceList (); @@ -76,6 +80,7 @@ InternetNode::operator = (InternetNode const &o) InternetNode::~InternetNode () { delete m_netDevices; + delete m_applicationList; delete m_l3Demux; delete m_ipv4L4Demux; } @@ -128,6 +133,12 @@ InternetNode::GetNetDeviceList() const return m_netDevices; } +ApplicationList* +InternetNode::GetApplicationList() const +{ + return m_applicationList; +} + L3Demux* InternetNode::GetL3Demux() const { diff --git a/src/node/internet-node.h b/src/node/internet-node.h index b61a23fc5..3ef7b1ed8 100644 --- a/src/node/internet-node.h +++ b/src/node/internet-node.h @@ -48,6 +48,7 @@ public: virtual TraceResolver *CreateTraceResolver (TraceContext const &context); // Capability access virtual NetDeviceList* GetNetDeviceList() const; + virtual ApplicationList* GetApplicationList() const; virtual L3Demux* GetL3Demux() const; virtual Ipv4L4Demux* GetIpv4L4Demux() const; virtual Ipv4 * GetIpv4 (void) const; @@ -59,6 +60,7 @@ private: void SetupLoopback (void); // Capabilities NetDeviceList* m_netDevices; + ApplicationList* m_applicationList; L3Demux* m_l3Demux; Ipv4L4Demux* m_ipv4L4Demux; std::string m_name; diff --git a/src/node/node.cc b/src/node/node.cc index 6fde6c5c8..00cb11609 100644 --- a/src/node/node.cc +++ b/src/node/node.cc @@ -124,6 +124,11 @@ Node::GetIpv4L4Demux() const return 0; } +ApplicationList* Node::GetApplicationList() const +{ + return 0; +} + NetDeviceList* Node::GetNetDeviceList() const { diff --git a/src/node/node.h b/src/node/node.h index 483183ed8..a1f2746c5 100644 --- a/src/node/node.h +++ b/src/node/node.h @@ -86,15 +86,20 @@ namespace ns3 { +class NodeList; + +class NetDeviceList; +class ApplicationList; + +// The below five may be encapsulated/abstracted in a Kernel or Stack class class L3Demux; class Ipv4L4Demux; -class NetDeviceList; class Ipv4; class Udp; class Arp; + class TraceContext; class TraceResolver; -class NodeList; class Node { friend class NodeList; @@ -168,6 +173,7 @@ public: virtual L3Demux* GetL3Demux() const; virtual Ipv4L4Demux* GetIpv4L4Demux() const; virtual NetDeviceList* GetNetDeviceList() const; + virtual ApplicationList* GetApplicationList() const; virtual Ipv4 * GetIpv4 (void) const; virtual Udp * GetUdp (void) const; virtual Arp * GetArp (void) const;