2 # NEPI, a framework to manage network experiments
3 # Copyright (C) 2014 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>
27 from optparse import OptionParser, SUPPRESS_HELP
29 from ns3wrapper import NS3Wrapper
31 class NS3WrapperMessage:
42 def handle_message(ns3_wrapper, msg, args):
43 if msg == NS3WrapperMessage.SHUTDOWN:
44 ns3_wrapper.shutdown()
46 ns3_wrapper.logger.debug("SHUTDOWN")
50 if msg == NS3WrapperMessage.STOP:
55 ns3_wrapper.logger.debug("STOP time=%s" % str(time))
57 ns3_wrapper.stop(time=time)
60 if msg == NS3WrapperMessage.START:
61 ns3_wrapper.logger.debug("START")
66 if msg == NS3WrapperMessage.CREATE:
67 clazzname = args.pop(0)
69 ns3_wrapper.logger.debug("CREATE %s %s" % (clazzname, str(args)))
71 uuid = ns3_wrapper.create(clazzname, *args)
74 if msg == NS3WrapperMessage.FACTORY:
75 type_name = args.pop(0)
78 ns3_wrapper.logger.debug("FACTORY %s %s" % (type_name, str(kwargs)))
80 uuid = ns3_wrapper.factory(type_name, **kwargs)
83 if msg == NS3WrapperMessage.INVOKE:
85 operation = args.pop(0)
87 ns3_wrapper.logger.debug("INVOKE %s %s %s" % (uuid, operation, str(args)))
89 uuid = ns3_wrapper.invoke(uuid, operation, *args)
92 if msg == NS3WrapperMessage.GET:
96 ns3_wrapper.logger.debug("GET %s %s" % (uuid, name))
98 value = ns3_wrapper.get(uuid, name)
101 if msg == NS3WrapperMessage.SET:
106 ns3_wrapper.logger.debug("SET %s %s" % (uuid, name, str(value)))
108 value = ns3_wrapper.set(uuid, name, value)
111 if msg == NS3WrapperMessage.TRACE:
112 ns3_wrapper.logger.debug("TRACE")
113 return "NOT IMPLEMENTED"
115 def create_socket(socket_name):
116 sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
117 sock.bind(socket_name)
124 while '\n' not in chunk:
126 chunk = conn.recv(1024)
127 except (OSError, socket.error), e:
128 if e[0] != errno.EINTR:
130 # Ignore eintr errors
139 msg = ''.join(msg).split('\n')[0]
141 # The message might have arguments that will be appended
142 # as a '|' separated list after the message identifier
144 arg = base64.b64decode(arg).rstrip()
145 return cPickle.loads(arg)
147 dargs = map(decode, msg.split("|"))
154 def send_reply(conn, reply):
155 encoded = base64.b64encode(cPickle.dumps(reply))
156 conn.send("%s\n" % encoded)
159 usage = ("usage: %prog -S <socket-name> -L <NS_LOG> -v ")
161 parser = OptionParser(usage = usage)
163 parser.add_option("-S", "--socket-name", dest="socket_name",
164 help = "Name for the unix socket used to interact with this process",
165 default = "tap.sock", type="str")
167 parser.add_option("-L", "--ns-log", dest="ns_log",
168 help = "NS_LOG environmental variable to be set",
169 default = "", type="str")
171 parser.add_option("-v", "--verbose",
172 help="Print debug output",
174 dest="verbose", default=False)
176 (options, args) = parser.parse_args()
178 return options.socket_name, options.verbose, options.ns_log
180 def run_server(socket_name, verbose = False, ns_log = None):
182 level = logging.DEBUG if verbose else logging.INFO
184 # Sets NS_LOG environmental variable for NS debugging
186 os.environ["NS_LOG"] = ns_log
188 ###### ns-3 wrapper instantiation
190 ns3_wrapper = NS3Wrapper(loglevel=level)
192 # create unix socket to receive instructions
193 sock = create_socket(socket_name)
196 # wait for messages to arrive and process them
200 conn, addr = sock.accept()
204 (msg, args) = recv_msg(conn)
205 except socket.timeout, e:
210 # Ignore - connection lost
213 if msg == NS3WrapperMessage.SHUTDOWN:
216 reply = handle_message(ns3_wrapper, msg, args)
219 send_reply(conn, reply)
223 if __name__ == '__main__':
225 (socket_name, verbose, ns_log) = get_options()
227 run_server(socket_name, verbose, ns_log)