Move API for socket factory to a Socket::CreateSocket () factory method
authorTom Henderson <tomh@tomh.org>
Mon, 12 May 2008 06:39:00 -0700
changeset 3116 c33b6d2775b7
parent 3115 f9921028c4ab
child 3117 c8160bbb2085
Move API for socket factory to a Socket::CreateSocket () factory method
examples/tcp-large-transfer.cc
examples/wifi-adhoc.cc
examples/wifi-ap.cc
samples/main-simple.cc
src/applications/onoff/onoff-application.cc
src/applications/packet-sink/packet-sink.cc
src/applications/udp-echo/udp-echo-client.cc
src/applications/udp-echo/udp-echo-server.cc
src/node/socket.cc
src/node/socket.h
src/routing/olsr/olsr-agent-impl.cc
--- a/examples/tcp-large-transfer.cc	Sun May 11 22:12:16 2008 -0700
+++ b/examples/tcp-large-transfer.cc	Mon May 12 06:39:00 2008 -0700
@@ -176,9 +176,8 @@
   apps.Start (Seconds (0.0));
 
   // and generate traffic to remote sink.
-  Ptr<SocketFactory> socketFactory = 
-    c0.Get (0)->GetObject<Tcp> ();
-  Ptr<Socket> localSocket = socketFactory->CreateSocket ();
+  //TypeId tid = TypeId::LookupByName ("ns3::Tcp");
+  Ptr<Socket> localSocket = Socket::CreateSocket (c0.Get (0), Tcp::GetTypeId ());
   localSocket->Bind ();
   Simulator::ScheduleNow (&StartFlow, localSocket, nBytes,
                           ipInterfs.GetAddress (1), servPort);
--- a/examples/wifi-adhoc.cc	Sun May 11 22:12:16 2008 -0700
+++ b/examples/wifi-adhoc.cc	Mon May 12 06:39:00 2008 -0700
@@ -102,9 +102,8 @@
 Ptr<Socket>
 Experiment::SetupPacketReceive (Ptr<Node> node)
 {
-  TypeId tid = TypeId::LookupByName ("ns3::PacketSocketFactory");
-  Ptr<SocketFactory> socketFactory = node->GetObject<SocketFactory> (tid);
-  Ptr<Socket> sink = socketFactory->CreateSocket ();
+  TypeId tid = TypeId::LookupByName ("ns3::PacketSocket");
+  Ptr<Socket> sink = Socket::CreateSocket (node, tid);
   sink->Bind ();
   sink->SetRecvCallback (MakeCallback (&Experiment::ReceivePacket, this));
   return sink;
@@ -137,7 +136,7 @@
   socket.SetPhysicalAddress (devices.Get (1)->GetAddress ());
   socket.SetProtocol (1);
 
-  OnOffHelper onoff ("ns3::PacketSocketFactory", Address (socket));
+  OnOffHelper onoff ("ns3::PacketSocket", Address (socket));
   onoff.SetAttribute ("OnTime", RandomVariableValue (ConstantVariable (250)));
   onoff.SetAttribute ("OffTime", RandomVariableValue (ConstantVariable (0)));
   onoff.SetAttribute ("DataRate", DataRateValue (DataRate (60000000)));
--- a/examples/wifi-ap.cc	Sun May 11 22:12:16 2008 -0700
+++ b/examples/wifi-ap.cc	Mon May 12 06:39:00 2008 -0700
@@ -162,7 +162,7 @@
   socket.SetPhysicalAddress (staDevs.Get (1)->GetAddress ());
   socket.SetProtocol (1);
 
-  OnOffHelper onoff ("ns3::PacketSocketFactory", Address (socket));
+  OnOffHelper onoff ("ns3::PacketSocket", Address (socket));
   onoff.SetAttribute ("OnTime", RandomVariableValue (ConstantVariable (42)));
   onoff.SetAttribute ("OffTime", RandomVariableValue (ConstantVariable (0)));
 
--- a/samples/main-simple.cc	Sun May 11 22:12:16 2008 -0700
+++ b/samples/main-simple.cc	Mon May 12 06:39:00 2008 -0700
@@ -49,13 +49,11 @@
 
 
   TypeId tid = TypeId::LookupByName ("ns3::Udp");
-  Ptr<SocketFactory> socketFactory = c.Get (0)->GetObject<SocketFactory> (tid);
-
-  Ptr<Socket> sink = socketFactory->CreateSocket ();
+  Ptr<Socket> sink = Socket::CreateSocket (c.Get (0), tid);
   InetSocketAddress local = InetSocketAddress (Ipv4Address::GetAny (), 80);
   sink->Bind (local);
 
-  Ptr<Socket> source = socketFactory->CreateSocket ();
+  Ptr<Socket> source = Socket::CreateSocket (c.Get (0), tid);
   InetSocketAddress remote = InetSocketAddress (Ipv4Address::GetLoopback (), 80);
   source->Connect (remote);
 
--- a/src/applications/onoff/onoff-application.cc	Sun May 11 22:12:16 2008 -0700
+++ b/src/applications/onoff/onoff-application.cc	Mon May 12 06:39:00 2008 -0700
@@ -130,8 +130,7 @@
   // Create the socket if not already
   if (!m_socket)
     {
-      Ptr<SocketFactory> socketFactory = GetNode ()->GetObject<SocketFactory> (m_tid);
-      m_socket = socketFactory->CreateSocket ();
+      m_socket = Socket::CreateSocket (GetNode(), m_tid);
       m_socket->Bind ();
       m_socket->Connect (m_peer);
     }
--- a/src/applications/packet-sink/packet-sink.cc	Sun May 11 22:12:16 2008 -0700
+++ b/src/applications/packet-sink/packet-sink.cc	Mon May 12 06:39:00 2008 -0700
@@ -80,9 +80,7 @@
   // Create the socket if not already
   if (!m_socket)
     {
-      Ptr<SocketFactory> socketFactory = 
-        GetNode ()->GetObject<SocketFactory> (m_tid);
-      m_socket = socketFactory->CreateSocket ();
+      m_socket = Socket::CreateSocket (GetNode(), m_tid);
       m_socket->Bind (m_local);
       m_socket->Listen (0);
     }
--- a/src/applications/udp-echo/udp-echo-client.cc	Sun May 11 22:12:16 2008 -0700
+++ b/src/applications/udp-echo/udp-echo-client.cc	Mon May 12 06:39:00 2008 -0700
@@ -96,9 +96,7 @@
   if (!m_socket)
     {
       TypeId tid = TypeId::LookupByName ("ns3::Udp");
-      Ptr<SocketFactory> socketFactory = 
-        GetNode ()->GetObject<SocketFactory> (tid);
-      m_socket = socketFactory->CreateSocket ();
+      m_socket = Socket::CreateSocket (GetNode(), tid);
       m_socket->Bind ();
       m_socket->Connect (InetSocketAddress (m_peerAddress, m_peerPort));
     }
--- a/src/applications/udp-echo/udp-echo-server.cc	Sun May 11 22:12:16 2008 -0700
+++ b/src/applications/udp-echo/udp-echo-server.cc	Mon May 12 06:39:00 2008 -0700
@@ -72,9 +72,7 @@
   if (!m_socket)
     {
       TypeId tid = TypeId::LookupByName ("ns3::Udp");
-      Ptr<SocketFactory> socketFactory = 
-        GetNode ()->GetObject<SocketFactory> (tid);
-      m_socket = socketFactory->CreateSocket ();
+      m_socket = Socket::CreateSocket (GetNode(), tid);
       InetSocketAddress local = InetSocketAddress (Ipv4Address::GetAny (), m_port);
       m_socket->Bind (local);
     }
--- a/src/node/socket.cc	Sun May 11 22:12:16 2008 -0700
+++ b/src/node/socket.cc	Mon May 12 06:39:00 2008 -0700
@@ -22,7 +22,9 @@
 
 #include "ns3/log.h"
 #include "ns3/packet.h"
+#include "node.h"
 #include "socket.h"
+#include "socket-factory.h"
 
 NS_LOG_COMPONENT_DEFINE ("Socket");
 
@@ -83,6 +85,16 @@
   NS_LOG_FUNCTION_NOARGS ();
 }
 
+Ptr<Socket> 
+Socket::CreateSocket (Ptr<Node> node, TypeId tid)
+{
+  Ptr<Socket> s;
+  Ptr<SocketFactory> socketFactory = node->GetObject<SocketFactory> (tid);
+  s = socketFactory->CreateSocket ();
+  NS_ASSERT (s != 0);
+  return s;
+}
+
 void 
 Socket::SetCloseCallback (Callback<void,Ptr<Socket> > closeCompleted)
 {
--- a/src/node/socket.h	Sun May 11 22:12:16 2008 -0700
+++ b/src/node/socket.h	Mon May 12 06:39:00 2008 -0700
@@ -99,6 +99,17 @@
   };
 
   /**
+   * This method wraps the creation of sockets that is performed
+   * by a socket factory on a given node based on a TypeId.
+   * 
+   * \return A smart pointer to a newly created socket.
+   * 
+   * \param node The node on which to create the socket
+   * \param tid The TypeId of the socket to create
+   */
+  static Ptr<Socket> CreateSocket (Ptr<Node> node, TypeId tid);
+
+  /**
    * \return the errno associated to the last call which failed in this
    *         socket. Each socket's errno is initialized to zero
    *         when the socket is created.
--- a/src/routing/olsr/olsr-agent-impl.cc	Sun May 11 22:12:16 2008 -0700
+++ b/src/routing/olsr/olsr-agent-impl.cc	Mon May 12 06:39:00 2008 -0700
@@ -258,9 +258,7 @@
   // Add OLSR as routing protocol, with slightly higher priority than
   // static routing.
   m_ipv4->AddRoutingProtocol (m_routingTable, 10);
-
-  Ptr<SocketFactory> socketFactory = GetObject<SocketFactory> (Udp::GetTypeId ());
-
+  
   Ipv4Address loopback ("127.0.0.1");
   for (uint32_t i = 0; i < m_ipv4->GetNInterfaces (); i++)
     {
@@ -281,7 +279,8 @@
         }
 
       // Create a socket to listen only on this interface
-      Ptr<Socket> socket = socketFactory->CreateSocket ();
+      Ptr<Socket> socket = Socket::CreateSocket (GetObject<Node> (), 
+        Udp::GetTypeId()); 
       socket->SetRecvCallback (MakeCallback (&AgentImpl::RecvOlsr,  this));
       if (socket->Bind (InetSocketAddress (addr, OLSR_PORT_NUMBER)))
         {