blob: 385ff2a58b5db2727676fdf975ae8d509b2d3582 [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
51 host.nfd.setStrategy("/ndn/edu", self.strategy)
52
53 # Start ping server
54 host.cmd("ndnpingserver /ndn/edu/" + str(host) + " > ping-server &")
55
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:
69 statusRouter = host.cmd("nfd-status -b | grep /ndn/edu/%C1.Router/cs/")
70 statusPrefix = host.cmd("nfd-status -b | grep /ndn/edu/")
71 didNodeConverge = True
72 for node in self.nodes.split(","):
73 if ("/ndn/edu/%C1.Router/cs/" + node) not in statusRouter:
74 didNodeConverge = False
75 didNlsrConverge = False
76 if str(host) != node and ("/ndn/edu/" + node) not in statusPrefix:
77 didNodeConverge = False
78 didNlsrConverge = False
79
80 host.cmd("echo " + str(didNodeConverge) + " > convergence-result &")
81
82 if didNlsrConverge:
83 print("NLSR has successfully converged.")
84 else:
85 print("NLSR has not converged. Exiting...")
86 for host in self.net.hosts:
87 host.nfd.stop()
88 sys.exit(1)
89
Vince Lehmancb20c542015-05-12 14:04:47 -050090 def ping(self, source, dest, nPings):
91 # Use "&" to run in background and perform parallel pings
92 print "Scheduling ping(s) from %s to %s" % (source.name, dest.name)
93 source.cmd("ndnping -t -c "+ str(nPings) + " /ndn/edu/" + dest.name + " >> ping-data/" + dest.name + ".txt &")
94 time.sleep(0.2)
95
ashu34c3ee02015-03-25 14:41:14 -050096 def startPings(self):
97 for host in self.net.hosts:
98 for other in self.net.hosts:
99 # Do not ping self
100 if host.name != other.name:
Vince Lehmancb20c542015-05-12 14:04:47 -0500101 self.ping(host, other, self.nPings)
ashu34c3ee02015-03-25 14:41:14 -0500102
Vince Lehmand96eed32015-10-22 13:57:27 -0500103 def failNode(self, host):
104 print("Bringing %s down" % host.name)
105 host.nfd.stop()
106
107 def recoverNode(self, host):
108 print("Bringing %s up" % host.name)
109 host.nfd.start()
110 host.nlsr.start()
111 host.nfd.setStrategy("/ndn/edu", self.strategy)
112 host.cmd("ndnpingserver /ndn/edu/" + str(host) + " > ping-server &")
113
Ashlesh Gawanded9c9e522015-10-15 16:40:12 -0500114 def startPctPings(self):
115 nNodesToPing = int(round(len(self.net.hosts)*self.pctTraffic))
116 print "Each node will ping %d node(s)" % nNodesToPing
117 # Temporarily store all the nodes being pinged by a particular node
118 nodesPingedList = []
119
120 for host in self.net.hosts:
121 # Create a circular list
122 pool = cycle(self.net.hosts)
123
124 # Move iterator to current node
125 next(x for x in pool if host.name == x.name)
126
127 # Track number of nodes to ping scheduled for this node
128 nNodesScheduled = 0
129
130 while nNodesScheduled < nNodesToPing:
131 other = pool.next()
132
133 # Do not ping self
134 if host.name != other.name:
135 self.ping(host, other, self.nPings)
136 nodesPingedList.append(other)
137
138 # Always increment because in 100% case a node should not ping itself
139 nNodesScheduled = nNodesScheduled + 1
140
141 self.pingedDict[host] = nodesPingedList
142 nodesPingedList = []
143
Vince Lehman3b8bc652015-06-18 15:01:47 -0500144 @staticmethod
145 def register(name, experimentClass):
146 ExperimentManager.register(name, experimentClass)
147