blob: 0be9f92234e77582211f511fe935522aa078ec4f [file] [log] [blame]
Vince Lehmanb8b18062015-07-14 13:07:22 -05001# -*- Mode:python; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2#
Alexander Lane9944cf52018-05-17 12:16:50 -05003# Copyright (C) 2015-2018, 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 = {}
38
39 def loadModules(self):
40 currentDir = os.path.dirname(__file__)
Ashlesh Gawande27b5e1b2018-08-06 17:47:15 -050041 experimentDir = "{}/{}".format(currentDir, "experiments")
Vince Lehman3b8bc652015-06-18 15:01:47 -050042 experimentModule = "ndn.experiments"
43
44 # Import and register experiments
45 for root, dirs, files in os.walk(experimentDir):
46 for filename in files:
47 if filename.endswith(".py") and filename != "__init__.py":
48 module = filename.replace(".py", "")
Ashlesh Gawande27b5e1b2018-08-06 17:47:15 -050049 subdir = os.path.basename(root)
50 if subdir == "experiments":
51 __import__("{}.{}".format(experimentModule, module))
52 else:
53 __import__("{}.{}.{}".format(experimentModule, subdir, module))
Vince Lehman3b8bc652015-06-18 15:01:47 -050054
55 def register(self, name, experimentClass):
56 if name not in self.experiments:
57 self.experiments[name] = experimentClass
58 else:
Ashlesh Gawande27b5e1b2018-08-06 17:47:15 -050059 raise _ExperimentManager.Error("Experiment '{}' has already been registered".format(name))
Vince Lehman3b8bc652015-06-18 15:01:47 -050060
61 def create(self, name, args):
62 if name in self.experiments:
63 return self.experiments[name](args)
64 else:
65 return None
66
67def __getInstance():
68 if _ExperimentManager.instance is None:
69 _ExperimentManager.instance = _ExperimentManager()
70 _ExperimentManager.instance.loadModules()
71
72 return _ExperimentManager.instance
73
74def register(name, experimentClass):
75 manager = __getInstance()
76 manager.register(name, experimentClass)
77
78def create(name, args):
79 manager = __getInstance()
80 return manager.create(name, args)
81
82def getExperimentNames():
83 manager = __getInstance()
84
85 experimentNames = []
86
87 for key in manager.experiments:
88 experimentNames.append(key)
89
90 return experimentNames
Ashlesh Gawande501d4d62017-10-25 13:12:11 -050091
Alexander Lane1bc9b472018-05-16 15:07:16 -050092def addExperimentArgs(parser):
93 # Find all experiment command line arguments and parse them.
Ashlesh Gawande501d4d62017-10-25 13:12:11 -050094 manager = __getInstance()
Alexander Lane1bc9b472018-05-16 15:07:16 -050095 for name in manager.experiments:
96 if hasattr(manager.experiments[name], "parseArguments"):
97 manager.experiments[name].parseArguments(parser)