From: Alina Quereilhac Date: Fri, 12 Dec 2014 18:45:37 +0000 (+0100) Subject: Synchronizing access to ns-3 linux client X-Git-Tag: nepi-3.2.0~46 X-Git-Url: http://git.onelab.eu/?p=nepi.git;a=commitdiff_plain;h=258959a9047e0a84d2b1889863d65808413162ca Synchronizing access to ns-3 linux client --- diff --git a/src/nepi/resources/linux/ns3/ns3client.py b/src/nepi/resources/linux/ns3/ns3client.py index f81e449e..1dd0c5a7 100644 --- a/src/nepi/resources/linux/ns3/ns3client.py +++ b/src/nepi/resources/linux/ns3/ns3client.py @@ -24,6 +24,7 @@ import os import socket import time import weakref +import threading from optparse import OptionParser, SUPPRESS_HELP @@ -34,8 +35,7 @@ class LinuxNS3Client(NS3Client): def __init__(self, simulation): super(LinuxNS3Client, self).__init__() self._simulation = weakref.ref(simulation) - - self._socat_proc = None + self._socket_lock = threading.Lock() @property def simulation(self): @@ -50,35 +50,36 @@ class LinuxNS3Client(NS3Client): encoded = "|".join(map(encode, msg)) - 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) - + 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 = cPickle.loads(base64.b64decode(reply)) return reply