87 lines
2.7 KiB
Python
87 lines
2.7 KiB
Python
# -*- Mode:python; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
|
|
#
|
|
# Copyright (C) 2015 The University of Memphis,
|
|
# Arizona Board of Regents,
|
|
# Regents of the University of California.
|
|
#
|
|
# This file is part of Mini-NDN.
|
|
# See AUTHORS.md for a complete list of Mini-NDN authors and contributors.
|
|
#
|
|
# Mini-NDN is free software: you can redistribute it and/or modify
|
|
# it under the terms of the GNU General Public License as published by
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# Mini-NDN is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU General Public License
|
|
# along with Mini-NDN, e.g., in COPYING.md file.
|
|
# If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
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
|