Merge branch 'tests'
[nepi.git] / src / nepi / resources / netns / netnsserver.py
index 826843e..f2c37f2 100644 (file)
@@ -3,9 +3,8 @@
 #    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
@@ -18,7 +17,7 @@
 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
 
 import base64
-import cPickle
+import pickle
 import errno
 import logging
 import os
@@ -90,7 +89,7 @@ def recv_msg(conn):
     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
@@ -111,9 +110,9 @@ def recv_msg(conn):
 
     def decode(item):
         item = base64.b64decode(item).rstrip()
-        return cPickle.loads(item)
+        return pickle.loads(item)
 
-    decoded = map(decode, msg.split("|"))
+    decoded = [ decode(x) for x in msg.split("|")]
 
     # decoded message
     dmsg_type = decoded.pop(0)
@@ -123,11 +122,11 @@ def recv_msg(conn):
     return (dmsg_type, dargs, dkwargs)
 
 def send_reply(conn, reply):
-    encoded = base64.b64encode(cPickle.dumps(reply))
+    encoded = base64.b64encode(pickle.dumps(reply))
     conn.send("%s\n" % encoded)
 
 def get_options():
-    usage = ("usage: %prog -S <socket-name> -L <ns-log>  -D <enable-dump> -v ")
+    usage = ("usage: %prog -S <socket-name> -D <enable-dump> -v ")
     
     parser = OptionParser(usage = usage)
 
@@ -135,12 +134,8 @@ def get_options():
         help = "Name for the unix socket used to interact with this process", 
         default = "tap.sock", type="str")
 
-    parser.add_option("-L", "--ns-log", dest="ns_log",
-        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 "
+        help = "Enable dumping the remote executed commands to a script "
             "in order to later reproduce and debug the experiment",
         action = "store_true",
         default = False)
@@ -152,17 +147,16 @@ def get_options():
 
     (options, args) = parser.parse_args()
     
-    return (options.socket_name, options.verbose, options.ns_log,
-            options.enable_dump)
+    return (options.socket_name, options.verbose, options.enable_dump)
 
-def run_server(socket_name, level = logging.INFO, ns_log = None, 
+def run_server(socket_name, level = logging.INFO, 
         enable_dump = False):
 
-    # Sets NS_LOG environmental variable for NS debugging
-    if ns_log:
-        os.environ["NS_LOG"] = ns_log
-
-    ###### ns-3 wrapper instantiation
+    ###### wrapper instantiation
+    if level == logging.DEBUG:
+        from syslog import LOG_DEBUG
+        import netns
+        netns.environ.set_log_level(LOG_DEBUG)
 
     wrapper = NetNSWrapper(loglevel=level, enable_dump = enable_dump)
     
@@ -181,7 +175,7 @@ def run_server(socket_name, level = logging.INFO, ns_log = None,
 
         try:
             (msg_type, args, kwargs) = recv_msg(conn)
-        except socket.timeout, e:
+        except socket.timeout as e:
             # Ingore time-out
             continue
 
@@ -209,14 +203,14 @@ def run_server(socket_name, level = logging.INFO, ns_log = None,
 
 if __name__ == '__main__':
             
-    (socket_name, verbose, ns_log, enable_dump) = get_options()
+    (socket_name, verbose, enable_dump) = get_options()
 
     ## configure logging
     FORMAT = "%(asctime)s %(name)s %(levelname)-4s %(message)s"
     level = logging.DEBUG if verbose else logging.INFO
-
+    
     logging.basicConfig(format = FORMAT, level = level)
 
     ## Run the server
-    run_server(socket_name, level, ns_log, enable_dump)
+    run_server(socket_name, level, enable_dump)