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>
20 ############ METHODS DEBUG NETNSWRAPPER EXECUTION
22 ## The netnswrapper works in ditributed mode, receiving instructions from
23 ## a remote client. This makes it very hard to debug scripting errors
24 ## in the client side. To simplify error debugging, when set to debug mode,
25 ## the ns3wrapper dumps every executed line to a script that can be then
26 ## executed locally to reproduce and debug the experiment.
28 ###########################################################################
32 class NetNSWrapperDebuger(object):
33 def __init__(self, enabled):
34 super(NetNSWrapperDebuger, self).__init__()
35 self._enabled = enabled
36 self._script_path = "debug.py"
45 def script_path(self):
46 return self._script_path
48 def dump_to_script(self, command):
49 with open(self.script_path, "a") as f:
50 f.write("%s" % command)
52 def dump_header(self):
57 from netnswrapper import NetNSWrapper
59 wrapper = NS3Wrapper()
62 self.dump_to_script(header)
64 def dump_factory(self, uuid, type_name, kwargs):
68 command = ("kwargs = %(kwargs)s\n"
69 "%(uuid)s = wrapper.factory(%(type_name)s, **kwargs)\n\n"
71 "uuid": self.format_value(uuid),
72 "type_name": self.format_value(type_name),
73 "kwargs": self.format_kwargs(kwargs)
76 self.dump_to_script(command)
78 def dump_create(self, uuid, clazzname, args):
82 command = ("args = %(args)s\n"
83 "%(uuid)s = wrapper.create(%(clazzname)s, *args)\n\n"
85 "uuid": self.format_value(uuid),
86 "clazzname": self.format_value(clazzname),
87 "args": self.format_args(args),
90 self.dump_to_script(command)
92 def dump_invoke(self, newuuid, uuid, operation, args, kwargs):
96 command = ("args = %(args)s\n"
97 "kwargs = %(kwargs)s\n"
98 "%(newuuid)s = wrapper.invoke(%(uuid)s, %(operation)s, *args, **kwargs)\n\n"
100 "newuuid": self.format_value(newuuid) if newuuid else "nothing",
101 "uuid": self.format_value(uuid),
102 "operation": self.format_value(operation),
103 "args": self.format_args(args),
104 "kwargs": self.format_kwargs(kwargs),
107 self.dump_to_script(command)
109 def dump_set(self, uuid, name, value):
113 command = ("wrapper.set(%(uuid)s, %(name)s, %(value)s)\n\n"
115 "uuid": self.format_value(uuid),
116 "name": self.format_value(name),
117 "value": self.format_value(value),
120 self.dump_to_script(command)
122 def dump_get(self, uuid, name):
126 command = ("wrapper.get(%(uuid)s, %(name)s)\n\n"
128 "uuid": self.format_value(uuid),
129 "name": self.format_value(name),
132 self.dump_to_script(command)
134 def dump_shutdown(self):
138 command = "wrapper.shutdown()\n\n"
139 self.dump_to_script(command)
141 def format_value(self, value):
142 if isinstance(value, str) and value.startswith("uuid"):
143 return value.replace("-", "")
146 return pprint.pformat(value)
148 def format_args(self, args):
149 return "[%s]" % ",".join(self.format_value(arg) for arg in args)
151 def format_kwargs(self, kwargs):
152 fkwargs = ["%s: %s" % (self.format_value(k), self.format_value(v)) for (k, v) in kwargs.items()]
154 return "dict({%s})" % ",".join(fkwargs)