2 # NEPI, a framework to manage network experiments
3 # Copyright (C) 2014 INRIA
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;
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.
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/>.
17 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
27 from optparse import OptionParser, SUPPRESS_HELP
29 from nepi.resources.netns.netnsclient import NetNSClient
30 from nepi.resources.netns.netnsserver import NetNSWrapperMessage
32 class LinuxNetNSClient(NetNSClient):
33 def __init__(self, emulation):
34 super(LinuxNetNSClient, self).__init__()
35 self._emulation = weakref.ref(emulation)
37 self._socat_proc = None
41 return self._emulation()
43 def send_msg(self, msg_type, *args, **kwargs):
44 msg = [msg_type, args, kwargs]
47 item = pickle.dumps(item)
48 return base64.b64encode(item)
50 encoded = "|".join(map(encode, msg))
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)
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\";"
64 "reply = sock.recv(1024);"
67 "encoded_message": encoded,
68 "socket_addr": self.emulation.remote_socket,
71 (reply, err), proc = self.emulation.node.execute(command,
74 if (err and proc.poll()) or reply.strip() == "":
75 msg = (" Couldn't connect to remote socket %s - REPLY: %s "
77 self.emulation.remote_socket, reply, err)
78 self.emulation.error(msg, reply, err)
79 raise RuntimeError(msg)
81 reply = pickle.loads(base64.b64decode(reply))
85 def create(self, *args, **kwargs):
86 return self.send_msg(NetNSWrapperMessage.CREATE, *args, **kwargs)
88 def invoke(self, *args, **kwargs):
89 return self.send_msg(NetNSWrapperMessage.INVOKE, *args, **kwargs)
91 def set(self, *args, **kwargs):
92 return self.send_msg(NetNSWrapperMessage.SET, *args, **kwargs)
94 def get(self, *args, **kwargs):
95 return self.send_msg(NetNSWrapperMessage.GET, *args, **kwargs)
97 def flush(self, *args, **kwargs):
98 return self.send_msg(NetNSWrapperMessage.FLUSH, *args, **kwargs)
100 def shutdown(self, *args, **kwargs):
102 return self.send_msg(NetNSWrapperMessage.SHUTDOWN, *args, **kwargs)