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 as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
31 class NetNSWrapper(object):
32 def __init__(self, loglevel = logging.INFO, enable_dump = False):
33 super(NetNSWrapper, self).__init__()
34 # holds reference to all C++ objects and variables in the simulation
35 self._objects = dict()
38 self._logger = logging.getLogger("netnswrapper")
39 self._logger.setLevel(loglevel)
41 # Object to dump instructions to reproduce and debug experiment
42 from netnswrapper_debug import NetNSWrapperDebuger
43 self._debuger = NetNSWrapperDebuger(enabled = enable_dump)
54 return "uuid%s" % uuid.uuid4()
56 def get_object(self, uuid):
57 return self._objects.get(uuid)
59 def create(self, clazzname, *args):
60 """ This method should be used to construct netns objects """
63 if clazzname not in ['open'] and not hasattr(netns, clazzname):
64 msg = "Type %s not supported" % (clazzname)
65 self.logger.error(msg)
67 uuid = self.make_uuid()
70 self.logger.debug("CREATE %s( %s )" % (clazzname, str(args)))
72 self.debuger.dump_create(uuid, clazzname, args)
75 if clazzname == "open":
78 obj = open(path, mode)
80 clazz = getattr(netns, clazzname)
82 # arguments starting with 'uuid' identify ns-3 C++
83 # objects and must be replaced by the actual object
84 realargs = self.replace_args(args)
86 obj = clazz(*realargs)
88 self._objects[uuid] = obj
91 self.logger.debug("RET CREATE ( uuid %s ) %s = %s( %s )" % (str(uuid),
92 str(obj), clazzname, str(args)))
97 def invoke(self, uuid, operation, *args, **kwargs):
98 newuuid = self.make_uuid()
101 self.logger.debug("INVOKE %s -> %s( %s, %s ) " % (
102 uuid, operation, str(args), str(kwargs)))
104 self.debuger.dump_invoke(newuuid, uuid, operation, args, kwargs)
107 obj = self.get_object(uuid)
109 method = getattr(obj, operation)
111 # arguments starting with 'uuid' identify netns
112 # objects and must be replaced by the actual object
113 realargs = self.replace_args(args)
114 realkwargs = self.replace_kwargs(kwargs)
116 result = method(*realargs, **realkwargs)
118 # If the result is an object (not a base value),
119 # then keep track of the object a return the object
120 # reference (newuuid)
121 if not (result is None or type(result) in [
122 bool, float, long, str, int]):
123 self._objects[newuuid] = result
127 self.logger.debug("RET INVOKE %s%s = %s -> %s(%s, %s) " % (
128 "(uuid %s) " % str(newuuid) if newuuid else "", str(result), uuid,
129 operation, str(args), str(kwargs)))
134 def set(self, uuid, name, value):
136 self.logger.debug("SET %s %s %s" % (uuid, name, str(value)))
138 self.debuger.dump_set(uuid, name, value)
141 obj = self.get_object(uuid)
142 setattr(obj, name, value)
145 self.logger.debug("RET SET %s = %s -> set(%s, %s)" % (str(value), uuid, name,
151 def get(self, uuid, name):
153 self.logger.debug("GET %s %s" % (uuid, name))
155 self.debuger.dump_get(uuid, name)
158 obj = self.get_object(uuid)
159 result = getattr(obj, name)
162 self.logger.debug("RET GET %s = %s -> get(%s)" % (str(result), uuid, name))
169 self.debuger.dump_shutdown()
180 self.logger.debug("SHUTDOWN")
183 def replace_args(self, args):
184 realargs = [self.get_object(arg) if \
185 str(arg).startswith("uuid") else arg for arg in args]
189 def replace_kwargs(self, kwargs):
190 realkwargs = dict([(k, self.get_object(v) \
191 if str(v).startswith("uuid") else v) \
192 for k,v in kwargs.iteritems()])