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 | |
| 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 | |
ashu | 34c3ee0 | 2015-03-25 14:41:14 -0500 | [diff] [blame^] | 24 | # 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) |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 26 | |
| 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 |
ashu | 34c3ee0 | 2015-03-25 14:41:14 -0500 | [diff] [blame^] | 34 | node.cmd("sudo cp ~/mini-ndn/ndn_utils/client.conf.sample %s" % self.clientConf) |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 35 | 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): |
ashu | 34c3ee0 | 2015-03-25 14:41:14 -0500 | [diff] [blame^] | 41 | if self.isRunning is True: |
| 42 | try: |
| 43 | os.kill(int(self.processId), 0) |
| 44 | except OSError: |
| 45 | self.isRunning = False |
| 46 | |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 47 | 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 | |
ashu | 34c3ee0 | 2015-03-25 14:41:14 -0500 | [diff] [blame^] | 59 | self.isRunning = False |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 60 | |
| 61 | def setStrategy(self, name, strategy): |
ashu | 34c3ee0 | 2015-03-25 14:41:14 -0500 | [diff] [blame^] | 62 | 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] | 63 | time.sleep(0.5) |