-
Notifications
You must be signed in to change notification settings - Fork 0
/
topo-simulation-bycsw.cc
199 lines (167 loc) · 5.59 KB
/
topo-simulation-bycsw.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include <iostream>
#include <fstream>
#include <string>
#include <cassert>
#include <algorithm>
#include <vector>
#include <random>
#include <ctime>
#include <list>
#include <sstream>
#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/internet-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/applications-module.h"
#include "ns3/ipv4-nix-vector-helper.h"
#include "ns3/topology-read-module.h"
using namespace ns3;
std::vector<int> random_pair()
{
srand(time(NULL));
int num_of_pair = (rand() % 25)+ 1;
std::vector<int> tmp;
for(int i = 0 ; i < 50 ; ++i)
{
tmp.push_back(i);
}
random_shuffle(tmp.begin(),tmp.end());
std::vector<int> res;
for(int i = 0 ; i < num_of_pair*2 ; ++i)
{
res.push_back(tmp[i]);
}
return res;
}
/*
static void CalculateDelay (Ptr<const Packet>p,const Address &address)
{
//static float k = 0;
//k++;
//static float m = -1;
//static float n = 0;
//n += (p->GetUid() - m)/2-1;
DelayJitterEstimation delayJitter;
delayJitter.RecordRx(p);
Time t = delayJitter.GetLastDelay();
std::cout << Simulator::Now ().GetSeconds () << "\t" << t.GetMilliSeconds() << std::endl;
//m = p->GetUid();
}
*/
NS_LOG_COMPONENT_DEFINE ("Topology Simulation.");
int main (int argc, char *argv[])
{
std::string format("Inet");
std::string input("/home/chenshenwei/Desktop/topo_inet.txt");
CommandLine cmd;
cmd.Parse (argc, argv);
LogComponentEnable ("UdpClient", LOG_LEVEL_INFO);
LogComponentEnable ("UdpServer", LOG_LEVEL_INFO);
//LogComponentEnable ("Topology Simulation.", LOG_LEVEL_INFO);
// read topology data
NS_LOG_INFO ("Read topology data.");
TopologyReaderHelper topoHelp;
topoHelp.SetFileName (input);
topoHelp.SetFileType (format);
Ptr<TopologyReader> inFile = topoHelp.GetTopologyReader ();
NodeContainer nodes;
if (inFile != 0)
{
nodes = inFile->Read ();
}
if (inFile->LinksSize () == 0)
{
NS_LOG_ERROR ("Problems reading the topology file. Failing.");
return -1;
}
// Create nodes and network stacks
NS_LOG_INFO ("Installing internet stack.");
InternetStackHelper internet;
// Setup NixVector Routing
Ipv4NixVectorHelper nixRouting;
internet.SetRoutingHelper(nixRouting);
internet.Install(nodes);
int totlinks = inFile->LinksSize ();
NS_LOG_INFO ("Creating node containers");
NodeContainer* nc = new NodeContainer[totlinks];
TopologyReader::ConstLinksIterator iter;
int i = 0;
for ( iter = inFile->LinksBegin (); iter != inFile->LinksEnd (); iter++, i++ )
{
nc[i] = NodeContainer (iter->GetFromNode (), iter->GetToNode ());
}
//NodeContainer* nc_host = new NodeContainer[50];
//for ( int j = 0 ; j < 50 ; ++j)
//{
//nc_host[j] = nc[j];
//}
NS_LOG_INFO ("creating net device containers");
NetDeviceContainer* ndc = new NetDeviceContainer[totlinks];
PointToPointHelper p2p;
for (int i = 0; i < totlinks; i++)
{
p2p.SetChannelAttribute ("Delay", StringValue ("2ms"));
p2p.SetDeviceAttribute ("DataRate", StringValue ("10Mbps"));
ndc[i] = p2p.Install (nc[i]);
}
NS_LOG_INFO ("Assign IP Addresses.");
Ipv4AddressHelper address;
address.SetBase ("10.0.0.0", "255.255.255.0");
Ipv4InterfaceContainer* ipic = new Ipv4InterfaceContainer[totlinks];
for (int i = 0; i < totlinks; i++)
{
ipic[i] = address.Assign (ndc[i]);
address.NewNetwork ();
}
NodeContainer hostNodes;
for ( unsigned int i = 0; i < 50; i++ )
{
Ptr<Node> hostNode = nodes.Get(i);
hostNodes.Add (hostNode);
}
//install applications onto the devices
NS_LOG_INFO ("Create applications.");
UdpServerHelper server(9);
ApplicationContainer serverContainer = server.Install(hostNodes);
serverContainer.Start(Seconds(0.0));
serverContainer.Stop(Seconds(30.0));
uint32_t MaxPacketSize = 210;
Time interPacketInterval = Seconds (0.00375);
//uint32_t maxPacketCount = 26;
UdpClientHelper client (ipic[0].GetAddress(0), 9);
//client.SetAttribute ("MaxPackets", UintegerValue (maxPacketCount));
client.SetAttribute ("Interval", TimeValue (interPacketInterval));
client.SetAttribute ("PacketSize", UintegerValue (MaxPacketSize));
ApplicationContainer clientContainer;
//std::cout<<1;
double current_time;
std::vector<int> host_pair;
int pair_num;
int current_pair;
for(current_time = 0.0 ; current_time < 30 ; current_time+=0.1)
{
host_pair = random_pair();
pair_num = host_pair.size()/2;
for(current_pair = 0 ; current_pair < pair_num ; ++current_pair)
{
client.SetAttribute("RemoteAddress", AddressValue(ipic[host_pair[2*current_pair+1]].GetAddress(0)));
clientContainer = client.Install(hostNodes.Get(host_pair[2*current_pair]));
clientContainer.Start(Seconds(current_time));
clientContainer.Stop(Seconds(current_time+0.1));
}
}
//for(int i = 0 ; i < 50 ; ++i){
//serverContainer.Get(i)->TraceConnectWithoutContext("PhyRxEnd", MakeCallback(&CalculateDelay));
//}
//AsciiTraceHelper ascii;
//p2p.EnableAsciiAll (ascii.CreateFileStream ("topo-simulation.tr"));
//p2p.EnablePcapAll ("topo-simulation");
NS_LOG_INFO ("Run Simulation.");
Simulator::Run ();
Simulator::Destroy ();
delete[] ipic;
delete[] ndc;
delete[] nc;
NS_LOG_INFO ("Done.");
return 0;
}