Adding linux ns3 server unit test
[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 from optparse import OptionParser, SUPPRESS_HELP
25
26 from nepi.resources.ns3.ns3client import NS3Client
27 from nepi.resources.ns3.ns3server import NS3WrapperMessage
28
29 class LinuxNS3Client(NS3Client):
30     def __init__(self, socket_name):
31         super(LinuxNS3Client, self).__init__()
32         self._socket_name = socket_name
33
34     @property
35     def socket_name(self):
36         return self._socket_name
37
38     def send_msg(self, msg, *args):
39         args = list(args)
40
41         sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
42         sock.connect(self.socket_name)
43
44         args.insert(0, msg)
45         def encode(arg):
46             arg = cPickle.dumps(arg)
47             return base64.b64encode(arg) 
48
49         encoded = "|".join(map(encode, args))
50         sock.send("%s\n" % encoded)
51         
52         reply = sock.recv(1024)
53         return cPickle.loads(base64.b64decode(reply))
54
55     def create(self, clazzname, *args):
56         args = list(args)
57         args.insert(0, clazzname)
58         
59         return self.send_msg(NS3WrapperMessage.CREATE, *args)
60
61     def factory(self, type_name, **kwargs):
62         args = [type_name]
63         args.append(kwargs)
64         
65         return self.send_msg(NS3WrapperMessage.FACTORY, *args)
66
67     def invoke(self, uuid, operation, *args):
68         args = list(args)
69         args.insert(0, operation)
70         args.insert(0, uuid)
71
72         return self.send_msg(NS3WrapperMessage.INVOKE, *args)
73
74     def set(self, uuid, name, value):
75         args = [uuid, name, value]
76
77         return self.send_msg(NS3WrapperMessage.SET, *args)
78
79     def get(self, uuid, name):
80         args = [uuid, name]
81
82         return self.send_msg(NS3WrapperMessage.GET, *args)
83
84     def trace(self, *args):
85         return self.send_msg(NS3WrapperMessage.TRACE, *args)
86
87     def start(self):
88         return self.send_msg(NS3WrapperMessage.START, [])
89
90     def stop(self, time = None):
91         args = None
92         if time:
93             args = [time]
94
95         return self.send_msg(NS3WrapperMessage.STOP, *args)
96
97     def shutdown(self):
98         return self.send_msg(NS3WrapperMessage.SHUTDOWN, [])
99