systematic use of context managers for dealing with files instead of open()/close...
[nepi.git] / src / nepi / resources / ns3 / ns3wrapper_debug.py
1 #
2 #    NEPI, a framework to manage network experiments
3 #    Copyright (C) 2013 INRIA
4 #
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;
8 #
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.
13 #
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/>.
16 #
17 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
18
19
20 ############ METHODS DEBUG NS3WRAPPER EXECUTION
21 ##
22 ## The ns3wrapper 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.
27 ##
28 ###########################################################################
29
30 import logging
31
32 SINGLETON = "singleton::"
33
34 class NS3WrapperDebuger(object):
35     def __init__(self, enabled):
36         super(NS3WrapperDebuger, self).__init__()
37         self._enabled = enabled
38         self._script_path = "debug.py"
39
40         self.dump_header()
41
42     @property
43     def enabled(self):
44         return self._enabled
45
46     @property
47     def script_path(self):
48         return self._script_path
49
50     def dump_to_script(self, command):
51         with open(self.script_path, "a") as f:
52             f.write("%s" % command)
53
54     def dump_header(self):
55         if not self.enabled:
56             return
57
58         header = """
59 from ns3wrapper import NS3Wrapper
60
61 wrapper = NS3Wrapper()
62
63 """
64         self.dump_to_script(header)
65
66     def dump_factory(self, uuid, type_name, kwargs):
67         if not self.enabled:
68             return
69
70         command = ("kwargs = %(kwargs)s\n"
71                 "%(uuid)s = wrapper.factory(%(type_name)s, **kwargs)\n\n" 
72                 ) % dict({
73                  "uuid": self.format_value(uuid),
74                  "type_name": self.format_value(type_name),
75                  "kwargs": self.format_kwargs(kwargs)
76                 })
77
78         self.dump_to_script(command)
79
80     def dump_create(self, uuid, clazzname, args):
81         if not self.enabled:
82             return
83
84         command = ("args = %(args)s\n"
85                 "%(uuid)s = wrapper.create(%(clazzname)s, *args)\n\n" 
86                 ) % dict({
87                  "uuid": self.format_value(uuid),
88                  "clazzname": self.format_value(clazzname),
89                  "args": self.format_args(args),
90                 })
91
92         self.dump_to_script(command)
93
94     def dump_invoke(self, newuuid, uuid, operation, args, kwargs):
95         if not self.enabled:
96             return
97
98         command = ("args = %(args)s\n"
99                    "kwargs = %(kwargs)s\n"
100                    "%(newuuid)s = wrapper.invoke(%(uuid)s, %(operation)s, *args, **kwargs)\n\n" 
101                 ) % dict({
102                  "newuuid": self.format_value(newuuid) if newuuid else "nothing",
103                  "uuid": self.format_value(uuid),
104                  "operation": self.format_value(operation),
105                  "args": self.format_args(args),
106                  "kwargs": self.format_kwargs(kwargs),
107                 })
108
109         self.dump_to_script(command)
110
111     def dump_set(self, uuid, name, value):
112         if not self.enabled:
113             return
114
115         command = ("wrapper.set(%(uuid)s, %(name)s, %(value)s)\n\n" 
116                 ) % dict({
117                  "uuid": self.format_value(uuid),
118                  "name": self.format_value(name),
119                  "value": self.format_value(value),
120                 })
121
122         self.dump_to_script(command)
123
124     def dump_get(self, uuid, name):
125         if not self.enabled:
126             return
127
128         command = ("wrapper.get(%(uuid)s, %(name)s)\n\n" 
129                 ) % dict({
130                  "uuid": self.format_value(uuid),
131                  "name": self.format_value(name),
132                 })
133         
134         self.dump_to_script(command)
135
136     def dump_start(self):
137         if not self.enabled:
138             return
139
140         command = "wrapper.start()\n\n"
141         self.dump_to_script(command)
142
143     def dump_stop(self, time = None):
144         if not self.enabled:
145             return
146
147         command = ("wrapper.stop(time=%(time)s)\n\n" 
148                 ) % dict({
149                  "time": self.format_value(time) if time else "None",
150                 })
151
152         self.dump_to_script(command)
153
154     def dump_shutdown(self):
155         if not self.enabled:
156             return
157
158         command = "wrapper.shutdown()\n\n"
159         self.dump_to_script(command)
160
161     def format_value(self, value):
162         if isinstance(value, str) and value.startswith("uuid"):
163             return value.replace("-", "")
164
165         import pprint 
166         return pprint.pformat(value)
167
168     def format_args(self, args):
169         fargs = map(self.format_value, args)
170         return "[%s]" % ",".join(fargs)
171
172     def format_kwargs(self, kwargs):
173         fkwargs = map(lambda (k,w): 
174                "%s: %s" % (self.format_value(k), self.format_value(w)), 
175             kwargs.iteritems())
176         
177         return  "dict({%s})" % ",".join(fkwargs)
178