blob: 969953ba9fbadc4a4bd52d70361303f8b5e1819b [file] [log] [blame]
Steve McIntyreb1cbad32014-12-12 21:40:25 +00001# Copyright 2014 Linaro Limited
2#
3# This program is free software; you can redistribute it and/or modify
4# it under the terms of the GNU General Public License as published by
5# the Free Software Foundation; either version 2 of the License, or
6# (at your option) any later version.
7#
8# This program is distributed in the hope that it will be useful,
9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11# GNU General Public License for more details.
12#
13# You should have received a copy of the GNU General Public License
14# along with this program; if not, write to the Free Software
15# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
16# MA 02110-1301, USA.
17#
18# Simple VLANd IPC module
19
Steve McIntyreb8832e02014-12-18 16:16:49 +000020import socket, json, time, os, sys, datetime
Steve McIntyrea3c87672014-12-15 17:59:48 +000021
22vlandpath = os.path.abspath(os.path.normpath(os.path.dirname(sys.argv[0])))
23sys.path.insert(0, vlandpath)
24from errors import CriticalError, InputError, ConfigError, SocketError
Steve McIntyreb1cbad32014-12-12 21:40:25 +000025
26class VlanIpc:
27 """VLANd IPC class"""
28
29 def server_init(self, host, port):
30 self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Steve McIntyre10dc6632015-01-23 18:04:11 +000031 self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
Steve McIntyreb1cbad32014-12-12 21:40:25 +000032
33 while True:
34 try:
35 self.socket.bind((host, port))
36 break
Steve McIntyredfa1e2c2014-12-23 13:24:33 +000037 except socket.error as e:
38 print "Can't bind to port %d: %s" % (port, e)
Steve McIntyreb1cbad32014-12-12 21:40:25 +000039 time.sleep(1)
40
41 def server_listen(self):
42 if self.socket is None:
43 raise SocketError("Server can't receive data: no socket")
44 self.socket.listen(1)
45
46 def server_recv(self):
47 if self.socket is None:
48 raise SocketError("Server can't receive data: no socket")
49
50 self.conn, addr = self.socket.accept()
51 data = self.conn.recv(8) # 32bit limit
52 count = int(data, 16)
53 c = 0
54 data = ''
55 while c < count:
56 data += self.conn.recv(1)
57 c += 1
58 try:
59 json_data = json.loads(data)
60 except ValueError:
61 self.conn.close()
62 self.conn = None
63 raise SocketError("Server unable to decode receieved data: corrupt?")
64
65 if 'client_name' not in json_data:
66 self.conn.close()
67 self.conn = None
68 raise SocketError("Server unable to detect client name: corrupt packet?")
69
70 return json_data
71
72 def server_reply(self, json_data):
73 if self.conn is None:
74 raise SocketError("Server can't send data: no connection")
75
76 data = self._format_message(json_data)
77 if not data:
78 self.conn.close()
79 self.conn = None
80 raise SocketError("Server unable to format reply data")
81
82 # send the actual number of bytes to read.
83 self.conn.send(data[0])
84 # now send the bytes.
85 self.conn.send(data[1])
86
87 def server_close(self):
88 if self.conn is not None:
Steve McIntyre10dc6632015-01-23 18:04:11 +000089 self.conn.shutdown(socket.SHUT_RDWR)
Steve McIntyreb1cbad32014-12-12 21:40:25 +000090 self.conn.close()
91
92 def client_connect(self, host, port):
93 self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
94 while True:
95 try:
96 ret = self.socket.connect_ex((host, port))
97 if ret:
98 self.socket.close()
99 self.socket = None
100 raise SocketError("Client can't send connect: %s" % ret)
101 else:
102 break
103 except socket.error:
104 time.sleep(1)
105 return True
106
107 def client_send(self, json_data):
108 if self.socket is None:
109 raise SocketError("Client can't send data: no socket")
110
111 data = self._format_message(json_data)
112 if not data:
113 self.socket.shutdown(socket.SHUT_RDWR)
114 self.socket.close()
115 self.socket = None
116 raise SocketError("Client unable to send data")
117
118 # send the actual number of bytes to read.
119 self.socket.send(data[0])
120 # now send the bytes.
121 self.socket.send(data[1])
122
123 def client_recv_and_close(self):
124 if self.socket is None:
125 raise SocketError("Client can't receieve data: no socket")
126
127 data = self.socket.recv(8) # 32bit limit
128 count = int(data, 16)
129 c = 0
130 data = ''
131 while c < count:
132 data += self.socket.recv(1)
133 c += 1
134 try:
135 json_data = json.loads(data)
136 except ValueError:
137 self.socket.close()
138 self.socket = None
139 raise SocketError("Client unable to decode receieved data: corrupt?")
140
141 self.socket.shutdown(socket.SHUT_RDWR)
142 self.socket.close()
143
144 return json_data
Steve McIntyreb8832e02014-12-18 16:16:49 +0000145
146 # The default JSON serialiser code can't deal with datetime
147 # objects by default, so let's tell it how to.
148 def _json_serial(self, obj):
149 """JSON serializer for objects not serialisable by default json code"""
150 if isinstance(obj, datetime.datetime):
151 serial = obj.isoformat()
152 return serial
Steve McIntyreb1cbad32014-12-12 21:40:25 +0000153
154 def _format_message(self, json_data):
155 try:
Steve McIntyreb8832e02014-12-18 16:16:49 +0000156 msgstr = json.dumps(json_data, default=self._json_serial)
Steve McIntyreb1cbad32014-12-12 21:40:25 +0000157 except ValueError:
158 return None
159 # "header" calculation
160 msglen = "%08X" % len(msgstr)
161 return (msglen, msgstr)