summaryrefslogtreecommitdiff
path: root/python3/vdebug/connection.py
blob: a73aa1113b7456faad49b5f6358eb44242ce6fb9 (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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
import errno
import queue
import socket
import sys
import threading
import time

from . import log


class ConnectionHandler:
    """Handles read and write operations to a given socket."""

    def __init__(self, socket, address):
        """Accept the socket used for reading and writing.

        socket -- the network socket
        """
        self.sock = socket
        self.address = address

    def __del__(self):
        """Make sure the connection is closed."""
        self.close()

    def isconnected(self):
        return 1

    def close(self):
        """Close the connection."""
        log.Log("Closing the socket", log.Logger.DEBUG)
        self.sock.close()

    def __recv_length(self):
        """Get the length of the proceeding message."""
        length = []
        while 1:
            c = self.sock.recv(1)
            if c == b'':
                self.close()
                raise EOFError('Socket Closed')
            if c == b'\x00':
                return int(b''.join(length))
            if c.isdigit():
                length.append(c)

    def __recv_null(self):
        """Receive a null byte."""
        while 1:
            c = self.sock.recv(1)
            if c == b'':
                self.close()
                raise EOFError('Socket Closed')
            if c == b'\x00':
                return

    def __recv_body(self, to_recv):
        body = []
        while to_recv > 0:
            buf = self.sock.recv(to_recv)
            if buf == b'':
                self.close()
                raise EOFError('Socket Closed')
            to_recv -= len(buf)
            body.append(buf.decode("utf-8"))
        return ''.join(body)

    def recv_msg(self):
        """Receive a message from the debugger.

        Returns a string, which is expected to be XML.
        """
        length = self.__recv_length()
        body = self.__recv_body(length)
        self.__recv_null()
        return body

    def send_msg(self, cmd):
        """Send a message to the debugger.

        cmd -- command to send
        """
        #self.sock.send(cmd + '\0')
        MSGLEN = len(cmd)
        totalsent = 0
        while totalsent < MSGLEN:
            sent = self.sock.send(bytes(cmd[totalsent:].encode()))
            if sent == 0:
                raise RuntimeError("socket connection broken")
            totalsent = totalsent + sent
        sent = self.sock.send(b'\x00')


class SocketCreator:

    def __init__(self, input_stream=None):
        """Create a new Connection.

        The connection is not established until open() is called.

        input_stream -- object for checking input stream and user interrupts (default None)
        """
        self.__sock = None
        self.input_stream = input_stream

    def start(self, host='', port=9000, timeout=30):
        """Listen for a connection from the debugger. Listening for the actual
        connection is handled by self.listen()

        host -- host name where debugger is running (default '')
        port -- port number which debugger is listening on (default 9000)
        timeout -- time in seconds to wait for a debugger connection before giving up (default 30)
        """
        print('Waiting for a connection (Ctrl-C to cancel, this message will '
              'self-destruct in ', timeout, ' seconds...)')
        serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        try:
            serv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
            serv.setblocking(1)
            serv.bind((host, port))
            serv.listen(5)
            self.__sock = self.listen(serv, timeout)
        except socket.timeout:
            raise TimeoutError("Timeout waiting for connection")
        finally:
            serv.close()

    def listen(self, serv, timeout):
        """Non-blocking listener. Provides support for keyboard interrupts from
        the user. Although it's non-blocking, the user interface will still
        block until the timeout is reached.

        serv -- Socket server to listen to.
        timeout -- Seconds before timeout.
        """
        start = time.time()
        while True:
            if (time.time() - start) > timeout:
                raise socket.timeout
            try:
                """Check for user interrupts"""
                if self.input_stream is not None:
                    self.input_stream.probe()
                return serv.accept()
            except socket.error:
                pass

    def clear(self):
        self.__sock = None

    def socket(self):
        return self.__sock

    def has_socket(self):
        return self.__sock is not None


class BackgroundSocketCreator(threading.Thread):

    def __init__(self, host, port, message_q, output_q):
        self.__message_q = message_q
        self.__output_q = output_q
        self.__host = host
        self.__port = port
        threading.Thread.__init__(self)

    @staticmethod
    def log(message):
        log.Log(message, log.Logger.DEBUG)

    def run(self):
        self.log("Started")
        self.log("Listening on port %s" % self.__port)
        try:
            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            s.setblocking(1)
            s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
            s.bind((self.__host, self.__port))
            s.settimeout(5) # timeout after 5 seconds so we can check messages
            s.listen(5)
            while 1:
                try:
                    self.__peek_for_exit()
                    client, address = s.accept()
                    self.log("Found client, %s" % str(address))
                    self.__output_q.put((client, address))
                    break
                except socket.error:
                    # No connection
                    pass
        except socket.error as socket_error:
            self.log("Error: %s" % str(sys.exc_info()))
            self.log("Stopping server")

            if socket_error.errno == errno.EADDRINUSE:
                self.log("Address already in use")
                print("Socket is already in use")
        except Exception:
            print("Exception caught")
            self.log("Error: %s" % str(sys.exc_info()))
            self.log("Stopping server")
        finally:
            self.log("Finishing socket server")
            s.close()

    def __peek_for_exit(self):
        try:
            # self.log("Checking for exit")
            self.__check_exit(self.__message_q.get_nowait())
        except queue.Empty:
            pass

    @staticmethod
    def __check_exit(message):
        if message == "exit":
            raise Exception("Exiting")


class SocketServer:

    def __init__(self):
        self.__message_q = queue.Queue(0)
        self.__socket_q = queue.Queue(1)
        self.__thread = None

    def __del__(self):
        self.stop()

    def start(self, host, port):
        if not self.is_alive():
            self.__thread = BackgroundSocketCreator(
                host, port, self.__message_q, self.__socket_q)
            self.__thread.start()

    def is_alive(self):
        return self.__thread and self.__thread.is_alive()

    def has_socket(self):
        return self.__socket_q.full()

    def socket(self):
        return self.__socket_q.get_nowait()

    def stop(self):
        if self.is_alive():
            self.__message_q.put_nowait("exit")
            self.__thread.join(3000)
        if self.has_socket():
            self.socket()[0].close()