0233d5f87bd8f026f13bff8305befa0efe63d4ba
[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 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
28 from optparse import OptionParser, SUPPRESS_HELP
29
30 from nepi.resources.netns.netnsclient import NetNSClient
31 from nepi.resources.netns.netnsserver import NetNSWrapperMessage
32
33 class LinuxNetNSClient(NetNSClient):
34     def __init__(self, emulation):
35         super(LinuxNetNSClient, self).__init__()
36         self._emulation = weakref.ref(emulation)
37
38         self._socat_proc = None
39
40     @property
41     def emulation(self):
42         return self._emulation()
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         if self.emulation.node.get("hostname") in ['localhost', '127.0.0.1']:
54             sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
55             sock.connect(self.emulation.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.emulation.remote_socket,
70                     }
71
72             (reply, err), proc = self.emulation.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.emulation.remote_socket, reply, err)
79                 self.emulation.error(msg, reply, err)
80                 raise RuntimeError(msg)
81                    
82         reply = cPickle.loads(base64.b64decode(reply))
83
84         return reply
85
86     def create(self, *args, **kwargs):
87         return self.send_msg(NetNSWrapperMessage.CREATE, *args, **kwargs)
88
89     def invoke(self, *args, **kwargs):
90         return self.send_msg(NetNSWrapperMessage.INVOKE, *args, **kwargs)
91
92     def set(self, *args, **kwargs):
93         return self.send_msg(NetNSWrapperMessage.SET, *args, **kwargs)
94
95     def get(self, *args, **kwargs):
96         return self.send_msg(NetNSWrapperMessage.GET, *args, **kwargs)
97
98     def flush(self, *args, **kwargs):
99         return self.send_msg(NetNSWrapperMessage.FLUSH, *args, **kwargs)
100
101     def shutdown(self, *args, **kwargs):
102         try:
103             return self.send_msg(NetNSWrapperMessage.SHUTDOWN, *args, **kwargs)
104         except:
105             pass
106
107         return None
108