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>
21 ############ METHODS DEBUG NS3WRAPPER EXECUTION
23 ## The ns3wrapper works in ditributed mode, receiving instructions from
24 ## a remote client. This makes it very hard to debug scripting errors
25 ## in the client side. To simplify error debugging, when set to debug mode,
26 ## the ns3wrapper dumps every executed line to a script that can be then
27 ## executed locally to reproduce and debug the experiment.
29 ###########################################################################
33 SINGLETON = "singleton::"
35 class NS3WrapperDebuger(object):
36 def __init__(self, enabled):
37 super(NS3WrapperDebuger, self).__init__()
38 self._enabled = enabled
39 self._script_path = "debug.py"
48 def script_path(self):
49 return self._script_path
51 def dump_to_script(self, command):
52 f = open(self.script_path, "a")
53 f.write("%s" % command)
56 def dump_header(self):
61 from ns3wrapper import NS3Wrapper
63 wrapper = NS3Wrapper()
66 self.dump_to_script(header)
68 def dump_factory(self, uuid, type_name, kwargs):
72 command = ("kwargs = %(kwargs)s\n"
73 "%(uuid)s = wrapper.factory(%(type_name)s, **kwargs)\n\n"
75 "uuid": self.format_value(uuid),
76 "type_name": self.format_value(type_name),
77 "kwargs": self.format_kwargs(kwargs)
80 self.dump_to_script(command)
82 def dump_create(self, uuid, clazzname, args):
86 command = ("args = %(args)s\n"
87 "%(uuid)s = wrapper.create(%(clazzname)s, *args)\n\n"
89 "uuid": self.format_value(uuid),
90 "clazzname": self.format_value(clazzname),
91 "args": self.format_args(args),
94 self.dump_to_script(command)
96 def dump_invoke(self, newuuid, uuid, operation, args, kwargs):
100 command = ("args = %(args)s\n"
101 "kwargs = %(kwargs)s\n"
102 "%(newuuid)s = wrapper.invoke(%(uuid)s, %(operation)s, *args, **kwargs)\n\n"
104 "newuuid": self.format_value(newuuid) if newuuid else "nothing",
105 "uuid": self.format_value(uuid),
106 "operation": self.format_value(operation),
107 "args": self.format_args(args),
108 "kwargs": self.format_kwargs(kwargs),
111 self.dump_to_script(command)
113 def dump_set(self, uuid, name, value):
117 command = ("wrapper.set(%(uuid)s, %(name)s, %(value)s)\n\n"
119 "uuid": self.format_value(uuid),
120 "name": self.format_value(name),
121 "value": self.format_value(value),
124 self.dump_to_script(command)
126 def dump_get(self, uuid, name):
130 command = ("wrapper.get(%(uuid)s, %(name)s)\n\n"
132 "uuid": self.format_value(uuid),
133 "name": self.format_value(name),
136 self.dump_to_script(command)
138 def dump_start(self):
142 command = "wrapper.start()\n\n"
143 self.dump_to_script(command)
145 def dump_stop(self, time = None):
149 command = ("wrapper.stop(time=%(time)s)\n\n"
151 "time": self.format_value(time) if time else "None",
154 self.dump_to_script(command)
156 def dump_shutdown(self):
160 command = "wrapper.shutdown()\n\n"
161 self.dump_to_script(command)
163 def format_value(self, value):
164 if isinstance(value, str) and value.startswith("uuid"):
165 return value.replace("-", "")
168 return pprint.pformat(value)
170 def format_args(self, args):
171 fargs = map(self.format_value, args)
172 return "[%s]" % ",".join(fargs)
174 def format_kwargs(self, kwargs):
175 fkwargs = map(lambda (k,w):
176 "%s: %s" % (self.format_value(k), self.format_value(w)),
179 return "dict({%s})" % ",".join(fkwargs)