blob: 817e8d987e35ad060350800d41ae45b4cb130462 [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
Ashlesh Gawande3a4afb12015-07-09 09:23:30 -050013 self.logLevel = node.params["params"].get("nfd-log-level", "NONE")
14
ashuef3490b2015-02-17 11:01:04 -060015 # Create home directory for a node
16 node.cmd("cd /tmp && mkdir %s" % node.name)
17 node.cmd("cd %s" % node.name)
18
19 self.homeFolder = "/tmp/%s" % node.name
20 self.confFile = "%s/%s.conf" % (self.homeFolder, node.name)
21 self.logFile = "%s/%s.log" % (self.homeFolder, node.name)
22 self.sockFile = "/var/run/%s.sock" % node.name
23 self.ndnFolder = "%s/.ndn" % self.homeFolder
24 self.clientConf = "%s/client.conf" % self.ndnFolder
25
ashu2ad32e22015-05-29 13:37:40 -050026 # Copy nfd.conf file from /usr/local/etc/mini-ndn to the node's home
27 node.cmd("sudo cp /usr/local/etc/mini-ndn/nfd.conf %s" % self.confFile)
ashuef3490b2015-02-17 11:01:04 -060028
Ashlesh Gawande3a4afb12015-07-09 09:23:30 -050029 # Set log level
30 node.cmd("sudo sed -i \'s|$LOG_LEVEL|%s|g\' %s" % (self.logLevel, self.confFile))
31
ashuef3490b2015-02-17 11:01:04 -060032 # Open the conf file and change socket file name
33 node.cmd("sudo sed -i 's|nfd.sock|%s.sock|g' %s" % (node.name, self.confFile))
34
35 # Make NDN folder
36 node.cmd("sudo mkdir %s" % self.ndnFolder)
37
38 # Copy the client.conf file and change the unix socket
ashu2ad32e22015-05-29 13:37:40 -050039 node.cmd("sudo cp /usr/local/etc/mini-ndn/client.conf.sample %s" % self.clientConf)
ashuef3490b2015-02-17 11:01:04 -060040 node.cmd("sudo sed -i 's|nfd.sock|%s.sock|g' %s" % (node.name, self.clientConf))
41
42 # Change home folder
43 node.cmd("export HOME=%s" % self.homeFolder)
44
45 def start(self):
ashu34c3ee02015-03-25 14:41:14 -050046 if self.isRunning is True:
47 try:
48 os.kill(int(self.processId), 0)
49 except OSError:
50 self.isRunning = False
51
ashuef3490b2015-02-17 11:01:04 -060052 if self.isRunning is False:
53 self.node.cmd("sudo nfd --config %s 2>> %s &" % (self.confFile, self.logFile))
54 self.processId = self.node.cmd("echo $!")[:-1]
55
56 time.sleep(2)
57
58 self.isRunning = True
59
60 def stop(self):
61 if self.isRunning is True:
62 self.node.cmd("sudo kill %s" % self.processId)
63
ashu34c3ee02015-03-25 14:41:14 -050064 self.isRunning = False
ashuef3490b2015-02-17 11:01:04 -060065
66 def setStrategy(self, name, strategy):
ashu34c3ee02015-03-25 14:41:14 -050067 self.node.cmd("nfdc set-strategy %s ndn:/localhost/nfd/strategy/%s" % (name, strategy))
ashuef3490b2015-02-17 11:01:04 -060068 time.sleep(0.5)