blob: 0e10ef17b4f681d30d0cbdec477440111c78f32c [file] [log] [blame]
Vince Lehmanb8b18062015-07-14 13:07:22 -05001# -*- Mode:python; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2#
Ashlesh Gawandeda475f02017-03-01 17:20:58 -06003# Copyright (C) 2015-2017, The University of Memphis,
Ashlesh Gawande0cccdb82016-08-15 12:58:06 -05004# Arizona Board of Regents,
5# Regents of the University of California.
Vince Lehmanb8b18062015-07-14 13:07:22 -05006#
7# This file is part of Mini-NDN.
8# See AUTHORS.md for a complete list of Mini-NDN authors and contributors.
9#
10# Mini-NDN is free software: you can redistribute it and/or modify
11# it under the terms of the GNU General Public License as published by
12# the Free Software Foundation, either version 3 of the License, or
13# (at your option) any later version.
14#
15# Mini-NDN is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18# GNU General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License
21# along with Mini-NDN, e.g., in COPYING.md file.
22# If not, see <http://www.gnu.org/licenses/>.
ashuef3490b2015-02-17 11:01:04 -060023
Saurab Dulal5d9ba602017-11-29 21:31:13 +000024import time, sys, os
Ashlesh Gawande792c6aa2015-07-10 12:18:36 -050025from ndn.ndn_application import NdnApplication
ashuef3490b2015-02-17 11:01:04 -060026
Saurab Dulal5d9ba602017-11-29 21:31:13 +000027
Ashlesh Gawande792c6aa2015-07-10 12:18:36 -050028class Nfd(NdnApplication):
Ashlesh Gawandeb07ada82016-08-05 14:29:17 -050029 STRATEGY_BEST_ROUTE = "best-route"
ashu34c3ee02015-03-25 14:41:14 -050030 STRATEGY_NCC = "ncc"
31
ashuef3490b2015-02-17 11:01:04 -060032 def __init__(self, node):
Ashlesh Gawande792c6aa2015-07-10 12:18:36 -050033 NdnApplication.__init__(self, node)
ashuef3490b2015-02-17 11:01:04 -060034
Ashlesh Gawande3a4afb12015-07-09 09:23:30 -050035 self.logLevel = node.params["params"].get("nfd-log-level", "NONE")
36
Saurab Dulal5d9ba602017-11-29 21:31:13 +000037 self.confFile = "{}/{}.conf".format(node.homeFolder, node.name)
38 self.logFile = "{}/{}.log".format(node.homeFolder, node.name)
39 self.sockFile = "/var/run/{}.sock".format(node.name)
40 self.ndnFolder = "{}/.ndn".format(node.homeFolder)
41 self.clientConf = "{}/client.conf".format(self.ndnFolder)
ashuef3490b2015-02-17 11:01:04 -060042
Saurab Dulal5d9ba602017-11-29 21:31:13 +000043 # Copy nfd.conf file from /usr/local/etc/ndn to the node's home
44
45 # Use nfd.conf as default configuration for NFD, else use the sample
46 if os.path.isfile("/usr/local/etc/ndn/nfd.conf") == True:
47 node.cmd("sudo cp /usr/local/etc/ndn/nfd.conf {}".format(self.confFile))
48 elif os.path.isfile("/usr/local/etc/ndn/nfd.conf.sample") == True:
49 node.cmd("sudo cp /usr/local/etc/ndn/nfd.conf.sample {}".format(self.confFile))
50 else:
51 sys.exit("nfd.conf or nfd.conf.sample cannot be found in the expected directory. Exit.")
ashuef3490b2015-02-17 11:01:04 -060052
Ashlesh Gawande3a4afb12015-07-09 09:23:30 -050053 # Set log level
Saurab Dulal5d9ba602017-11-29 21:31:13 +000054 node.cmd("infoedit -f {} -s log.default_level -v {}".format(self.confFile, self.logLevel))
ashuef3490b2015-02-17 11:01:04 -060055 # Open the conf file and change socket file name
Saurab Dulal5d9ba602017-11-29 21:31:13 +000056 node.cmd("infoedit -f {} -s face_system.unix.path -v /var/run/{}.sock".format(self.confFile, node.name))
ashuef3490b2015-02-17 11:01:04 -060057
58 # Make NDN folder
Saurab Dulal5d9ba602017-11-29 21:31:13 +000059 node.cmd("sudo mkdir {}".format(self.ndnFolder))
ashuef3490b2015-02-17 11:01:04 -060060
61 # Copy the client.conf file and change the unix socket
Saurab Dulal5d9ba602017-11-29 21:31:13 +000062 node.cmd("sudo cp /usr/local/etc/ndn/client.conf.sample {}".format(self.clientConf))
63
64 node.cmd("sudo sed -i 's|nfd.sock|{}.sock|g' {}".format(node.name, self.clientConf))
ashuef3490b2015-02-17 11:01:04 -060065
66 # Change home folder
Saurab Dulal5d9ba602017-11-29 21:31:13 +000067 node.cmd("export HOME={}".format(node.homeFolder))
ashuef3490b2015-02-17 11:01:04 -060068
69 def start(self):
Saurab Dulal5d9ba602017-11-29 21:31:13 +000070 NdnApplication.start(self, "setsid nfd --config {} >> {} 2>&1 &".format(self.confFile, self.logFile))
Ashlesh Gawande792c6aa2015-07-10 12:18:36 -050071 time.sleep(2)
ashuef3490b2015-02-17 11:01:04 -060072
73 def setStrategy(self, name, strategy):
Saurab Dulal5d9ba602017-11-29 21:31:13 +000074 self.node.cmd("nfdc strategy set {} ndn:/localhost/nfd/strategy/{}".format(name, strategy))
ashuef3490b2015-02-17 11:01:04 -060075 time.sleep(0.5)