diff --git a/examples/simple-p2p.cc b/examples/simple-p2p.cc index 998256168..461782eef 100644 --- a/examples/simple-p2p.cc +++ b/examples/simple-p2p.cc @@ -107,15 +107,15 @@ int main (int argc, char *argv[]) Ptr n3 = new InternetNode (); // We create the channels first without any IP addressing information - PointToPointChannel *channel0 = + Ptr channel0 = PointToPointTopology::AddPointToPointLink ( n0, n2, DataRate(5000000), MilliSeconds(2)); - PointToPointChannel *channel1 = + Ptr channel1 = PointToPointTopology::AddPointToPointLink ( n1, n2, DataRate(5000000), MilliSeconds(2)); - PointToPointChannel *channel2 = + Ptr channel2 = PointToPointTopology::AddPointToPointLink ( n2, n3, DataRate(1500000), MilliSeconds(10)); @@ -123,17 +123,14 @@ int main (int argc, char *argv[]) PointToPointTopology::AddIpv4Addresses ( channel0, n0, Ipv4Address("10.1.1.1"), n2, Ipv4Address("10.1.1.2")); - channel0->Unref (); PointToPointTopology::AddIpv4Addresses ( channel1, n1, Ipv4Address("10.1.2.1"), n2, Ipv4Address("10.1.2.2")); - channel1->Unref (); PointToPointTopology::AddIpv4Addresses ( channel2, n2, Ipv4Address("10.1.3.1"), n3, Ipv4Address("10.1.3.2")); - channel2->Unref (); // Create the OnOff application to send UDP datagrams of size // 210 bytes at a rate of 448 Kb/s @@ -146,10 +143,8 @@ int main (int argc, char *argv[]) DataRate(448000), 210); // Add to Node's ApplicationList (takes ownership of pointer) - ApplicationList *apl0 = n0->QueryInterface - (ApplicationList::iid); + Ptr apl0 = n0->QueryInterface (ApplicationList::iid); apl0->Add(ooff0); - apl0->Unref (); // Start the application ooff0->Start(Seconds(1.0)); @@ -165,22 +160,19 @@ int main (int argc, char *argv[]) DataRate(448000), 210); // Add to Node's ApplicationList (takes ownership of pointer) - ApplicationList *apl3 = n3->QueryInterface (ApplicationList::iid); + Ptr apl3 = n3->QueryInterface (ApplicationList::iid); apl3->Add(ooff1); - apl3->Unref (); // Start the application ooff1->Start(Seconds(1.1)); ooff1->Stop (Seconds(10.0)); // Here, finish off packet routing configuration // This will likely set by some global StaticRouting object in the future - IIpv4 *ipv4; + Ptr ipv4; ipv4 = n0->QueryInterface (IIpv4::iid); ipv4->SetDefaultRoute (Ipv4Address ("10.1.1.2"), 1); - ipv4->Unref (); ipv4 = n3->QueryInterface (IIpv4::iid); ipv4->SetDefaultRoute (Ipv4Address ("10.1.3.1"), 1); - ipv4->Unref (); n0 = 0; n1 = 0; diff --git a/samples/main-simple.cc b/samples/main-simple.cc index 9505f0f16..db6f57736 100644 --- a/samples/main-simple.cc +++ b/samples/main-simple.cc @@ -9,7 +9,7 @@ using namespace ns3; static void -GenerateTraffic (Socket *socket, uint32_t size) +GenerateTraffic (Ptr socket, uint32_t size) { std::cout << "at=" << Simulator::Now ().GetSeconds () << "s, tx bytes=" << size << std::endl; socket->Send (0, size); @@ -24,13 +24,13 @@ GenerateTraffic (Socket *socket, uint32_t size) } static void -SocketPrinter (Socket *socket, uint32_t size, const Ipv4Address &from, uint16_t fromPort) +SocketPrinter (Ptr socket, uint32_t size, const Ipv4Address &from, uint16_t fromPort) { std::cout << "at=" << Simulator::Now ().GetSeconds () << "s, rx bytes=" << size << std::endl; } static void -PrintTraffic (Socket *socket) +PrintTraffic (Ptr socket) { socket->RecvDummy (MakeCallback (&SocketPrinter)); } @@ -40,17 +40,14 @@ RunSimulation (void) { Ptr a = new InternetNode (); - IUdp *udp; - udp = a->QueryInterface (IUdp::iid); + Ptr udp = a->QueryInterface (IUdp::iid); - Socket *sink = udp->CreateSocket (); + Ptr sink = udp->CreateSocket (); sink->Bind (80); - Socket *source = udp->CreateSocket (); + Ptr source = udp->CreateSocket (); source->Connect (Ipv4Address::GetLoopback (), 80); - udp->Unref (); - GenerateTraffic (source, 500); PrintTraffic (sink); @@ -58,11 +55,6 @@ RunSimulation (void) Simulator::Run (); Simulator::Destroy (); - - sink->Unref (); - source->Unref (); - - std::cout << "o" << std::endl; } int main (int argc, char *argv[]) diff --git a/src/applications/onoff-application.cc b/src/applications/onoff-application.cc index 6c6e6f847..347399791 100644 --- a/src/applications/onoff-application.cc +++ b/src/applications/onoff-application.cc @@ -92,11 +92,7 @@ OnOffApplication::~OnOffApplication() void OnOffApplication::DoDispose (void) { - if (m_socket != 0) - { - m_socket->Unref (); - m_socket = 0; - } + m_socket = 0; delete m_onTime; delete m_offTime; @@ -170,9 +166,8 @@ void OnOffApplication::StartApplication() // Called at time specified by Star this)); #endif - IUdp *udp = GetNode ()->QueryInterface (IUdp::iid); + Ptr udp = GetNode ()->QueryInterface (IUdp::iid); m_socket = udp->CreateSocket (); - udp->Unref (); m_socket->Connect (m_peerIP, m_peerPort); } StopApplication(); // Insure no pending event @@ -263,13 +258,13 @@ void OnOffApplication::SendPacket() ScheduleNextTx(); } -void OnOffApplication::ConnectionSucceeded(Socket*) +void OnOffApplication::ConnectionSucceeded(Ptr) { m_connected = true; ScheduleStartEvent(); } -void OnOffApplication::ConnectionFailed(Socket*) +void OnOffApplication::ConnectionFailed(Ptr) { cout << "OnOffApplication, Connection Failed" << endl; } diff --git a/src/applications/onoff-application.h b/src/applications/onoff-application.h index ff19e7458..0902a2885 100644 --- a/src/applications/onoff-application.h +++ b/src/applications/onoff-application.h @@ -71,7 +71,7 @@ public: // Static methods static void DefaultSize(uint32_t s) { g_defaultSize = s;} public: - Socket * m_socket; // Associated socket + Ptr m_socket; // Associated socket Ipv4Address m_peerIP; // Peer IP address uint16_t m_peerPort; // Peer port bool m_connected; // True if connected @@ -97,9 +97,9 @@ private: void ScheduleNextTx(); void ScheduleStartEvent(); void ScheduleStopEvent(); - void ConnectionSucceeded(Socket*); - void ConnectionFailed(Socket*); - void Ignore(Socket*); + void ConnectionSucceeded(Ptr); + void ConnectionFailed(Ptr); + void Ignore(Ptr); protected: }; diff --git a/src/core/ns-unknown-manager.cc b/src/core/ns-unknown-manager.cc index f20438d4c..6f4ab3bfb 100644 --- a/src/core/ns-unknown-manager.cc +++ b/src/core/ns-unknown-manager.cc @@ -49,10 +49,10 @@ bool operator == (const ClassId &a, const ClassId &b) return a.m_classId == b.m_classId; } -NsUnknown * +Ptr NsUnknownManager::Create (ClassId classId) { - Callback callback = DoGetCallback (classId); + Callback > callback = DoGetCallback (classId); return callback (); } @@ -141,9 +141,8 @@ A::A () m_oneBoolInvoked (false), m_oneUi32Invoked (false) { - B *b = new B (); + ns3::Ptr b = new B (); AddInterface (b); - b->Unref (); } A::A (bool bo) @@ -153,9 +152,8 @@ A::A (bool bo) m_oneUi32Invoked (false), m_bool (bo) { - B *b = new B (); + ns3::Ptr b = new B (); AddInterface (b); - b->Unref (); } A::A (uint32_t i) @@ -165,9 +163,8 @@ A::A (uint32_t i) m_oneUi32Invoked (true), m_ui32 (i) { - B *b = new B (); + ns3::Ptr b = new B (); AddInterface (b); - b->Unref (); } } @@ -189,14 +186,13 @@ NsUnknownManagerTest::RunTests (void) { bool ok = true; - A *a = 0; + Ptr a = 0; a = NsUnknownManager::Create (A::cidZero, A::iid); if (a == 0 || !a->m_zeroInvoked) { ok = false; } - a->Unref (); a = NsUnknownManager::Create (A::cidOneBool, A::iid, true); if (a == 0 || @@ -205,7 +201,6 @@ NsUnknownManagerTest::RunTests (void) { ok = false; } - a->Unref (); a = NsUnknownManager::Create (A::cidOneBool, A::iid, false); if (a == 0 || @@ -214,7 +209,6 @@ NsUnknownManagerTest::RunTests (void) { ok = false; } - a->Unref (); a = NsUnknownManager::Create (A::cidOneUi32, A::iid, 10); if (a == 0 || @@ -223,7 +217,6 @@ NsUnknownManagerTest::RunTests (void) { ok = false; } - a->Unref (); a = NsUnknownManager::Create (A::cidOneUi32, A::iid, (uint32_t)10); if (a == 0 || @@ -232,14 +225,12 @@ NsUnknownManagerTest::RunTests (void) { ok = false; } - a->Unref (); - B *b = NsUnknownManager::Create (A::cidOneUi32, B::iid, 10); + Ptr b = NsUnknownManager::Create (A::cidOneUi32, B::iid, 10); if (b == 0) { ok = false; } - b->Unref (); return ok; } diff --git a/src/core/ns-unknown-manager.h b/src/core/ns-unknown-manager.h index 34440e3ed..c2941b830 100644 --- a/src/core/ns-unknown-manager.h +++ b/src/core/ns-unknown-manager.h @@ -27,6 +27,7 @@ #include "callback.h" #include "ns-unknown.h" #include "fatal-error.h" +#include "ptr.h" namespace ns3 { @@ -82,51 +83,51 @@ public: * Create an instance of the object identified by its * ClassId. This method invokes the default constructor. */ - static NsUnknown *Create (ClassId classId); + static Ptr Create (ClassId classId); /** * \param classId class id of the constructor to invoke. * \param a1 argument to pass to the constructor. * \return a pointer to the instance created. - * \overload NsUnknown *Create (ClassId) + * \overload Create (ClassId) * * Create an instance of the object identified by its * ClassId. */ template - static NsUnknown *Create (ClassId classId, T1 a1); + static Ptr Create (ClassId classId, T1 a1); /** * \param classId class id of the constructor to invoke. * \param a1 first argument to pass to the constructor. * \param a2 second argument to pass to the constructor. * \return a pointer to the instance created. - * \overload NsUnknown *Create (ClassId) + * \overload Create (ClassId) * * Create an instance of the object identified by its * ClassId. */ template - static NsUnknown *Create (ClassId classId, T1 a1, T2 a2); + static Ptr Create (ClassId classId, T1 a1, T2 a2); /** * \param classId class id of the constructor to invoke. * \param iid interface id to query for * \return a pointer to the instance created. - * \overload NsUnknown *Create (ClassId) + * \overload Create (ClassId) * * Create an instance of the object identified by its * ClassId, call QueryInterface on it, and return the * result. */ template - static T *Create (ClassId classId, Iid iid); + static Ptr Create (ClassId classId, Iid iid); template - static T *Create (ClassId classId, Iid iid, T1 a1); + static Ptr Create (ClassId classId, Iid iid, T1 a1); template - static T *Create (ClassId classId, Iid iid, T1 a1, T2 a2); + static Ptr Create (ClassId classId, Iid iid, T1 a1, T2 a2); /** * \param name the symbolic name to associate to this @@ -136,7 +137,7 @@ public: template static ClassId RegisterConstructor (std::string name) { - static Callback callback = + static Callback > callback = MakeCallback (&NsUnknownManager::MakeObjectZero); return NsUnknownManager::Register (name, &callback); } @@ -150,7 +151,7 @@ public: template static ClassId RegisterConstructor (std::string name) { - static Callback callback = MakeCallback (&NsUnknownManager::MakeObjectOne); + static Callback ,T1> callback = MakeCallback (&NsUnknownManager::MakeObjectOne); return NsUnknownManager::Register (name, &callback); } @@ -163,7 +164,7 @@ public: template static ClassId RegisterConstructor (std::string name) { - static Callback callback = MakeCallback (&NsUnknownManager::MakeObjectTwo); + static Callback,T1,T2> callback = MakeCallback (&NsUnknownManager::MakeObjectTwo); return NsUnknownManager::Register (name, &callback); } private: @@ -172,16 +173,16 @@ private: template - static Callback DoGetCallback (ClassId classId); + static Callback,T1,T2,T3,T4,T5> DoGetCallback (ClassId classId); template - static NsUnknown *MakeObjectZero (void); + static Ptr MakeObjectZero (void); template - static NsUnknown *MakeObjectOne (T1 a1); + static Ptr MakeObjectOne (T1 a1); template - static NsUnknown *MakeObjectTwo (T1 a1, T2 a2); + static Ptr MakeObjectTwo (T1 a1, T2 a2); typedef std::vector > List; static List *GetList (void); @@ -196,7 +197,7 @@ namespace ns3 { template -Callback +Callback,T1,T2,T3,T4,T5> NsUnknownManager::DoGetCallback (ClassId classId) { CallbackBase *callback = Lookup (classId); @@ -204,73 +205,70 @@ NsUnknownManager::DoGetCallback (ClassId classId) { NS_FATAL_ERROR ("Invalid Class Id."); } - Callback reference; + Callback, T1,T2,T3,T4,T5> reference; reference.Assign (*callback); return reference; } template -NsUnknown * +Ptr NsUnknownManager::Create (ClassId classId, T1 a1) { - Callback callback = DoGetCallback (classId); + Callback, T1> callback = DoGetCallback (classId); return callback (a1); } template -NsUnknown * +Ptr NsUnknownManager::Create (ClassId classId, T1 a1, T2 a2) { - Callback callback = DoGetCallback (classId); + Callback , T1,T2> callback = DoGetCallback (classId); return callback (a1, a2); } template -T * +Ptr NsUnknownManager::Create (ClassId classId, Iid iid) { - NsUnknown *obj = Create (classId); - T *i = obj->QueryInterface (iid); - obj->Unref (); + Ptr obj = Create (classId); + Ptr i = obj->QueryInterface (iid); return i; } template -T * +Ptr NsUnknownManager::Create (ClassId classId, Iid iid, T1 a1) { - NsUnknown *obj = Create (classId, a1); - T *i = obj->QueryInterface (iid); - obj->Unref (); + Ptr obj = Create (classId, a1); + Ptr i = obj->QueryInterface (iid); return i; } template -T * +Ptr NsUnknownManager::Create (ClassId classId, Iid iid, T1 a1, T2 a2) { - NsUnknown *obj = Create (classId, a1, a2); - T *i = obj->QueryInterface (iid); - obj->Unref (); + Ptr obj = Create (classId, a1, a2); + Ptr i = obj->QueryInterface (iid); return i; } template -NsUnknown * +Ptr NsUnknownManager::MakeObjectZero (void) { return new T (); } template -NsUnknown * +Ptr NsUnknownManager::MakeObjectOne (T1 a1) { return new T (a1); } template -NsUnknown * +Ptr NsUnknownManager::MakeObjectTwo (T1 a1, T2 a2) { return new T (a1, a2); diff --git a/src/core/ns-unknown.cc b/src/core/ns-unknown.cc index b3edea6ad..05d384929 100644 --- a/src/core/ns-unknown.cc +++ b/src/core/ns-unknown.cc @@ -53,9 +53,9 @@ public: void RefAll (NsUnknownImpl *other); void Unref (void); void UnrefAll (void); - NsUnknown *DoQueryInterface (Iid iid) const; + NsUnknown *PeekQueryInterface (Iid iid) const; void DoDisposeAll (void); - void AddInterface (NsUnknown *interface); + void AddInterface (NsUnknown * interface); void AddSelfInterface (Iid iid, NsUnknown *interface); private: typedef std::list > List; @@ -64,7 +64,7 @@ private: bool m_disposed; }; -NsUnknownImpl::NsUnknownImpl (Iid iid, NsUnknown *interface) +NsUnknownImpl::NsUnknownImpl (Iid iid, NsUnknown * interface) : m_ref (0), m_disposed (false) { @@ -124,14 +124,13 @@ NsUnknownImpl::DoDisposeAll (void) m_disposed = true; } NsUnknown * -NsUnknownImpl::DoQueryInterface (Iid iid) const +NsUnknownImpl::PeekQueryInterface (Iid iid) const { for (List::const_iterator i = m_list.begin (); i != m_list.end (); i++) { if (i->first == iid) { - i->second->Ref (); return i->second; } } @@ -206,25 +205,26 @@ NsUnknown::UnrefInternal (void) } } -NsUnknown * +Ptr NsUnknown::DoQueryInterface (Iid iid) const { - return m_impl->DoQueryInterface (iid); + return m_impl->PeekQueryInterface (iid); } void -NsUnknown::AddInterface (NsUnknown *interface) +NsUnknown::AddInterface (Ptr interface) { - m_impl->AddInterface (interface); - m_impl->RefAll (interface->m_impl); - interface->m_impl->UnrefAll (); - interface->m_impl = m_impl; + NsUnknown *p = interface.Peek (); + m_impl->AddInterface (p); + m_impl->RefAll (p->m_impl); + p->m_impl->UnrefAll (); + p->m_impl = m_impl; } void -NsUnknown::AddSelfInterface (Iid iid, NsUnknown *interface) +NsUnknown::AddSelfInterface (Iid iid, Ptr interface) { - m_impl->AddSelfInterface (iid, interface); + m_impl->AddSelfInterface (iid, interface.Peek ()); } @@ -316,31 +316,26 @@ InterfaceTest::RunTests (void) //DerivedAB *derivedAB; - A *a = new A (); - a->Unref (); + Ptr a = new A (); a = new A (); - A *a1 = a->QueryInterface (A::iid); + Ptr a1 = a->QueryInterface (A::iid); if (a1 == 0 || a1 != a) { ok = false; } - a1->Unref (); a1 = a->QueryInterface (A::iid); if (a1 == 0 || a1 != a) { ok = false; } - a1->Unref (); - a->Unref (); - B *b = new B (); - B *b1 = b->QueryInterface (B::iid); + Ptr b = new B (); + Ptr b1 = b->QueryInterface (B::iid); if (b1 == 0 || b1 != b) { ok = false; } - b1->Unref (); a = new A (); a->AddInterface (b); @@ -349,43 +344,33 @@ InterfaceTest::RunTests (void) { ok = false; } - b1->Unref (); a1 = b->QueryInterface (A::iid); if (a1 == 0 || a1 != a) { ok = false; } - a1->Unref (); a1 = a->QueryInterface (A::iid); if (a1 == 0 || a1 != a) { ok = false; } - a1->Unref (); b1 = a->QueryInterface (B::iid); if (b1 == 0 || b1 != b) { ok = false; } - b1->Unref (); - - a->Unref (); - b->Unref (); - Derived *derived = new Derived (); - Base *base = derived->QueryInterface (Base::iid); + Ptr derived = new Derived (); + Ptr base = derived->QueryInterface (Base::iid); if (base == 0) { ok = false; } - Derived *derived1 = base->QueryInterface (Derived::iid); + Ptr derived1 = base->QueryInterface (Derived::iid); if (derived1 == 0 || derived1 != derived) { ok = false; } - derived1->Unref (); - base->Unref (); - derived->Unref (); return ok; } diff --git a/src/core/ns-unknown.h b/src/core/ns-unknown.h index 046407a34..5637d0101 100644 --- a/src/core/ns-unknown.h +++ b/src/core/ns-unknown.h @@ -22,7 +22,7 @@ #define INTERFACE_H #include - +#include "ptr.h" namespace ns3 { @@ -56,7 +56,7 @@ public: * \param iid the NsUnknown id of the requested interface */ template - T *QueryInterface (Iid iid) const; + Ptr QueryInterface (Iid iid) const; /** * \param interface another interface @@ -66,7 +66,7 @@ public: * will be able to perform QI on each other and their lifetimes * will be found by the same reference count. */ - void AddInterface (NsUnknown *interface); + void AddInterface (Ptr interface); void Dispose (void); protected: @@ -87,7 +87,7 @@ protected: * (typically, your subclass has added API), you need to call * this method to associate an interface id to your interface. */ - void AddSelfInterface (Iid iid, NsUnknown *interface); + void AddSelfInterface (Iid iid, Ptr interface); protected: /** * Subclasses who want to handle the "dispose" event should @@ -99,7 +99,7 @@ protected: private: friend class NsUnknownImpl; NsUnknown (); - NsUnknown *DoQueryInterface (Iid iid) const; + Ptr DoQueryInterface (Iid iid) const; void RefInternal (void); void UnrefInternal (void); NsUnknownImpl *m_impl; @@ -111,13 +111,13 @@ private: namespace ns3 { template -T * +Ptr NsUnknown::QueryInterface (Iid iid) const { - NsUnknown *found = DoQueryInterface (iid); + Ptr found = DoQueryInterface (iid); if (found != 0) { - return dynamic_cast (found); + return Ptr (dynamic_cast (found.Peek ())); } return 0; } diff --git a/src/devices/p2p/p2p-channel.cc b/src/devices/p2p/p2p-channel.cc index c0a8342b7..2833c8bb1 100644 --- a/src/devices/p2p/p2p-channel.cc +++ b/src/devices/p2p/p2p-channel.cc @@ -70,11 +70,11 @@ PointToPointChannel::PointToPointChannel( } void -PointToPointChannel::Attach(PointToPointNetDevice *device) +PointToPointChannel::Attach(Ptr device) { NS_DEBUG("PointToPointChannel::Attach (" << device << ")"); NS_ASSERT(m_nDevices < N_DEVICES && "Only two devices permitted"); - NS_ASSERT(device); + NS_ASSERT(device != 0); m_link[m_nDevices].m_src = device; ++m_nDevices; @@ -92,7 +92,7 @@ PointToPointChannel::Attach(PointToPointNetDevice *device) } bool -PointToPointChannel::TransmitStart(Packet& p, PointToPointNetDevice* src) +PointToPointChannel::TransmitStart(Packet& p, Ptr src) { NS_DEBUG ("PointToPointChannel::TransmitStart (" << &p << ", " << src << ")"); @@ -117,7 +117,7 @@ PointToPointChannel::TransmitStart(Packet& p, PointToPointNetDevice* src) } bool -PointToPointChannel::TransmitEnd(Packet& p, PointToPointNetDevice* src) +PointToPointChannel::TransmitEnd(Packet& p, Ptr src) { NS_DEBUG("PointToPointChannel::TransmitEnd (" << &p << ", " << src << ")"); NS_DEBUG ("PointToPointChannel::TransmitEnd (): UID is " << @@ -147,7 +147,7 @@ PointToPointChannel::TransmitEnd(Packet& p, PointToPointNetDevice* src) void PointToPointChannel::PropagationCompleteEvent( Packet p, - PointToPointNetDevice *src) + Ptr src) { NS_DEBUG("PointToPointChannel::PropagationCompleteEvent (" << &p << ", " << src << ")"); @@ -169,7 +169,7 @@ PointToPointChannel::GetNDevices (void) const return m_nDevices; } -NetDevice * +Ptr PointToPointChannel::GetDevice (uint32_t i) const { NS_ASSERT(i < 2); diff --git a/src/devices/p2p/p2p-channel.h b/src/devices/p2p/p2p-channel.h index 13256a2d7..dacdc9c0b 100644 --- a/src/devices/p2p/p2p-channel.h +++ b/src/devices/p2p/p2p-channel.h @@ -21,6 +21,7 @@ #include #include "ns3/channel.h" +#include "ns3/ptr.h" #include "ns3/packet.h" #include "ns3/nstime.h" #include "ns3/data-rate.h" @@ -62,13 +63,13 @@ public: PointToPointChannel (const std::string& name, const DataRate& bps, const Time& delay); - void Attach (PointToPointNetDevice* device); - bool TransmitStart (Packet& p, PointToPointNetDevice *src); - bool TransmitEnd (Packet &p, PointToPointNetDevice *src); - void PropagationCompleteEvent(Packet p, PointToPointNetDevice *src); + void Attach (Ptr device); + bool TransmitStart (Packet& p, Ptr src); + bool TransmitEnd (Packet &p, Ptr src); + void PropagationCompleteEvent(Packet p, Ptr src); virtual uint32_t GetNDevices (void) const; - virtual NetDevice *GetDevice (uint32_t i) const; + virtual Ptr GetDevice (uint32_t i) const; virtual DataRate GetDataRate (void); virtual Time GetDelay (void); @@ -92,8 +93,8 @@ private: public: Link() : m_state (INITIALIZING), m_src (0), m_dst (0) {} WireState m_state; - PointToPointNetDevice *m_src; - PointToPointNetDevice *m_dst; + Ptr m_src; + Ptr m_dst; }; Link m_link[N_DEVICES]; diff --git a/src/devices/p2p/p2p-net-device.cc b/src/devices/p2p/p2p-net-device.cc index 06d8fb269..6fb6edac9 100644 --- a/src/devices/p2p/p2p-net-device.cc +++ b/src/devices/p2p/p2p-net-device.cc @@ -81,18 +81,12 @@ PointToPointNetDevice::PointToPointNetDevice (const PointToPointNetDevice& nd) m_txMachineState(READY), m_bps (nd.m_bps), m_tInterframeGap (nd.m_tInterframeGap), - m_channel(0), + m_channel(nd.m_channel), m_queue(0), m_rxTrace () { NS_DEBUG ("PointToPointNetDevice::PointToPointNetDevice (" << &nd << ")"); - if (nd.m_channel) - { - m_channel = nd.m_channel; - m_channel->Ref (); - } - if (nd.m_queue) { m_queue = nd.m_queue; @@ -102,12 +96,8 @@ PointToPointNetDevice::PointToPointNetDevice (const PointToPointNetDevice& nd) void PointToPointNetDevice::DoDispose() { - if (m_channel != 0) - { - m_channel->Unref (); - m_channel = 0; - } - NetDevice::DoDispose (); + m_channel = 0; + NetDevice::DoDispose (); } // @@ -305,18 +295,11 @@ PointToPointNetDevice::DoCreateTraceResolver (TraceContext const &context) } bool -PointToPointNetDevice::Attach (PointToPointChannel* ch) +PointToPointNetDevice::Attach (Ptr ch) { NS_DEBUG ("PointToPointNetDevice::Attach (" << &ch << ")"); - if (m_channel) - { - m_channel->Unref (); - m_channel = 0; - } - m_channel = ch; - m_channel->Ref (); m_channel->Attach(this); m_bps = m_channel->GetDataRate (); @@ -358,10 +341,9 @@ PointToPointNetDevice::GetQueue(void) const return m_queue; } -Channel* +Ptr PointToPointNetDevice::DoGetChannel(void) const { - m_channel->Ref(); return m_channel; } diff --git a/src/devices/p2p/p2p-net-device.h b/src/devices/p2p/p2p-net-device.h index 2d27921c4..fc4ece354 100644 --- a/src/devices/p2p/p2p-net-device.h +++ b/src/devices/p2p/p2p-net-device.h @@ -140,7 +140,7 @@ public: * @see SetInterframeGap () * @param ch a pointer to the channel to which this object is being attached. */ - bool Attach(PointToPointChannel* ch); + bool Attach(Ptr ch); /** * Attach a queue to the PointToPointNetDevice. * @@ -189,7 +189,7 @@ protected: * @see PointToPointChannel * @returns a pointer to the channel */ - virtual Channel *DoGetChannel(void) const; + virtual Ptr DoGetChannel(void) const; private: /** * Send a Packet Down the Wire. @@ -288,7 +288,7 @@ private: * attached. * @see class PointToPointChannel */ - PointToPointChannel* m_channel; + Ptr m_channel; /** * The Queue which this PointToPointNetDevice uses as a packet source. * Management of this Queue has been delegated to the PointToPointNetDevice diff --git a/src/devices/p2p/p2p-topology.cc b/src/devices/p2p/p2p-topology.cc index 5f024a668..d06093fd4 100644 --- a/src/devices/p2p/p2p-topology.cc +++ b/src/devices/p2p/p2p-topology.cc @@ -38,38 +38,33 @@ namespace ns3 { -PointToPointChannel * +Ptr PointToPointTopology::AddPointToPointLink( Ptr n1, Ptr n2, const DataRate& bps, const Time& delay) { - PointToPointChannel* channel = new PointToPointChannel(bps, delay); - channel->Ref (); + Ptr channel = new PointToPointChannel(bps, delay); - PointToPointNetDevice* net1 = new PointToPointNetDevice(n1); - net1->Ref (); + Ptr net1 = new PointToPointNetDevice(n1); net1->AddQueue(Queue::Default().Copy()); n1->AddDevice (net1); net1->Attach (channel); - net1->Unref (); - PointToPointNetDevice* net2 = new PointToPointNetDevice(n2); - net2->Ref (); + Ptr net2 = new PointToPointNetDevice(n2); net2->AddQueue(Queue::Default().Copy()); n2->AddDevice (net2); net2->Attach (channel); - net2->Unref (); return channel; } bool PointToPointTopology::AddIpv4Addresses( - const PointToPointChannel *chan, + Ptr chan, Ptr n1, const Ipv4Address& addr1, Ptr n2, const Ipv4Address& addr2) { @@ -81,8 +76,8 @@ PointToPointTopology::AddIpv4Addresses( // The PointToPoint channel is used to find the relevant NetDevices NS_ASSERT (chan->GetNDevices () == 2); - NetDevice* nd1 = chan->GetDevice (0); - NetDevice* nd2 = chan->GetDevice (1); + Ptr nd1 = chan->GetDevice (0); + Ptr nd2 = chan->GetDevice (1); // Make sure that nd1 belongs to n1 and nd2 to n2 if ( (nd1->GetNode ()->GetId () == n2->GetId () ) && (nd2->GetNode ()->GetId () == n1->GetId () ) ) @@ -92,14 +87,14 @@ PointToPointTopology::AddIpv4Addresses( NS_ASSERT (nd1->GetNode ()->GetId () == n1->GetId ()); NS_ASSERT (nd2->GetNode ()->GetId () == n2->GetId ()); - IIpv4 *ip1 = n1->QueryInterface (IIpv4::iid); + Ptr ip1 = n1->QueryInterface (IIpv4::iid); uint32_t index1 = ip1->AddInterface (nd1); ip1->SetAddress (index1, addr1); ip1->SetNetworkMask (index1, netmask); ip1->SetUp (index1); - IIpv4 *ip2 = n2->QueryInterface (IIpv4::iid); + Ptr ip2 = n2->QueryInterface (IIpv4::iid); uint32_t index2 = ip2->AddInterface (nd2); ip2->SetAddress (index2, addr2); @@ -108,10 +103,7 @@ PointToPointTopology::AddIpv4Addresses( ip1->AddHostRouteTo (addr2, index1); ip2->AddHostRouteTo (addr1, index2); - - ip1->Unref (); - ip2->Unref (); - + return true; } @@ -121,18 +113,13 @@ PointToPointTopology::AddIpv4Addresses( // there are possibly multiple devices connecting n1 and n2 (for example // wireless with two devices on different channels) this will return // the first one found. -PointToPointNetDevice* PointToPointTopology::GetNetDevice(Ptr n1, Ptr n2) +Ptr PointToPointTopology::GetNetDevice(Ptr n1, Ptr n2) { - // First get the NetDeviceList capability from node 1 - NetDeviceList* ndl1 = n1->GetNetDeviceList(); - if (!ndl1) return 0; // No devices, return nil - // Get the list of devices - const NetDeviceList::NetDevices_t& dlist = ndl1->GetAll(); for (NetDeviceList::NetDevices_t::const_iterator i = dlist.Begin(); i != dlist.End(); ++i) { // Check each device - NetDevice* nd = *i; // next device - Channel* c = nd->GetChannel(); + Ptr nd = *i; // next device + Ptr c = nd->GetChannel(); if (!c) continue; // No channel if (c->NodeIsPeer(n2)) return nd; // found it } @@ -140,26 +127,26 @@ PointToPointNetDevice* PointToPointTopology::GetNetDevice(Ptr n1, Ptr PointToPointTopology::GetChannel( Ptr n1, Ptr n2 ) { - NetDevice* nd = GetNetDevice(n1, n2); + Ptr nd = GetNetDevice(n1, n2); if (!nd) return 0; // No net device, so no channel return nd->GetChannel(); } Queue* PointToPointTopology::GetQueue(Ptr n1, Ptr n2) { - NetDevice* nd = GetNetDevice(n1, n2); + Ptr nd = GetNetDevice(n1, n2); if (!nd) return 0; // No net device, so in queue return nd->GetQueue(); } Queue* PointToPointTopology::SetQueue(Ptr n1, Ptr n2, const Queue& q) { - NetDevice* nd = GetNetDevice(n1, n2); + Ptr nd = GetNetDevice(n1, n2); if (!nd) return 0; // No net device, can't set queue // Add the specified queue to the netdevice return nd->SetQueue(q); @@ -168,22 +155,18 @@ Queue* PointToPointTopology::SetQueue(Ptr n1, Ptr n2, const Queue& q #endif #ifdef GFR -P2PChannel* Topology::AddDuplexLink(Ptr n1, const IPAddr& ip1, +P2PChannel Topology::AddDuplexLink(Ptr n1, const IPAddr& ip1, Ptr n2, const IPAddr& ip2, const Rate& rate, const Time& delay) { - // First get the NetDeviceList capability from each node - NetDeviceList* ndl1 = n1->GetNetDeviceList(); - NetDeviceList* ndl2 = n2->GetNetDeviceList(); - if (!ndl1 || !ndl2) return nil; // Both ends must have NetDeviceList // Get the net devices P2PNetDevice* nd1 = ndl1->Add(P2PNetDevice(n1, rate, nil)); P2PNetDevice* nd2 = ndl2->Add(P2PNetDevice(n1, rate, nd1->GetChannel())); // Not implemented yet. Add appropriate layer 2 protocol for // the net devices. // Get the L3 proto for node 1 and configure it with this device - L3Demux* l3demux1 = n1->GetL3Demux(); - L3Protocol* l3proto1 = nil; + Ptr l3demux1 = n1->GetL3Demux(); + Ptr l3proto1 = nil; // If the node 1 l3 demux exists, find the coresponding l3 protocol if (l3demux1) l3proto1 = l3demux1->Lookup(ip1.L3Proto()); // If the l3 protocol exists, configure this net device. Use a mask @@ -191,8 +174,8 @@ P2PChannel* Topology::AddDuplexLink(Ptr n1, const IPAddr& ip1, // of this link if (l3proto1) l3proto1->AddNetDevice(nd1, ip1, ip1.GetMask(ip1.Size()*8)); // Same for node 2 - L3Demux* l3demux2 = n2->GetL3Demux(); - L3Protocol* l3proto2 = nil; + Ptr l3demux2 = n2->GetL3Demux(); + Ptr l3proto2 = nil; // If the node 2 l3 demux exists, find the coresponding l3 protocol if (l3demux2) l3proto2 = l3demux2->Lookup(ip2.L3Proto()); if (l3proto2) l3proto2->AddNetDevice(nd2, ip2, ip2.GetMask(ip2.Size()*8)); @@ -200,23 +183,23 @@ P2PChannel* Topology::AddDuplexLink(Ptr n1, const IPAddr& ip1, } // Get the channel connecting node n1 to node n2 -Channel* Topology::GetChannel(Ptr n1, Ptr n2) +Ptr Topology::GetChannel(Ptr n1, Ptr n2) { - NetDevice* nd = GetNetDevice(n1, n2); + Ptr nd = GetNetDevice(n1, n2); if (!nd) return 0; // No net device, so no channel return nd->GetChannel(); } Queue* Topology::GetQueue(Ptr n1, Ptr n2) { - NetDevice* nd = GetNetDevice(n1, n2); + Ptr nd = GetNetDevice(n1, n2); if (!nd) return 0; // No net device, so in queue return nd->GetQueue(); } Queue* Topology::SetQueue(Ptr n1, Ptr n2, const Queue& q) { - NetDevice* nd = GetNetDevice(n1, n2); + Ptr nd = GetNetDevice(n1, n2); if (!nd) return 0; // No net device, can't set queue // Add the specified queue to the netdevice return nd->SetQueue(q); diff --git a/src/devices/p2p/p2p-topology.h b/src/devices/p2p/p2p-topology.h index ecd927ff3..282c3a0a3 100644 --- a/src/devices/p2p/p2p-topology.h +++ b/src/devices/p2p/p2p-topology.h @@ -51,22 +51,22 @@ public: * with the specified IP addresses, with specified maximum transmission rate * and propagation delay. */ - static PointToPointChannel* AddPointToPointLink( + static Ptr AddPointToPointLink( Ptr, Ptr, const DataRate&, const Time&); static bool AddIpv4Addresses( - const PointToPointChannel*, + Ptr, Ptr, const Ipv4Address&, Ptr, const Ipv4Address&); /** * Get the connecting node n1 to node n2 */ - static PointToPointChannel* GetChannel(Ptr, Ptr); + static Ptr GetChannel(Ptr, Ptr); /** * Get the NetDevice connecting node n1 to n2 */ - static PointToPointNetDevice* GetNetDevice(Ptr, Ptr); + static Ptr GetNetDevice(Ptr, Ptr); /** * Get the queue associated with a link between two nodes */ diff --git a/src/internet-node/arp-cache.cc b/src/internet-node/arp-cache.cc index 6a6f71bc9..e9f48561d 100644 --- a/src/internet-node/arp-cache.cc +++ b/src/internet-node/arp-cache.cc @@ -28,24 +28,21 @@ namespace ns3 { -ArpCache::ArpCache (NetDevice *device, Ipv4Interface *interface) +ArpCache::ArpCache (Ptr device, Ipv4Interface *interface) : m_device (device), m_interface (interface), m_aliveTimeout (Seconds (120)), m_deadTimeout (Seconds (100)), m_waitReplyTimeout (Seconds (1)) -{ - m_device->Ref (); -} +{} ArpCache::~ArpCache () { - m_device->Unref (); Flush (); } -NetDevice * -ArpCache::PeekDevice (void) const +Ptr +ArpCache::GetDevice (void) const { return m_device; } diff --git a/src/internet-node/arp-cache.h b/src/internet-node/arp-cache.h index 5923d2ca9..6a1d182be 100644 --- a/src/internet-node/arp-cache.h +++ b/src/internet-node/arp-cache.h @@ -27,6 +27,7 @@ #include "ns3/net-device.h" #include "ns3/ipv4-address.h" #include "ns3/mac-address.h" +#include "ns3/ptr.h" #include "sgi-hashmap.h" namespace ns3 { @@ -38,10 +39,10 @@ class ArpCache { public: class Entry; - ArpCache (NetDevice *device, Ipv4Interface *interface); + ArpCache (Ptr device, Ipv4Interface *interface); ~ArpCache (); - NetDevice *PeekDevice (void) const; + Ptr GetDevice (void) const; Ipv4Interface *GetInterface (void) const; void SetAliveTimeout (Time aliveTimeout); @@ -91,7 +92,7 @@ private: typedef sgi::hash_map Cache; typedef sgi::hash_map::iterator CacheI; - NetDevice *m_device; + Ptr m_device; Ipv4Interface *m_interface; Time m_aliveTimeout; Time m_deadTimeout; diff --git a/src/internet-node/arp-ipv4-interface.cc b/src/internet-node/arp-ipv4-interface.cc index bf80bfe71..509bd241f 100644 --- a/src/internet-node/arp-ipv4-interface.cc +++ b/src/internet-node/arp-ipv4-interface.cc @@ -31,7 +31,7 @@ namespace ns3 { -ArpIpv4Interface::ArpIpv4Interface (Ptr node, NetDevice *device) +ArpIpv4Interface::ArpIpv4Interface (Ptr node, Ptr device) : Ipv4Interface (device), m_node (node) {} @@ -42,10 +42,10 @@ TraceResolver * ArpIpv4Interface::DoCreateTraceResolver (TraceContext const &context) { CompositeTraceResolver *resolver = new CompositeTraceResolver (context); - if (PeekDevice () != 0) + if (GetDevice () != 0) { resolver->Add ("netdevice", - MakeCallback (&NetDevice::CreateTraceResolver, PeekDevice ()), + MakeCallback (&NetDevice::CreateTraceResolver, GetDevice ().Peek ()), ArpIpv4Interface::NETDEVICE); } @@ -55,21 +55,20 @@ ArpIpv4Interface::DoCreateTraceResolver (TraceContext const &context) void ArpIpv4Interface::SendTo (Packet p, Ipv4Address dest) { - NS_ASSERT (PeekDevice () != 0); - if (PeekDevice ()->NeedsArp ()) + NS_ASSERT (GetDevice () != 0); + if (GetDevice ()->NeedsArp ()) { - IArpPrivate * arp = m_node->QueryInterface (IArpPrivate::iid); + Ptr arp = m_node->QueryInterface (IArpPrivate::iid); MacAddress hardwareDestination; - bool found = arp->Lookup (p, dest, PeekDevice (), &hardwareDestination); + bool found = arp->Lookup (p, dest, GetDevice (), &hardwareDestination); if (found) { - PeekDevice ()->Send (p, hardwareDestination, Ipv4::PROT_NUMBER); + GetDevice ()->Send (p, hardwareDestination, Ipv4::PROT_NUMBER); } - arp->Unref (); } else { - PeekDevice ()->Send (p, PeekDevice ()->GetBroadcast (), Ipv4::PROT_NUMBER); + GetDevice ()->Send (p, GetDevice ()->GetBroadcast (), Ipv4::PROT_NUMBER); } } diff --git a/src/internet-node/arp-ipv4-interface.h b/src/internet-node/arp-ipv4-interface.h index d75e7a491..b1c0698f1 100644 --- a/src/internet-node/arp-ipv4-interface.h +++ b/src/internet-node/arp-ipv4-interface.h @@ -43,7 +43,7 @@ class ArpIpv4Interface : public Ipv4Interface NETDEVICE, ARP, }; - ArpIpv4Interface (Ptr node, NetDevice *device); + ArpIpv4Interface (Ptr node, Ptr device); virtual ~ArpIpv4Interface (); private: diff --git a/src/internet-node/arp.cc b/src/internet-node/arp.cc index feb3ed6a0..3a085793e 100644 --- a/src/internet-node/arp.cc +++ b/src/internet-node/arp.cc @@ -63,18 +63,17 @@ Arp::CreateTraceResolver (TraceContext const &context) } ArpCache * -Arp::FindCache (NetDevice *device) +Arp::FindCache (Ptr device) { for (CacheList::const_iterator i = m_cacheList.begin (); i != m_cacheList.end (); i++) { - if ((*i)->PeekDevice () == device) + if ((*i)->GetDevice () == device) { return *i; } } - IIpv4Private *ipv4 = m_node->QueryInterface (IIpv4Private::iid); + Ptr ipv4 = m_node->QueryInterface (IIpv4Private::iid); Ipv4Interface *interface = ipv4->FindInterfaceForDevice (device); - ipv4->Unref (); ArpCache * cache = new ArpCache (device, interface); NS_ASSERT (device->IsBroadcast ()); device->SetLinkChangeCallback (MakeCallback (&ArpCache::Flush, cache)); @@ -83,7 +82,7 @@ Arp::FindCache (NetDevice *device) } void -Arp::Receive(Packet& packet, NetDevice *device) +Arp::Receive(Packet& packet, Ptr device) { ArpCache *cache = FindCache (device); ArpHeader arp; @@ -132,7 +131,7 @@ Arp::Receive(Packet& packet, NetDevice *device) } bool Arp::Lookup (Packet &packet, Ipv4Address destination, - NetDevice *device, + Ptr device, MacAddress *hardwareDestination) { ArpCache *cache = FindCache (device); @@ -204,25 +203,25 @@ void Arp::SendArpRequest (ArpCache const *cache, Ipv4Address to) { ArpHeader arp; - arp.SetRequest (cache->PeekDevice ()->GetAddress (), + arp.SetRequest (cache->GetDevice ()->GetAddress (), cache->GetInterface ()->GetAddress (), - cache->PeekDevice ()->GetBroadcast (), + cache->GetDevice ()->GetBroadcast (), to); Packet packet; packet.AddHeader (arp); - cache->PeekDevice ()->Send (packet, cache->PeekDevice ()->GetBroadcast (), PROT_NUMBER); + cache->GetDevice ()->Send (packet, cache->GetDevice ()->GetBroadcast (), PROT_NUMBER); } void Arp::SendArpReply (ArpCache const *cache, Ipv4Address toIp, MacAddress toMac) { ArpHeader arp; - arp.SetReply (cache->PeekDevice ()->GetAddress (), + arp.SetReply (cache->GetDevice ()->GetAddress (), cache->GetInterface ()->GetAddress (), toMac, toIp); Packet packet; packet.AddHeader (arp); - cache->PeekDevice ()->Send (packet, toMac, PROT_NUMBER); + cache->GetDevice ()->Send (packet, toMac, PROT_NUMBER); } }//namespace ns3 diff --git a/src/internet-node/arp.h b/src/internet-node/arp.h index d436a99a2..0273d5e3b 100644 --- a/src/internet-node/arp.h +++ b/src/internet-node/arp.h @@ -46,15 +46,15 @@ public: virtual TraceResolver *CreateTraceResolver (TraceContext const &context); - virtual void Receive(Packet& p, NetDevice *device); + virtual void Receive(Packet& p, Ptr device); bool Lookup (Packet &p, Ipv4Address destination, - NetDevice *device, + Ptr device, MacAddress *hardwareDestination); protected: virtual void DoDispose (void); private: typedef std::list CacheList; - ArpCache *FindCache (NetDevice *device); + ArpCache *FindCache (Ptr device); void SendArpRequest (ArpCache const *cache, Ipv4Address to); void SendArpReply (ArpCache const *cache, Ipv4Address toIp, MacAddress toMac); CacheList m_cacheList; diff --git a/src/internet-node/i-arp-private.cc b/src/internet-node/i-arp-private.cc index 89b628c27..28cf56f7c 100644 --- a/src/internet-node/i-arp-private.cc +++ b/src/internet-node/i-arp-private.cc @@ -21,17 +21,16 @@ #include "i-arp-private.h" #include "arp.h" #include "ns3/assert.h" +#include "ns3/net-device.h" namespace ns3 { const Iid IArpPrivate::iid ("IArpPrivate"); -IArpPrivate::IArpPrivate (Arp *arp) +IArpPrivate::IArpPrivate (Ptr arp) : NsUnknown (IArpPrivate::iid), m_arp (arp) -{ - m_arp->Ref (); -} +{} IArpPrivate::~IArpPrivate () { NS_ASSERT (m_arp == 0); @@ -39,7 +38,7 @@ IArpPrivate::~IArpPrivate () bool IArpPrivate::Lookup (Packet &p, Ipv4Address destination, - NetDevice *device, + Ptr device, MacAddress *hardwareDestination) { return m_arp->Lookup (p, destination, device, hardwareDestination); @@ -48,7 +47,6 @@ IArpPrivate::Lookup (Packet &p, Ipv4Address destination, void IArpPrivate::DoDispose (void) { - m_arp->Unref (); m_arp = 0; NsUnknown::DoDispose (); } diff --git a/src/internet-node/i-arp-private.h b/src/internet-node/i-arp-private.h index 39aeb04eb..7b3647601 100644 --- a/src/internet-node/i-arp-private.h +++ b/src/internet-node/i-arp-private.h @@ -35,15 +35,15 @@ class IArpPrivate : public NsUnknown { public: static const Iid iid; - IArpPrivate (Arp *arp); + IArpPrivate (Ptr arp); virtual ~IArpPrivate (); bool Lookup (Packet &p, Ipv4Address destination, - NetDevice *device, + Ptr device, MacAddress *hardwareDestination); protected: virtual void DoDispose (void); private: - Arp *m_arp; + Ptr m_arp; }; } // namespace ns3 diff --git a/src/internet-node/i-ipv4-impl.cc b/src/internet-node/i-ipv4-impl.cc index 82b5d0155..6ac4a58cd 100644 --- a/src/internet-node/i-ipv4-impl.cc +++ b/src/internet-node/i-ipv4-impl.cc @@ -21,14 +21,13 @@ #include "i-ipv4-impl.h" #include "ipv4.h" #include "ns3/assert.h" +#include "ns3/net-device.h" namespace ns3 { -IIpv4Impl::IIpv4Impl (Ipv4 *ipv4) +IIpv4Impl::IIpv4Impl (Ptr ipv4) : m_ipv4 (ipv4) -{ - m_ipv4->Ref (); -} +{} IIpv4Impl::~IIpv4Impl () { NS_ASSERT (m_ipv4 == 0); @@ -36,7 +35,6 @@ IIpv4Impl::~IIpv4Impl () void IIpv4Impl::DoDispose (void) { - m_ipv4->Unref (); m_ipv4 = 0; } @@ -90,7 +88,7 @@ IIpv4Impl::RemoveRoute (uint32_t i) return m_ipv4->RemoveRoute (i); } uint32_t -IIpv4Impl::AddInterface (NetDevice *device) +IIpv4Impl::AddInterface (Ptr device) { return m_ipv4->AddInterface (device); } diff --git a/src/internet-node/i-ipv4-impl.h b/src/internet-node/i-ipv4-impl.h index b3240a089..3c9fc1b66 100644 --- a/src/internet-node/i-ipv4-impl.h +++ b/src/internet-node/i-ipv4-impl.h @@ -22,6 +22,7 @@ #define I_IPV4_IMPL_H #include "ns3/i-ipv4.h" +#include "ns3/ptr.h" namespace ns3 { @@ -30,7 +31,7 @@ class Ipv4; class IIpv4Impl : public IIpv4 { public: - IIpv4Impl (Ipv4 *ipv4); + IIpv4Impl (Ptr ipv4); virtual ~IIpv4Impl (); @@ -51,7 +52,7 @@ public: virtual uint32_t GetNRoutes (void); virtual Ipv4Route *GetRoute (uint32_t i); virtual void RemoveRoute (uint32_t i); - virtual uint32_t AddInterface (NetDevice *device); + virtual uint32_t AddInterface (Ptr device); virtual uint32_t GetNInterfaces (void); virtual void SetAddress (uint32_t i, Ipv4Address address); @@ -65,7 +66,7 @@ public: protected: virtual void DoDispose (void); private: - Ipv4 *m_ipv4; + Ptr m_ipv4; }; } // namespace ns3 diff --git a/src/internet-node/i-ipv4-private.cc b/src/internet-node/i-ipv4-private.cc index 2921d5262..95fe4249f 100644 --- a/src/internet-node/i-ipv4-private.cc +++ b/src/internet-node/i-ipv4-private.cc @@ -21,17 +21,16 @@ #include "i-ipv4-private.h" #include "ipv4.h" #include "ns3/assert.h" +#include "ns3/net-device.h" namespace ns3 { const Iid IIpv4Private::iid ("IIpv4Private"); -IIpv4Private::IIpv4Private (Ipv4 *ipv4) +IIpv4Private::IIpv4Private (Ptr ipv4) : NsUnknown (IIpv4Private::iid), m_ipv4 (ipv4) -{ - m_ipv4->Ref (); -} +{} IIpv4Private::~IIpv4Private () { NS_ASSERT (m_ipv4 == 0); @@ -48,19 +47,18 @@ IIpv4Private::Send (Packet const &packet, Ipv4Address source, m_ipv4->Send (packet, source, destination, protocol); } Ipv4Interface * -IIpv4Private::FindInterfaceForDevice (NetDevice const*device) +IIpv4Private::FindInterfaceForDevice (Ptrdevice) { return m_ipv4->FindInterfaceForDevice (device); } void -IIpv4Private::Receive(Packet& p, NetDevice *device) +IIpv4Private::Receive(Packet& p, Ptr device) { m_ipv4->Receive (p, device); } void IIpv4Private::DoDispose (void) { - m_ipv4->Unref (); m_ipv4 = 0; NsUnknown::DoDispose (); } diff --git a/src/internet-node/i-ipv4-private.h b/src/internet-node/i-ipv4-private.h index a300f725f..61df90c96 100644 --- a/src/internet-node/i-ipv4-private.h +++ b/src/internet-node/i-ipv4-private.h @@ -23,6 +23,7 @@ #include "ns3/ns-unknown.h" #include "ns3/ipv4-address.h" +#include "ns3/ptr.h" #include namespace ns3 { @@ -38,18 +39,18 @@ class IIpv4Private : public NsUnknown { public: static const Iid iid; - IIpv4Private (Ipv4 *ipv4); + IIpv4Private (Ptr ipv4); virtual ~IIpv4Private (); TraceResolver *CreateTraceResolver (TraceContext const &context); void Send (Packet const &packet, Ipv4Address source, Ipv4Address destination, uint8_t protocol); - Ipv4Interface *FindInterfaceForDevice (NetDevice const*device); - void Receive(Packet& p, NetDevice *device); + Ipv4Interface *FindInterfaceForDevice (Ptrdevice); + void Receive(Packet& p, Ptr device); protected: virtual void DoDispose (void); private: - Ipv4 *m_ipv4; + Ptr m_ipv4; }; } // namespace ns3 diff --git a/src/internet-node/i-udp-impl.cc b/src/internet-node/i-udp-impl.cc index 00b522f83..b0b094f44 100644 --- a/src/internet-node/i-udp-impl.cc +++ b/src/internet-node/i-udp-impl.cc @@ -20,21 +20,20 @@ */ #include "i-udp-impl.h" #include "udp.h" +#include "ns3/socket.h" #include "ns3/assert.h" namespace ns3 { -IUdpImpl::IUdpImpl (Udp *udp) +IUdpImpl::IUdpImpl (Ptr udp) : m_udp (udp) -{ - m_udp->Ref (); -} +{} IUdpImpl::~IUdpImpl () { NS_ASSERT (m_udp == 0); } -Socket * +Ptr IUdpImpl::CreateSocket (void) { return m_udp->CreateSocket (); @@ -43,11 +42,7 @@ IUdpImpl::CreateSocket (void) void IUdpImpl::DoDispose (void) { - if (m_udp != 0) - { - m_udp->Unref (); - m_udp = 0; - } + m_udp = 0; IUdp::DoDispose (); } diff --git a/src/internet-node/i-udp-impl.h b/src/internet-node/i-udp-impl.h index 5acbac900..7bd9c4bae 100644 --- a/src/internet-node/i-udp-impl.h +++ b/src/internet-node/i-udp-impl.h @@ -22,6 +22,7 @@ #define I_UDP_IMPL_H #include "ns3/i-udp.h" +#include "ns3/ptr.h" namespace ns3 { @@ -30,15 +31,15 @@ class Udp; class IUdpImpl : public IUdp { public: - IUdpImpl (Udp *udp); + IUdpImpl (Ptr udp); virtual ~IUdpImpl (); - virtual Socket *CreateSocket (void); + virtual Ptr CreateSocket (void); protected: virtual void DoDispose (void); private: - Udp *m_udp; + Ptr m_udp; }; } // namespace ns3 diff --git a/src/internet-node/internet-node.cc b/src/internet-node/internet-node.cc index abe48a41d..33902fecf 100644 --- a/src/internet-node/internet-node.cc +++ b/src/internet-node/internet-node.cc @@ -41,35 +41,22 @@ namespace ns3 { InternetNode::InternetNode() { - Ipv4 *ipv4 = new Ipv4 (this); - Arp *arp = new Arp (this); - Udp *udp = new Udp (this); + Ptr ipv4 = new Ipv4 (this); + Ptr arp = new Arp (this); + Ptr udp = new Udp (this); - ipv4->Ref (); - arp->Ref (); - udp->Ref (); - - ApplicationList *applicationList = new ApplicationList(this); - L3Demux *l3Demux = new L3Demux(this); - Ipv4L4Demux *ipv4L4Demux = new Ipv4L4Demux(this); - - applicationList->Ref (); - l3Demux->Ref (); - ipv4L4Demux->Ref (); + Ptr applicationList = new ApplicationList(this); + Ptr l3Demux = new L3Demux(this); + Ptr ipv4L4Demux = new Ipv4L4Demux(this); l3Demux->Insert (ipv4); l3Demux->Insert (arp); ipv4L4Demux->Insert (udp); - IUdpImpl *udpImpl = new IUdpImpl (udp); - IArpPrivate *arpPrivate = new IArpPrivate (arp); - IIpv4Impl *ipv4Impl = new IIpv4Impl (ipv4); - IIpv4Private *ipv4Private = new IIpv4Private (ipv4); - - udpImpl->Ref (); - arpPrivate->Ref (); - ipv4Impl->Ref (); - ipv4Private->Ref (); + Ptr udpImpl = new IUdpImpl (udp); + Ptr arpPrivate = new IArpPrivate (arp); + Ptr ipv4Impl = new IIpv4Impl (ipv4); + Ptr ipv4Private = new IIpv4Private (ipv4); NsUnknown::AddInterface (ipv4Private); NsUnknown::AddInterface (ipv4Impl); @@ -78,18 +65,6 @@ InternetNode::InternetNode() NsUnknown::AddInterface (applicationList); NsUnknown::AddInterface (l3Demux); NsUnknown::AddInterface (ipv4L4Demux); - - - applicationList->Unref (); - l3Demux->Unref (); - ipv4L4Demux->Unref (); - arp->Unref (); - ipv4->Unref (); - udp->Unref (); - udpImpl->Unref (); - arpPrivate->Unref (); - ipv4Impl->Unref (); - ipv4Private->Unref (); } InternetNode::~InternetNode () @@ -105,11 +80,10 @@ TraceResolver * InternetNode::CreateTraceResolver (TraceContext const &context) { CompositeTraceResolver *resolver = new CompositeTraceResolver (context); - IIpv4Private *ipv4 = QueryInterface (IIpv4Private::iid); + Ptr ipv4 = QueryInterface (IIpv4Private::iid); resolver->Add ("ipv4", - MakeCallback (&IIpv4Private::CreateTraceResolver, ipv4), + MakeCallback (&IIpv4Private::CreateTraceResolver, ipv4.Peek ()), InternetNode::IPV4); - ipv4->Unref (); return resolver; } @@ -121,17 +95,16 @@ InternetNode::DoDispose() } void -InternetNode::DoAddDevice (NetDevice *device) const +InternetNode::DoAddDevice (Ptr device) const { device->SetReceiveCallback (MakeCallback (&InternetNode::ReceiveFromDevice, this)); } bool -InternetNode::ReceiveFromDevice (NetDevice *device, const Packet &p, uint16_t protocolNumber) const +InternetNode::ReceiveFromDevice (Ptr device, const Packet &p, uint16_t protocolNumber) const { - L3Demux *demux = QueryInterface (L3Demux::iid); - L3Protocol *target = demux->PeekProtocol (protocolNumber); - demux->Unref (); + Ptr demux = QueryInterface (L3Demux::iid); + Ptr target = demux->GetProtocol (protocolNumber); if (target != 0) { Packet packet = p; diff --git a/src/internet-node/internet-node.h b/src/internet-node/internet-node.h index 835acb870..04da92036 100644 --- a/src/internet-node/internet-node.h +++ b/src/internet-node/internet-node.h @@ -47,8 +47,8 @@ public: protected: virtual void DoDispose(void); private: - virtual void DoAddDevice (NetDevice *device) const; - bool ReceiveFromDevice (NetDevice *device, const Packet &p, uint16_t protocolNumber) const; + virtual void DoAddDevice (Ptr device) const; + bool ReceiveFromDevice (Ptr device, const Packet &p, uint16_t protocolNumber) const; std::string m_name; }; diff --git a/src/internet-node/ipv4-interface.cc b/src/internet-node/ipv4-interface.cc index e8bc9b116..cfc398582 100644 --- a/src/internet-node/ipv4-interface.cc +++ b/src/internet-node/ipv4-interface.cc @@ -31,26 +31,16 @@ namespace ns3 { * becoming useable, the user must invoke SetUp on them * once the final Ipv4 address and mask has been set. */ -Ipv4Interface::Ipv4Interface (NetDevice *nd) +Ipv4Interface::Ipv4Interface (Ptr nd) : m_netdevice (nd), m_ifup(false) -{ - if (m_netdevice != 0) - { - m_netdevice->Ref (); - } -} +{} Ipv4Interface::~Ipv4Interface () -{ - if (m_netdevice != 0) - { - m_netdevice->Unref (); - } -} +{} -NetDevice* -Ipv4Interface::PeekDevice (void) const +Ptr +Ipv4Interface::GetDevice (void) const { return m_netdevice; } diff --git a/src/internet-node/ipv4-interface.h b/src/internet-node/ipv4-interface.h index ef2b56408..1098c567c 100644 --- a/src/internet-node/ipv4-interface.h +++ b/src/internet-node/ipv4-interface.h @@ -25,6 +25,7 @@ #include #include "ns3/ipv4-address.h" +#include "ns3/ptr.h" namespace ns3 { @@ -69,7 +70,7 @@ public: * This value can be zero in which case the MTU * of this interface will be 2^(16-1). */ - Ipv4Interface (NetDevice *nd); + Ipv4Interface (Ptr nd); virtual ~Ipv4Interface(); /** @@ -87,7 +88,7 @@ public: * \returns the underlying NetDevice. This method can return * zero if this interface has no associated NetDevice. */ - NetDevice *PeekDevice (void) const; + Ptr GetDevice (void) const; /** * \param a set the ipv4 address of this interface. @@ -153,7 +154,7 @@ public: private: virtual void SendTo (Packet p, Ipv4Address dest) = 0; virtual TraceResolver *DoCreateTraceResolver (TraceContext const &context) = 0; - NetDevice* m_netdevice; + Ptr m_netdevice; bool m_ifup; Ipv4Address m_address; Ipv4Mask m_netmask; diff --git a/src/internet-node/ipv4-l4-demux.cc b/src/internet-node/ipv4-l4-demux.cc index 2a026de36..36549a1ee 100644 --- a/src/internet-node/ipv4-l4-demux.cc +++ b/src/internet-node/ipv4-l4-demux.cc @@ -43,10 +43,10 @@ Ipv4L4Demux::~Ipv4L4Demux() void Ipv4L4Demux::DoDispose (void) { - for (L4List_t::const_iterator i = m_protocols.begin(); i != m_protocols.end(); ++i) + for (L4List_t::iterator i = m_protocols.begin(); i != m_protocols.end(); ++i) { (*i)->Dispose (); - (*i)->Unref (); + *i = 0; } m_protocols.clear (); m_node = 0; @@ -59,25 +59,24 @@ Ipv4L4Demux::CreateTraceResolver (TraceContext const &context) CompositeTraceResolver *resolver = new CompositeTraceResolver (context); for (L4List_t::const_iterator i = m_protocols.begin(); i != m_protocols.end(); ++i) { - Ipv4L4Protocol *protocol = *i; + Ptr protocol = *i; std::string protValue; std::ostringstream oss (protValue); oss << (*i)->GetProtocolNumber (); Ipv4L4ProtocolTraceType protocolNumber = (*i)->GetProtocolNumber (); resolver->Add (protValue, - MakeCallback (&Ipv4L4Protocol::CreateTraceResolver, protocol), + MakeCallback (&Ipv4L4Protocol::CreateTraceResolver, protocol.Peek ()), protocolNumber); } return resolver; } void -Ipv4L4Demux::Insert(Ipv4L4Protocol *protocol) +Ipv4L4Demux::Insert(Ptr protocol) { - protocol->Ref (); m_protocols.push_back (protocol); } -Ipv4L4Protocol* -Ipv4L4Demux::PeekProtocol(int protocolNumber) +Ptr +Ipv4L4Demux::GetProtocol(int protocolNumber) { for (L4List_t::iterator i = m_protocols.begin(); i != m_protocols.end(); ++i) { @@ -89,7 +88,7 @@ Ipv4L4Demux::PeekProtocol(int protocolNumber) return 0; } void -Ipv4L4Demux::Erase(Ipv4L4Protocol*protocol) +Ipv4L4Demux::Erase(Ptr protocol) { m_protocols.remove (protocol); } diff --git a/src/internet-node/ipv4-l4-demux.h b/src/internet-node/ipv4-l4-demux.h index d52ce34b9..193addab5 100644 --- a/src/internet-node/ipv4-l4-demux.h +++ b/src/internet-node/ipv4-l4-demux.h @@ -65,7 +65,7 @@ public: * a working L4 Protocol and returned from this method. * The caller does not get ownership of the returned pointer. */ - void Insert(Ipv4L4Protocol *protocol); + void Insert(Ptr protocol); /** * \param protocolNumber number of protocol to lookup * in this L4 Demux @@ -75,17 +75,17 @@ public: * to forward packets up the stack to the right protocol. * It is also called from InternetNode::GetUdp for example. */ - Ipv4L4Protocol* PeekProtocol(int protocolNumber); + Ptr GetProtocol(int protocolNumber); /** * \param protocol protocol to remove from this demux. * * The input value to this method should be the value * returned from the Ipv4L4Protocol::Insert method. */ - void Erase(Ipv4L4Protocol*protocol); + void Erase (Ptr protocol); private: virtual void DoDispose (void); - typedef std::list L4List_t; + typedef std::list > L4List_t; L4List_t m_protocols; Ptr m_node; }; diff --git a/src/internet-node/ipv4-loopback-interface.cc b/src/internet-node/ipv4-loopback-interface.cc index 6b68dc871..97bffb066 100644 --- a/src/internet-node/ipv4-loopback-interface.cc +++ b/src/internet-node/ipv4-loopback-interface.cc @@ -43,9 +43,8 @@ Ipv4LoopbackInterface::DoCreateTraceResolver (TraceContext const &context) void Ipv4LoopbackInterface::SendTo (Packet packet, Ipv4Address dest) { - IIpv4Private *ipv4 = m_node->QueryInterface (IIpv4Private::iid); - ipv4->Receive (packet, PeekDevice ()); - ipv4->Unref (); + Ptr ipv4 = m_node->QueryInterface (IIpv4Private::iid); + ipv4->Receive (packet, GetDevice ()); } }//namespace ns3 diff --git a/src/internet-node/ipv4.cc b/src/internet-node/ipv4.cc index 6a0697290..14f58f99e 100644 --- a/src/internet-node/ipv4.cc +++ b/src/internet-node/ipv4.cc @@ -27,6 +27,7 @@ #include "ns3/ipv4-address.h" #include "ns3/ipv4-route.h" #include "ns3/node.h" +#include "ns3/net-device.h" #include "ipv4.h" #include "ipv4-l4-protocol.h" @@ -310,7 +311,7 @@ Ipv4::RemoveRoute (uint32_t index) uint32_t -Ipv4::AddInterface (NetDevice *device) +Ipv4::AddInterface (Ptr device) { Ipv4Interface *interface = new ArpIpv4Interface (m_node, device); return AddIpv4Interface (interface); @@ -344,11 +345,11 @@ Ipv4::GetNInterfaces (void) const } Ipv4Interface * -Ipv4::FindInterfaceForDevice (NetDevice const*device) +Ipv4::FindInterfaceForDevice (Ptr device) { for (Ipv4InterfaceList::const_iterator i = m_interfaces.begin (); i != m_interfaces.end (); i++) { - if ((*i)->PeekDevice () == device) + if ((*i)->GetDevice () == device) { return *i; } @@ -357,12 +358,12 @@ Ipv4::FindInterfaceForDevice (NetDevice const*device) } void -Ipv4::Receive(Packet& packet, NetDevice *device) +Ipv4::Receive(Packet& packet, Ptr device) { uint32_t index = 0; for (Ipv4InterfaceList::const_iterator i = m_interfaces.begin (); i != m_interfaces.end (); i++) { - if ((*i)->PeekDevice () == device) + if ((*i)->GetDevice () == device) { m_rxTrace (packet, index); break; @@ -442,7 +443,7 @@ Ipv4::SendRealOut (Packet const &p, Ipv4Header const &ip, Ipv4Route const &route bool -Ipv4::Forwarding (Packet const &packet, Ipv4Header &ipHeader, NetDevice *device) +Ipv4::Forwarding (Packet const &packet, Ipv4Header &ipHeader, Ptr device) { for (Ipv4InterfaceList::const_iterator i = m_interfaces.begin (); i != m_interfaces.end (); i++) @@ -458,7 +459,7 @@ Ipv4::Forwarding (Packet const &packet, Ipv4Header &ipHeader, NetDevice *device) i != m_interfaces.end (); i++) { Ipv4Interface *interface = *i; - if (interface->PeekDevice () == device) + if (interface->GetDevice () == device) { if (ipHeader.GetDestination ().IsEqual (interface->GetBroadcast ())) { @@ -504,9 +505,8 @@ Ipv4::Forwarding (Packet const &packet, Ipv4Header &ipHeader, NetDevice *device) void Ipv4::ForwardUp (Packet p, Ipv4Header const&ip) { - Ipv4L4Demux *demux = m_node->QueryInterface (Ipv4L4Demux::iid); - Ipv4L4Protocol *protocol = demux->PeekProtocol (ip.GetProtocol ()); - demux->Unref (); + Ptr demux = m_node->QueryInterface (Ipv4L4Demux::iid); + Ptr protocol = demux->GetProtocol (ip.GetProtocol ()); protocol->Receive (p, ip.GetSource (), ip.GetDestination ()); } diff --git a/src/internet-node/ipv4.h b/src/internet-node/ipv4.h index b041b0e78..1b88f1863 100644 --- a/src/internet-node/ipv4.h +++ b/src/internet-node/ipv4.h @@ -165,7 +165,7 @@ public: * to disable it, you can invoke Ipv4Interface::SetDown which will * make sure that it is never used during packet forwarding. */ - uint32_t AddInterface (NetDevice *device); + uint32_t AddInterface (Ptr device); /** * \param i index of interface to return * \returns the requested interface @@ -182,7 +182,7 @@ public: * Try to find an Ipv4Interface whose NetDevice is equal to * the input NetDevice. */ - Ipv4Interface *FindInterfaceForDevice (NetDevice const*device); + Ipv4Interface *FindInterfaceForDevice (Ptr device); /** @@ -192,7 +192,7 @@ public: * - implement a per-NetDevice ARP cache * - send back arp replies on the right device */ - virtual void Receive(Packet& p, NetDevice *device); + virtual void Receive(Packet& p, Ptr device); /** * \param packet packet to send @@ -220,7 +220,7 @@ protected: virtual void DoDispose (void); private: void SendRealOut (Packet const &packet, Ipv4Header const &ip, Ipv4Route const &route); - bool Forwarding (Packet const &packet, Ipv4Header &ipHeader, NetDevice *device); + bool Forwarding (Packet const &packet, Ipv4Header &ipHeader, Ptr device); void ForwardUp (Packet p, Ipv4Header const&ip); uint32_t AddIpv4Interface (Ipv4Interface *interface); void SetupLoopback (void); diff --git a/src/internet-node/l3-demux.cc b/src/internet-node/l3-demux.cc index 8f5ddc56f..1388ebb9b 100644 --- a/src/internet-node/l3-demux.cc +++ b/src/internet-node/l3-demux.cc @@ -45,7 +45,7 @@ L3Demux::DoDispose (void) for (L3Map_t::iterator i = m_protocols.begin(); i != m_protocols.end(); ++i) { i->second->Dispose (); - i->second->Unref (); + i->second = 0; } m_protocols.clear (); m_node = 0; @@ -63,23 +63,25 @@ L3Demux::CreateTraceResolver (TraceContext const &context) const oss << i->second->GetProtocolNumber (); ProtocolTraceType context = i->second->GetProtocolNumber (); resolver->Add (protValue, - MakeCallback (&L3Protocol::CreateTraceResolver, i->second), + MakeCallback (&L3Protocol::CreateTraceResolver, i->second.Peek ()), context); } return resolver; } -void L3Demux::Insert(L3Protocol *p) +void L3Demux::Insert(Ptr p) { - p->Ref (); m_protocols.insert(L3Map_t::value_type(p->GetProtocolNumber (), p)); } -L3Protocol* -L3Demux::PeekProtocol (int p) +Ptr +L3Demux::GetProtocol (int p) { // Look up a protocol by protocol number L3Map_t::iterator i = m_protocols.find(p); - if (i == m_protocols.end()) return 0; // Not found + if (i == m_protocols.end()) + { + return 0; + } return i->second; // Return the protocol } diff --git a/src/internet-node/l3-demux.h b/src/internet-node/l3-demux.h index 9e3ffdc0a..18f1cd962 100644 --- a/src/internet-node/l3-demux.h +++ b/src/internet-node/l3-demux.h @@ -68,7 +68,7 @@ public: * a working L3 Protocol and returned from this method. * The caller does not get ownership of the returned pointer. */ - void Insert(ns3::L3Protocol * protocol); + void Insert(Ptr protocol); /** * \param protocolNumber number of protocol to lookup * in this L4 Demux @@ -78,18 +78,18 @@ public: * to forward packets up the stack to the right protocol. * It is also called from InternetNode::GetIpv4 for example. */ - ns3::L3Protocol* PeekProtocol (int protocolNumber); + Ptr GetProtocol (int protocolNumber); /** * \param protocol protocol to remove from this demux. * * The input value to this method should be the value * returned from the L3Protocol::Insert method. */ - void Erase(ns3::L3Protocol*protocol); + void Erase(Ptr protocol); protected: virtual void DoDispose (void); private: - typedef std::map L3Map_t; + typedef std::map > L3Map_t; Ptr m_node; L3Map_t m_protocols; diff --git a/src/internet-node/l3-protocol.h b/src/internet-node/l3-protocol.h index 00d780568..56c6a5914 100644 --- a/src/internet-node/l3-protocol.h +++ b/src/internet-node/l3-protocol.h @@ -26,6 +26,7 @@ #define L3_PROTOCOL_H #include "ns3/object.h" +#include "ns3/ptr.h" namespace ns3 { @@ -53,7 +54,7 @@ public: * - implement a per-NetDevice ARP cache * - send back arp replies on the right device */ - virtual void Receive(Packet& p, NetDevice *device) = 0; + virtual void Receive(Packet& p, Ptr device) = 0; protected: virtual void DoDispose (void); diff --git a/src/internet-node/udp-socket.cc b/src/internet-node/udp-socket.cc index 52f0a749d..233376318 100644 --- a/src/internet-node/udp-socket.cc +++ b/src/internet-node/udp-socket.cc @@ -26,7 +26,7 @@ namespace ns3 { -UdpSocket::UdpSocket (Ptr node, Udp *udp) +UdpSocket::UdpSocket (Ptr node, Ptr udp) : m_endPoint (0), m_node (node), m_udp (udp), @@ -34,9 +34,7 @@ UdpSocket::UdpSocket (Ptr node, Udp *udp) m_shutdownSend (false), m_shutdownRecv (false), m_connected (false) -{ - m_udp->Ref (); -} +{} UdpSocket::~UdpSocket () { m_node = 0; @@ -55,11 +53,7 @@ UdpSocket::~UdpSocket () m_udp->DeAllocate (m_endPoint); NS_ASSERT (m_endPoint == 0); } - if (m_udp != 0) - { - m_udp->Unref (); - m_udp = 0; - } + m_udp = 0; } Ptr @@ -73,11 +67,7 @@ UdpSocket::Destroy (void) { m_node = 0; m_endPoint = 0; - if (m_udp != 0) - { - m_udp->Unref (); - m_udp = 0; - } + m_udp = 0; } int UdpSocket::FinishBind (void) @@ -135,7 +125,7 @@ UdpSocket::ShutdownRecv (void) } void -UdpSocket::DoClose(ns3::Callback closeCompleted) +UdpSocket::DoClose(ns3::Callback > closeCompleted) { // XXX: we should set the close state and check it in all API methods. if (!closeCompleted.IsNull ()) @@ -146,9 +136,9 @@ UdpSocket::DoClose(ns3::Callback closeCompleted) void UdpSocket::DoConnect(const Ipv4Address & address, uint16_t portNumber, - ns3::Callback connectionSucceeded, - ns3::Callback connectionFailed, - ns3::Callback halfClose) + ns3::Callback > connectionSucceeded, + ns3::Callback > connectionFailed, + ns3::Callback > halfClose) { m_defaultAddress = address; m_defaultPort = portNumber; @@ -159,9 +149,9 @@ UdpSocket::DoConnect(const Ipv4Address & address, m_connected = true; } int -UdpSocket::DoAccept(ns3::Callback connectionRequest, - ns3::Callback newConnectionCreated, - ns3::Callback closeRequested) +UdpSocket::DoAccept(ns3::Callback, const Ipv4Address&, uint16_t> connectionRequest, + ns3::Callback, const Ipv4Address&, uint16_t> newConnectionCreated, + ns3::Callback > closeRequested) { // calling accept on a udp socket is a programming error. m_errno = EOPNOTSUPP; @@ -170,7 +160,7 @@ UdpSocket::DoAccept(ns3::Callback c int UdpSocket::DoSend (const uint8_t* buffer, uint32_t size, - ns3::Callback dataSent) + ns3::Callback, uint32_t> dataSent) { if (!m_connected) { @@ -190,7 +180,7 @@ UdpSocket::DoSend (const uint8_t* buffer, } int UdpSocket::DoSendPacketTo (const Packet &p, Ipv4Address daddr, uint16_t dport, - ns3::Callback dataSent) + ns3::Callback, uint32_t> dataSent) { if (m_endPoint == 0) { @@ -219,7 +209,7 @@ UdpSocket::DoSendTo(const Ipv4Address &address, uint16_t port, const uint8_t *buffer, uint32_t size, - ns3::Callback dataSent) + ns3::Callback, uint32_t> dataSent) { if (m_connected) { @@ -238,12 +228,12 @@ UdpSocket::DoSendTo(const Ipv4Address &address, return DoSendPacketTo (p, address, port, dataSent); } void -UdpSocket::DoRecv(ns3::Callback callback) +UdpSocket::DoRecv(ns3::Callback, const uint8_t*, uint32_t,const Ipv4Address&, uint16_t> callback) { m_rxCallback = callback; } void -UdpSocket::DoRecvDummy(ns3::Callback callback) +UdpSocket::DoRecvDummy(ns3::Callback, uint32_t,const Ipv4Address&, uint16_t> callback) { m_dummyRxCallback = callback; } diff --git a/src/internet-node/udp-socket.h b/src/internet-node/udp-socket.h index cab448b13..9d2d5805e 100644 --- a/src/internet-node/udp-socket.h +++ b/src/internet-node/udp-socket.h @@ -39,7 +39,7 @@ public: /** * Create an unbound udp socket. */ - UdpSocket (Ptr node, Udp *udp); + UdpSocket (Ptr node, Ptr udp); virtual ~UdpSocket (); virtual enum SocketErrno GetErrno (void) const; @@ -52,25 +52,25 @@ public: virtual int ShutdownRecv (void); private: - virtual void DoClose(ns3::Callback closeCompleted); + virtual void DoClose(ns3::Callback > closeCompleted); virtual void DoConnect(const Ipv4Address & address, uint16_t portNumber, - ns3::Callback connectionSucceeded, - ns3::Callback connectionFailed, - ns3::Callback halfClose); - virtual int DoAccept(ns3::Callback connectionRequest, - ns3::Callback newConnectionCreated, - ns3::Callback closeRequested); + ns3::Callback > connectionSucceeded, + ns3::Callback > connectionFailed, + ns3::Callback > halfClose); + virtual int DoAccept(ns3::Callback, const Ipv4Address&, uint16_t> connectionRequest, + ns3::Callback, const Ipv4Address&, uint16_t> newConnectionCreated, + ns3::Callback > closeRequested); virtual int DoSend (const uint8_t* buffer, uint32_t size, - ns3::Callback dataSent); + ns3::Callback, uint32_t> dataSent); virtual int DoSendTo(const Ipv4Address &address, uint16_t port, const uint8_t *buffer, uint32_t size, - ns3::Callback dataSent); - virtual void DoRecv(ns3::Callback); - virtual void DoRecvDummy(ns3::Callback); + ns3::Callback, uint32_t> dataSent); + virtual void DoRecv(ns3::Callback, const uint8_t*, uint32_t,const Ipv4Address&, uint16_t>); + virtual void DoRecvDummy(ns3::Callback, uint32_t,const Ipv4Address&, uint16_t>); private: friend class Udp; @@ -79,15 +79,15 @@ private: void ForwardUp (const Packet &p, Ipv4Address saddr, uint16_t sport); void Destroy (void); int DoSendPacketTo (const Packet &p, Ipv4Address daddr, uint16_t dport, - ns3::Callback dataSent); + ns3::Callback, uint32_t> dataSent); Ipv4EndPoint *m_endPoint; Ptr m_node; - Udp *m_udp; + Ptr m_udp; Ipv4Address m_defaultAddress; uint16_t m_defaultPort; - Callback m_dummyRxCallback; - Callback m_rxCallback; + Callback,uint32_t,const Ipv4Address &,uint16_t> m_dummyRxCallback; + Callback,uint8_t const*,uint32_t,const Ipv4Address &,uint16_t> m_rxCallback; enum SocketErrno m_errno; bool m_shutdownSend; bool m_shutdownRecv; diff --git a/src/internet-node/udp.cc b/src/internet-node/udp.cc index 96bc58056..cc6eef871 100644 --- a/src/internet-node/udp.cc +++ b/src/internet-node/udp.cc @@ -65,11 +65,10 @@ Udp::DoDispose (void) Ipv4L4Protocol::DoDispose (); } -Socket * +Ptr Udp::CreateSocket (void) { - Socket *socket = new UdpSocket (m_node, this); - socket->Ref (); + Ptr socket = new UdpSocket (m_node, this); return socket; } @@ -138,11 +137,10 @@ Udp::Send (Packet packet, packet.AddHeader (udpHeader); - IIpv4Private *ipv4 = m_node->QueryInterface (IIpv4Private::iid); + Ptr ipv4 = m_node->QueryInterface (IIpv4Private::iid); if (ipv4 != 0) { ipv4->Send (packet, saddr, daddr, PROT_NUMBER); - ipv4->Unref (); } } diff --git a/src/internet-node/udp.h b/src/internet-node/udp.h index 5413f1bca..fdb707230 100644 --- a/src/internet-node/udp.h +++ b/src/internet-node/udp.h @@ -46,7 +46,7 @@ public: virtual TraceResolver *CreateTraceResolver (TraceContext const &context); - Socket *CreateSocket (void); + Ptr CreateSocket (void); Ipv4EndPoint *Allocate (void); Ipv4EndPoint *Allocate (Ipv4Address address); diff --git a/src/node/channel.h b/src/node/channel.h index 0afb4da0b..aefbf98b3 100644 --- a/src/node/channel.h +++ b/src/node/channel.h @@ -25,6 +25,7 @@ #include #include #include "ns3/object.h" +#include "ns3/ptr.h" namespace ns3 { @@ -57,7 +58,7 @@ public: * * This method must be implemented by subclasses. */ - virtual NetDevice *GetDevice (uint32_t i) const = 0; + virtual Ptr GetDevice (uint32_t i) const = 0; protected: virtual ~Channel (); diff --git a/src/node/i-ipv4.h b/src/node/i-ipv4.h index 11197de90..27fc31dd8 100644 --- a/src/node/i-ipv4.h +++ b/src/node/i-ipv4.h @@ -116,7 +116,7 @@ public: * to disable it, you can invoke Ipv4Interface::SetDown which will * make sure that it is never used during packet forwarding. */ - virtual uint32_t AddInterface (NetDevice *device) = 0; + virtual uint32_t AddInterface (Ptr device) = 0; /** * \returns the number of interfaces added by the user. */ diff --git a/src/node/i-udp.h b/src/node/i-udp.h index 85163935a..32a92c7ce 100644 --- a/src/node/i-udp.h +++ b/src/node/i-udp.h @@ -22,6 +22,7 @@ #define I_UDP_H #include "ns3/ns-unknown.h" +#include "ns3/ptr.h" namespace ns3 { @@ -34,7 +35,7 @@ public: IUdp (); - virtual Socket *CreateSocket (void) = 0; + virtual Ptr CreateSocket (void) = 0; }; } // namespace ns3 diff --git a/src/node/net-device.cc b/src/node/net-device.cc index 67054e03a..bd5b2946f 100644 --- a/src/node/net-device.cc +++ b/src/node/net-device.cc @@ -183,7 +183,7 @@ NetDevice::CreateTraceResolver (TraceContext const &context) return DoCreateTraceResolver (context); } -Channel * +Ptr NetDevice::GetChannel (void) const { return DoGetChannel (); @@ -236,7 +236,7 @@ NetDevice::NeedsArp (void) const } void -NetDevice::SetReceiveCallback (Callback cb) +NetDevice::SetReceiveCallback (Callback,const Packet &,uint16_t> cb) { m_receiveCallback = cb; } diff --git a/src/node/net-device.h b/src/node/net-device.h index 2b7506f1a..78ed233d5 100644 --- a/src/node/net-device.h +++ b/src/node/net-device.h @@ -79,7 +79,7 @@ public: * returned can be zero if the NetDevice is not yet connected * to any channel. */ - Channel *GetChannel (void) const; + Ptr GetChannel (void) const; /** * \return the current MacAddress of this interface. @@ -174,7 +174,7 @@ public: bool NeedsArp (void) const; - void SetReceiveCallback (Callback cb); + void SetReceiveCallback (Callback,const Packet &,uint16_t> cb); protected: /** @@ -241,7 +241,7 @@ public: virtual bool SendTo (Packet& p, const MacAddress& dest) = 0; virtual bool DoNeedsArp (void) const = 0; virtual TraceResolver *DoCreateTraceResolver (TraceContext const &context) = 0; - virtual Channel *DoGetChannel (void) const = 0; + virtual Ptr DoGetChannel (void) const = 0; Ptr m_node; std::string m_name; uint16_t m_ifIndex; @@ -253,7 +253,7 @@ public: bool m_isMulticast; bool m_isPointToPoint; Callback m_linkChangeCallback; - Callback m_receiveCallback; + Callback,const Packet &,uint16_t> m_receiveCallback; }; }; // namespace ns3 diff --git a/src/node/node.cc b/src/node/node.cc index baf74d17c..a23fdde35 100644 --- a/src/node/node.cc +++ b/src/node/node.cc @@ -69,16 +69,15 @@ Node::SetSystemId(uint32_t s ) } uint32_t -Node::AddDevice (NetDevice *device) +Node::AddDevice (Ptr device) { - device->Ref (); uint32_t index = m_devices.size (); m_devices.push_back (device); DoAddDevice (device); device->SetIfIndex(index); return index; } -NetDevice * +Ptr Node::GetDevice (uint32_t index) const { return m_devices[index]; @@ -91,12 +90,12 @@ Node::GetNDevices (void) const void Node::DoDispose() { - for (std::vector::iterator i = m_devices.begin (); + for (std::vector >::iterator i = m_devices.begin (); i != m_devices.end (); i++) { - NetDevice *device = *i; + Ptr device = *i; device->Dispose (); - device->Unref (); + *i = 0; } m_devices.clear (); NsUnknown::DoDispose (); diff --git a/src/node/node.h b/src/node/node.h index 4b072cc3e..e5b30e1da 100644 --- a/src/node/node.h +++ b/src/node/node.h @@ -50,18 +50,18 @@ public: uint32_t GetSystemId (void) const; void SetSystemId(uint32_t s); - uint32_t AddDevice (NetDevice *device); - NetDevice *GetDevice (uint32_t index) const; + uint32_t AddDevice (Ptr device); + Ptr GetDevice (uint32_t index) const; uint32_t GetNDevices (void) const; protected: virtual void DoDispose (void); private: - virtual void DoAddDevice (NetDevice *device) const = 0; + virtual void DoAddDevice (Ptr device) const = 0; uint32_t m_id; // Node id for this node uint32_t m_sid; // System id for this node - std::vector m_devices; + std::vector > m_devices; }; } //namespace ns3 diff --git a/src/node/socket.cc b/src/node/socket.cc index 577bb8bae..75cd3a023 100644 --- a/src/node/socket.cc +++ b/src/node/socket.cc @@ -6,7 +6,7 @@ Socket::~Socket () {} void -Socket::Close(Callback closeCompleted) +Socket::Close(Callback > closeCompleted) { DoClose (closeCompleted); } @@ -14,23 +14,23 @@ Socket::Close(Callback closeCompleted) void Socket::Connect(const Ipv4Address & address, uint16_t portNumber, - Callback connectionSucceeded, - Callback connectionFailed, - Callback halfClose) + Callback > connectionSucceeded, + Callback > connectionFailed, + Callback > halfClose) { DoConnect (address, portNumber, connectionSucceeded, connectionFailed, halfClose); } int -Socket::Accept(Callback connectionRequest, - Callback newConnectionCreated, - Callback closeRequested) +Socket::Accept(Callback, const Ipv4Address&, uint16_t> connectionRequest, + Callback, const Ipv4Address&, uint16_t> newConnectionCreated, + Callback > closeRequested) { return DoAccept (connectionRequest, newConnectionCreated, closeRequested); } int Socket::Send (const uint8_t* buffer, uint32_t size, - Callback dataSent) + Callback, uint32_t> dataSent) { return DoSend (buffer, size, dataSent); } @@ -39,42 +39,42 @@ Socket::SendTo(const Ipv4Address &address, uint16_t port, const uint8_t *buffer, uint32_t size, - Callback dataSent) + Callback, uint32_t> dataSent) { return DoSendTo (address, port, buffer, size, dataSent); } void -Socket::Recv(Callback callback) +Socket::Recv(Callback, const uint8_t*, uint32_t,const Ipv4Address&, uint16_t> callback) { DoRecv (callback); } void -Socket::RecvDummy(Callback callback) +Socket::RecvDummy(Callback, uint32_t,const Ipv4Address&, uint16_t> callback) { DoRecvDummy (callback); } bool -Socket::RefuseAllConnections (Socket* socket, const Ipv4Address& address, uint16_t port) +Socket::RefuseAllConnections (Ptr socket, const Ipv4Address& address, uint16_t port) { return false; } void -Socket::DummyCallbackVoidSocket (Socket *socket) +Socket::DummyCallbackVoidSocket (Ptr socket) {} void -Socket::DummyCallbackVoidSocketUi32 (Socket *socket, uint32_t) +Socket::DummyCallbackVoidSocketUi32 (Ptr socket, uint32_t) {} void -Socket::DummyCallbackVoidSocketUi32Ipv4AddressUi16 (Socket *socket, uint32_t, const Ipv4Address &, uint16_t) +Socket::DummyCallbackVoidSocketUi32Ipv4AddressUi16 (Ptr socket, uint32_t, const Ipv4Address &, uint16_t) {} void -Socket::DummyCallbackVoidSocketBufferUi32Ipv4AddressUi16 (Socket *socket, const uint8_t *, uint32_t, +Socket::DummyCallbackVoidSocketBufferUi32Ipv4AddressUi16 (Ptr socket, const uint8_t *, uint32_t, const Ipv4Address &, uint16_t) {} void -Socket::DummyCallbackVoidSocketIpv4AddressUi16 (Socket *socket, const Ipv4Address &, uint16_t) +Socket::DummyCallbackVoidSocketIpv4AddressUi16 (Ptr socket, const Ipv4Address &, uint16_t) {} diff --git a/src/node/socket.h b/src/node/socket.h index 396b79d71..82769b405 100644 --- a/src/node/socket.h +++ b/src/node/socket.h @@ -114,7 +114,7 @@ public: * After the Close call, the socket is no longer valid, and cannot * safely be used for subsequent operations. */ - void Close(Callback closeCompleted = MakeCallback (&Socket::DummyCallbackVoidSocket)); + void Close(Callback > closeCompleted = MakeCallback (&Socket::DummyCallbackVoidSocket)); /** * \returns zero on success, -1 on failure. @@ -147,9 +147,9 @@ public: */ void Connect(const Ipv4Address & address, uint16_t portNumber, - Callback connectionSucceeded = MakeCallback(&Socket::DummyCallbackVoidSocket), - Callback connectionFailed = MakeCallback(&Socket::DummyCallbackVoidSocket), - Callback halfClose = MakeCallback(&Socket::DummyCallbackVoidSocket)); + Callback > connectionSucceeded = MakeCallback(&Socket::DummyCallbackVoidSocket), + Callback > connectionFailed = MakeCallback(&Socket::DummyCallbackVoidSocket), + Callback > halfClose = MakeCallback(&Socket::DummyCallbackVoidSocket)); /** * \brief Accept connection requests from remote hosts @@ -170,11 +170,11 @@ public: * \param closeRequested Callback for connection close request from peer. * XXX: when is this callback invoked ? */ - int Accept(Callback connectionRequest = + int Accept(Callback, const Ipv4Address&, uint16_t> connectionRequest = MakeCallback(&Socket::RefuseAllConnections), - Callback newConnectionCreated = + Callback, const Ipv4Address&, uint16_t> newConnectionCreated = MakeCallback (&Socket::DummyCallbackVoidSocketIpv4AddressUi16), - Callback closeRequested = MakeCallback (&Socket::DummyCallbackVoidSocket)); + Callback > closeRequested = MakeCallback (&Socket::DummyCallbackVoidSocket)); /** * \brief Send data (or dummy data) to the remote host @@ -186,7 +186,7 @@ public: */ int Send (const uint8_t* buffer, uint32_t size, - Callback dataSent = MakeCallback (&Socket::DummyCallbackVoidSocketUi32)); + Callback, uint32_t> dataSent = MakeCallback (&Socket::DummyCallbackVoidSocketUi32)); /** * \brief Send data to a specified peer. @@ -202,7 +202,7 @@ public: uint16_t port, const uint8_t *buffer, uint32_t size, - Callback dataSent = MakeCallback (&Socket::DummyCallbackVoidSocketUi32)); + Callback, uint32_t> dataSent = MakeCallback (&Socket::DummyCallbackVoidSocketUi32)); /** * \brief Receive data @@ -213,7 +213,7 @@ public: * allocation to hold the dummy memory into a buffer which can be passed * to the user. Instead, consider using the RecvDummy method. */ - void Recv(Callback = + void Recv(Callback, const uint8_t*, uint32_t,const Ipv4Address&, uint16_t> = MakeCallback (&Socket::DummyCallbackVoidSocketBufferUi32Ipv4AddressUi16)); /** @@ -223,38 +223,38 @@ public: * This method is included because it is vastly more efficient than the * Recv method when you use dummy payload. */ - void RecvDummy(Callback = + void RecvDummy(Callback, uint32_t,const Ipv4Address&, uint16_t> = MakeCallback (&Socket::DummyCallbackVoidSocketUi32Ipv4AddressUi16)); private: - virtual void DoClose(Callback closeCompleted) = 0; + virtual void DoClose(Callback > closeCompleted) = 0; virtual void DoConnect(const Ipv4Address & address, uint16_t portNumber, - Callback connectionSucceeded, - Callback connectionFailed, - Callback halfClose) = 0; - virtual int DoAccept(Callback connectionRequest, - Callback newConnectionCreated, - Callback closeRequested) = 0; + Callback > connectionSucceeded, + Callback > connectionFailed, + Callback > halfClose) = 0; + virtual int DoAccept(Callback, const Ipv4Address&, uint16_t> connectionRequest, + Callback, const Ipv4Address&, uint16_t> newConnectionCreated, + Callback > closeRequested) = 0; virtual int DoSend (const uint8_t* buffer, uint32_t size, - Callback dataSent) = 0; + Callback, uint32_t> dataSent) = 0; virtual int DoSendTo(const Ipv4Address &address, uint16_t port, const uint8_t *buffer, uint32_t size, - Callback dataSent) = 0; - virtual void DoRecv(Callback receive) = 0; - virtual void DoRecvDummy(Callback) = 0; + Callback, uint32_t> dataSent) = 0; + virtual void DoRecv(Callback, const uint8_t*, uint32_t,const Ipv4Address&, uint16_t> receive) = 0; + virtual void DoRecvDummy(Callback, uint32_t,const Ipv4Address&, uint16_t>) = 0; - static bool RefuseAllConnections (Socket* socket, const Ipv4Address& address, uint16_t port); - static void DummyCallbackVoidSocket (Socket *socket); - static void DummyCallbackVoidSocketUi32 (Socket *socket, uint32_t); - static void DummyCallbackVoidSocketUi32Ipv4AddressUi16 (Socket *socket, uint32_t, const Ipv4Address &, uint16_t); - static void DummyCallbackVoidSocketBufferUi32Ipv4AddressUi16 (Socket *socket, const uint8_t *, uint32_t, + static bool RefuseAllConnections (Ptr socket, const Ipv4Address& address, uint16_t port); + static void DummyCallbackVoidSocket (Ptr socket); + static void DummyCallbackVoidSocketUi32 (Ptr socket, uint32_t); + static void DummyCallbackVoidSocketUi32Ipv4AddressUi16 (Ptr socket, uint32_t, const Ipv4Address &, uint16_t); + static void DummyCallbackVoidSocketBufferUi32Ipv4AddressUi16 (Ptr socket, const uint8_t *, uint32_t, const Ipv4Address &, uint16_t); - static void DummyCallbackVoidSocketIpv4AddressUi16 (Socket *socket, const Ipv4Address &, uint16_t); + static void DummyCallbackVoidSocketIpv4AddressUi16 (Ptr socket, const Ipv4Address &, uint16_t); }; } //namespace ns3