ndn: Add NLSR experiments

Change-Id: I0b86b121a5c8bf6a6e6e8df5027fe49561a9283b
diff --git a/ndn/experiments/__init__.py b/ndn/experiments/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/ndn/experiments/__init__.py
diff --git a/ndn/experiments/experiment.py b/ndn/experiments/experiment.py
new file mode 100644
index 0000000..9487e22
--- /dev/null
+++ b/ndn/experiments/experiment.py
@@ -0,0 +1,70 @@
+#!/usr/bin/python
+
+import time
+import sys
+
+class Experiment:
+
+    def __init__(self, net, nodes, convergenceTime, nPings, strategy):
+        self.net = net
+        self.nodes = nodes
+        self.convergenceTime = convergenceTime
+        self.nPings = nPings
+        self.strategy = strategy
+
+    def start(self):
+        self.setup()
+        self.run()
+
+    def setup(self):
+        for host in self.net.hosts:
+            # Set strategy
+            host.nfd.setStrategy("/ndn/edu", self.strategy)
+
+            # Start ping server
+            host.cmd("ndnpingserver /ndn/edu/" + str(host) + " > ping-server &")
+
+            # Create folder to store ping data
+            host.cmd("mkdir ping-data")
+
+        # Wait for convergence time period
+        print "Waiting " + str(self.convergenceTime) + " seconds for convergence..."
+        time.sleep(self.convergenceTime)
+        print "...done"
+
+        # To check whether all the nodes of NLSR have converged
+        didNlsrConverge = True
+
+        # Checking for convergence
+        for host in self.net.hosts:
+            statusRouter = host.cmd("nfd-status -b | grep /ndn/edu/%C1.Router/cs/")
+            statusPrefix = host.cmd("nfd-status -b | grep /ndn/edu/")
+            didNodeConverge = True
+            for node in self.nodes.split(","):
+                    if ("/ndn/edu/%C1.Router/cs/" + node) not in statusRouter:
+                        didNodeConverge = False
+                        didNlsrConverge = False
+                    if str(host) != node and ("/ndn/edu/" + node) not in statusPrefix:
+                        didNodeConverge = False
+                        didNlsrConverge = False
+
+            host.cmd("echo " + str(didNodeConverge) + " > convergence-result &")
+
+        if didNlsrConverge:
+            print("NLSR has successfully converged.")
+        else:
+            print("NLSR has not converged. Exiting...")
+            for host in self.net.hosts:
+                host.nfd.stop()
+            sys.exit(1)
+
+    def startPings(self):
+        for host in self.net.hosts:
+            for other in self.net.hosts:
+                # Do not ping self
+                if host.name != other.name:
+                    # Use "&" to run in background and perform parallel pings
+                    print "Scheduling ping(s) from %s to %s" % (host.name, other.name)
+                    host.cmd("ndnping -t -c "+ str(self.nPings) + " /ndn/edu/" + other.name + " > ping-data/" + other.name + ".txt &")
+                    time.sleep(0.2)
+
diff --git a/ndn/experiments/failure_experiment.py b/ndn/experiments/failure_experiment.py
new file mode 100644
index 0000000..d4e9380
--- /dev/null
+++ b/ndn/experiments/failure_experiment.py
@@ -0,0 +1,42 @@
+#!/usr/bin/python
+
+from ndn.experiments.experiment import Experiment
+from ndn.nlsr import Nlsr
+
+import time
+
+class FailureExperiment(Experiment):
+
+    def __init__(self, net, nodes, convergenceTime, strategy):
+
+        Experiment.__init__(self, net, nodes, convergenceTime, 300, strategy)
+        self.PING_COLLECTION_TIME_BEFORE_FAILURE = 60
+        self.PING_COLLECTION_TIME_AFTER_RECOVERY = 90
+
+    def run(self):
+        self.startPings()
+
+        # After the pings are scheduled, collect pings for 1 minute
+        time.sleep(self.PING_COLLECTION_TIME_BEFORE_FAILURE)
+
+        # Bring down CSU
+        for host in self.net.hosts:
+            if host.name == "csu":
+                print("Bringing CSU down")
+                host.nfd.stop()
+                break
+
+        # CSU is down for 2 minutes
+        time.sleep(120)
+
+        # Bring CSU back up
+        for host in self.net.hosts:
+            if host.name == "csu":
+                print("Bringing CSU up")
+                host.nfd.start()
+                host.nlsr.start()
+                host.nfd.setStrategy("/ndn/edu", self.strategy)
+                host.cmd("ndnpingserver /ndn/edu/" + str(host) + " > ping-server &")
+
+        # Collect pings for more seconds after CSU is up
+        time.sleep(self.PING_COLLECTION_TIME_AFTER_RECOVERY)
diff --git a/ndn/experiments/pingall_experiment.py b/ndn/experiments/pingall_experiment.py
new file mode 100644
index 0000000..3bbfc1f
--- /dev/null
+++ b/ndn/experiments/pingall_experiment.py
@@ -0,0 +1,19 @@
+#!/usr/bin/python
+
+from ndn.experiments.experiment import Experiment
+
+import time
+
+class PingallExperiment(Experiment):
+
+    def __init__(self, net, nodes, convergenceTime, nPings, strategy):
+
+        Experiment.__init__(self, net, nodes, convergenceTime, nPings, strategy)
+        self.COLLECTION_PERIOD_BUFFER = 10
+
+
+    def run(self):
+        self.startPings()
+
+        # For pingall experiment sleep for the number of pings + some offset
+        time.sleep(self.nPings + self.COLLECTION_PERIOD_BUFFER)