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>
24 from optparse import OptionParser, SUPPRESS_HELP
26 from ns3wrapper import NS3Wrapper
28 class NS3WrapperMessage:
39 def handle_message(ns3_wrapper, msg, args):
40 if msg == NS3WrapperMessage.SHUTDOWN:
41 ns3_wrapper.shutdown()
44 if msg == NS3WrapperMessage.STOP:
49 ns3_wrapper.stop(time=time)
52 if msg == NS3WrapperMessage.START:
56 if msg == NS3WrapperMessage.CREATE:
57 clazzname = args.pop(0)
59 uuid = ns3_wrapper.create(clazzname, *args)
62 if msg == NS3WrapperMessage.FACTORY:
63 type_name = args.pop(0)
66 uuid = ns3_wrapper.factory(type_name, **kwargs)
69 if msg == NS3WrapperMessage.INVOKE:
71 operation = args.pop(0)
73 uuid = ns3_wrapper.invoke(uuid, operation, *args)
76 if msg == NS3WrapperMessage.GET:
80 value = ns3_wrapper.get(uuid, name)
83 if msg == NS3WrapperMessage.SET:
88 value = ns3_wrapper.set(uuid, name, value)
91 if msg == NS3WrapperMessage.TRACE:
92 return "NOT IMPLEMENTED"
94 def create_socket(socket_name):
95 sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
96 sock.bind(socket_name)
103 while '\n' not in chunk:
105 chunk = conn.recv(1024)
106 except (OSError, socket.error), e:
107 if e[0] != errno.EINTR:
109 # Ignore eintr errors
118 msg = ''.join(msg).split('\n')[0]
120 # The message might have arguments that will be appended
121 # as a '|' separated list after the message identifier
123 arg = base64.b64decode(arg).rstrip()
124 return cPickle.loads(arg)
126 dargs = map(decode, msg.split("|"))
133 def send_reply(conn, reply):
134 encoded = base64.b64encode(cPickle.dumps(reply))
135 conn.send("%s\n" % encoded)
138 usage = ("usage: %prog -S <socket-name>")
140 parser = OptionParser(usage = usage)
142 parser.add_option("-S", "--socket-name", dest="socket_name",
143 help = "Name for the unix socket used to interact with this process",
144 default = "tap.sock", type="str")
146 (options, args) = parser.parse_args()
148 return options.socket_name
150 def run_server(socket_name):
151 ns3_wrapper = NS3Wrapper()
153 # create unix socket to receive instructions
154 sock = create_socket(socket_name)
157 # wait for messages to arrive and process them
161 conn, addr = sock.accept()
165 (msg, args) = recv_msg(conn)
166 except socket.timeout, e:
171 # Ignore - connection lost
174 ns3_wrapper.logger.debug("Message received %s args %s" % ( msg, str(args)))
176 if msg == NS3WrapperMessage.SHUTDOWN:
179 reply = handle_message(ns3_wrapper, msg, args)
182 send_reply(conn, reply)
186 if __name__ == '__main__':
188 socket_name = get_options()
190 run_server(socket_name)