README moves to markdown
[nepi.git] / src / nepi / resources / linux / netns / netnsclient.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
27 from optparse import OptionParser, SUPPRESS_HELP
28
29 from nepi.resources.netns.netnsclient import NetNSClient
30 from nepi.resources.netns.netnsserver import NetNSWrapperMessage
31
32 class LinuxNetNSClient(NetNSClient):
33     def __init__(self, emulation):
34         super(LinuxNetNSClient, self).__init__()
35         self._emulation = weakref.ref(emulation)
36
37         self._socat_proc = None
38
39     @property
40     def emulation(self):
41         return self._emulation()
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         if self.emulation.node.get("hostname") in ['localhost', '127.0.0.1']:
53             sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
54             sock.connect(self.emulation.remote_socket)
55             sock.send("%s\n" % encoded)
56             reply = sock.recv(1024)
57             sock.close()
58         else:
59             command = ( "python -c 'import socket;"
60                 "sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM);"
61                 "sock.connect(\"%(socket_addr)s\");"
62                 "msg = \"%(encoded_message)s\\n\";"
63                 "sock.send(msg);"
64                 "reply = sock.recv(1024);"
65                 "sock.close();"
66                 "print reply'") % {
67                     "encoded_message": encoded,
68                     "socket_addr": self.emulation.remote_socket,
69                     }
70
71             (reply, err), proc = self.emulation.node.execute(command, 
72                     with_lock = True) 
73
74             if (err and proc.poll()) or reply.strip() == "":
75                 msg = (" Couldn't connect to remote socket %s - REPLY: %s "
76                       "- ERROR: %s ") % (
77                         self.emulation.remote_socket, reply, err)
78                 self.emulation.error(msg, reply, err)
79                 raise RuntimeError(msg)
80                    
81         reply = pickle.loads(base64.b64decode(reply))
82
83         return reply
84
85     def create(self, *args, **kwargs):
86         return self.send_msg(NetNSWrapperMessage.CREATE, *args, **kwargs)
87
88     def invoke(self, *args, **kwargs):
89         return self.send_msg(NetNSWrapperMessage.INVOKE, *args, **kwargs)
90
91     def set(self, *args, **kwargs):
92         return self.send_msg(NetNSWrapperMessage.SET, *args, **kwargs)
93
94     def get(self, *args, **kwargs):
95         return self.send_msg(NetNSWrapperMessage.GET, *args, **kwargs)
96
97     def flush(self, *args, **kwargs):
98         return self.send_msg(NetNSWrapperMessage.FLUSH, *args, **kwargs)
99
100     def shutdown(self, *args, **kwargs):
101         try:
102             return self.send_msg(NetNSWrapperMessage.SHUTDOWN, *args, **kwargs)
103         except:
104             pass
105
106         return None
107