blob: b33fc37be50b458f1d4f440139d6e06507b664a4 [file] [log] [blame]
ashuef3490b2015-02-17 11:01:04 -06001#!/usr/bin/env python
2
3import time
ashuef3490b2015-02-17 11:01:04 -06004
5class Nfd:
ashu34c3ee02015-03-25 14:41:14 -05006 STRATEGY_BEST_ROUTE_V3 = "best-route/%FD%03"
7 STRATEGY_NCC = "ncc"
8
ashuef3490b2015-02-17 11:01:04 -06009 def __init__(self, node):
10 self.node = node
11 self.isRunning = False
12
13 # Create home directory for a node
14 node.cmd("cd /tmp && mkdir %s" % node.name)
15 node.cmd("cd %s" % node.name)
16
17 self.homeFolder = "/tmp/%s" % node.name
18 self.confFile = "%s/%s.conf" % (self.homeFolder, node.name)
19 self.logFile = "%s/%s.log" % (self.homeFolder, node.name)
20 self.sockFile = "/var/run/%s.sock" % node.name
21 self.ndnFolder = "%s/.ndn" % self.homeFolder
22 self.clientConf = "%s/client.conf" % self.ndnFolder
23
ashu34c3ee02015-03-25 14:41:14 -050024 # Copy nfd.conf file from mini-ndn/ndn_utils to the node's home
25 node.cmd("sudo cp ~/mini-ndn/ndn_utils/nfd.conf %s" % self.confFile)
ashuef3490b2015-02-17 11:01:04 -060026
27 # Open the conf file and change socket file name
28 node.cmd("sudo sed -i 's|nfd.sock|%s.sock|g' %s" % (node.name, self.confFile))
29
30 # Make NDN folder
31 node.cmd("sudo mkdir %s" % self.ndnFolder)
32
33 # Copy the client.conf file and change the unix socket
ashu34c3ee02015-03-25 14:41:14 -050034 node.cmd("sudo cp ~/mini-ndn/ndn_utils/client.conf.sample %s" % self.clientConf)
ashuef3490b2015-02-17 11:01:04 -060035 node.cmd("sudo sed -i 's|nfd.sock|%s.sock|g' %s" % (node.name, self.clientConf))
36
37 # Change home folder
38 node.cmd("export HOME=%s" % self.homeFolder)
39
40 def start(self):
ashu34c3ee02015-03-25 14:41:14 -050041 if self.isRunning is True:
42 try:
43 os.kill(int(self.processId), 0)
44 except OSError:
45 self.isRunning = False
46
ashuef3490b2015-02-17 11:01:04 -060047 if self.isRunning is False:
48 self.node.cmd("sudo nfd --config %s 2>> %s &" % (self.confFile, self.logFile))
49 self.processId = self.node.cmd("echo $!")[:-1]
50
51 time.sleep(2)
52
53 self.isRunning = True
54
55 def stop(self):
56 if self.isRunning is True:
57 self.node.cmd("sudo kill %s" % self.processId)
58
ashu34c3ee02015-03-25 14:41:14 -050059 self.isRunning = False
ashuef3490b2015-02-17 11:01:04 -060060
61 def setStrategy(self, name, strategy):
ashu34c3ee02015-03-25 14:41:14 -050062 self.node.cmd("nfdc set-strategy %s ndn:/localhost/nfd/strategy/%s" % (name, strategy))
ashuef3490b2015-02-17 11:01:04 -060063 time.sleep(0.5)