Merge branch 'tests'
[nepi.git] / src / nepi / resources / linux / ns3 / ns3client.py
index 59b0458..ed41bdb 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
 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
 
 import base64
-import cPickle
+import pickle
 import errno
+import os
 import socket
+import time
 import weakref
+import threading
 
 from optparse import OptionParser, SUPPRESS_HELP
 
@@ -32,99 +34,84 @@ class LinuxNS3Client(NS3Client):
     def __init__(self, simulation):
         super(LinuxNS3Client, self).__init__()
         self._simulation = weakref.ref(simulation)
-
-        self._socat_proc = None
-        self.connect_client()
+        self._socket_lock = threading.Lock()
 
     @property
     def simulation(self):
         return self._simulation()
 
-    def connect_client(self):
-        if self.simulation.node.get("hostname") in ['localhost', '127.0.0.1']:
-            return
-
-        (out, err), self._socat_proc = self.simulation.node.socat(
-                self.simulation.local_socket,
-                self.simulation.remote_socket) 
-
-    def send_msg(self, msg, *args):
-        args = list(args)
-
-        sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
-        sock.connect(self.simulation.local_socket)
-
-        args.insert(0, msg)
-        def encode(arg):
-            arg = cPickle.dumps(arg)
-            return base64.b64encode(arg) 
-
-        encoded = "|".join(map(encode, args))
-        sock.send("%s\n" % encoded)
-        
-        reply = sock.recv(1024)
-        return cPickle.loads(base64.b64decode(reply))
-
-    def create(self, clazzname, *args):
-        args = list(args)
-        args.insert(0, clazzname)
-        
-        return self.send_msg(NS3WrapperMessage.CREATE, *args)
-
-    def factory(self, type_name, **kwargs):
-        args = [type_name]
-        args.append(kwargs)
-        
-        return self.send_msg(NS3WrapperMessage.FACTORY, *args)
-
-    def invoke(self, uuid, operation, *args):
-        args = list(args)
-        args.insert(0, operation)
-        args.insert(0, uuid)
-
-        return self.send_msg(NS3WrapperMessage.INVOKE, *args)
-
-    def set(self, uuid, name, value):
-        args = [uuid, name, value]
-
-        return self.send_msg(NS3WrapperMessage.SET, *args)
-
-    def get(self, uuid, name):
-        args = [uuid, name]
-
-        return self.send_msg(NS3WrapperMessage.GET, *args)
-
-    def trace(self, *args):
-        return self.send_msg(NS3WrapperMessage.TRACE, *args)
-
-    def start(self):
-        return self.send_msg(NS3WrapperMessage.START, [])
-
-    def stop(self, time = None):
-        args = None
-        if time:
-            args = [time]
-
-        return self.send_msg(NS3WrapperMessage.STOP, *args)
-
-    def shutdown(self):
-        ret = None
-
-        try:
-            ret = self.send_msg(NS3WrapperMessage.SHUTDOWN, [])
-        except:
-            pass
-
-        try:
-            if self._socat_proc:
-                self._socat_proc.kill()
-        except:
-            pass
-
+    def send_msg(self, msg_type, *args, **kwargs):
+        msg = [msg_type, args, kwargs]
+
+        def encode(item):
+            item = pickle.dumps(item)
+            return base64.b64encode(item)
+
+        encoded = "|".join(map(encode, msg))
+
+        with self._socket_lock:
+            if self.simulation.node.get("hostname") in ['localhost', '127.0.0.1']:
+                sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
+                sock.connect(self.simulation.remote_socket)
+                sock.send("%s\n" % encoded)
+                reply = sock.recv(1024)
+                sock.close()
+            else:
+                command = ( "python -c 'import socket;"
+                    "sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM);"
+                    "sock.connect(\"%(socket_addr)s\");"
+                    "msg = \"%(encoded_message)s\\n\";"
+                    "sock.send(msg);"
+                    "reply = sock.recv(1024);"
+                    "sock.close();"
+                    "print reply'") % {
+                        "encoded_message": encoded,
+                        "socket_addr": self.simulation.remote_socket,
+                        }
+
+                (reply, err), proc = self.simulation.node.execute(command, 
+                        with_lock = True) 
+
+                if (err and proc.poll()) or reply.strip() == "":
+                    msg = (" Couldn't connect to remote socket %s - REPLY: %s "
+                          "- ERROR: %s ") % (
+                            self.simulation.remote_socket, reply, err)
+                    self.simulation.error(msg, reply, err)
+                    raise RuntimeError(msg)
+                       
+        reply = pickle.loads(base64.b64decode(reply))
+
+        return reply
+
+    def create(self, *args, **kwargs):
+        return self.send_msg(NS3WrapperMessage.CREATE, *args, **kwargs)
+
+    def factory(self, *args, **kwargs):
+        return self.send_msg(NS3WrapperMessage.FACTORY, *args, **kwargs)
+
+    def invoke(self, *args, **kwargs):
+        return self.send_msg(NS3WrapperMessage.INVOKE, *args, **kwargs)
+
+    def set(self, *args, **kwargs):
+        return self.send_msg(NS3WrapperMessage.SET, *args, **kwargs)
+
+    def get(self, *args, **kwargs):
+        return self.send_msg(NS3WrapperMessage.GET, *args, **kwargs)
+
+    def flush(self, *args, **kwargs):
+        return self.send_msg(NS3WrapperMessage.FLUSH, *args, **kwargs)
+
+    def start(self, *args, **kwargs):
+        return self.send_msg(NS3WrapperMessage.START, *args, **kwargs)
+
+    def stop(self, *args, **kwargs):
+        return self.send_msg(NS3WrapperMessage.STOP, *args, **kwargs)
+
+    def shutdown(self, *args, **kwargs):
         try:
-            os.remove(self.simulation.local_socket)
+            return self.send_msg(NS3WrapperMessage.SHUTDOWN, *args, **kwargs)
         except:
             pass
 
-        return ret
+        return None