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