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:
38 def handle_message(ns3_wrapper, msg, args):
39 if msg == NS3WrapperMessage.SHUTDOWN:
40 ns3_wrapper.shutdown()
43 if msg == NS3WrapperMessage.STOP:
48 ns3_wrapper.stop(time=time)
51 if msg == NS3WrapperMessage.START:
55 if msg == NS3WrapperMessage.CREATE:
56 clazzname = args.pop(0)
58 uuid = ns3_wrapper.create(clazzname, *args)
61 if msg == NS3WrapperMessage.INVOKE:
63 operation = args.pop(0)
65 uuid = ns3_wrapper.invoke(uuid, operation, *args)
68 if msg == NS3WrapperMessage.GET:
72 value = ns3_wrapper.get(uuid, name)
75 if msg == NS3WrapperMessage.SET:
80 value = ns3_wrapper.set(uuid, name, value)
83 if msg == NS3WrapperMessage.TRACE:
84 return "NOT IMPLEMENTED"
86 def create_socket(socket_name):
87 sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
88 sock.bind(socket_name)
95 while '\n' not in chunk:
97 chunk = conn.recv(1024)
98 except (OSError, socket.error), e:
99 if e[0] != errno.EINTR:
101 # Ignore eintr errors
110 msg = ''.join(msg).split('\n')[0]
112 # The message might have arguments that will be appended
113 # as a '|' separated list after the message identifier
115 arg = base64.b64decode(arg).rstrip()
116 return cPickle.loads(arg)
118 dargs = map(decode, msg.split("|"))
125 def send_reply(conn, reply):
126 encoded = base64.b64encode(cPickle.dumps(reply))
127 conn.send("%s\n" % encoded)
130 usage = ("usage: %prog -S <socket-name>")
132 parser = OptionParser(usage = usage)
134 parser.add_option("-S", "--socket-name", dest="socket_name",
135 help = "Name for the unix socket used to interact with this process",
136 default = "tap.sock", type="str")
138 (options, args) = parser.parse_args()
140 return options.socket_name
142 def run_server(socket_name):
143 ns3_wrapper = NS3Wrapper()
145 # create unix socket to receive instructions
146 sock = create_socket(socket_name)
149 # wait for messages to arrive and process them
153 conn, addr = sock.accept()
157 (msg, args) = recv_msg(conn)
158 except socket.timeout, e:
163 # Ignore - connection lost
166 ns3_wrapper.logger.debug("Message received %s args %s" % ( msg, str(args)))
168 if msg == NS3WrapperMessage.SHUTDOWN:
171 reply = handle_message(ns3_wrapper, msg, args)
174 send_reply(conn, reply)
178 if __name__ == '__main__':
180 socket_name = get_options()
182 run_server(socket_name)