# Copyright (C) 2014 INRIA
#
# This program is free software: you can redistribute it and/or modify
-# it under the terms of the GNU General Public License as published by
-# the Free Software Foundation, either version 3 of the License, or
-# (at your option) any later version.
+# it under the terms of the GNU General Public License version 2 as
+# published by the Free Software Foundation;
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
return "FLUSHED"
-def create_socket(socket_name):
+def open_socket(socket_name):
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
sock.bind(socket_name)
return sock
+def close_socket(sock):
+ try:
+ sock.close()
+ except:
+ pass
+
def recv_msg(conn):
msg = []
chunk = ''
while '\n' not in chunk:
try:
chunk = conn.recv(1024)
- except (OSError, socket.error), e:
+ except (OSError, socket.error) as e:
if e[0] != errno.EINTR:
raise
# Ignore eintr errors
ns3_wrapper.logger.info("STARTING...")
# create unix socket to receive instructions
- sock = create_socket(socket_name)
+ sock = open_socket(socket_name)
sock.listen(0)
# wait for messages to arrive and process them
while not stop:
conn, addr = sock.accept()
- conn.settimeout(5)
+ conn.settimeout(30)
try:
(msg_type, args, kwargs) = recv_msg(conn)
- except socket.timeout, e:
+ except socket.timeout as e:
# Ingore time-out
+ close_socket(conn)
continue
if not msg_type:
# Ignore - connection lost
- break
+ close_socket(conn)
+ continue
if msg_type == NS3WrapperMessage.SHUTDOWN:
stop = True
import traceback
err = traceback.format_exc()
ns3_wrapper.logger.error(err)
+ close_socket(conn)
raise
try:
send_reply(conn, reply)
except socket.error:
- break
+ import traceback
+ err = traceback.format_exc()
+ ns3_wrapper.logger.error(err)
+ close_socket(conn)
+ raise
+ close_socket(conn)
+
+ close_socket(sock)
+
ns3_wrapper.logger.info("EXITING...")
if __name__ == '__main__':