use ref/unref for Node in more cases
authorMathieu Lacage <mathieu.lacage@sophia.inria.fr>
Wed, 02 May 2007 15:07:33 +0200
changeset 493 68542941fc8a
parent 492 c3b8cfde5ba7
child 494 b783e010cb4a
use ref/unref for Node in more cases
examples/simple-p2p.cc
src/node/application-list.cc
src/node/application-list.h
src/node/application.cc
src/node/application.h
src/node/capability.cc
src/node/capability.h
src/node/onoff-application.cc
src/node/onoff-application.h
--- a/examples/simple-p2p.cc	Wed May 02 14:52:34 2007 +0200
+++ b/examples/simple-p2p.cc	Wed May 02 15:07:33 2007 +0200
@@ -118,7 +118,7 @@
   // Create the OnOff application to send UDP datagrams of size
   // 210 bytes at a rate of 448 Kb/s
   OnOffApplication* ooff0 = new OnOffApplication(
-    *n0, 
+    n0, 
     Ipv4Address("10.1.3.2"), 
     80, 
     ConstantVariable(1), 
@@ -133,7 +133,7 @@
 
   // Create a similar flow from n3 to n1, starting at time 1.1 seconds
   OnOffApplication* ooff1 = new OnOffApplication(
-    *n3, 
+    n3, 
     Ipv4Address("10.1.2.1"), 
     80, 
     ConstantVariable(1), 
--- a/src/node/application-list.cc	Wed May 02 14:52:34 2007 +0200
+++ b/src/node/application-list.cc	Wed May 02 15:07:33 2007 +0200
@@ -40,7 +40,7 @@
 { // Destructor, nothing needed as the SmartSet destroys itself
 }
 
-ApplicationList* ApplicationList::Copy(Node& n) const 
+ApplicationList* ApplicationList::Copy(Node * n) const 
 { // Copy this app list
   ApplicationList* r = new ApplicationList();
   r->SetNode(n);
@@ -53,7 +53,7 @@
   m_apps.Add(a);
 }
 
-void ApplicationList::SetNode(Node& n)
+void ApplicationList::SetNode(Node * n)
 {
   Capability::SetNode(n);
   // Set the node pointer in each application
--- a/src/node/application-list.h	Wed May 02 14:52:34 2007 +0200
+++ b/src/node/application-list.h	Wed May 02 15:07:33 2007 +0200
@@ -37,8 +37,8 @@
   // 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
+  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
   // Manage the list
   template <typename T> T* AddCopy(const T& t)  // Add a new application
--- a/src/node/application.cc	Wed May 02 14:52:34 2007 +0200
+++ b/src/node/application.cc	Wed May 02 15:07:33 2007 +0200
@@ -23,7 +23,6 @@
 
 #include "application.h"
 #include "node.h"
-#include "node-reference.h"
 #include "ns3/nstime.h"
 #include "ns3/random-variable.h"
 #include "ns3/simulator.h"
@@ -35,19 +34,20 @@
 // Application Methods
 
 // \brief Application Constructor
-  Application::Application(const Node& n) 
-    : m_node (0),
+Application::Application(Node * n) 
+    : m_node (n),
       m_startVar(0), m_stopVar(0),
       m_start(false), m_stop(false)
 {
-  SetNode(n);
+  m_node->Ref ();
 }
 
 Application::Application(const Application& o)
     : m_node(0), m_startVar(0), m_stopVar(0),
       m_start(false), m_stop(false)
 { // Copy constructor
-  if (o.GetNode())m_node = new NodeReference(*o.GetNode());
+  m_node = o.m_node;
+  m_node->Ref ();
   // Copy the start and stop random variables if they exist
   if (o.m_startVar) m_startVar = o.m_startVar->Copy();
   if (o.m_stopVar)  m_stopVar  = o.m_stopVar->Copy();
@@ -59,7 +59,7 @@
 // \brief Application Destructor
 Application::~Application()
 {
-  delete m_node;
+  m_node->Unref ();
   // Cancel the start/stop events if scheduled
   if (m_start) Simulator::Cancel(m_startEvent);
   if (m_stop)  Simulator::Cancel(m_stopEvent);
@@ -71,9 +71,10 @@
 Application& Application::operator=(const Application& rhs)
 {
   if (this == &rhs) return *this; // Self assignment
-  delete m_node;
+  m_node->Unref ();
   m_node = 0;
-  if (rhs.GetNode())m_node = new NodeReference(*rhs.GetNode());
+  m_node = rhs.m_node;
+  m_node->Ref ();
 
   delete m_startVar;
   m_startVar = 0;
@@ -130,15 +131,19 @@
 // \brief Assign this application to a given node
 // Called by the application manager capability when adding
 // an application to a node.
-void Application::SetNode(const Node& n)
+void Application::SetNode(Node * n)
 {
-  delete m_node;
-  m_node = new NodeReference(n);
+  if (m_node != 0)
+    {
+      m_node->Unref ();
+    }
+  m_node = n;
+  m_node->Ref ();
 }
   
-Node* Application::GetNode() const
+Node* Application::PeekNode() const
 {
-  return m_node->GetNode();
+  return m_node;
 }
 
 // Protected methods
--- a/src/node/application.h	Wed May 02 14:52:34 2007 +0200
+++ b/src/node/application.h	Wed May 02 15:07:33 2007 +0200
@@ -54,11 +54,10 @@
 
 class Node;
 class RandomVariable;
-class NodeReference;
   
 class Application {
 public:
-  Application(const Node&);
+  Application(Node *);
   Application(const Application&);  // Copy constructor
   Application& operator=(const Application&); // Assignment operator
   virtual ~Application();
@@ -99,13 +98,13 @@
   // \brief Attaches an application to a specific node
   // Specifies which node object this application is associated with.
   // \param Node object to associate with this application.
-  void SetNode(const Node&);
+  void SetNode(Node *);
 
   // \brief Returns the pointer to the attached node.
-  Node* GetNode() const;
+  Node* PeekNode() const;
   
   // Members
-  NodeReference*  m_node;      // All applications have an associated node
+  Node          * m_node;      // All applications have an associated node
   RandomVariable* m_startVar;  // Random variable for start time
   RandomVariable* m_stopVar;   // Random variable for stop time
   EventId         m_startEvent;// Event identifier for start event
--- a/src/node/capability.cc	Wed May 02 14:52:34 2007 +0200
+++ b/src/node/capability.cc	Wed May 02 15:07:33 2007 +0200
@@ -23,7 +23,6 @@
 
 #include "capability.h"
 #include "node.h"
-#include "node-reference.h"
 
 namespace ns3 {
   
@@ -33,47 +32,67 @@
 }
 
 Capability::Capability(Node* n)
+  : m_node (n)
 {
-  m_node = new NodeReference(*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)
 {
-  m_node = new NodeReference(*o.GetNode());
+  if (m_node != 0)
+    {
+      m_node->Ref ();
+    }
 }
 
-
 // Assignment operator
 Capability& Capability::operator=(const Capability& rhs)
 {
   if (this == &rhs) return *this;  // Self assignment
-  delete m_node;
-  m_node = new NodeReference(*rhs.GetNode());
+  if (m_node != 0)
+    {
+      m_node->Unref ();
+    }
+  m_node = rhs.m_node;
+  if (m_node != 0)
+    {
+      m_node->Ref ();
+    }
   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)
+void Capability::SetNode(Node *node)
 {
-  delete m_node;
-  m_node = new NodeReference(n);
+  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;
-  return m_node->GetNode();
+  m_node->Ref ();
+  return m_node;
 }
 
 } // namespace ns3
--- a/src/node/capability.h	Wed May 02 14:52:34 2007 +0200
+++ b/src/node/capability.h	Wed May 02 15:07:33 2007 +0200
@@ -42,11 +42,11 @@
   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 Capability* Copy(Node *node) const = 0;
+  virtual void SetNode(Node *node);
   virtual Node* GetNode() const;
 private:
-  NodeReference*   m_node;  // Node on which this capability is assigned
+  Node*   m_node;  // Node on which this capability is assigned
 };
 
 }  //namespace ns3
--- a/src/node/onoff-application.cc	Wed May 02 14:52:34 2007 +0200
+++ b/src/node/onoff-application.cc	Wed May 02 15:07:33 2007 +0200
@@ -43,7 +43,7 @@
 
 // Constructors
 
-  OnOffApplication::OnOffApplication(const Node& n, 
+  OnOffApplication::OnOffApplication(Node * n, 
                                      const Ipv4Address  rip,   // Remote IP addr
                                      uint16_t       rport, // Remote port
                                      const  RandomVariable& ontime,
@@ -68,7 +68,7 @@
 {
 }
 
-OnOffApplication::OnOffApplication(const Node& n, const OnOffApplication& c)
+OnOffApplication::OnOffApplication(Node * n, const OnOffApplication& c)
   : Application(n), 
     m_socket(0),
     m_peerIP(c.m_peerIP),
@@ -148,7 +148,7 @@
   if (!m_socket)
     { // Create the socket using the specified layer 4 protocol
 #ifdef NOTYET
-      m_socket = GetNode()->GetKernel()->CreateGenericSocket(*m_l4Proto);
+      m_socket = PeekNode()->GetKernel()->CreateGenericSocket(*m_l4Proto);
       m_socket->Bind();  // Choose any available port local port
       m_socket->Connect(*m_peerIP, m_peerPort,
                         MakeCallback(&OnOffApplication::ConnectionSucceeded,
@@ -156,7 +156,7 @@
                         MakeCallback(&OnOffApplication::ConnectionFailed,
                                      this));
 #endif
-      m_socket = GetNode ()->GetUdp ()->CreateSocket ();
+      m_socket = PeekNode ()->GetUdp ()->CreateSocket ();
       m_socket->Connect (m_peerIP, m_peerPort);
     }
   StopApplication();                         // Insure no pending event
--- a/src/node/onoff-application.h	Wed May 02 14:52:34 2007 +0200
+++ b/src/node/onoff-application.h	Wed May 02 15:07:33 2007 +0200
@@ -40,7 +40,7 @@
 class OnOffApplication : public Application {
 
 public:
-  OnOffApplication(const Node& n,
+  OnOffApplication(Node * n,
                    const Ipv4Address,  // Peer IP address
                    uint16_t,           // Peer port
                    const RandomVariable&,     // Random variable for On time
@@ -48,7 +48,7 @@
                    DataRate  = g_defaultRate,  // Data rate when on
                    uint32_t = g_defaultSize);  // Size of packets
 
-  OnOffApplication(const Node& n, const OnOffApplication&); // Copy constructor
+  OnOffApplication(Node * n, const OnOffApplication&); // Copy constructor
   virtual ~OnOffApplication();               // Destructor
   virtual void StartApplication();    // Called at time specified by Start
   virtual void StopApplication();     // Called at time specified by Stop