2 # NEPI, a framework to manage network experiments
3 # Copyright (C) 2013 INRIA
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License version 2 as
7 # published by the Free Software Foundation;
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
25 class NetNSWrapper(object):
26 def __init__(self, loglevel = logging.INFO, enable_dump = False):
27 super(NetNSWrapper, self).__init__()
28 # holds reference to all C++ objects and variables in the simulation
29 self._objects = dict()
32 self._logger = logging.getLogger("netnswrapper")
33 self._logger.setLevel(loglevel)
35 # Object to dump instructions to reproduce and debug experiment
36 from netnswrapper_debug import NetNSWrapperDebuger
37 self._debuger = NetNSWrapperDebuger(enabled = enable_dump)
48 return "uuid%s" % uuid.uuid4()
50 def get_object(self, uuid):
51 return self._objects.get(uuid)
53 def create(self, clazzname, *args):
54 """ This method should be used to construct netns objects """
57 if clazzname not in ['open'] and not hasattr(netns, clazzname):
58 msg = "Type %s not supported" % (clazzname)
59 self.logger.error(msg)
61 uuid = self.make_uuid()
64 self.logger.debug("CREATE %s( %s )" % (clazzname, str(args)))
66 self.debuger.dump_create(uuid, clazzname, args)
69 if clazzname == "open":
72 obj = open(path, mode)
74 clazz = getattr(netns, clazzname)
76 # arguments starting with 'uuid' identify ns-3 C++
77 # objects and must be replaced by the actual object
78 realargs = self.replace_args(args)
80 obj = clazz(*realargs)
82 self._objects[uuid] = obj
85 self.logger.debug("RET CREATE ( uuid %s ) %s = %s( %s )" % (str(uuid),
86 str(obj), clazzname, str(args)))
91 def invoke(self, uuid, operation, *args, **kwargs):
92 newuuid = self.make_uuid()
95 self.logger.debug("INVOKE %s -> %s( %s, %s ) " % (
96 uuid, operation, str(args), str(kwargs)))
98 self.debuger.dump_invoke(newuuid, uuid, operation, args, kwargs)
101 obj = self.get_object(uuid)
103 method = getattr(obj, operation)
105 # arguments starting with 'uuid' identify netns
106 # objects and must be replaced by the actual object
107 realargs = self.replace_args(args)
108 realkwargs = self.replace_kwargs(kwargs)
110 result = method(*realargs, **realkwargs)
112 # If the result is an object (not a base value),
113 # then keep track of the object a return the object
114 # reference (newuuid)
115 # xxx the two instances of `int` are from 2to3
116 if not (result is None or type(result) in [
117 bool, float, int, str, int]):
118 self._objects[newuuid] = result
122 self.logger.debug("RET INVOKE %s%s = %s -> %s(%s, %s) " % (
123 "(uuid %s) " % str(newuuid) if newuuid else "", str(result), uuid,
124 operation, str(args), str(kwargs)))
129 def set(self, uuid, name, value):
131 self.logger.debug("SET %s %s %s" % (uuid, name, str(value)))
133 self.debuger.dump_set(uuid, name, value)
136 obj = self.get_object(uuid)
137 setattr(obj, name, value)
140 self.logger.debug("RET SET %s = %s -> set(%s, %s)" % (str(value), uuid, name,
146 def get(self, uuid, name):
148 self.logger.debug("GET %s %s" % (uuid, name))
150 self.debuger.dump_get(uuid, name)
153 obj = self.get_object(uuid)
154 result = getattr(obj, name)
157 self.logger.debug("RET GET %s = %s -> get(%s)" % (str(result), uuid, name))
164 self.debuger.dump_shutdown()
175 self.logger.debug("SHUTDOWN")
178 def replace_args(self, args):
179 realargs = [self.get_object(arg) if \
180 str(arg).startswith("uuid") else arg for arg in args]
184 def replace_kwargs(self, kwargs):
185 realkwargs = dict([(k, self.get_object(v) \
186 if str(v).startswith("uuid") else v) \
187 for k,v in kwargs.items()])