Adding trace support for ns3 RMs
[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 socket
24 import weakref
25
26 from optparse import OptionParser, SUPPRESS_HELP
27
28 from nepi.resources.ns3.ns3client import NS3Client
29 from nepi.resources.ns3.ns3server import NS3WrapperMessage
30
31 class LinuxNS3Client(NS3Client):
32     def __init__(self, simulation):
33         super(LinuxNS3Client, self).__init__()
34         self._simulation = weakref.ref(simulation)
35
36         self._socat_proc = None
37         self.connect_client()
38
39     @property
40     def simulation(self):
41         return self._simulation()
42
43     def connect_client(self):
44         if self.simulation.node.get("hostname") in ['localhost', '127.0.0.1']:
45             return
46
47         (out, err), self._socat_proc = self.simulation.node.socat(
48                 self.simulation.local_socket,
49                 self.simulation.remote_socket) 
50
51     def send_msg(self, msg_type, *args, **kwargs):
52         sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
53         sock.connect(self.simulation.local_socket)
54
55         msg = [msg_type, args, kwargs]
56
57         def encode(item):
58             item = cPickle.dumps(item)
59             return base64.b64encode(item)
60
61         encoded = "|".join(map(encode, msg))
62         sock.send("%s\n" % encoded)
63        
64         reply = sock.recv(1024)
65         return cPickle.loads(base64.b64decode(reply))
66
67     def create(self, *args, **kwargs):
68         return self.send_msg(NS3WrapperMessage.CREATE, *args, **kwargs)
69
70     def factory(self, *args, **kwargs):
71         return self.send_msg(NS3WrapperMessage.FACTORY, *args, **kwargs)
72
73     def invoke(self, *args, **kwargs):
74         return self.send_msg(NS3WrapperMessage.INVOKE, *args, **kwargs)
75
76     def set(self, *args, **kwargs):
77         return self.send_msg(NS3WrapperMessage.SET, *args, **kwargs)
78
79     def get(self, *args, **kwargs):
80         return self.send_msg(NS3WrapperMessage.GET, *args, **kwargs)
81
82     def flush(self, *args, **kwargs):
83         return self.send_msg(NS3WrapperMessage.FLUSH, *args, **kwargs)
84
85     def start(self, *args, **kwargs):
86         return self.send_msg(NS3WrapperMessage.START, *args, **kwargs)
87
88     def stop(self, *args, **kwargs):
89         return self.send_msg(NS3WrapperMessage.STOP, *args, **kwargs)
90
91     def shutdown(self, *args, **kwargs):
92         ret = None
93
94         try:
95             ret = self.send_msg(NS3WrapperMessage.SHUTDOWN, *args, **kwargs)
96         except:
97             pass
98
99         try:
100             if self._socat_proc:
101                 self._socat_proc.kill()
102         except:
103             pass
104
105         try:
106             os.remove(self.simulation.local_socket)
107         except:
108             pass
109
110         return ret
111