csma version of install star and example
This commit is contained in:
201
examples/csma-star.cc
Normal file
201
examples/csma-star.cc
Normal file
@@ -0,0 +1,201 @@
|
||||
/* -*- 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/core-module.h"
|
||||
#include "ns3/simulator-module.h"
|
||||
#include "ns3/node-module.h"
|
||||
#include "ns3/helper-module.h"
|
||||
#include "ns3/global-route-manager.h"
|
||||
|
||||
// Network topology (default)
|
||||
//
|
||||
// n2 + + n3 .
|
||||
// | ... |\ /| ... | .
|
||||
// ======= \ / ======= .
|
||||
// CSMA \ / CSMA .
|
||||
// \ / .
|
||||
// n1 +--- n0 ---+ n4 .
|
||||
// | ... | / \ | ... | .
|
||||
// ======= / \ ======= .
|
||||
// CSMA / \ CSMA .
|
||||
// / \ .
|
||||
// n6 + + n5 .
|
||||
// | ... | | ... | .
|
||||
// ======= ======= .
|
||||
// CSMA CSMA .
|
||||
//
|
||||
|
||||
using namespace ns3;
|
||||
|
||||
NS_LOG_COMPONENT_DEFINE ("CsmaStar");
|
||||
|
||||
int
|
||||
main (int argc, char *argv[])
|
||||
{
|
||||
//
|
||||
// Make the random number generators generate reproducible results.
|
||||
//
|
||||
RandomVariable::UseGlobalSeed (1, 1, 2, 3, 5, 8);
|
||||
|
||||
//
|
||||
// Set up some default values for the simulation.
|
||||
//
|
||||
Config::SetDefault ("ns3::OnOffApplication::PacketSize", UintegerValue (137));
|
||||
|
||||
// ??? try and stick 15kb/s into the data rate
|
||||
Config::SetDefault ("ns3::OnOffApplication::DataRate", StringValue ("14kb/s"));
|
||||
|
||||
//
|
||||
// Default number of nodes in the star. Overridable by command line argument.
|
||||
//
|
||||
uint32_t nNodes = 7;
|
||||
|
||||
CommandLine cmd;
|
||||
cmd.AddValue("nNodes", "Number of nodes to place in the star", nNodes);
|
||||
cmd.Parse (argc, argv);
|
||||
|
||||
NS_LOG_INFO ("Create nodes.");
|
||||
NodeContainer hubNode;
|
||||
NodeContainer spokeNodes;
|
||||
hubNode.Create (1);
|
||||
Ptr<Node> hub = hubNode.Get (0);
|
||||
spokeNodes.Create (nNodes - 1);
|
||||
|
||||
CsmaHelper csma;
|
||||
csma.SetChannelAttribute ("DataRate", StringValue ("100Mbps"));
|
||||
csma.SetChannelAttribute ("Delay", StringValue ("1ms"));
|
||||
|
||||
NS_LOG_INFO ("Build star topology.");
|
||||
NetDeviceContainer hubDevices, spokeDevices;
|
||||
csma.InstallStar (hubNode.Get (0), spokeNodes, hubDevices, spokeDevices);
|
||||
|
||||
NodeContainer fillNodes;
|
||||
|
||||
//
|
||||
// Just to be nasy, hang some more nodes off of the CSMA channel for each
|
||||
// spoke, so that there are a total of 16 nodes on each channel. Stash
|
||||
// all of these new devices into a container.
|
||||
//
|
||||
NetDeviceContainer fillDevices;
|
||||
|
||||
uint32_t nFill = 14;
|
||||
for (uint32_t i = 0; i < spokeDevices.GetN (); ++i)
|
||||
{
|
||||
Ptr<Channel> channel = spokeDevices.Get (i)->GetChannel ();
|
||||
Ptr<CsmaChannel> csmaChannel = channel->GetObject<CsmaChannel> ();
|
||||
NodeContainer newNodes;
|
||||
NetDeviceContainer newDevices;
|
||||
newNodes.Create (nFill);
|
||||
fillNodes.Add (newNodes);
|
||||
fillDevices.Add (csma.Install (newNodes, csmaChannel));
|
||||
}
|
||||
|
||||
NS_LOG_INFO ("Install internet stack on all nodes.");
|
||||
InternetStackHelper internet;
|
||||
internet.Install (NodeContainer (hubNode, spokeNodes, fillNodes));
|
||||
|
||||
NS_LOG_INFO ("Assign IP Addresses.");
|
||||
Ipv4AddressHelper address;
|
||||
|
||||
//
|
||||
// Assign IPv4 interfaces and IP addresses to the devices we previously
|
||||
// created. Keep track of the resulting addresses, one for the addresses
|
||||
// of the hub node, and one for addresses on the spoke nodes. Despite the
|
||||
// name of the class, what is visible to clients is really the address.
|
||||
//
|
||||
Ipv4InterfaceContainer hubAddresses;
|
||||
Ipv4InterfaceContainer spokeAddresses;
|
||||
|
||||
for(uint32_t i = 0; i < spokeNodes.GetN (); ++i)
|
||||
{
|
||||
std::ostringstream subnet;
|
||||
subnet << "10.1." << i << ".0";
|
||||
NS_LOG_INFO ("Assign IP Addresses for CSMA subnet " << subnet.str ());
|
||||
address.SetBase (subnet.str ().c_str (), "255.255.255.0");
|
||||
hubAddresses.Add (address.Assign (hubDevices.Get (i)));
|
||||
spokeAddresses.Add (address.Assign (spokeDevices.Get (i)));
|
||||
}
|
||||
|
||||
NS_LOG_INFO ("Create applications.");
|
||||
//
|
||||
// Create a packet sink on the star "hub" to receive packets.
|
||||
//
|
||||
uint16_t port = 50000;
|
||||
Address hubLocalAddress (InetSocketAddress (Ipv4Address::GetAny (), port));
|
||||
PacketSinkHelper packetSinkHelper ("ns3::TcpSocketFactory", hubLocalAddress);
|
||||
ApplicationContainer hubApp = packetSinkHelper.Install (hubNode);
|
||||
hubApp.Start (Seconds (1.0));
|
||||
hubApp.Stop (Seconds (10.0));
|
||||
|
||||
//
|
||||
// Create OnOff applications to send TCP to the hub, one on each spoke node.
|
||||
//
|
||||
OnOffHelper onOffHelper ("ns3::TcpSocketFactory", Address ());
|
||||
onOffHelper.SetAttribute ("OnTime", RandomVariableValue (ConstantVariable (1)));
|
||||
onOffHelper.SetAttribute ("OffTime", RandomVariableValue (ConstantVariable (0)));
|
||||
|
||||
ApplicationContainer spokeApps;
|
||||
|
||||
for (uint32_t i = 0; i < spokeNodes.GetN (); ++i)
|
||||
{
|
||||
AddressValue remoteAddress (InetSocketAddress (hubAddresses.GetAddress (i), port));
|
||||
onOffHelper.SetAttribute ("Remote", remoteAddress);
|
||||
spokeApps.Add (onOffHelper.Install (spokeNodes.Get (i)));
|
||||
}
|
||||
|
||||
spokeApps.Start (Seconds (1.0));
|
||||
spokeApps.Stop (Seconds (10.0));
|
||||
|
||||
//
|
||||
// Because we are evil, we also add OnOff applications to send TCP to the hub
|
||||
// from the fill devices on each CSMA link. The first nFill nodes in the
|
||||
// fillNodes container are on the CSMA network talking to the zeroth device
|
||||
// on the hub node. The next nFill nodes are on the CSMA network talking to
|
||||
// the first device on the hub node, etc. So the ith fillNode is associated
|
||||
// with the hub address found on the (i / nFill)th device on the hub node.
|
||||
//
|
||||
ApplicationContainer fillApps;
|
||||
|
||||
for (uint32_t i = 0; i < fillNodes.GetN (); ++i)
|
||||
{
|
||||
AddressValue remoteAddress (InetSocketAddress (hubAddresses.GetAddress (i / nFill), port));
|
||||
onOffHelper.SetAttribute ("Remote", remoteAddress);
|
||||
fillApps.Add (onOffHelper.Install (fillNodes.Get (i)));
|
||||
}
|
||||
|
||||
fillApps.Start (Seconds (1.0));
|
||||
fillApps.Stop (Seconds (10.0));
|
||||
|
||||
NS_LOG_INFO ("Enable static global routing.");
|
||||
//
|
||||
// Turn on global static routing so we can actually be routed across the star.
|
||||
//
|
||||
GlobalRouteManager::PopulateRoutingTables ();
|
||||
|
||||
NS_LOG_INFO ("Enable pcap tracing.");
|
||||
//
|
||||
// Do pcap tracing on all devices on all nodes.
|
||||
//
|
||||
PointToPointHelper::EnablePcapAll ("csma-star");
|
||||
|
||||
NS_LOG_INFO ("Run Simulation.");
|
||||
Simulator::Run ();
|
||||
Simulator::Destroy ();
|
||||
NS_LOG_INFO ("Done.");
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -76,6 +76,10 @@ def build(bld):
|
||||
['point-to-point', 'internet-stack'])
|
||||
obj.source = 'star.cc'
|
||||
|
||||
obj = bld.create_ns3_program('csma-star',
|
||||
['csma', 'internet-stack'])
|
||||
obj.source = 'csma-star.cc'
|
||||
|
||||
obj = bld.create_ns3_program('wifi-adhoc',
|
||||
['core', 'simulator', 'mobility', 'wifi'])
|
||||
obj.source = 'wifi-adhoc.cc'
|
||||
|
||||
@@ -199,6 +199,19 @@ CsmaHelper::Install (const NodeContainer &c, Ptr<CsmaChannel> channel)
|
||||
return container;
|
||||
}
|
||||
|
||||
void
|
||||
CsmaHelper::InstallStar (Ptr<Node> hub, NodeContainer spokes,
|
||||
NetDeviceContainer& hubDevices, NetDeviceContainer& spokeDevices)
|
||||
{
|
||||
for (uint32_t i = 0; i < spokes.GetN (); ++i)
|
||||
{
|
||||
NodeContainer nodes (hub, spokes.Get (i));
|
||||
NetDeviceContainer nd = Install (nodes);
|
||||
hubDevices.Add (nd.Get (0));
|
||||
spokeDevices.Add (nd.Get (1));
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
CsmaHelper::EnqueueEvent (Ptr<PcapWriter> writer, Ptr<const Packet> packet)
|
||||
{
|
||||
|
||||
@@ -179,6 +179,39 @@ public:
|
||||
*/
|
||||
NetDeviceContainer Install (const NodeContainer &c, Ptr<CsmaChannel> channel);
|
||||
|
||||
/**
|
||||
* \brief Make a star network topology.
|
||||
*
|
||||
* Given a pointer to a node that will become the hub of the star, and a
|
||||
* NodeContainer containing pointers to the nodes that will become the
|
||||
* spokes; we construct CSMA net devices on the hub (corresponding to the
|
||||
* spokes) and store them in the hubDevices NetDeviceContainer. We add a
|
||||
* net device to each spoke node and store them in the spokeDevices
|
||||
* NetDeviceContainer. A CSMA is created for each spoke.
|
||||
*
|
||||
* Usually when one thinks of a star network, one thinks of point-to-point
|
||||
* links. We're just using a single pair of devices on a multi-point-to-point
|
||||
* network "drops" as the link. You are free to add any number of other
|
||||
* devices on the link if you want.
|
||||
*
|
||||
* The ordering of the devices in the hubDevices container is according to
|
||||
* the order of the spokes container -- that is, hubDevices[0] will be the
|
||||
* net device used on the hub that talks to spokes[0]. the container entry
|
||||
* spokeDevices[0] will have the device that hubDevices[0] talks to -- those
|
||||
* two devices are the ones that connect hub to spokes[0].
|
||||
*
|
||||
* \param hub The central node of the star network
|
||||
* \param spokes A NodeContainer of the nodes that will be the spoke (leaf)
|
||||
* nodes
|
||||
* \param hubDevices A NetDeviceContainer that will be filled with pointers
|
||||
* to the point-to-point net devices created on the hub.
|
||||
* \param spokeDevices A NetDeviceContainer that will be filled with pointers
|
||||
* to the point-to-point net devices created on each of
|
||||
* the spokes.
|
||||
*/
|
||||
void InstallStar (Ptr<Node> hub, NodeContainer spokes,
|
||||
NetDeviceContainer& hubDevices, NetDeviceContainer& spokeDevices);
|
||||
|
||||
private:
|
||||
static void RxEvent (Ptr<PcapWriter> writer, Ptr<const Packet> packet);
|
||||
static void EnqueueEvent (Ptr<PcapWriter> writer, Ptr<const Packet> packet);
|
||||
|
||||
Reference in New Issue
Block a user