-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathccn1.cc
396 lines (325 loc) · 12.3 KB
/
ccn1.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/* -*- 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
*
* Author: Tommaso Pecorella <[email protected]>
* Author: Valerio Sartini <[email protected]>
*
* This program conducts a simple experiment: It builds up a topology based on
* either Inet or Orbis trace files. A random node is then chosen, and all the
* other nodes will send a packet to it. The TTL is measured and reported as an histogram.
*
*/
#include <ctime>
#include <sstream>
//#include <sstream>
#include "ns3/ptr.h"
#include "ns3/log.h"
#include "ns3/simulator.h"
#include "ns3/packet.h"
#include "ns3/callback.h"
#include "ns3/string.h"
#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/ndn-app-face.h"
#include "ns3/ndn-interest.h"
#include "ns3/ndn-content-object.h"
#include "ns3/ndnSIM-module.h"
#include "ns3/ndn-app.h"
#include "../src/ndnSIM/apps/cdn-ip-app.h"
//#include "ns3/ndn-producer.h"
#include "../src/ndnSIM/apps/ndn-producer.h"
#include "../src/ndnSIM/model/fib/ndn-fib-entry.h"
#include <boost/foreach.hpp>
#include <boost/ref.hpp>
#include <boost/lexical_cast.hpp>
#include "ns3/topology-read-module.h"
//#include "../../internet/model/rtt-estimator.h"
#include "ns3/inet-topology-reader.h"
#include <list>
#include <ns3/ndnSIM/utils/tracers/ndn-app-delay-tracer.h>
#include <vector>
using namespace std;
using namespace ns3;
using namespace ndn;
NS_LOG_COMPONENT_DEFINE ("ShockExperiment");
//static std::list<unsigned int> data;
//typedef ns3::ndn::InterestHeader InterestHeader;
static void TraceStatus(fib::FaceMetric::Status old, fib::FaceMetric::Status now) {
NS_LOG_INFO("- Change Status from "<<old<<" to "<<now);
}
static void SinkIst(Ptr<const InterestHeader> header, Ptr<App> app, Ptr<Face> face)
{
NS_LOG_INFO("+ Respodning with ContentObject "<< boost::cref(*header));
}
//static void SinkRx (const Ptr<const InterestHeader> &interest, Ptr<Packet> packet)
//{
//
// //InterestHeader ist;
// //p->PeekHeader (ist);
// NS_LOG_DEBUG("TTL: " << interest->GetName() << std::endl);
//}
//
//static void IstRtt(RttEstimator old_rtt, RttEstimator new_rtt){
// cout<<old_rtt<<new_rtt<<endl;
//}
// ----------------------------------------------------------------------
// -- main
// ----------------------------------------------
int main (int argc, char *argv[])
{
LogComponentEnable("ndn.App", LOG_LEVEL_INFO);
LogComponentEnable("ndn.Producer", LOG_LEVEL_FUNCTION);
LogComponentEnable("InetTopologyReader", LOG_LEVEL_INFO);
LogComponentEnable("ndn.fw.Nacks", LOG_LEVEL_DEBUG);
//LogComponentEnable("ndn.cs.Lru", LOG_LEVEL_INFO);
LogComponentEnable("ndn.GlobalRoutingHelper", LOG_LEVEL_DEBUG);
LogComponentEnable("ndn.ConsumerZipfMandelbrot", LOG_LEVEL_INFO);
LogComponentEnable("ndn.Consumer", LOG_LEVEL_INFO);
LogComponentEnable("ShockExperiment", LOG_LEVEL_INFO); //all-logic,function, info, debug, warn, error, uncond
LogComponentEnable("ndn.fib.Entry", LOG_LEVEL_FUNCTION);
LogComponentEnable("ndn.CDNIPApp", LOG_LEVEL_INFO);
std::string format ("Inet");
std::string input ("shock/input/sprint-topology.txt");
stringstream settings;
int seed = 3;
double duration = 3.0;
int producerNum = 2;
std::string csSize = "ZERO";
std::string consumerClass="ConsumerCbr";//consumerCbr
std::string nack = "true";
std::string tracefile = "";
CommandLine cmd;
cmd.AddValue ("format", "Format to use for data input [Orbis|Inet|Rocketfuel].", format);
cmd.AddValue ("input", "Name of the input file.", input);
cmd.AddValue("seed", "seed of RNG", seed);
cmd.AddValue("duration", "simulation time", duration);
cmd.AddValue("producerNum", "number of producers", producerNum);
cmd.AddValue("consumerClass", "class type of consumer", consumerClass);
cmd.AddValue("csSize", "size of CS", csSize);
cmd.AddValue("nack", "enable Nack or not", nack);
cmd.AddValue("trace", "trace file", tracefile);
cmd.Parse (argc, argv);
string ZERO = boost::lexical_cast<string>(std::numeric_limits<uint32_t>::max ());
cmd.Parse (argc, argv);
if (csSize == "Zero" || csSize == "ZERO" ||csSize=="-1") {
csSize = ZERO;
NS_LOG_INFO("2csSize="<<csSize);
}else{
NS_LOG_INFO("csSize="<<csSize);
}
NS_LOG_INFO("consumerClass"<<consumerClass);
if (tracefile == ""){
if (nack == "true") {
if (consumerClass=="CDNIPApp") {
tracefile = "shock/output/Nack-ip.txt";
} else {
tracefile = "shock/output/Nack-enable.txt";
}
}else if (nack=="false"){
tracefile = "shock/output/Nack-disable.txt";
}
}
settings<<"#seed="<<seed<<" duration="<<duration<<" producerNum="<<producerNum<<" csSize="<<csSize<<" consumerClass="<<consumerClass;
settings<<" nack="<<nack<<" trace="<<tracefile;
Config::SetDefault ("ns3::PointToPointNetDevice::DataRate", StringValue ("1Mbps"));
//Config::SetDefault ("ns3::PointToPointChannel::Delay", StringValue ("10ms"));
//Config::SetDefault ("ns3::DropTailQueue::MaxPackets", StringValue("5"));
Config::SetDefault("ns3::ndn::fw::Nacks::EnableNACKs", StringValue(nack));
Ptr<TopologyReader> inFile = 0;
TopologyReaderHelper topoHelp;
NodeContainer nodes;
topoHelp.SetFileName (input);
topoHelp.SetFileType (format);
inFile = topoHelp.GetTopologyReader ();
if (inFile != 0)
{
nodes = inFile->Read ();
}
if (inFile->LinksSize () == 0)
{
NS_LOG_ERROR ("Problems reading the topology file. Failing.");
return -1;
}
int totlinks = inFile->LinksSize ();
int totnodes = nodes.GetN();
NS_LOG_INFO("NodesSize="<<totnodes<<", LinksSize="<<totlinks);
settings<<"\nnodesSize="<<totnodes<<" linksSize="<<totlinks;
//NodeContainer nodes;
TopologyReader::ConstLinksIterator iter;
int i = 0;
PointToPointHelper p2p;
for ( iter = inFile->LinksBegin (); iter != inFile->LinksEnd (); iter++, i++ )
{
p2p.Install(iter->GetFromNode(), iter->GetToNode());
NS_LOG_LOGIC("p2p link "<<i<<" : from "<<iter->GetFromNode()->GetId()<<" to "<<iter->GetToNode()->GetId());
}
ndn::StackHelper ccnxHelper;
ccnxHelper.SetForwardingStrategy ("ns3::ndn::fw::BestRoute::PerOutFaceLimits",
"Limit", "ns3::ndn::Limits::Rate");
ccnxHelper.EnableLimits(true, Seconds(0.1), 1100, 50);
// ccnxHelper.SetForwardingStrategy ("ns3::ndn::fw::BestRoute");
// if (nack == "false") {
// ccnxHelper.SetForwardingStrategy ("ns3::ndn::fw::BestRoute");
// } else {
// ccnxHelper.SetForwardingStrategy ("ns3::ndn::fw::BestRoute::PerOutFaceLimits",
// "Limit", "ns3::ndn::Limits::Rate");
// ccnxHelper.EnableLimits(true, Seconds(0.1), 1100, 50);
// }
ccnxHelper.SetContentStore ("ns3::ndn::cs::Lru", "MaxSize", csSize);
ccnxHelper.InstallAll ();
SeedManager::SetSeed (seed);
ndn::GlobalRoutingHelper ccnxGlobalRoutingHelper;
ccnxGlobalRoutingHelper.InstallAll ();
UniformVariable rng = UniformVariable();
int *nodesFlag = new int[totnodes];
for (int i=0; i<totnodes; i++) {
nodesFlag[i] = 0;
}
int *producersID = new int[producerNum];
for (int i=0; i<producerNum; i++){
producersID[i] = -1;
}
int maxpdc = 0;
for (int i=0; i<producerNum; i++){
//SeedManager::SetSeed (seed+seed*i); // Changes seed from default of 1 to 3
int pdc;
if (i == 0) {
pdc = 9;
} else if (i == 1){
pdc = 20;
} else if (i == 2) {
pdc = 30;
} else if (i == 3) {
pdc = 15;
} else if (i == 4) {
pdc = 16;
} else {
pdc = rng.GetInteger(0, totnodes-1); //[0, totnodes-1]
}
if (nodesFlag[pdc] >0){
i--;
continue;
}
if (pdc > maxpdc){
maxpdc = pdc;
}
nodesFlag[pdc] = 1;
producersID[i] = pdc;
settings<<"\n #producer"<<i<<"="<<pdc;
NS_LOG_LOGIC("add a producer node id="<<pdc);
}//for i
std::string prefix = "/prefix";
Ptr<Node> node;
ndn::AppHelper producerHelper("ns3::ndn::Producer");
ndn::AppHelper consumerHelper("ns3::ndn::"+consumerClass);
//ndn::AppHelper consumerHelper("ns3::ndn::CDNIPApp");
std::string aPrefix;
stringstream strStream;
for (int i=0; i<totnodes; i++){
NS_LOG_DEBUG("i="<<i);
strStream.str("");
node = nodes.Get(i);
NS_LOG_DEBUG("i="<<i<<" node="<<node);
if (consumerClass != "CDNIPApp") {
if (nodesFlag[i] ==0){ //consumer /prefix/producerID
strStream <<i;
aPrefix = prefix + "/"+strStream.str();
NS_LOG_LOGIC("prefix="<<aPrefix<<" attached to node "<<i);
consumerHelper.SetPrefix(aPrefix);
consumerHelper.SetAttribute("Frequency", StringValue("25")); //128 = 1Mbps(/8/1024), 512Kbps(/8/1024)=64
consumerHelper.SetAttribute ("Randomize", StringValue ("exponential"));
consumerHelper.Install(node);
} else if (nodesFlag[i] == 1){ //producer
aPrefix = prefix;
strStream<<i;
Names::Add("producer"+strStream.str(), node);
producerHelper.SetPrefix(aPrefix);
producerHelper.SetAttribute ("PayloadSize", StringValue("1024")); //Bytes
ApplicationContainer appCon = producerHelper.Install(node);
node->GetApplication(0)->TraceConnectWithoutContext("ReceivedInterests", MakeCallback(&SinkIst));
ccnxGlobalRoutingHelper.AddOrigin(prefix, node);
}
} else { //CDN-IP-App
if (nodesFlag[i] == 0) {
// int pdc = producersID[1];
// strStream << pdc;
// aPrefix = prefix + "/" + strStream.str();
std::string providers;
//vector<NameComponents> providers;
for (int j=0; j<producerNum; j++) {
strStream.str("");
int pdc = producersID[j];
strStream << pdc<<"/"<<i;
aPrefix = prefix + "/" + strStream.str();
if (j>0) {
providers += ";";
}
providers += aPrefix;
}
NS_LOG_DEBUG("i"<<i<<" providers: "<<providers);
consumerHelper.SetAttribute("Producers", StringValue(providers));
NS_LOG_DEBUG("prefix="<<aPrefix<<" attached to node "<<i);
//consumerHelper.SetPrefix(aPrefix);
NS_LOG_DEBUG("prefix2="<<aPrefix<<" attached to node "<<i);
consumerHelper.SetAttribute("Frequency", StringValue("25")); //128 = 1Mbps(/8/1024), 512Kbps(/8/1024)=64
NS_LOG_DEBUG("prefix3="<<aPrefix<<" attached to node "<<i);
consumerHelper.SetAttribute ("Randomize", StringValue ("exponential"));
NS_LOG_DEBUG("prefix4="<<aPrefix<<" attached to node "<<i);
consumerHelper.Install(node);
NS_LOG_DEBUG("i="<<i<<" ends");
} else if (nodesFlag[i] == 1){ //producer
strStream <<i;
aPrefix = prefix + "/" + strStream.str();
Names::Add("producer"+strStream.str(), node);
producerHelper.SetPrefix(aPrefix);
producerHelper.SetAttribute ("PayloadSize", StringValue("1024")); //Bytes
ApplicationContainer appCon = producerHelper.Install(node);
//appCon.Get(0)
node->GetApplication(0)->TraceConnectWithoutContext("ReceivedInterests", MakeCallback(&SinkIst));
ccnxGlobalRoutingHelper.AddOrigin(aPrefix, node);
}
}
}
ccnxGlobalRoutingHelper.CalculateRoutes ();
for (int i=0; i<totnodes; i++){
node = nodes.Get(i);
Ptr<ndn::Fib> fib = node->GetObject<ndn::Fib> ();
for (Ptr<ndn::fib::Entry> entry = fib->Begin ();
entry != fib->End ();
entry = fib->Next (entry))
{
BOOST_FOREACH (const ndn::fib::FaceMetric & faceMetric, entry->m_faces)
{
const_cast<ndn::fib::FaceMetric &> (faceMetric).GetStatusTrace ().ConnectWithoutContext (MakeCallback (&TraceStatus));
}
}
}
NS_LOG_INFO(settings.str());
NS_LOG_INFO ("Run Simulation.");
Simulator::Stop(Seconds(duration));
boost::tuple< boost::shared_ptr<std::ostream>, std::list<Ptr<ndn::AppDelayTracer> > >
tracers = ndn::AppDelayTracer::InstallAll (tracefile);
Simulator::Run ();
Simulator::Destroy ();
delete[] producersID;
delete[] nodesFlag;
NS_LOG_INFO(settings.str());
NS_LOG_INFO ("Done.");
return 0;
// end main
}