blob: 347ee26af79783f12e7d4c6c5036577fb4169585 [file] [log] [blame]
Vince Lehmanb8b18062015-07-14 13:07:22 -05001# -*- Mode:python; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2#
3# Copyright (C) 2015 The University of Memphis,
4# 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/>.
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__)
41 experimentDir = "%s/%s" % (currentDir, "experiments")
42 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", "")
49 __import__("%s.%s" % (experimentModule, module))
50
51 def register(self, name, experimentClass):
52 if name not in self.experiments:
53 self.experiments[name] = experimentClass
54 else:
55 raise _ExperimentManager.Error("Experiment '%s' has already been registered" % name)
56
57 def create(self, name, args):
58 if name in self.experiments:
59 return self.experiments[name](args)
60 else:
61 return None
62
63def __getInstance():
64 if _ExperimentManager.instance is None:
65 _ExperimentManager.instance = _ExperimentManager()
66 _ExperimentManager.instance.loadModules()
67
68 return _ExperimentManager.instance
69
70def register(name, experimentClass):
71 manager = __getInstance()
72 manager.register(name, experimentClass)
73
74def create(name, args):
75 manager = __getInstance()
76 return manager.create(name, args)
77
78def getExperimentNames():
79 manager = __getInstance()
80
81 experimentNames = []
82
83 for key in manager.experiments:
84 experimentNames.append(key)
85
86 return experimentNames