INVOKE = "INVOKE"
SET = "SET"
GET = "GET"
- ENABLE_TRACE = "ENABLE_TRACE"
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, kwargs):
+ if msg_type == NS3WrapperMessage.SHUTDOWN:
ns3_wrapper.shutdown()
ns3_wrapper.logger.debug("SHUTDOWN")
return "BYEBYE"
- if msg == NS3WrapperMessage.STOP:
- time = None
- if args:
- time = args[0]
+ if msg_type == NS3WrapperMessage.STOP:
+ time = kwargs.get("time")
ns3_wrapper.logger.debug("STOP time=%s" % str(time))
ns3_wrapper.stop(time=time)
return "STOPPED"
- if msg == NS3WrapperMessage.START:
+ if msg_type == NS3WrapperMessage.START:
ns3_wrapper.logger.debug("START")
ns3_wrapper.start()
return "STARTED"
- if msg == NS3WrapperMessage.CREATE:
+ if msg_type == NS3WrapperMessage.CREATE:
clazzname = args.pop(0)
- ns3_wrapper.logger.debug("CREATE %s %s" % (clazzname, str(args)))
+ result = ns3_wrapper.create(clazzname, *args)
- uuid = ns3_wrapper.create(clazzname, *args)
- return uuid
+ ns3_wrapper.logger.debug("%s CREATE %s %s" % (str(result), clazzname,
+ str(args)))
+
+ return result
- if msg == NS3WrapperMessage.FACTORY:
+ if msg_type == NS3WrapperMessage.FACTORY:
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
+ result = ns3_wrapper.factory(type_name, **kwargs)
+
+ ns3_wrapper.logger.debug("%s FACTORY %s %s" % (str(result), type_name,
+ str(kwargs)))
+
+ return result
- if msg == NS3WrapperMessage.INVOKE:
+ if msg_type == NS3WrapperMessage.INVOKE:
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
+
+ result = ns3_wrapper.invoke(uuid, operation, *args, **kwargs)
+
+ ns3_wrapper.logger.debug("%s INVOKE %s %s %s %s " % (str(result), uuid,
+ operation, str(args), str(kwargs)))
+
+ return result
- if msg == NS3WrapperMessage.GET:
+ if msg_type == NS3WrapperMessage.GET:
uuid = args.pop(0)
name = args.pop(0)
- ns3_wrapper.logger.debug("GET %s %s" % (uuid, name))
+ result = ns3_wrapper.get(uuid, name)
+
+ ns3_wrapper.logger.debug("%s GET %s %s" % (str(result), uuid, name))
- value = ns3_wrapper.get(uuid, name)
- return value
+ return result
- if msg == NS3WrapperMessage.SET:
+ if msg_type == NS3WrapperMessage.SET:
uuid = args.pop(0)
name = args.pop(0)
value = args.pop(0)
- ns3_wrapper.logger.debug("SET %s %s %s" % (uuid, name, str(value)))
+ result = ns3_wrapper.set(uuid, name, value)
- value = ns3_wrapper.set(uuid, name, value)
- return value
-
- if msg == NS3WrapperMessage.ENABLE_TRACE:
- ns3_wrapper.logger.debug("ENABLE_TRACE")
+ ns3_wrapper.logger.debug("%s SET %s %s %s" % (str(result), uuid, name,
+ str(value)))
- return "NOT YET IMPLEMENTED"
+ return result
- 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
# 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)
- dargs = map(decode, msg.split("|"))
+ decoded = map(decode, msg.split("|"))
# 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, dargs)
+ return (dmsg_type, dargs, dkwargs)
def send_reply(conn, reply):
encoded = base64.b64encode(cPickle.dumps(reply))
conn.settimeout(5)
try:
- (msg, args) = recv_msg(conn)
+ (msg_type, args, kwargs) = recv_msg(conn)
except socket.timeout, e:
# Ingore time-out
continue
- if not msg:
+ if not msg_type:
# Ignore - connection lost
break
- if msg == NS3WrapperMessage.SHUTDOWN:
+ if msg_type == NS3WrapperMessage.SHUTDOWN:
stop = True
try:
- reply = handle_message(ns3_wrapper, msg, args)
+ reply = handle_message(ns3_wrapper, msg_type, args, kwargs)
except:
import traceback
err = traceback.format_exc()