diff --git a/SConstruct b/SConstruct index 1c7ba13fb..5a16b9fd7 100644 --- a/SConstruct +++ b/SConstruct @@ -207,6 +207,7 @@ node.add_sources ([ 'channel.cc', 'node-list.cc', 'socket.cc', + 'i-socket-factory.cc', 'i-udp.cc', 'i-ipv4.cc', 'application.cc', @@ -223,6 +224,7 @@ node.add_inst_headers ([ 'channel.h', 'node-list.h', 'socket.h', + 'i-socket-factory.h', 'i-udp.h', 'i-ipv4.h', 'application.h', @@ -263,7 +265,7 @@ inode.add_sources ([ 'header-utils.cc', 'udp-socket.cc', 'ipv4-end-point-demux.cc', - 'i-udp-impl.cc', + 'i-udp-socket-factory-impl.cc', 'i-arp-private.cc', 'i-ipv4-impl.cc', 'i-ipv4-private.cc', @@ -283,7 +285,7 @@ inode.add_headers ([ 'queue.h', 'arp-ipv4-interface.h', 'udp-socket.h', - 'i-udp-impl.h', + 'i-udp-socket-factory-impl.h', 'udp.h', 'i-arp-private.h', 'i-ipv4-impl.h', diff --git a/examples/simple-p2p.cc b/examples/simple-p2p.cc index 00104109f..e36bde711 100644 --- a/examples/simple-p2p.cc +++ b/examples/simple-p2p.cc @@ -149,6 +149,7 @@ int main (int argc, char *argv[]) n0, Ipv4Address("10.1.3.2"), 80, + "IUdp", ConstantVariable(1), ConstantVariable(0)); // Start the application @@ -160,6 +161,7 @@ int main (int argc, char *argv[]) n3, Ipv4Address("10.1.2.1"), 80, + "IUdp", ConstantVariable(1), ConstantVariable(0)); // Start the application diff --git a/samples/main-simple.cc b/samples/main-simple.cc index eb35aee81..e6ebde758 100644 --- a/samples/main-simple.cc +++ b/samples/main-simple.cc @@ -2,7 +2,7 @@ #include "ns3/internet-node.h" #include "ns3/simulator.h" -#include "ns3/i-udp.h" +#include "ns3/i-socket-factory.h" #include "ns3/socket.h" #include "ns3/nstime.h" @@ -40,12 +40,13 @@ RunSimulation (void) { Ptr a = MakeInternetNode (); - Ptr udp = a->QueryInterface (IUdp::iid); + InterfaceId iid = InterfaceId::LookupByName ("IUdp"); + Ptr socketFactory = a->QueryInterface (iid); - Ptr sink = udp->CreateSocket (); + Ptr sink = socketFactory->CreateSocket (); sink->Bind (80); - Ptr source = udp->CreateSocket (); + Ptr source = socketFactory->CreateSocket (); source->Connect (Ipv4Address::GetLoopback (), 80); GenerateTraffic (source, 500); diff --git a/src/applications/onoff-application.cc b/src/applications/onoff-application.cc index 6ee5c6763..f7d53d69c 100644 --- a/src/applications/onoff-application.cc +++ b/src/applications/onoff-application.cc @@ -29,7 +29,7 @@ #include "ns3/random-variable.h" #include "ns3/socket.h" #include "ns3/simulator.h" -#include "ns3/i-udp.h" +#include "ns3/i-socket-factory.h" #include "ns3/default-value.h" #include "onoff-application.h" @@ -48,19 +48,22 @@ static IntegerDefaultValue g_defaultSize ("OnOffApplicationPacketSize" OnOffApplication::OnOffApplication(Ptr n, const Ipv4Address rip, - uint16_t rport, + uint16_t rport, + std::string iid, const RandomVariable& ontime, const RandomVariable& offtime) : Application(n), m_cbrRate (g_defaultRate.GetValue ()) { - Construct (n, rip, rport, ontime, offtime, + Construct (n, rip, rport, iid, + ontime, offtime, g_defaultSize.GetValue ()); } OnOffApplication::OnOffApplication(Ptr n, const Ipv4Address rip, - uint16_t rport, + uint16_t rport, + std::string iid, const RandomVariable& ontime, const RandomVariable& offtime, DataRate rate, @@ -68,13 +71,15 @@ OnOffApplication::OnOffApplication(Ptr n, : Application(n), m_cbrRate (rate) { - Construct (n, rip, rport, ontime, offtime, size); + Construct (n, rip, rport, iid, + ontime, offtime, size); } void OnOffApplication::Construct (Ptr n, const Ipv4Address rip, - uint16_t rport, + uint16_t rport, + std::string iid, const RandomVariable& onTime, const RandomVariable& offTime, uint32_t size) @@ -90,6 +95,7 @@ OnOffApplication::Construct (Ptr n, m_lastStartTime = Seconds (0); m_maxBytes = 0xffffffff; m_totBytes = 0; + m_iid = iid; } @@ -134,8 +140,9 @@ void OnOffApplication::StartApplication() // Called at time specified by Star // Create the socket if not already if (!m_socket) { - Ptr udp = GetINode ()->QueryInterface (IUdp::iid); - m_socket = udp->CreateSocket (); + InterfaceId iid = InterfaceId::LookupByName (m_iid); + Ptr socketFactory = GetINode ()->QueryInterface (iid); + m_socket = socketFactory->CreateSocket (); m_socket->Bind (); m_socket->Connect (m_peerIp, m_peerPort); } diff --git a/src/applications/onoff-application.h b/src/applications/onoff-application.h index 6185d1138..b75248289 100644 --- a/src/applications/onoff-application.h +++ b/src/applications/onoff-application.h @@ -60,6 +60,7 @@ public: OnOffApplication(Ptr n, const Ipv4Address rip, uint16_t rport, + std::string iid, const RandomVariable& ontime, const RandomVariable& offtime); @@ -75,6 +76,7 @@ public: OnOffApplication(Ptr n, const Ipv4Address rip, uint16_t rport, + std::string iid, const RandomVariable& ontime, const RandomVariable& offtime, DataRate rate, @@ -110,6 +112,7 @@ private: void Construct (Ptr n, const Ipv4Address rip, uint16_t rport, + std::string iid, const RandomVariable& ontime, const RandomVariable& offtime, uint32_t size); @@ -135,6 +138,7 @@ private: EventId m_startStopEvent; // Event id for next start or stop event EventId m_sendEvent; // Eventid of pending "send packet" event bool m_sending; // True if currently in sending state + std::string m_iid; private: void ScheduleNextTx(); diff --git a/src/core/interface.cc b/src/core/interface.cc index 030c5a031..040d3a903 100644 --- a/src/core/interface.cc +++ b/src/core/interface.cc @@ -38,6 +38,16 @@ InterfaceId::InterfaceId (std::string name) : m_iid (Singleton::Get ()->Allocate (name)) {} +InterfaceId::InterfaceId (uint32_t iid) + : m_iid (iid) +{} + +InterfaceId +InterfaceId::LookupByName (std::string name) +{ + return InterfaceId (Singleton::Get ()->LookupByName (name)); +} + bool operator == (const InterfaceId &a, const InterfaceId &b) { return a.m_iid == b.m_iid; diff --git a/src/core/interface.h b/src/core/interface.h index 307152a9d..d05e976fa 100644 --- a/src/core/interface.h +++ b/src/core/interface.h @@ -32,7 +32,9 @@ class InterfaceId { public: InterfaceId (std::string name); + static InterfaceId LookupByName (std::string); private: + InterfaceId (uint32_t iid); friend bool operator == (const InterfaceId &a, const InterfaceId &b); uint32_t m_iid; }; diff --git a/src/node/i-socket-factory.cc b/src/node/i-socket-factory.cc new file mode 100644 index 000000000..cffb01add --- /dev/null +++ b/src/node/i-socket-factory.cc @@ -0,0 +1,31 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2007 INRIA + * 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: Mathieu Lacage + */ +#include "i-socket-factory.h" + +namespace ns3 { + +const InterfaceId ISocketFactory::iid ("ISocketFactory"); + +ISocketFactory::ISocketFactory () + : Interface (ISocketFactory::iid) +{} + +} // namespace ns3 diff --git a/src/node/i-socket-factory.h b/src/node/i-socket-factory.h new file mode 100644 index 000000000..c23f3c6e0 --- /dev/null +++ b/src/node/i-socket-factory.h @@ -0,0 +1,44 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2007 INRIA + * 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: Mathieu Lacage + */ +#ifndef I_SOCKET_FACTORY_H +#define I_SOCKET_FACTORY_H + +#include "ns3/interface.h" +#include "ns3/ptr.h" + +namespace ns3 { + +class Socket; + +class ISocketFactory : public Interface +{ +public: + static const InterfaceId iid; + + ISocketFactory (); + + virtual Ptr CreateSocket (void) = 0; +}; + +} // namespace ns3 + + +#endif /* I_SOCKET_FACTORY_H */ diff --git a/src/node/i-udp.cc b/src/node/i-udp.cc index 5bf31aa0d..fedd9ac24 100644 --- a/src/node/i-udp.cc +++ b/src/node/i-udp.cc @@ -25,7 +25,8 @@ namespace ns3 { const InterfaceId IUdp::iid ("IUdp"); IUdp::IUdp () - : Interface (IUdp::iid) -{} +{ + AddSelfInterface (IUdp::iid, this); +} } // namespace ns3 diff --git a/src/node/i-udp.h b/src/node/i-udp.h index 8fc071371..15140b5af 100644 --- a/src/node/i-udp.h +++ b/src/node/i-udp.h @@ -21,14 +21,13 @@ #ifndef I_UDP_H #define I_UDP_H -#include "ns3/interface.h" -#include "ns3/ptr.h" +#include "i-socket-factory.h" namespace ns3 { class Socket; -class IUdp : public Interface +class IUdp : public ISocketFactory { public: static const InterfaceId iid;