aboutsummaryrefslogtreecommitdiff
path: root/Vland/ipc/ipc.py
blob: 5961ed11e855d4954648e5b0d7c8511eab5102a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#  Copyright 2014-2015 Linaro Limited
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#  MA 02110-1301, USA.
#
#  Simple VLANd IPC module

import socket
import json
import time
import datetime
import os
import sys
import logging

from Vland.errors import CriticalError, InputError, ConfigError, SocketError

class VlanIpc:
    """VLANd IPC class"""

    def __init__(self):
        self.conn = None
        self.socket = None

    def server_init(self, host, port):
        self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self.conn = None

        while True:
            try:
                self.socket.bind((host, port))
                break
            except socket.error as e:
                print "Can't bind to port %d: %s" % (port, e)
                time.sleep(1)

    def server_listen(self):
        if self.socket is None:
            raise SocketError("Server can't receive data: no socket")
        self.socket.listen(1)

    def server_recv(self):
        if self.socket is None:
            raise SocketError("Server can't receive data: no socket")

        self.conn, addr = self.socket.accept()
        logging.debug("server: Connection from")
        logging.debug(addr)
        data = self.conn.recv(8)  # 32bit limit
        count = int(data, 16)
        c = 0
        data = ''
        while c < count:
            data += self.conn.recv(1)
            c += 1
        try:
            json_data = json.loads(data)
        except ValueError:
            self.conn.close()
            self.conn = None
            raise SocketError("Server unable to decode receieved data: corrupt?")

        if 'client_name' not in json_data:
            self.conn.close()
            self.conn = None
            raise SocketError("Server unable to detect client name: corrupt packet?")

        return json_data

    def server_reply(self, json_data):
        if self.conn is None:
            raise SocketError("Server can't send data: no connection")

        data = self._format_message(json_data)
        if not data:
            self.conn.close()
            self.conn = None
            raise SocketError("Server unable to format reply data")

        try:
            # send the actual number of bytes to read.
            self.conn.send(data[0])
            # now send the bytes.
            self.conn.send(data[1])
        except socket.error as e:
            logging.error("Can't send response to client: %s", e)
            logging.error("Was trying to send data:")
            logging.error(data)

    def server_close(self):
        if self.conn is not None:
            self.conn.shutdown(socket.SHUT_RDWR)
            self.conn.close()

    def client_connect(self, host, port):
        self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        while True:
            try:
                ret = self.socket.connect_ex((host, port))
                if ret:
                    self.socket.close()
                    self.socket = None
                    raise SocketError("Client can't send connect: %s" % ret)
                else:
                    break
            except socket.error:
                time.sleep(1)
        return True

    def client_send(self, json_data):
        if self.socket is None:
            raise SocketError("Client can't send data: no socket")

        data = self._format_message(json_data)
        if not data:
            self.socket.shutdown(socket.SHUT_RDWR)
            self.socket.close()
            self.socket = None
            raise SocketError("Client unable to send data")

        # send the actual number of bytes to read.
        self.socket.send(data[0])
        # now send the bytes.
        self.socket.send(data[1])

    def client_recv_and_close(self):
        if self.socket is None:
            raise SocketError("Client can't receieve data: no socket")

        data = self.socket.recv(8)  # 32bit limit
        count = int(data, 16)
        c = 0
        data = ''
        while c < count:
            data += self.socket.recv(1)
            c += 1
        try:
            json_data = json.loads(data)
        except ValueError:
            self.socket.close()
            self.socket = None
            raise SocketError("Client unable to decode receieved data: corrupt?")

        self.socket.shutdown(socket.SHUT_RDWR)
        self.socket.close()

        return json_data

    # The default JSON serialiser code can't deal with datetime
    # objects by default, so let's tell it how to.
    def _json_serial(self, obj):
        """JSON serializer for objects not serialisable by default json code"""
        if isinstance(obj, datetime.datetime):
            serial = obj.isoformat()
            return serial

    def _format_message(self, json_data):
        try:
            msgstr = json.dumps(json_data, default=self._json_serial)
        except ValueError:
            return None
        # "header" calculation
        msglen = "%08X" % len(msgstr)
        return (msglen, msgstr)