implement Dispose properly throughout the network stack
This commit is contained in:
1 parent
f09be37481
commit
4e7a215f6a
10 files changed
+90
-194
No files matched your search
@@ -27,12 +27,24 @@
|
||||
namespace ns3{
|
||||
|
||||
ApplicationList::ApplicationList(Node* n)
|
||||
: Capability(n)
|
||||
{}
|
||||
|
||||
void
|
||||
ApplicationList::Dispose (void)
|
||||
{
|
||||
for (std::vector<Application*>::const_iterator i = m_apps.begin();
|
||||
i != m_apps.end(); ++i)
|
||||
{
|
||||
Application *app = *i;
|
||||
app->Dispose ();
|
||||
delete app;
|
||||
}
|
||||
m_apps.clear ();
|
||||
}
|
||||
|
||||
ApplicationList::~ApplicationList()
|
||||
{ // Destructor, nothing needed as the SmartSet destroys itself
|
||||
Dispose ();
|
||||
}
|
||||
|
||||
ApplicationList* ApplicationList::Copy(Node * n) const
|
||||
@@ -44,46 +56,29 @@ ApplicationList* ApplicationList::Copy(Node * n) const
|
||||
void
|
||||
ApplicationList::Add(Application* a)
|
||||
{
|
||||
m_apps.Add(a);
|
||||
m_apps.push_back(a);
|
||||
}
|
||||
|
||||
void ApplicationList::SetNode(Node * n)
|
||||
{
|
||||
Capability::SetNode(n);
|
||||
// Set the node pointer in each application
|
||||
for (SmartSet<Application*>::const_iterator i = m_apps.Begin();
|
||||
i != m_apps.End(); ++i)
|
||||
for (std::vector<Application *>::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<Application*>::size_type ApplicationList::Count() const
|
||||
uint32_t ApplicationList::Count() const
|
||||
{
|
||||
return m_apps.Size();
|
||||
return m_apps.size();
|
||||
}
|
||||
|
||||
Application* ApplicationList::Get(SmartSet<Application*>::size_type i) const
|
||||
Application* ApplicationList::Get(uint32_t i) const
|
||||
{ // Get the i'th application. Note, this is linear time in N
|
||||
if (m_apps.Empty()) return 0; // List is empty
|
||||
SmartSet<Application*>::const_iterator k = m_apps.Begin();
|
||||
while(i > 0)
|
||||
{
|
||||
if (k == m_apps.End()) return 0; // Not found
|
||||
--i;
|
||||
++k;
|
||||
}
|
||||
return *k;
|
||||
}
|
||||
|
||||
const SmartSet<Application*>& ApplicationList::GetAll() const
|
||||
{
|
||||
return m_apps;
|
||||
if (m_apps.empty()) return 0; // List is empty
|
||||
return m_apps[i];
|
||||
}
|
||||
|
||||
}//namespace ns3
|
||||
@@ -25,17 +25,17 @@
|
||||
#define __APPLICATION_LIST_H__
|
||||
|
||||
#include "application.h"
|
||||
#include "ns3/smartset.h"
|
||||
#include "capability.h"
|
||||
#include <vector>
|
||||
|
||||
namespace ns3 {
|
||||
|
||||
class ApplicationList : public Capability {
|
||||
class ApplicationList {
|
||||
public:
|
||||
ApplicationList(Node*);
|
||||
// Copy constructor not needed, default one is correct
|
||||
~ApplicationList();
|
||||
virtual ~ApplicationList();
|
||||
// Inherited from Capabilty
|
||||
void Dispose (void);
|
||||
virtual ApplicationList* Copy(Node*) const;
|
||||
virtual void SetNode(Node *); // Sets the node for all apps
|
||||
virtual void Add(Application*); // Add an already new'ed app
|
||||
@@ -43,16 +43,15 @@ public:
|
||||
template <typename T> T* AddCopy(const T& t) // Add a new application
|
||||
{
|
||||
T* a = t.Copy();
|
||||
m_apps.Add(a);
|
||||
m_apps.push_back(a);
|
||||
return a;
|
||||
}
|
||||
void Remove(Application*); // Application has finished
|
||||
SmartSet<Application*>::size_type Count() const; // Number of applications
|
||||
Application* Get(SmartSet<Application*>::size_type) const; // Get app by index
|
||||
const SmartSet<Application*>& GetAll() const; // Get the entire app list
|
||||
uint32_t Count() const; // Number of applications
|
||||
Application* Get(uint32_t i) const; // Get app by index
|
||||
|
||||
private:
|
||||
SmartSet<Application*> m_apps;
|
||||
std::vector<Application*> m_apps;
|
||||
};
|
||||
|
||||
}//namespace ns3
|
||||
|
||||
+23
-5
@@ -59,13 +59,31 @@ Application::Application(const Application& o)
|
||||
// \brief Application Destructor
|
||||
Application::~Application()
|
||||
{
|
||||
m_node->Unref ();
|
||||
// Cancel the start/stop events if scheduled
|
||||
if (m_start) Simulator::Cancel(m_startEvent);
|
||||
if (m_stop) Simulator::Cancel(m_stopEvent);
|
||||
// Delete the random variablse
|
||||
Dispose ();
|
||||
}
|
||||
|
||||
void
|
||||
Application::Dispose (void)
|
||||
{
|
||||
if (m_node != 0)
|
||||
{
|
||||
m_node->Unref ();
|
||||
m_node = 0;
|
||||
}
|
||||
if (m_start)
|
||||
{
|
||||
Simulator::Cancel(m_startEvent);
|
||||
m_start = false;
|
||||
}
|
||||
if (m_stop)
|
||||
{
|
||||
Simulator::Cancel(m_stopEvent);
|
||||
m_stop = false;
|
||||
}
|
||||
delete m_startVar;
|
||||
m_startVar = 0;
|
||||
delete m_stopVar;
|
||||
m_stopVar = 0;
|
||||
}
|
||||
|
||||
Application& Application::operator=(const Application& rhs)
|
||||
|
||||
@@ -61,6 +61,8 @@ public:
|
||||
Application(const Application&); // Copy constructor
|
||||
Application& operator=(const Application&); // Assignment operator
|
||||
virtual ~Application();
|
||||
|
||||
virtual void Dispose (void);
|
||||
|
||||
virtual Application* Copy() const = 0; // All applications must provide
|
||||
|
||||
|
||||
@@ -1,94 +0,0 @@
|
||||
// -*- 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<riley@ece.gatech.edu>
|
||||
//
|
||||
|
||||
// Implementation of the capability base class.
|
||||
|
||||
#include "capability.h"
|
||||
#include "node.h"
|
||||
|
||||
namespace ns3 {
|
||||
|
||||
Capability::Capability(Node* n)
|
||||
: m_node (n)
|
||||
{
|
||||
m_node->Ref ();
|
||||
}
|
||||
|
||||
Capability::~Capability ()
|
||||
{
|
||||
if (m_node != 0)
|
||||
{
|
||||
m_node->Unref ();
|
||||
}
|
||||
}
|
||||
|
||||
// Copy constructor
|
||||
Capability::Capability(const Capability& o)
|
||||
: m_node (o.m_node)
|
||||
{
|
||||
if (m_node != 0)
|
||||
{
|
||||
m_node->Ref ();
|
||||
}
|
||||
}
|
||||
|
||||
// Assignment operator
|
||||
Capability& Capability::operator=(const Capability& rhs)
|
||||
{
|
||||
if (this == &rhs) return *this; // Self assignment
|
||||
if (m_node != 0)
|
||||
{
|
||||
m_node->Unref ();
|
||||
}
|
||||
m_node = rhs.m_node;
|
||||
if (m_node != 0)
|
||||
{
|
||||
m_node->Ref ();
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
// SetNode should be overridden by any capability subclass
|
||||
// that manages other objects needing node information, such
|
||||
// as the ApplicationList.
|
||||
void Capability::SetNode(Node *node)
|
||||
{
|
||||
if (m_node != 0)
|
||||
{
|
||||
m_node->Unref ();
|
||||
}
|
||||
m_node = node;
|
||||
if (m_node != 0)
|
||||
{
|
||||
m_node->Ref ();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Node* Capability::GetNode() const
|
||||
{
|
||||
if (!m_node) return 0;
|
||||
m_node->Ref ();
|
||||
return m_node;
|
||||
}
|
||||
|
||||
} // namespace ns3
|
||||
|
||||
@@ -1,52 +0,0 @@
|
||||
// -*- 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<riley@ece.gatech.edu>
|
||||
//
|
||||
// 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.
|
||||
|
||||
namespace ns3 {
|
||||
class Node;
|
||||
class NodeReference;
|
||||
|
||||
class Capability
|
||||
{
|
||||
public:
|
||||
Capability(Node* n);
|
||||
Capability(const Capability&); // Copy constructor
|
||||
virtual Capability& operator=(const Capability&); // Assignment operator
|
||||
virtual ~Capability();
|
||||
virtual Capability* Copy(Node *node) const = 0;
|
||||
virtual void SetNode(Node *node);
|
||||
virtual Node* GetNode() const;
|
||||
private:
|
||||
Node* m_node; // Node on which this capability is assigned
|
||||
};
|
||||
|
||||
} //namespace ns3
|
||||
#endif
|
||||
@@ -48,12 +48,10 @@ InternetNode::InternetNode()
|
||||
|
||||
InternetNode::~InternetNode ()
|
||||
{
|
||||
delete m_applicationList;
|
||||
delete m_l3Demux;
|
||||
delete m_ipv4L4Demux;
|
||||
Dispose ();
|
||||
}
|
||||
|
||||
void
|
||||
void
|
||||
InternetNode::SetName (std::string name)
|
||||
{
|
||||
m_name = name;
|
||||
@@ -77,8 +75,25 @@ InternetNode::CreateTraceResolver (TraceContext const &context)
|
||||
|
||||
void InternetNode::Dispose()
|
||||
{
|
||||
m_l3Demux->Dispose ();
|
||||
m_ipv4L4Demux->Dispose ();
|
||||
if (m_l3Demux != 0)
|
||||
{
|
||||
m_l3Demux->Dispose ();
|
||||
delete m_l3Demux;
|
||||
m_l3Demux = 0;
|
||||
}
|
||||
if (m_ipv4L4Demux != 0)
|
||||
{
|
||||
m_ipv4L4Demux->Dispose ();
|
||||
delete m_ipv4L4Demux;
|
||||
m_ipv4L4Demux = 0;
|
||||
}
|
||||
|
||||
if (m_applicationList != 0)
|
||||
{
|
||||
m_applicationList->Dispose ();
|
||||
delete m_applicationList;
|
||||
m_applicationList = 0;
|
||||
}
|
||||
|
||||
// chain up.
|
||||
Node::Dispose ();
|
||||
|
||||
@@ -88,10 +88,23 @@ OnOffApplication::OnOffApplication(Node * n, const OnOffApplication& c)
|
||||
}
|
||||
|
||||
OnOffApplication::~OnOffApplication()
|
||||
{
|
||||
Dispose ();
|
||||
}
|
||||
|
||||
void
|
||||
OnOffApplication::Dispose (void)
|
||||
{
|
||||
delete m_socket;
|
||||
delete m_onTime;
|
||||
delete m_offTime;
|
||||
|
||||
m_socket = 0;
|
||||
m_onTime = 0;
|
||||
m_offTime = 0;
|
||||
|
||||
// chain up
|
||||
Application::Dispose ();
|
||||
}
|
||||
|
||||
#ifdef REMOVE_THIS
|
||||
|
||||
@@ -54,6 +54,8 @@ public:
|
||||
virtual void StopApplication(); // Called at time specified by Stop
|
||||
virtual OnOffApplication* Copy() const;// Make a copy of the application
|
||||
|
||||
virtual void Dispose (void);
|
||||
|
||||
// Event handlers
|
||||
void StartSending();
|
||||
void StopSending();
|
||||
|
||||
Reference in new issue
Block a user