blob: 5f2a174f6f5e37af7ec024dc3d00c50ff8e508bb [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/>.
Vince Lehman3b8bc652015-06-18 15:01:47 -050023
24import os
25
26class _ExperimentManager:
27
28 class Error(Exception):
29 def __init__(self, what):
30 self.what = what
31 def __str__(self):
32 return repr(self.what)
33
34 instance = None
35
36 def __init__(self):
37 self.experiments = {}
Ashlesh Gawande501d4d62017-10-25 13:12:11 -050038 self.expArgs = {}
Vince Lehman3b8bc652015-06-18 15:01:47 -050039
40 def loadModules(self):
41 currentDir = os.path.dirname(__file__)
42 experimentDir = "%s/%s" % (currentDir, "experiments")
43 experimentModule = "ndn.experiments"
44
45 # Import and register experiments
46 for root, dirs, files in os.walk(experimentDir):
47 for filename in files:
48 if filename.endswith(".py") and filename != "__init__.py":
49 module = filename.replace(".py", "")
50 __import__("%s.%s" % (experimentModule, module))
51
52 def register(self, name, experimentClass):
53 if name not in self.experiments:
54 self.experiments[name] = experimentClass
Ashlesh Gawande501d4d62017-10-25 13:12:11 -050055 try:
56 helpStr = experimentClass.arguments()
57 if type(helpStr) is str:
58 self.expArgs[name] = experimentClass.arguments()
59 except:
60 pass
Vince Lehman3b8bc652015-06-18 15:01:47 -050061 else:
62 raise _ExperimentManager.Error("Experiment '%s' has already been registered" % name)
63
64 def create(self, name, args):
65 if name in self.experiments:
66 return self.experiments[name](args)
67 else:
68 return None
69
70def __getInstance():
71 if _ExperimentManager.instance is None:
72 _ExperimentManager.instance = _ExperimentManager()
73 _ExperimentManager.instance.loadModules()
74
75 return _ExperimentManager.instance
76
77def register(name, experimentClass):
78 manager = __getInstance()
79 manager.register(name, experimentClass)
80
81def create(name, args):
82 manager = __getInstance()
83 return manager.create(name, args)
84
85def getExperimentNames():
86 manager = __getInstance()
87
88 experimentNames = []
89
90 for key in manager.experiments:
91 experimentNames.append(key)
92
93 return experimentNames
Ashlesh Gawande501d4d62017-10-25 13:12:11 -050094
95def getExperimentArgs():
96 manager = __getInstance()
97
98 return manager.expArgs