-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstar.cc
executable file
·143 lines (118 loc) · 4.99 KB
/
star.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include "ns3/netanim-module.h"
#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/netanim-module.h"
#include "ns3/internet-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/applications-module.h"
#include "ns3/point-to-point-layout-module.h"
#include "ns3/mobility-module.h"
// Network topology (default)
//
// n2 n3 n4 .
// \ | / .
// \|/ .
// n1--- n0---n5 .
// /|\ .
// / | \ .
// n8 n7 n6 .
//
using namespace ns3;
NS_LOG_COMPONENT_DEFINE ("Star");
int
main (int argc, char *argv[])
{
//
// 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 nSpokes = 8;
LogComponentEnable("Star", LOG_LEVEL_INFO);
CommandLine cmd;
cmd.AddValue ("nSpokes", "Number of nodes to place in the star", nSpokes);
cmd.Parse (argc, argv);
NS_LOG_LOGIC ("Build star topology.");
PointToPointHelper pointToPoint;
pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));
PointToPointStarHelper star (nSpokes, pointToPoint);
NS_LOG_LOGIC ("Install internet stack on all nodes.");
InternetStackHelper internet;
star.InstallStack (internet);
NS_LOG_LOGIC ("Assign IP Addresses.");
star.AssignIpv4Addresses (Ipv4AddressHelper ("10.1.1.0", "255.255.255.0"));
NS_LOG_LOGIC ("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 (star.GetHub ());
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", StringValue ("ns3::ConstantRandomVariable[Constant=1]"));
onOffHelper.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0]"));
ApplicationContainer spokeApps;
std::cout<<"Node counts: "<<star.SpokeCount()<<std::endl;
for (uint32_t i = 0; i < star.SpokeCount (); ++i)
{
AddressValue remoteAddress (InetSocketAddress (star.GetHubIpv4Address (i), port));
onOffHelper.SetAttribute ("Remote", remoteAddress);
spokeApps.Add (onOffHelper.Install (star.GetSpokeNode (i)));
}
spokeApps.Start (Seconds (1.0));
spokeApps.Stop (Seconds (10.0));
NS_LOG_LOGIC ("Enable static global routing.");
//
// Turn on global static routing so we can actually be routed across the star.
//
Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
NS_LOG_LOGIC ("Enable pcap tracing.");
//
// Do pcap tracing on all point-to-point devices on all nodes.
//
pointToPoint.EnablePcapAll ("star");
MobilityHelper mobility;
//mobility.SetPositionAllocator ("ns3::GridPositionAllocator",
// "MinX", DoubleValue (0.0),
// "MinY", DoubleValue (0.0),
// "DeltaX", DoubleValue (5.0),
// "DeltaY", DoubleValue (10.0),
// "GridWidth", UintegerValue (3),
// "LayoutType", StringValue ("RowFirst"));
mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
mobility.Install(star.GetSpokeNode(0));
mobility.Install(star.GetSpokeNode(1));
mobility.Install(star.GetSpokeNode(2));
mobility.Install(star.GetSpokeNode(3));
mobility.Install(star.GetSpokeNode(4));
mobility.Install(star.GetSpokeNode(5));
mobility.Install(star.GetSpokeNode(6));
mobility.Install(star.GetSpokeNode(7));
mobility.Install(star.GetHub());
AnimationInterface anim("star.xml");
anim.SetConstantPosition(star.GetSpokeNode(0), 0,-1,0);
anim.SetConstantPosition(star.GetSpokeNode(1), -1,-1,0);
anim.SetConstantPosition(star.GetSpokeNode(2), -1,0,0);
anim.SetConstantPosition(star.GetSpokeNode(3), -1,1,0);
anim.SetConstantPosition(star.GetSpokeNode(4), 0,1,0);
anim.SetConstantPosition(star.GetSpokeNode(5), 1,1,0);
anim.SetConstantPosition(star.GetSpokeNode(6), 1,0,0);
anim.SetConstantPosition(star.GetSpokeNode(7), 1,-1,0);
anim.SetConstantPosition(star.GetHub(), 0,0,0);
NS_LOG_LOGIC ("Run Simulation.");
Simulator::Run ();
Simulator::Destroy ();
NS_LOG_LOGIC ("Done.");
return 0;
}