blob: f7284a8784547d130c2ff20d2497e556268aa952 [file] [log] [blame]
Vince Lehmanb8b18062015-07-14 13:07:22 -05001# -*- Mode:python; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2#
3# Copyright (C) 2015 The University of Memphis,
4# Arizona Board of Regents,
5# Regents of the University of California.
6#
7# This file is part of Mini-NDN.
8# See AUTHORS.md for a complete list of Mini-NDN authors and contributors.
9#
10# Mini-NDN is free software: you can redistribute it and/or modify
11# it under the terms of the GNU General Public License as published by
12# the Free Software Foundation, either version 3 of the License, or
13# (at your option) any later version.
14#
15# Mini-NDN is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18# GNU General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License
21# along with Mini-NDN, e.g., in COPYING.md file.
22# If not, see <http://www.gnu.org/licenses/>.
ashu34c3ee02015-03-25 14:41:14 -050023
24import time
25import sys
Ashlesh Gawanded9c9e522015-10-15 16:40:12 -050026from itertools import cycle
ashu34c3ee02015-03-25 14:41:14 -050027
Vince Lehman3b8bc652015-06-18 15:01:47 -050028from ndn import ExperimentManager
29
ashu34c3ee02015-03-25 14:41:14 -050030class Experiment:
31
Vince Lehman3b8bc652015-06-18 15:01:47 -050032 def __init__(self, args):
33 self.net = args["net"]
34 self.nodes = args["nodes"]
35 self.convergenceTime = args["ctime"]
36 self.nPings = args["nPings"]
37 self.strategy = args["strategy"]
Ashlesh Gawanded9c9e522015-10-15 16:40:12 -050038 self.pctTraffic = float(args["pctTraffic"])
39
40 # Used to restart pings on the recovered node if any
41 self.pingedDict = {}
42
ashu34c3ee02015-03-25 14:41:14 -050043
44 def start(self):
45 self.setup()
46 self.run()
47
48 def setup(self):
49 for host in self.net.hosts:
50 # Set strategy
Ashlesh Gawandee144ceb2016-11-14 13:56:24 -060051 host.nfd.setStrategy("/ndn/", self.strategy)
ashu34c3ee02015-03-25 14:41:14 -050052
53 # Start ping server
Ashlesh Gawandee144ceb2016-11-14 13:56:24 -060054 host.cmd("ndnpingserver /ndn/" + str(host) + "-site/" + str(host) + " > ping-server &")
ashu34c3ee02015-03-25 14:41:14 -050055
56 # Create folder to store ping data
57 host.cmd("mkdir ping-data")
58
59 # Wait for convergence time period
60 print "Waiting " + str(self.convergenceTime) + " seconds for convergence..."
61 time.sleep(self.convergenceTime)
62 print "...done"
63
64 # To check whether all the nodes of NLSR have converged
65 didNlsrConverge = True
66
67 # Checking for convergence
68 for host in self.net.hosts:
Ashlesh Gawandef932a182016-12-19 23:45:26 -060069 statusRouter = host.cmd("nfdc fib list | grep site/%C1.Router/cs/")
70 statusPrefix = host.cmd("nfdc fib list | grep ndn | grep site | grep -v Router")
ashu34c3ee02015-03-25 14:41:14 -050071 didNodeConverge = True
72 for node in self.nodes.split(","):
Ashlesh Gawandee144ceb2016-11-14 13:56:24 -060073 if ( ("/ndn/" + node + "-site/%C1.Router/cs/" + node) not in statusRouter or
74 str(host) != node and ("/ndn/" + node + "-site/" + node) not in statusPrefix ):
75 didNodeConverge = False
76 didNlsrConverge = False
ashu34c3ee02015-03-25 14:41:14 -050077
78 host.cmd("echo " + str(didNodeConverge) + " > convergence-result &")
79
80 if didNlsrConverge:
81 print("NLSR has successfully converged.")
82 else:
83 print("NLSR has not converged. Exiting...")
Ashlesh Gawande3807c1b2016-08-05 16:27:02 -050084 self.net.stop()
ashu34c3ee02015-03-25 14:41:14 -050085 sys.exit(1)
86
Vince Lehmancb20c542015-05-12 14:04:47 -050087 def ping(self, source, dest, nPings):
88 # Use "&" to run in background and perform parallel pings
89 print "Scheduling ping(s) from %s to %s" % (source.name, dest.name)
Ashlesh Gawandee144ceb2016-11-14 13:56:24 -060090 source.cmd("ndnping -t -c "+ str(nPings) + " /ndn/" + dest.name + "-site/" + dest.name + " >> ping-data/" + dest.name + ".txt &")
Vince Lehmancb20c542015-05-12 14:04:47 -050091 time.sleep(0.2)
92
ashu34c3ee02015-03-25 14:41:14 -050093 def startPings(self):
94 for host in self.net.hosts:
95 for other in self.net.hosts:
96 # Do not ping self
97 if host.name != other.name:
Vince Lehmancb20c542015-05-12 14:04:47 -050098 self.ping(host, other, self.nPings)
ashu34c3ee02015-03-25 14:41:14 -050099
Vince Lehmand96eed32015-10-22 13:57:27 -0500100 def failNode(self, host):
101 print("Bringing %s down" % host.name)
102 host.nfd.stop()
103
104 def recoverNode(self, host):
105 print("Bringing %s up" % host.name)
106 host.nfd.start()
107 host.nlsr.start()
Ashlesh Gawandee144ceb2016-11-14 13:56:24 -0600108 host.nfd.setStrategy("/ndn/", self.strategy)
109 host.cmd("ndnpingserver /ndn/" + str(host) + "-site/" + str(host) + " > ping-server &")
Vince Lehmand96eed32015-10-22 13:57:27 -0500110
Ashlesh Gawanded9c9e522015-10-15 16:40:12 -0500111 def startPctPings(self):
112 nNodesToPing = int(round(len(self.net.hosts)*self.pctTraffic))
113 print "Each node will ping %d node(s)" % nNodesToPing
114 # Temporarily store all the nodes being pinged by a particular node
115 nodesPingedList = []
116
117 for host in self.net.hosts:
118 # Create a circular list
119 pool = cycle(self.net.hosts)
120
121 # Move iterator to current node
122 next(x for x in pool if host.name == x.name)
123
124 # Track number of nodes to ping scheduled for this node
125 nNodesScheduled = 0
126
127 while nNodesScheduled < nNodesToPing:
128 other = pool.next()
129
130 # Do not ping self
131 if host.name != other.name:
132 self.ping(host, other, self.nPings)
133 nodesPingedList.append(other)
134
135 # Always increment because in 100% case a node should not ping itself
136 nNodesScheduled = nNodesScheduled + 1
137
138 self.pingedDict[host] = nodesPingedList
139 nodesPingedList = []
140
Vince Lehman3b8bc652015-06-18 15:01:47 -0500141 @staticmethod
142 def register(name, experimentClass):
143 ExperimentManager.register(name, experimentClass)