blob: 5b75270b86f8e77c26974d2d3b5641f000593d99 [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 McIntyre6efb2792015-02-09 06:36:11 +000032 self.conn = None
Steve McIntyreb1cbad32014-12-12 21:40:25 +000033
34 while True:
35 try:
36 self.socket.bind((host, port))
37 break
Steve McIntyredfa1e2c2014-12-23 13:24:33 +000038 except socket.error as e:
39 print "Can't bind to port %d: %s" % (port, e)
Steve McIntyreb1cbad32014-12-12 21:40:25 +000040 time.sleep(1)
41
42 def server_listen(self):
43 if self.socket is None:
44 raise SocketError("Server can't receive data: no socket")
45 self.socket.listen(1)
46
47 def server_recv(self):
48 if self.socket is None:
49 raise SocketError("Server can't receive data: no socket")
50
51 self.conn, addr = self.socket.accept()
52 data = self.conn.recv(8) # 32bit limit
53 count = int(data, 16)
54 c = 0
55 data = ''
56 while c < count:
57 data += self.conn.recv(1)
58 c += 1
59 try:
60 json_data = json.loads(data)
61 except ValueError:
62 self.conn.close()
63 self.conn = None
64 raise SocketError("Server unable to decode receieved data: corrupt?")
65
66 if 'client_name' not in json_data:
67 self.conn.close()
68 self.conn = None
69 raise SocketError("Server unable to detect client name: corrupt packet?")
70
71 return json_data
72
73 def server_reply(self, json_data):
74 if self.conn is None:
75 raise SocketError("Server can't send data: no connection")
76
77 data = self._format_message(json_data)
78 if not data:
79 self.conn.close()
80 self.conn = None
81 raise SocketError("Server unable to format reply data")
82
83 # send the actual number of bytes to read.
84 self.conn.send(data[0])
85 # now send the bytes.
86 self.conn.send(data[1])
87
88 def server_close(self):
89 if self.conn is not None:
Steve McIntyre10dc6632015-01-23 18:04:11 +000090 self.conn.shutdown(socket.SHUT_RDWR)
Steve McIntyreb1cbad32014-12-12 21:40:25 +000091 self.conn.close()
92
93 def client_connect(self, host, port):
94 self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
95 while True:
96 try:
97 ret = self.socket.connect_ex((host, port))
98 if ret:
99 self.socket.close()
100 self.socket = None
101 raise SocketError("Client can't send connect: %s" % ret)
102 else:
103 break
104 except socket.error:
105 time.sleep(1)
106 return True
107
108 def client_send(self, json_data):
109 if self.socket is None:
110 raise SocketError("Client can't send data: no socket")
111
112 data = self._format_message(json_data)
113 if not data:
114 self.socket.shutdown(socket.SHUT_RDWR)
115 self.socket.close()
116 self.socket = None
117 raise SocketError("Client unable to send data")
118
119 # send the actual number of bytes to read.
120 self.socket.send(data[0])
121 # now send the bytes.
122 self.socket.send(data[1])
123
124 def client_recv_and_close(self):
125 if self.socket is None:
126 raise SocketError("Client can't receieve data: no socket")
127
128 data = self.socket.recv(8) # 32bit limit
129 count = int(data, 16)
130 c = 0
131 data = ''
132 while c < count:
133 data += self.socket.recv(1)
134 c += 1
135 try:
136 json_data = json.loads(data)
137 except ValueError:
138 self.socket.close()
139 self.socket = None
140 raise SocketError("Client unable to decode receieved data: corrupt?")
141
142 self.socket.shutdown(socket.SHUT_RDWR)
143 self.socket.close()
144
145 return json_data
Steve McIntyreb8832e02014-12-18 16:16:49 +0000146
147 # The default JSON serialiser code can't deal with datetime
148 # objects by default, so let's tell it how to.
149 def _json_serial(self, obj):
150 """JSON serializer for objects not serialisable by default json code"""
151 if isinstance(obj, datetime.datetime):
152 serial = obj.isoformat()
153 return serial
Steve McIntyreb1cbad32014-12-12 21:40:25 +0000154
155 def _format_message(self, json_data):
156 try:
Steve McIntyreb8832e02014-12-18 16:16:49 +0000157 msgstr = json.dumps(json_data, default=self._json_serial)
Steve McIntyreb1cbad32014-12-12 21:40:25 +0000158 except ValueError:
159 return None
160 # "header" calculation
161 msglen = "%08X" % len(msgstr)
162 return (msglen, msgstr)