ns-3 test passing
[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, *args, **kwargs):
52         args = list(args)
53              
54         sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
55         sock.connect(self.simulation.local_socket)
56
57         timeout = kwargs.get("timeout")
58         if timeout:
59              sock.settimeout(timeout)
60
61         args.insert(0, msg)
62         def encode(arg):
63             arg = cPickle.dumps(arg)
64             return base64.b64encode(arg) 
65
66         encoded = "|".join(map(encode, args))
67         sock.send("%s\n" % encoded)
68        
69         reply = sock.recv(1024)
70         return cPickle.loads(base64.b64decode(reply))
71
72     def create(self, clazzname, *args, **kwargs):
73         args = list(args)
74         args.insert(0, clazzname)
75         
76         return self.send_msg(NS3WrapperMessage.CREATE, *args, **kwargs)
77
78     def factory(self, type_name, **kwargs):
79         args = [type_name]
80         args.append(kwargs)
81         
82         return self.send_msg(NS3WrapperMessage.FACTORY, *args, **kwargs)
83
84     def invoke(self, uuid, operation, *args, **kwargs):
85         args = list(args)
86         args.insert(0, operation)
87         args.insert(0, uuid)
88
89         return self.send_msg(NS3WrapperMessage.INVOKE, *args, **kwargs)
90
91     def set(self, uuid, name, value, **kwargs):
92         args = [uuid, name, value]
93
94         return self.send_msg(NS3WrapperMessage.SET, *args, **kwargs)
95
96     def get(self, uuid, name, **kwargs):
97         args = [uuid, name]
98
99         return self.send_msg(NS3WrapperMessage.GET, *args, **kwargs)
100
101     def enable_trace(self, *args, **kwargs):
102         return self.send_msg(NS3WrapperMessage.ENABLE_TRACE, *args, **kwargs)
103
104     def flush(self, **kwargs):
105         args = []
106         return self.send_msg(NS3WrapperMessage.FLUSH, *args, **kwargs)
107
108     def start(self, **kwargs):
109         args = []
110         return self.send_msg(NS3WrapperMessage.START, *args, **kwargs)
111
112     def stop(self, **kwargs):
113         args = []
114         time = kwargs.get("time")
115         if time: args.append(time)
116
117         return self.send_msg(NS3WrapperMessage.STOP, *args, **kwargs)
118
119     def shutdown(self, **kwargs):
120         args = []
121         ret = None
122
123         try:
124             ret = self.send_msg(NS3WrapperMessage.SHUTDOWN, *args, **kwargs)
125         except:
126             pass
127
128         try:
129             if self._socat_proc:
130                 self._socat_proc.kill()
131         except:
132             pass
133
134         try:
135             os.remove(self.simulation.local_socket)
136         except:
137             pass
138
139         return ret
140