ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | import time |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 4 | |
| 5 | class Nfd: |
ashu | 34c3ee0 | 2015-03-25 14:41:14 -0500 | [diff] [blame] | 6 | STRATEGY_BEST_ROUTE_V3 = "best-route/%FD%03" |
| 7 | STRATEGY_NCC = "ncc" |
| 8 | |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 9 | def __init__(self, node): |
| 10 | self.node = node |
| 11 | self.isRunning = False |
| 12 | |
Ashlesh Gawande | 3a4afb1 | 2015-07-09 09:23:30 -0500 | [diff] [blame^] | 13 | self.logLevel = node.params["params"].get("nfd-log-level", "NONE") |
| 14 | |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 15 | # 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 | |
ashu | 2ad32e2 | 2015-05-29 13:37:40 -0500 | [diff] [blame] | 26 | # 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) |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 28 | |
Ashlesh Gawande | 3a4afb1 | 2015-07-09 09:23:30 -0500 | [diff] [blame^] | 29 | # Set log level |
| 30 | node.cmd("sudo sed -i \'s|$LOG_LEVEL|%s|g\' %s" % (self.logLevel, self.confFile)) |
| 31 | |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 32 | # 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 |
ashu | 2ad32e2 | 2015-05-29 13:37:40 -0500 | [diff] [blame] | 39 | node.cmd("sudo cp /usr/local/etc/mini-ndn/client.conf.sample %s" % self.clientConf) |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 40 | 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): |
ashu | 34c3ee0 | 2015-03-25 14:41:14 -0500 | [diff] [blame] | 46 | if self.isRunning is True: |
| 47 | try: |
| 48 | os.kill(int(self.processId), 0) |
| 49 | except OSError: |
| 50 | self.isRunning = False |
| 51 | |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 52 | 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 | |
ashu | 34c3ee0 | 2015-03-25 14:41:14 -0500 | [diff] [blame] | 64 | self.isRunning = False |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 65 | |
| 66 | def setStrategy(self, name, strategy): |
ashu | 34c3ee0 | 2015-03-25 14:41:14 -0500 | [diff] [blame] | 67 | self.node.cmd("nfdc set-strategy %s ndn:/localhost/nfd/strategy/%s" % (name, strategy)) |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 68 | time.sleep(0.5) |