ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame^] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | import time |
| 4 | from os.path import expanduser |
| 5 | |
| 6 | class Nfd: |
| 7 | def __init__(self, node): |
| 8 | self.node = node |
| 9 | self.isRunning = False |
| 10 | |
| 11 | # Create home directory for a node |
| 12 | node.cmd("cd /tmp && mkdir %s" % node.name) |
| 13 | node.cmd("cd %s" % node.name) |
| 14 | |
| 15 | self.homeFolder = "/tmp/%s" % node.name |
| 16 | self.confFile = "%s/%s.conf" % (self.homeFolder, node.name) |
| 17 | self.logFile = "%s/%s.log" % (self.homeFolder, node.name) |
| 18 | self.sockFile = "/var/run/%s.sock" % node.name |
| 19 | self.ndnFolder = "%s/.ndn" % self.homeFolder |
| 20 | self.clientConf = "%s/client.conf" % self.ndnFolder |
| 21 | |
| 22 | # Copy file that checks FIB |
| 23 | node.cmd("sudo cp ~/mn-ndn/ndn_utils/checkFIB %s/checkFIB" % self.homeFolder) |
| 24 | |
| 25 | # Copy nfd.conf file from mn-ndn/ndn_utils to the node's home |
| 26 | node.cmd("sudo cp ~/mn-ndn/ndn_utils/nfd.conf %s" % self.confFile) |
| 27 | |
| 28 | # Open the conf file and change socket file name |
| 29 | node.cmd("sudo sed -i 's|nfd.sock|%s.sock|g' %s" % (node.name, self.confFile)) |
| 30 | |
| 31 | # Make NDN folder |
| 32 | node.cmd("sudo mkdir %s" % self.ndnFolder) |
| 33 | |
| 34 | # Copy the client.conf file and change the unix socket |
| 35 | node.cmd("sudo cp ~/mn-ndn/ndn_utils/client.conf.sample %s" % self.clientConf) |
| 36 | node.cmd("sudo sed -i 's|nfd.sock|%s.sock|g' %s" % (node.name, self.clientConf)) |
| 37 | |
| 38 | # Change home folder |
| 39 | node.cmd("export HOME=%s" % self.homeFolder) |
| 40 | |
| 41 | def start(self): |
| 42 | if self.isRunning is False: |
| 43 | self.node.cmd("sudo nfd --config %s 2>> %s &" % (self.confFile, self.logFile)) |
| 44 | self.processId = self.node.cmd("echo $!")[:-1] |
| 45 | |
| 46 | time.sleep(2) |
| 47 | |
| 48 | self.isRunning = True |
| 49 | |
| 50 | def stop(self): |
| 51 | if self.isRunning is True: |
| 52 | self.node.cmd("sudo kill %s" % self.processId) |
| 53 | |
| 54 | self.isRunning = False |
| 55 | |
| 56 | def setStrategy(self, name, strategy): |
| 57 | node.cmd("nfdc set-strategy %s ndn:/localhost/nfd/strategy/%s" % (name, strategy)) |
| 58 | time.sleep(0.5) |