Synchronizing access to ns-3 linux client
[nepi.git] / src / nepi / resources / linux / ns3 / ns3client.py
1 #
2 #    NEPI, a framework to manage network experiments
3 #    Copyright (C) 2014 INRIA
4 #
5 #    This program is free software: you can redistribute it and/or modify
6 #    it under the terms of the GNU General Public License as published by
7 #    the Free Software Foundation, either version 3 of the License, or
8 #    (at your option) any later version.
9 #
10 #    This program is distributed in the hope that it will be useful,
11 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 #    GNU General Public License for more details.
14 #
15 #    You should have received a copy of the GNU General Public License
16 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 #
18 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
19
20 import base64
21 import cPickle
22 import errno
23 import os
24 import socket
25 import time
26 import weakref
27 import threading
28
29 from optparse import OptionParser, SUPPRESS_HELP
30
31 from nepi.resources.ns3.ns3client import NS3Client
32 from nepi.resources.ns3.ns3server import NS3WrapperMessage
33
34 class LinuxNS3Client(NS3Client):
35     def __init__(self, simulation):
36         super(LinuxNS3Client, self).__init__()
37         self._simulation = weakref.ref(simulation)
38         self._socket_lock = threading.Lock()
39
40     @property
41     def simulation(self):
42         return self._simulation()
43
44     def send_msg(self, msg_type, *args, **kwargs):
45         msg = [msg_type, args, kwargs]
46
47         def encode(item):
48             item = cPickle.dumps(item)
49             return base64.b64encode(item)
50
51         encoded = "|".join(map(encode, msg))
52
53         with self._socket_lock:
54             if self.simulation.node.get("hostname") in ['localhost', '127.0.0.1']:
55                 sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
56                 sock.connect(self.simulation.remote_socket)
57                 sock.send("%s\n" % encoded)
58                 reply = sock.recv(1024)
59                 sock.close()
60             else:
61                 command = ( "python -c 'import socket;"
62                     "sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM);"
63                     "sock.connect(\"%(socket_addr)s\");"
64                     "msg = \"%(encoded_message)s\\n\";"
65                     "sock.send(msg);"
66                     "reply = sock.recv(1024);"
67                     "sock.close();"
68                     "print reply'") % {
69                         "encoded_message": encoded,
70                         "socket_addr": self.simulation.remote_socket,
71                         }
72
73                 (reply, err), proc = self.simulation.node.execute(command, 
74                         with_lock = True) 
75
76                 if (err and proc.poll()) or reply.strip() == "":
77                     msg = (" Couldn't connect to remote socket %s - REPLY: %s "
78                           "- ERROR: %s ") % (
79                             self.simulation.remote_socket, reply, err)
80                     self.simulation.error(msg, reply, err)
81                     raise RuntimeError(msg)
82                        
83         reply = cPickle.loads(base64.b64decode(reply))
84
85         return reply
86
87     def create(self, *args, **kwargs):
88         return self.send_msg(NS3WrapperMessage.CREATE, *args, **kwargs)
89
90     def factory(self, *args, **kwargs):
91         return self.send_msg(NS3WrapperMessage.FACTORY, *args, **kwargs)
92
93     def invoke(self, *args, **kwargs):
94         return self.send_msg(NS3WrapperMessage.INVOKE, *args, **kwargs)
95
96     def set(self, *args, **kwargs):
97         return self.send_msg(NS3WrapperMessage.SET, *args, **kwargs)
98
99     def get(self, *args, **kwargs):
100         return self.send_msg(NS3WrapperMessage.GET, *args, **kwargs)
101
102     def flush(self, *args, **kwargs):
103         return self.send_msg(NS3WrapperMessage.FLUSH, *args, **kwargs)
104
105     def start(self, *args, **kwargs):
106         return self.send_msg(NS3WrapperMessage.START, *args, **kwargs)
107
108     def stop(self, *args, **kwargs):
109         return self.send_msg(NS3WrapperMessage.STOP, *args, **kwargs)
110
111     def shutdown(self, *args, **kwargs):
112         try:
113             return self.send_msg(NS3WrapperMessage.SHUTDOWN, *args, **kwargs)
114         except:
115             pass
116
117         return None
118