From 2425ad39ef99157cc05df51bba4504747d7c0eeb Mon Sep 17 00:00:00 2001 From: Craig Dowell Date: Tue, 9 Oct 2007 21:54:03 -0700 Subject: [PATCH] bus network --- tutorial/bus-network.cc | 65 ++++++++++++++++++++++++++++++++ tutorial/bus-network.h | 53 ++++++++++++++++++++++++++ tutorial/tutorial-bus-network.cc | 63 +++++++++++++++++++++++++++++++ tutorial/wscript | 6 +++ 4 files changed, 187 insertions(+) create mode 100644 tutorial/bus-network.cc create mode 100644 tutorial/bus-network.h create mode 100644 tutorial/tutorial-bus-network.cc diff --git a/tutorial/bus-network.cc b/tutorial/bus-network.cc new file mode 100644 index 000000000..7f6051340 --- /dev/null +++ b/tutorial/bus-network.cc @@ -0,0 +1,65 @@ +/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2007 University of Washington + * + * 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/mac48-address.h" +#include "ns3/csma-net-device.h" +#include "ns3/csma-topology.h" +#include "ns3/csma-ipv4-topology.h" + +#include "bus-network.h" +#include "ipv4-address-generator.h" + +namespace ns3 { + +BusNetwork::BusNetwork ( + Ipv4Mask mask, + Ipv4Address network, + Ipv4Address startAddress, + DataRate bps, + Time delay, + uint32_t n) +{ + Ipv4AddressGenerator::SeedNetwork (mask, network); + Ipv4AddressGenerator::SeedAddress (mask, startAddress); + + m_channel = CsmaTopology::CreateCsmaChannel (bps, delay); + + for (uint32_t i = 0; i < n; ++i) + { + Ptr node = Create (); + uint32_t nd = CsmaIpv4Topology::AddIpv4CsmaNetDevice (node, m_channel, + Mac48Address::Allocate ()); + Ipv4Address address = Ipv4AddressGenerator::AllocateAddress (mask, + network); + CsmaIpv4Topology::AddIpv4Address (node, nd, address, mask); + m_nodes.push_back (node); + } +} + +BusNetwork::~BusNetwork () +{ + m_nodes.erase (m_nodes.begin (), m_nodes.end ()); +} + + Ptr +BusNetwork::GetNode (uint32_t n) +{ + return m_nodes[n]; +} + +}; // namespace ns3 diff --git a/tutorial/bus-network.h b/tutorial/bus-network.h new file mode 100644 index 000000000..b69b6a6d5 --- /dev/null +++ b/tutorial/bus-network.h @@ -0,0 +1,53 @@ +/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2007 University of Washington + * + * 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 BUS_NETWORK_H +#define BUS_NETWORK_H + +#include +#include "ns3/ptr.h" +#include "ns3/node.h" +#include "ns3/ipv4-address.h" +#include "ns3/data-rate.h" +#include "ns3/csma-channel.h" + +namespace ns3 { + +class BusNetwork +{ +public: + BusNetwork ( + Ipv4Mask mask, + Ipv4Address network, + Ipv4Address startAddress, + DataRate bps, + Time delay, + uint32_t n); + + virtual ~BusNetwork (); + + Ptr GetNode (uint32_t n); + +private: + std::vector > m_nodes; + Ptr m_channel; +}; + +}; // namespace ns3 + +#endif /* BUS_NETWORK_H */ diff --git a/tutorial/tutorial-bus-network.cc b/tutorial/tutorial-bus-network.cc new file mode 100644 index 000000000..0bb3ef9d3 --- /dev/null +++ b/tutorial/tutorial-bus-network.cc @@ -0,0 +1,63 @@ + +/* + * 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/log.h" +#include "ns3/ipv4-address.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 "bus-network.h" + +NS_LOG_COMPONENT_DEFINE ("BusNetworkSimulation"); + +using namespace ns3; + +int +main (int argc, char *argv[]) +{ + LogComponentEnable ("BusNetworkSimulation", LOG_LEVEL_ALL); + LogComponentEnable ("BusNetwork", LOG_LEVEL_ALL); + + NS_LOG_INFO ("Bus Network Simulation"); + + BusNetwork busNetwork (Ipv4Mask ("255.255.0.0"), Ipv4Address ("10.1.0.0"), + Ipv4Address ("0.0.0.0"), DataRate(10000000), MilliSeconds(20), 10); + + uint32_t port = 7; + + Ptr n0 = busNetwork.GetNode (0); + Ptr client = Create (n0, "10.1.0.1", port, + 1, Seconds(1.), 1024); + + Ptr n1 = busNetwork.GetNode (1); + Ptr server = Create (n1, port); + + server->Start(Seconds(1.)); + client->Start(Seconds(2.)); + + server->Stop (Seconds(10.)); + client->Stop (Seconds(10.)); + + AsciiTrace asciitrace ("tutorial.tr"); + asciitrace.TraceAllQueues (); + asciitrace.TraceAllNetDeviceRx (); + + Simulator::Run (); + Simulator::Destroy (); +} diff --git a/tutorial/wscript b/tutorial/wscript index 7e51208e3..b51f21c46 100644 --- a/tutorial/wscript +++ b/tutorial/wscript @@ -36,3 +36,9 @@ def build(bld): obj = bld.create_ns3_program('testipv4', ['core']) obj.source = ['testipv4.cc', 'ipv4-address-generator.cc'] + + obj = bld.create_ns3_program('tutorial-bus-network', + ['core']) + obj.source = ['tutorial-bus-network.cc', + 'bus-network.cc', + 'ipv4-address-generator.cc']