port to helper API
authorMathieu Lacage <mathieu.lacage@sophia.inria.fr>
Mon, 31 Mar 2008 13:25:01 -0700
changeset 2817 edb3d28c7f9a
parent 2816 23c5900c3fdc
child 2818 e949c567019c
port to helper API
tutorial/point-to-point-ipv4-topology.cc
tutorial/point-to-point-ipv4-topology.h
tutorial/tutorial-star.cc
tutorial/wscript
--- a/tutorial/point-to-point-ipv4-topology.cc	Mon Mar 31 13:21:22 2008 -0700
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,75 +0,0 @@
-/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/*
- * 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
- */
-
-#include "ns3/assert.h"
-#include "ns3/log.h"
-#include "ns3/nstime.h"
-#include "ns3/internet-node.h"
-#include "ns3/ipv4-address.h"
-#include "ns3/ipv4.h"
-#include "ns3/queue.h"
-#include "ns3/drop-tail-queue.h"
-
-#include "ns3/point-to-point-channel.h"
-#include "ns3/point-to-point-net-device.h"
-#include "point-to-point-ipv4-topology.h"
-
-namespace ns3 {
-
-  Ptr<PointToPointChannel>
-PointToPointIpv4Topology::CreateChannel (
-  const DataRate& bps,
-  const Time& delay)
-{
-  return CreateObject<PointToPointChannel> ("BitRate", bps, "Delay", delay);
-}
-
-  uint32_t
-PointToPointIpv4Topology::AddNetDevice (
-  Ptr<Node> node,
-  Ptr<PointToPointChannel> channel)
-{
-  NS_ASSERT (channel->GetNDevices () <= 1);
-
-  Ptr<PointToPointNetDevice> nd = 
-    CreateObject<PointToPointNetDevice> ("Address", Mac48Address::Allocate ());
-  node->AddDevice (nd);
-  Ptr<Queue> q = CreateObject<DropTailQueue> ();
-  nd->AddQueue(q);
-  nd->Attach (channel);
-
-  return nd->GetIfIndex ();
-}
-
-  uint32_t
-PointToPointIpv4Topology::AddAddress (
-  Ptr<Node> node,
-  uint32_t netDeviceNumber,
-  Ipv4Address address,
-  Ipv4Mask mask)
-{
-  Ptr<NetDevice> nd = node->GetDevice(netDeviceNumber);
-  Ptr<Ipv4> ipv4 = node->GetObject<Ipv4> ();
-  uint32_t ifIndex = ipv4->AddInterface (nd);
-
-  ipv4->SetAddress (ifIndex, address);
-  ipv4->SetNetworkMask (ifIndex, mask);
-  ipv4->SetUp (ifIndex);
-
-  return ifIndex;
-}
-
-} // namespace ns3
--- a/tutorial/point-to-point-ipv4-topology.h	Mon Mar 31 13:21:22 2008 -0700
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,49 +0,0 @@
-/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/*
- * 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
- */
-
-#ifndef POINT_TO_POINT_IPV4_TOPOLOGY_H
-#define POINT_TO_POINT_IPV4_TOPOLOGY_H
-
-#include "ns3/ptr.h"
-
-namespace ns3 {
-
-class PointToPointChannel;
-class Node;
-class Ipv4Address;
-class Ipv4Mask;
-class DataRate;
-
-class PointToPointIpv4Topology {
-public:
-  static Ptr<PointToPointChannel> CreateChannel (
-    const DataRate& dataRate, const Time& delay);
-
-  static uint32_t AddNetDevice(
-    Ptr<Node> node,
-    Ptr<PointToPointChannel> channel);
-
-  static uint32_t AddAddress(
-    Ptr<Node> node,
-    uint32_t ndIndex,
-    Ipv4Address address,
-    Ipv4Mask mask);
-};
-
-} // namespace ns3
-
-#endif // POINT_TO_POINT_IPV4_TOPOLOGY_H
-
--- a/tutorial/tutorial-star.cc	Mon Mar 31 13:21:22 2008 -0700
+++ b/tutorial/tutorial-star.cc	Mon Mar 31 13:25:01 2008 -0700
@@ -14,25 +14,14 @@
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 
-#include "ns3/log.h"
-#include "ns3/ptr.h"
-#include "ns3/internet-node.h"
-#include "ns3/point-to-point-channel.h"
-#include "ns3/mac48-address.h"
-#include "ns3/point-to-point-net-device.h"
-#include "ns3/udp-echo-client.h"
-#include "ns3/udp-echo-server.h"
-#include "ns3/simulator.h"
-#include "ns3/nstime.h"
-#include "ns3/ascii-trace.h"
-#include "ns3/pcap-trace.h"
-#include "ns3/global-route-manager.h"
-#include "ns3/inet-socket-address.h"
-#include "ns3/uinteger.h"
+#include <fstream>
 
-#include "point-to-point-ipv4-topology.h"
+#include "ns3/core-module.h"
+#include "ns3/node-module.h"
+#include "ns3/helper-module.h"
+#include "ns3/simulator-module.h"
 
-NS_LOG_COMPONENT_DEFINE ("StarSimulation");
+NS_LOG_COMPONENT_DEFINE ("StarRoutingSimulation");
 
 using namespace ns3;
 
@@ -49,125 +38,67 @@
 int 
 main (int argc, char *argv[])
 {
-  LogComponentEnable ("StarSimulation", LOG_LEVEL_INFO);
-
-  NS_LOG_INFO ("Star Topology Simulation");
+  LogComponentEnable ("StarRoutingSimulation", LOG_LEVEL_INFO);
 
-  Ptr<Node> n0 = CreateObject<InternetNode> ();
-  Ptr<Node> n1 = CreateObject<InternetNode> ();
-  Ptr<Node> n2 = CreateObject<InternetNode> ();
-  Ptr<Node> n3 = CreateObject<InternetNode> ();
-  Ptr<Node> n4 = CreateObject<InternetNode> ();
-  Ptr<Node> n5 = CreateObject<InternetNode> ();
-  Ptr<Node> n6 = CreateObject<InternetNode> ();
-
-  Ptr<PointToPointChannel> link01 = 
-    PointToPointIpv4Topology::CreateChannel (DataRate (38400), 
-    MilliSeconds (20));
-
-  uint32_t nd01 = PointToPointIpv4Topology::AddNetDevice (n0,
-    link01);
-
-  Ptr<PointToPointChannel> link02 = 
-    PointToPointIpv4Topology::CreateChannel (DataRate (38400), 
-    MilliSeconds (20));
+  NS_LOG_INFO ("Star Topology with Routing Simulation");
 
-  uint32_t nd02 = PointToPointIpv4Topology::AddNetDevice (n0,
-    link02);
-
-  Ptr<PointToPointChannel> link03 = 
-    PointToPointIpv4Topology::CreateChannel (DataRate (38400), 
-    MilliSeconds (20));
-
-  uint32_t nd03 = PointToPointIpv4Topology::AddNetDevice (n0,
-    link03);
+  NodeContainer n;
+  n.Create (7);
+  NodeContainer n01 = NodeContainer (n.Get (0), n.Get (1));
+  NodeContainer n02 = NodeContainer (n.Get (0), n.Get (2));
+  NodeContainer n03 = NodeContainer (n.Get (0), n.Get (3));
+  NodeContainer n04 = NodeContainer (n.Get (0), n.Get (4));
+  NodeContainer n05 = NodeContainer (n.Get (0), n.Get (5));
+  NodeContainer n06 = NodeContainer (n.Get (0), n.Get (6));
 
-  Ptr<PointToPointChannel> link04 = 
-    PointToPointIpv4Topology::CreateChannel (DataRate (38400), 
-    MilliSeconds (20));
-
-  uint32_t nd04 = PointToPointIpv4Topology::AddNetDevice (n0, 
-    link04);
+  InternetStackHelper internet;
+  internet.Build (n);
 
-  Ptr<PointToPointChannel> link05 = 
-    PointToPointIpv4Topology::CreateChannel (DataRate (38400), 
-    MilliSeconds (20));
-
-  uint32_t nd05 = PointToPointIpv4Topology::AddNetDevice (n0,
-    link05);
+  PointToPointHelper p2p;
+  p2p.SetChannelParameter ("BitRate", DataRate (38400));
+  p2p.SetChannelParameter ("Delay", MilliSeconds (20));
 
-  Ptr<PointToPointChannel> link06 = 
-    PointToPointIpv4Topology::CreateChannel (DataRate (38400), 
-    MilliSeconds (20));
-
-  uint32_t nd06 = PointToPointIpv4Topology::AddNetDevice (n0, link06);
-
-  uint32_t nd1 = PointToPointIpv4Topology::AddNetDevice (n1, link01);
-  uint32_t nd2 = PointToPointIpv4Topology::AddNetDevice (n2, link02);
-  uint32_t nd3 = PointToPointIpv4Topology::AddNetDevice (n3, link03);
-  uint32_t nd4 = PointToPointIpv4Topology::AddNetDevice (n4, link04);
-  uint32_t nd5 = PointToPointIpv4Topology::AddNetDevice (n5, link05);
-  uint32_t nd6 = PointToPointIpv4Topology::AddNetDevice (n6, link06);
-
-  PointToPointIpv4Topology::AddAddress (n0, nd01, "10.1.1.1", 
-    "255.255.255.252");
-
-  PointToPointIpv4Topology::AddAddress (n1, nd1, "10.1.1.2", 
-    "255.255.255.252");
-
-  PointToPointIpv4Topology::AddAddress (n0, nd02, "10.1.2.1", 
-    "255.255.255.252");
+  NetDeviceContainer d01 = p2p.Build (n01);
+  NetDeviceContainer d02 = p2p.Build (n02);
+  NetDeviceContainer d03 = p2p.Build (n03);
+  NetDeviceContainer d04 = p2p.Build (n04);
+  NetDeviceContainer d05 = p2p.Build (n05);
+  NetDeviceContainer d06 = p2p.Build (n06);
 
-  PointToPointIpv4Topology::AddAddress (n2, nd2, "10.1.2.2", 
-    "255.255.255.252");
-
-  PointToPointIpv4Topology::AddAddress (n0, nd03, "10.1.3.1", 
-    "255.255.255.252");
-
-  PointToPointIpv4Topology::AddAddress (n3, nd3, "10.1.2.2", 
-    "255.255.255.252");
-
-  PointToPointIpv4Topology::AddAddress (n0, nd04, "10.1.4.1", 
-    "255.255.255.252");
-
-  PointToPointIpv4Topology::AddAddress (n4, nd4, "10.1.4.2", 
-    "255.255.255.252");
-
-  PointToPointIpv4Topology::AddAddress (n0, nd05, "10.1.5.1", 
-    "255.255.255.252");
-
-  PointToPointIpv4Topology::AddAddress (n5, nd5, "10.1.5.2", 
-    "255.255.255.252");
-
-  PointToPointIpv4Topology::AddAddress (n0, nd06, "10.1.6.1", 
-    "255.255.255.252");
-
-  PointToPointIpv4Topology::AddAddress (n6, nd6, "10.1.6.2", 
-    "255.255.255.252");
+  Ipv4AddressHelper ipv4;
+  ipv4.SetBase ("10.1.1.0", "255.255.255.252");
+  Ipv4InterfaceContainer i01 = ipv4.Allocate (d01);
+  ipv4.SetBase ("10.1.2.0", "255.255.255.252");
+  Ipv4InterfaceContainer i02 = ipv4.Allocate (d02);
+  ipv4.SetBase ("10.1.3.0", "255.255.255.252");
+  Ipv4InterfaceContainer i03 = ipv4.Allocate (d03);
+  ipv4.SetBase ("10.1.4.0", "255.255.255.252");
+  Ipv4InterfaceContainer i04 = ipv4.Allocate (d04);
+  ipv4.SetBase ("10.1.5.0", "255.255.255.252");
+  Ipv4InterfaceContainer i05 = ipv4.Allocate (d05);
+  ipv4.SetBase ("10.1.6.0", "255.255.255.252");
+  Ipv4InterfaceContainer i06 = ipv4.Allocate (d06);
 
   uint16_t port = 7;
 
-  Ptr<UdpEchoClient> client = 
-    CreateObject<UdpEchoClient> ("RemoteIpv4", Ipv4Address ("10.1.1.2"),
-                                 "RemotePort", Uinteger (port), 
-                                 "MaxPackets", Uinteger (1), 
-                                 "Interval", Seconds(1.), 
-                                 "PacketSize", Uinteger (1024));
-  n0->AddApplication (client);
+  UdpEchoServerHelper server;
+  server.SetPort (port);
+  ApplicationContainer apps = server.Build (n.Get (1));
+  apps.Start (Seconds (1.0));
+  apps.Stop (Seconds (10.0));
 
-  Ptr<UdpEchoServer> server = 
-    CreateObject<UdpEchoServer> ("Port", Uinteger (port));
-  n1->AddApplication (server);
-
-  server->Start(Seconds(1.));
-  client->Start(Seconds(2.));
+  UdpEchoClientHelper client;
+  client.SetRemote (i01.GetAddress (1), port);
+  client.SetAppAttribute ("MaxPackets", Uinteger (1));
+  client.SetAppAttribute ("Interval", Seconds (1.0));
+  client.SetAppAttribute ("PacketSize", Uinteger (1024));
+  apps = client.Build (n.Get (0));
+  apps.Start (Seconds (2.0));
+  apps.Stop (Seconds (10.0));
 
-  server->Stop (Seconds(10.));
-  client->Stop (Seconds(10.));
-
-  AsciiTrace asciitrace ("tutorial.tr");
-  asciitrace.TraceAllQueues ();
-  asciitrace.TraceAllNetDeviceRx ();
+  std::ofstream ascii;
+  ascii.open ("tutorial.tr");
+  PointToPointHelper::EnableAscii (ascii);
 
   Simulator::Run ();
   Simulator::Destroy ();
--- a/tutorial/wscript	Mon Mar 31 13:21:22 2008 -0700
+++ b/tutorial/wscript	Mon Mar 31 13:25:01 2008 -0700
@@ -17,12 +17,10 @@
     obj.source = 'tutorial-point-to-point.cc'
 
     obj = bld.create_ns3_program('tutorial-star', ['internet-node', 'point-to-point'])
-    obj.source = ['tutorial-star.cc',
-                  'point-to-point-ipv4-topology.cc']
+    obj.source = ['tutorial-star.cc']
 
     obj = bld.create_ns3_program('tutorial-star-routing', ['internet-node', 'point-to-point'])
-    obj.source = ['tutorial-star-routing.cc',
-                  'point-to-point-ipv4-topology.cc']
+    obj.source = ['tutorial-star-routing.cc']
 
     obj = bld.create_ns3_program('tutorial-linear-dumbbell', ['internet-node', 'point-to-point'])
     obj.source = 'tutorial-linear-dumbbell.cc'