blob: 23092daf85a1fe3e8ef7e4cdbdc588bc1e17a1ac [file] [log] [blame]
Ashlesh Gawande6c86e302019-09-17 22:27:05 -05001# -*- Mode:python; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2#
Saurab Dulal576a4192020-08-25 00:55:22 -05003# Copyright (C) 2015-2020, The University of Memphis,
Ashlesh Gawande6c86e302019-09-17 22:27:05 -05004# Arizona Board of Regents,
5# Regents of the University of California.
6#
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/>.
23
24import sys
25from os.path import isfile
26from subprocess import call
dulalsaurab0ed77722020-09-24 22:32:58 +000027from six.moves.urllib.parse import quote
28
Ashlesh Gawande6c86e302019-09-17 22:27:05 -050029from mininet.cli import CLI
30
31sshbase = ['ssh', '-q', '-t', '-i/home/mininet/.ssh/id_rsa']
32scpbase = ['scp', '-i', '/home/mininet/.ssh/id_rsa']
33devnull = open('/dev/null', 'w')
34
dulalsaurab0ed77722020-09-24 22:32:58 +000035def getSafeName(namePrefix):
36 """
37 Check if the prefix/string is safe to use with ndn commands or not.
38 return safe prefix.
39 :param namePrefix: name of the prefix
40 """
41 # remove redundant "/"es, multiple "/"es are an invalid representation for empty name component
42 # https://named-data.net/doc/NDN-packet-spec/current/changelog.html#version-0-3
43 namePrefix= "/" + ("/".join(filter(None, namePrefix.split("/"))))
44 return quote(namePrefix, safe='/')
45
Ashlesh Gawande6c86e302019-09-17 22:27:05 -050046def ssh(login, cmd):
47 rcmd = sshbase + [login, cmd]
48 call(rcmd, stdout=devnull, stderr=devnull)
49
50def scp(*args):
51 tmp = []
52 for arg in args:
53 tmp.append(arg)
54 rcmd = scpbase + tmp
55 call(rcmd, stdout=devnull, stderr=devnull)
56
57def copyExistentFile(node, fileList, destination):
58 for f in fileList:
59 if isfile(f):
60 node.cmd('cp {} {}'.format(f, destination))
61 break
62 if not isfile(destination):
63 fileName = destination.split('/')[-1]
64 raise IOError('{} not found in expected directory.'.format(fileName))
65
66def popenGetEnv(node, envDict=None):
67 env = {}
68 homeDir = node.params['params']['homeDir']
69 printenv = node.popen('printenv'.split(), cwd=homeDir).communicate()[0].decode('utf-8')
70 for var in printenv.split('\n'):
71 if var == '':
72 break
73 p = var.split('=')
74 env[p[0]] = p[1]
75 env['HOME'] = homeDir
76
77 if envDict is not None:
78 for key, value in envDict.items():
79 env[key] = str(value)
80
81 return env
82
83def getPopen(host, cmd, envDict=None, **params):
84 return host.popen(cmd, cwd=host.params['params']['homeDir'],
85 env=popenGetEnv(host, envDict), **params)
86
87class MiniNDNCLI(CLI):
88 prompt = 'mini-ndn> '
89 def __init__(self, mininet, stdin=sys.stdin, script=None):
90 CLI.__init__(self, mininet, stdin, script)
Alexander Laneea2d5d62019-10-04 16:48:52 -050091
Junxiao Shi48ada892021-11-04 09:02:21 -060092try:
93 from mn_wifi.cli import CLI as CLI_wifi
94
95 class MiniNDNWifiCLI(CLI_wifi):
96 prompt = 'mini-ndn-wifi> '
97 def __init__(self, mininet, stdin=sys.stdin, script=None):
98 CLI_wifi.__init__(self, mininet, stdin, script)
99
100except ImportError:
101 class MiniNDNWifiCLI:
102 def __init__(self):
103 raise ImportError('Mininet-WiFi is not installed')