Implement ExperimentManager
refs: #2852
Change-Id: I49b50989477914ae4b076d6d39ca661f50fc92aa
diff --git a/ndn/experiment_manager.py b/ndn/experiment_manager.py
new file mode 100644
index 0000000..69dad29
--- /dev/null
+++ b/ndn/experiment_manager.py
@@ -0,0 +1,65 @@
+#!/usr/bin/python
+
+import os
+
+class _ExperimentManager:
+
+ class Error(Exception):
+ def __init__(self, what):
+ self.what = what
+ def __str__(self):
+ return repr(self.what)
+
+ instance = None
+
+ def __init__(self):
+ self.experiments = {}
+
+ def loadModules(self):
+ currentDir = os.path.dirname(__file__)
+ experimentDir = "%s/%s" % (currentDir, "experiments")
+ experimentModule = "ndn.experiments"
+
+ # Import and register experiments
+ for root, dirs, files in os.walk(experimentDir):
+ for filename in files:
+ if filename.endswith(".py") and filename != "__init__.py":
+ module = filename.replace(".py", "")
+ __import__("%s.%s" % (experimentModule, module))
+
+ def register(self, name, experimentClass):
+ if name not in self.experiments:
+ self.experiments[name] = experimentClass
+ else:
+ raise _ExperimentManager.Error("Experiment '%s' has already been registered" % name)
+
+ def create(self, name, args):
+ if name in self.experiments:
+ return self.experiments[name](args)
+ else:
+ return None
+
+def __getInstance():
+ if _ExperimentManager.instance is None:
+ _ExperimentManager.instance = _ExperimentManager()
+ _ExperimentManager.instance.loadModules()
+
+ return _ExperimentManager.instance
+
+def register(name, experimentClass):
+ manager = __getInstance()
+ manager.register(name, experimentClass)
+
+def create(name, args):
+ manager = __getInstance()
+ return manager.create(name, args)
+
+def getExperimentNames():
+ manager = __getInstance()
+
+ experimentNames = []
+
+ for key in manager.experiments:
+ experimentNames.append(key)
+
+ return experimentNames