From a053ff68969ebc2732e7595dc7eecf12e489a330 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Mon, 31 Mar 2008 13:25:01 -0700 Subject: [PATCH] port to helper API --- tutorial/point-to-point-ipv4-topology.cc | 75 ---------- tutorial/point-to-point-ipv4-topology.h | 49 ------ tutorial/tutorial-star.cc | 181 +++++++---------------- tutorial/wscript | 6 +- 4 files changed, 58 insertions(+), 253 deletions(-) delete mode 100644 tutorial/point-to-point-ipv4-topology.cc delete mode 100644 tutorial/point-to-point-ipv4-topology.h diff --git a/tutorial/point-to-point-ipv4-topology.cc b/tutorial/point-to-point-ipv4-topology.cc deleted file mode 100644 index 8f4efd241..000000000 --- a/tutorial/point-to-point-ipv4-topology.cc +++ /dev/null @@ -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 -PointToPointIpv4Topology::CreateChannel ( - const DataRate& bps, - const Time& delay) -{ - return CreateObject ("BitRate", bps, "Delay", delay); -} - - uint32_t -PointToPointIpv4Topology::AddNetDevice ( - Ptr node, - Ptr channel) -{ - NS_ASSERT (channel->GetNDevices () <= 1); - - Ptr nd = - CreateObject ("Address", Mac48Address::Allocate ()); - node->AddDevice (nd); - Ptr q = CreateObject (); - nd->AddQueue(q); - nd->Attach (channel); - - return nd->GetIfIndex (); -} - - uint32_t -PointToPointIpv4Topology::AddAddress ( - Ptr node, - uint32_t netDeviceNumber, - Ipv4Address address, - Ipv4Mask mask) -{ - Ptr nd = node->GetDevice(netDeviceNumber); - Ptr ipv4 = node->GetObject (); - uint32_t ifIndex = ipv4->AddInterface (nd); - - ipv4->SetAddress (ifIndex, address); - ipv4->SetNetworkMask (ifIndex, mask); - ipv4->SetUp (ifIndex); - - return ifIndex; -} - -} // namespace ns3 diff --git a/tutorial/point-to-point-ipv4-topology.h b/tutorial/point-to-point-ipv4-topology.h deleted file mode 100644 index c2a9a4391..000000000 --- a/tutorial/point-to-point-ipv4-topology.h +++ /dev/null @@ -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 CreateChannel ( - const DataRate& dataRate, const Time& delay); - - static uint32_t AddNetDevice( - Ptr node, - Ptr channel); - - static uint32_t AddAddress( - Ptr node, - uint32_t ndIndex, - Ipv4Address address, - Ipv4Mask mask); -}; - -} // namespace ns3 - -#endif // POINT_TO_POINT_IPV4_TOPOLOGY_H - diff --git a/tutorial/tutorial-star.cc b/tutorial/tutorial-star.cc index 3cc4437f0..4c12fd91b 100644 --- a/tutorial/tutorial-star.cc +++ b/tutorial/tutorial-star.cc @@ -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 -#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 @@ using namespace ns3; int main (int argc, char *argv[]) { - LogComponentEnable ("StarSimulation", LOG_LEVEL_INFO); + LogComponentEnable ("StarRoutingSimulation", LOG_LEVEL_INFO); - NS_LOG_INFO ("Star Topology Simulation"); + NS_LOG_INFO ("Star Topology with Routing Simulation"); - Ptr n0 = CreateObject (); - Ptr n1 = CreateObject (); - Ptr n2 = CreateObject (); - Ptr n3 = CreateObject (); - Ptr n4 = CreateObject (); - Ptr n5 = CreateObject (); - Ptr n6 = CreateObject (); + 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 link01 = - PointToPointIpv4Topology::CreateChannel (DataRate (38400), - MilliSeconds (20)); + InternetStackHelper internet; + internet.Build (n); - uint32_t nd01 = PointToPointIpv4Topology::AddNetDevice (n0, - link01); + PointToPointHelper p2p; + p2p.SetChannelParameter ("BitRate", DataRate (38400)); + p2p.SetChannelParameter ("Delay", MilliSeconds (20)); - Ptr link02 = - PointToPointIpv4Topology::CreateChannel (DataRate (38400), - MilliSeconds (20)); + 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); - uint32_t nd02 = PointToPointIpv4Topology::AddNetDevice (n0, - link02); - - Ptr link03 = - PointToPointIpv4Topology::CreateChannel (DataRate (38400), - MilliSeconds (20)); - - uint32_t nd03 = PointToPointIpv4Topology::AddNetDevice (n0, - link03); - - Ptr link04 = - PointToPointIpv4Topology::CreateChannel (DataRate (38400), - MilliSeconds (20)); - - uint32_t nd04 = PointToPointIpv4Topology::AddNetDevice (n0, - link04); - - Ptr link05 = - PointToPointIpv4Topology::CreateChannel (DataRate (38400), - MilliSeconds (20)); - - uint32_t nd05 = PointToPointIpv4Topology::AddNetDevice (n0, - link05); - - Ptr 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"); - - 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 client = - CreateObject ("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 server = - CreateObject ("Port", Uinteger (port)); - n1->AddApplication (server); + 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->Start(Seconds(1.)); - client->Start(Seconds(2.)); - - 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 (); diff --git a/tutorial/wscript b/tutorial/wscript index dc0afd09f..bc40b24fe 100644 --- a/tutorial/wscript +++ b/tutorial/wscript @@ -17,12 +17,10 @@ def build(bld): 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'