git://git.onelab.eu
/
nepi.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
applied the except and raise fixers to the master branch to close the gap with py3
[nepi.git]
/
src
/
nepi
/
resources
/
ns3
/
ns3server.py
diff --git
a/src/nepi/resources/ns3/ns3server.py
b/src/nepi/resources/ns3/ns3server.py
index
0b27a68
..
8b3cd79
100644
(file)
--- a/
src/nepi/resources/ns3/ns3server.py
+++ b/
src/nepi/resources/ns3/ns3server.py
@@
-3,9
+3,8
@@
# Copyright (C) 2014 INRIA
#
# This program is free software: you can redistribute it and/or modify
# 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
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
@@
-35,88
+34,59
@@
class NS3WrapperMessage:
INVOKE = "INVOKE"
SET = "SET"
GET = "GET"
INVOKE = "INVOKE"
SET = "SET"
GET = "GET"
- ENABLE_TRACE = "ENABLE_TRACE"
FLUSH = "FLUSH"
START = "START"
STOP = "STOP"
SHUTDOWN = "SHUTDOWN"
FLUSH = "FLUSH"
START = "START"
STOP = "STOP"
SHUTDOWN = "SHUTDOWN"
-def handle_message(ns3_wrapper, msg
,
args):
- if msg == NS3WrapperMessage.SHUTDOWN:
+def handle_message(ns3_wrapper, msg
_type, args, kw
args):
+ if msg
_type
== NS3WrapperMessage.SHUTDOWN:
ns3_wrapper.shutdown()
ns3_wrapper.shutdown()
-
- ns3_wrapper.logger.debug("SHUTDOWN")
-
+
return "BYEBYE"
return "BYEBYE"
- if msg == NS3WrapperMessage.STOP:
- time = None
- if args:
- time = args[0]
-
- ns3_wrapper.logger.debug("STOP time=%s" % str(time))
+ if msg_type == NS3WrapperMessage.STOP:
+ time = kwargs.get("time")
ns3_wrapper.stop(time=time)
ns3_wrapper.stop(time=time)
- return "STOPPED"
- if msg == NS3WrapperMessage.START:
- ns3_wrapper.logger.debug("START")
+ return "STOPPED"
+ if msg_type == NS3WrapperMessage.START:
ns3_wrapper.start()
ns3_wrapper.start()
+
return "STARTED"
return "STARTED"
- if msg == NS3WrapperMessage.CREATE:
+ if msg
_type
== NS3WrapperMessage.CREATE:
clazzname = args.pop(0)
clazzname = args.pop(0)
- ns3_wrapper.logger.debug("CREATE %s %s" % (clazzname, str(args)))
-
- uuid = ns3_wrapper.create(clazzname, *args)
- return uuid
-
- if msg == NS3WrapperMessage.FACTORY:
+ return ns3_wrapper.create(clazzname, *args)
+
+ if msg_type == NS3WrapperMessage.FACTORY:
type_name = args.pop(0)
type_name = args.pop(0)
- kwargs = args.pop(0)
-
- ns3_wrapper.logger.debug("FACTORY %s %s" % (type_name, str(kwargs)))
-
- uuid = ns3_wrapper.factory(type_name, **kwargs)
- return uuid
- if msg == NS3WrapperMessage.INVOKE:
+ return ns3_wrapper.factory(type_name, **kwargs)
+
+ if msg_type == NS3WrapperMessage.INVOKE:
uuid = args.pop(0)
operation = args.pop(0)
uuid = args.pop(0)
operation = args.pop(0)
-
- ns3_wrapper.logger.debug("INVOKE %s %s %s " % (uuid, operation,
- str(args)))
-
- uuid = ns3_wrapper.invoke(uuid, operation, *args)
- return uuid
+
+ return ns3_wrapper.invoke(uuid, operation, *args, **kwargs)
- if msg == NS3WrapperMessage.GET:
+ if msg
_type
== NS3WrapperMessage.GET:
uuid = args.pop(0)
name = args.pop(0)
uuid = args.pop(0)
name = args.pop(0)
- ns3_wrapper.logger.debug("GET %s %s" % (uuid, name))
-
- value = ns3_wrapper.get(uuid, name)
- return value
-
- if msg == NS3WrapperMessage.SET:
+ return ns3_wrapper.get(uuid, name)
+
+ if msg_type == NS3WrapperMessage.SET:
uuid = args.pop(0)
name = args.pop(0)
value = args.pop(0)
uuid = args.pop(0)
name = args.pop(0)
value = args.pop(0)
- ns3_wrapper.logger.debug("SET %s %s %s" % (uuid, name, str(value)))
-
- value = ns3_wrapper.set(uuid, name, value)
- return value
-
- if msg == NS3WrapperMessage.ENABLE_TRACE:
- ns3_wrapper.logger.debug("ENABLE_TRACE")
+ return ns3_wrapper.set(uuid, name, value)
- return "NOT YET IMPLEMENTED"
-
- if msg == NS3WrapperMessage.FLUSH:
+ if msg_type == NS3WrapperMessage.FLUSH:
# Forces flushing output and error streams.
# NS-3 output will stay unflushed until the program exits or
# explicit invocation flush is done
# Forces flushing output and error streams.
# NS-3 output will stay unflushed until the program exits or
# explicit invocation flush is done
@@
-127,11
+97,17
@@
def handle_message(ns3_wrapper, msg, args):
return "FLUSHED"
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
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 = ''
def recv_msg(conn):
msg = []
chunk = ''
@@
-139,7
+115,7
@@
def recv_msg(conn):
while '\n' not in chunk:
try:
chunk = conn.recv(1024)
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
if e[0] != errno.EINTR:
raise
# Ignore eintr errors
@@
-151,27
+127,32
@@
def recv_msg(conn):
# empty chunk = EOF
break
# empty chunk = EOF
break
- msg = ''.join(msg).split('\n')[0]
+ msg = ''.join(msg).strip()
+
+ # The message is formatted as follows:
+ # MESSAGE_TYPE|args|kwargs
+ #
+ # where MESSAGE_TYPE, args and kwargs are pickld and enoded in base64
- # The message might have arguments that will be appended
- # as a '|' separated list after the message identifier
- def decode(arg):
- arg = base64.b64decode(arg).rstrip()
- return cPickle.loads(arg)
+ def decode(item):
+ item = base64.b64decode(item).rstrip()
+ return cPickle.loads(item)
- d
args
= map(decode, msg.split("|"))
+ d
ecoded
= map(decode, msg.split("|"))
# decoded message
# decoded message
- dmsg = dargs.pop(0)
+ dmsg_type = decoded.pop(0)
+ dargs = list(decoded.pop(0)) # transforming touple into list
+ dkwargs = decoded.pop(0)
- return (dmsg
, d
args)
+ return (dmsg
_type, dargs, dkw
args)
def send_reply(conn, reply):
encoded = base64.b64encode(cPickle.dumps(reply))
conn.send("%s\n" % encoded)
def get_options():
def send_reply(conn, reply):
encoded = base64.b64encode(cPickle.dumps(reply))
conn.send("%s\n" % encoded)
def get_options():
- usage = ("usage: %prog -S <socket-name> -L <
NS_LOG
> -v ")
+ usage = ("usage: %prog -S <socket-name> -L <
ns-log> -D <enable-dump
> -v ")
parser = OptionParser(usage = usage)
parser = OptionParser(usage = usage)
@@
-183,6
+164,12
@@
def get_options():
help = "NS_LOG environmental variable to be set",
default = "", type="str")
help = "NS_LOG environmental variable to be set",
default = "", type="str")
+ parser.add_option("-D", "--enable-dump", dest="enable_dump",
+ help = "Enable dumping the remote executed ns-3 commands to a script "
+ "in order to later reproduce and debug the experiment",
+ action = "store_true",
+ default = False)
+
parser.add_option("-v", "--verbose",
help="Print debug output",
action="store_true",
parser.add_option("-v", "--verbose",
help="Print debug output",
action="store_true",
@@
-190,9
+177,11
@@
def get_options():
(options, args) = parser.parse_args()
(options, args) = parser.parse_args()
- return (options.socket_name, options.verbose, options.ns_log)
+ return (options.socket_name, options.verbose, options.ns_log,
+ options.enable_dump)
-def run_server(socket_name, level = logging.INFO, ns_log = None):
+def run_server(socket_name, level = logging.INFO, ns_log = None,
+ enable_dump = False):
# Sets NS_LOG environmental variable for NS debugging
if ns_log:
# Sets NS_LOG environmental variable for NS debugging
if ns_log:
@@
-200,12
+189,12
@@
def run_server(socket_name, level = logging.INFO, ns_log = None):
###### ns-3 wrapper instantiation
###### ns-3 wrapper instantiation
- ns3_wrapper = NS3Wrapper(loglevel=level)
+ ns3_wrapper = NS3Wrapper(loglevel=level
, enable_dump = enable_dump
)
ns3_wrapper.logger.info("STARTING...")
# create unix socket to receive instructions
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
sock.listen(0)
# wait for messages to arrive and process them
@@
-213,39
+202,50
@@
def run_server(socket_name, level = logging.INFO, ns_log = None):
while not stop:
conn, addr = sock.accept()
while not stop:
conn, addr = sock.accept()
- conn.settimeout(
5
)
+ conn.settimeout(
30
)
try:
try:
- (msg
,
args) = recv_msg(conn)
- except socket.timeout
,
e:
+ (msg
_type, args, kw
args) = recv_msg(conn)
+ except socket.timeout
as
e:
# Ingore time-out
# Ingore time-out
+ close_socket(conn)
continue
continue
- if not msg:
+ if not msg
_type
:
# Ignore - connection lost
# Ignore - connection lost
- break
+ close_socket(conn)
+ continue
- if msg == NS3WrapperMessage.SHUTDOWN:
+ if msg
_type
== NS3WrapperMessage.SHUTDOWN:
stop = True
try:
stop = True
try:
- reply = handle_message(ns3_wrapper, msg
,
args)
+ reply = handle_message(ns3_wrapper, msg
_type, args, kw
args)
except:
import traceback
err = traceback.format_exc()
ns3_wrapper.logger.error(err)
except:
import traceback
err = traceback.format_exc()
ns3_wrapper.logger.error(err)
+ close_socket(conn)
raise
try:
send_reply(conn, reply)
except socket.error:
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__':
ns3_wrapper.logger.info("EXITING...")
if __name__ == '__main__':
- (socket_name, verbose, ns_log) = get_options()
+ (socket_name, verbose, ns_log
, enable_dump
) = get_options()
## configure logging
FORMAT = "%(asctime)s %(name)s %(levelname)-4s %(message)s"
## configure logging
FORMAT = "%(asctime)s %(name)s %(levelname)-4s %(message)s"
@@
-254,5
+254,5
@@
if __name__ == '__main__':
logging.basicConfig(format = FORMAT, level = level)
## Run the server
logging.basicConfig(format = FORMAT, level = level)
## Run the server
- run_server(socket_name, level, ns_log)
+ run_server(socket_name, level, ns_log
, enable_dump
)