Пример #1
0
    def connection_made(self, transport):
        self._connected = True
        self._transport = transport
        self._obj_streamer = ObjectStreamer()
        self._obj_streamer.auto_listen(self, prefix='on_')

        self._transport.write('['.encode())  # start a json array
        self._write_pending_data()
Пример #2
0
    def connection_made(self, transport):
        self._connected = True
        self._transport = transport
        self._obj_streamer = ObjectStreamer()
        self._obj_streamer.auto_listen(self, prefix='on_')

        self._transport.send = self._transport.write
        self._send_q = SendQueue(transport, self.is_connected)

        self._transport.write('['.encode())  # start a json array
        self._send_q.send()
Пример #3
0
    def connection_made(self, transport):
        self._connected = True
        self._transport = transport
        self._obj_streamer = ObjectStreamer()
        self._obj_streamer.auto_listen(self, prefix='on_')

        self._transport.write('['.encode())  # start a json array
        self._write_pending_data()
Пример #4
0
    def connection_made(self, transport):
        self._connected = True
        self._transport = transport
        self._obj_streamer = ObjectStreamer()
        self._obj_streamer.auto_listen(self, prefix='on_')

        self._transport.send = self._transport.write
        self._send_q = SendQueue(transport, self.is_connected)

        self._transport.write('['.encode())  # start a json array
        self._send_q.send()
Пример #5
0
 def set_streamer(self):
     self._obj_streamer = ObjectStreamer()
     self._obj_streamer.auto_listen(self, prefix='on_')
     self._obj_streamer.consume('[')
Пример #6
0
class JSONProtocol(asyncio.Protocol):
    logger = logging.getLogger(__name__)

    def __init__(self):
        self._send_q = None
        self._connected = False
        self._transport = None
        self._obj_streamer = None
        self._pending_data = []

    @staticmethod
    def _make_frame(packet):
        string = json.dumps(packet, cls=TrellioEncoder) + ','
        return string.encode()

    def is_connected(self):
        return self._connected

    def _write_pending_data(self):
        for packet in self._pending_data:
            frame = self._make_frame(packet)
            self._transport.write(frame)
        self._pending_data.clear()

    def connection_made(self, transport):
        self._connected = True
        self._transport = transport
        try:
            self._transport.send = self._transport.write
        except:
            pass
        self._send_q = SendQueue(transport, self.is_connected)
        self.set_streamer()
        self._send_q.send()

    def set_streamer(self):
        self._obj_streamer = ObjectStreamer()
        self._obj_streamer.auto_listen(self, prefix='on_')
        self._obj_streamer.consume('[')

    def connection_lost(self, exc):
        self._connected = False
        self.logger.info('Peer closed %s', self._transport.get_extra_info('peername'))

    def send(self, packet):
        frame = self._make_frame(packet)
        self._send_q.send(frame)
        self.logger.debug('Data sent: %s', frame.decode())

    def close(self):
        self._transport.write(']'.encode())  # end the json array
        self._transport.close()

    def data_received(self, byte_data):
        string_data = byte_data.decode()
        self.logger.debug('Data received: %s', string_data)
        try:
            self._obj_streamer.consume(string_data)
        except:
            # recover from invalid data
            self.logger.exception('Invalid data received')
            self.set_streamer()

    def on_object_stream_start(self):
        raise RuntimeError('Incorrect JSON Streaming Format: expect a JSON Array to start at root, got object')

    def on_object_stream_end(self):
        del self._obj_streamer
        raise RuntimeError('Incorrect JSON Streaming Format: expect a JSON Array to end at root, got object')

    def on_array_stream_start(self):
        self.logger.debug('Array Stream started')

    def on_array_stream_end(self):
        del self._obj_streamer
        self.logger.debug('Array Stream ended')

    def on_pair(self, pair):
        self.logger.debug('Pair {}'.format(pair))
        raise RuntimeError('Received a key-value pair object - expected elements only')
Пример #7
0
 def set_streamer(self):
     self._obj_streamer = ObjectStreamer()
     self._obj_streamer.auto_listen(self, prefix='on_')
     self._obj_streamer.consume('[')
Пример #8
0
class JSONProtocol(asyncio.Protocol):
    logger = logging.getLogger(__name__)

    def __init__(self):
        self._send_q = None
        self._connected = False
        self._transport = None
        self._obj_streamer = None
        self._pending_data = []

    @staticmethod
    def _make_frame(packet):
        string = json.dumps(packet, cls=TrellioEncoder) + ','
        return string.encode()

    def is_connected(self):
        return self._connected

    def _write_pending_data(self):
        for packet in self._pending_data:
            frame = self._make_frame(packet)
            self._transport.write(frame)
        self._pending_data.clear()

    def connection_made(self, transport):
        self._connected = True
        self._transport = transport
        try:
            self._transport.send = self._transport.write
        except:
            pass
        self._send_q = SendQueue(transport, self.is_connected)
        self.set_streamer()
        self._send_q.send()

    def set_streamer(self):
        self._obj_streamer = ObjectStreamer()
        self._obj_streamer.auto_listen(self, prefix='on_')
        self._obj_streamer.consume('[')

    def connection_lost(self, exc):
        self._connected = False
        self.logger.info('Peer closed %s',
                         self._transport.get_extra_info('peername'))

    def send(self, packet):
        frame = self._make_frame(packet)
        self._send_q.send(frame)
        self.logger.debug('Data sent: %s', frame.decode())

    def close(self):
        self._transport.write(']'.encode())  # end the json array
        self._transport.close()

    def data_received(self, byte_data):
        string_data = byte_data.decode()
        self.logger.debug('Data received: %s', string_data)
        try:
            self._obj_streamer.consume(string_data)
        except:
            # recover from invalid data
            self.logger.exception('Invalid data received')
            self.set_streamer()

    def on_object_stream_start(self):
        raise RuntimeError(
            'Incorrect JSON Streaming Format: expect a JSON Array to start at root, got object'
        )

    def on_object_stream_end(self):
        del self._obj_streamer
        raise RuntimeError(
            'Incorrect JSON Streaming Format: expect a JSON Array to end at root, got object'
        )

    def on_array_stream_start(self):
        self.logger.debug('Array Stream started')

    def on_array_stream_end(self):
        del self._obj_streamer
        self.logger.debug('Array Stream ended')

    def on_pair(self, pair):
        self.logger.debug('Pair {}'.format(pair))
        raise RuntimeError(
            'Received a key-value pair object - expected elements only')
Пример #9
0
class JSONProtocol(asyncio.Protocol):

    logger = logging.getLogger(__name__)

    def __init__(self):
        self._pending_data = []
        self._connected = False
        self._transport = None
        self._obj_streamer = None

    def _make_frame(self, packet):
        string = json.dumps(packet) + ','
        return string

    def _write_pending_data(self):
        for packet in self._pending_data:
            frame = self._make_frame(packet)
            self._transport.write(frame.encode())

    def connection_made(self, transport):
        self._connected = True
        self._transport = transport
        self._obj_streamer = ObjectStreamer()
        self._obj_streamer.auto_listen(self, prefix='on_')

        self._transport.write('['.encode())  # start a json array
        self._write_pending_data()

    def connection_lost(self, exc):
        self._connected = False
        self.logger.debug('Peer closed the connection')

    def send(self, packet: dict):
        string = json.dumps(packet) + ','
        if self._connected:
            self._transport.write(string.encode())
            self.logger.debug('Data sent: {}'.format(string))
        else:
            self._pending_data.append(packet)
            self.logger.debug('Appended data: {}'.format(self._pending_data))

    def close(self):
        self._transport.write('bye]'.encode())  # end the json array
        self._transport.close()

    def data_received(self, byte_data):
        string_data = byte_data.decode()
        self.logger.debug('Data received: {}'.format(string_data))
        self._obj_streamer.consume(string_data)

    def on_object_stream_start(self):
        raise RuntimeError('Incorrect JSON Streaming Format: expect a JSON Array to start at root, got object')

    def on_object_stream_end(self):
        del self._obj_streamer
        raise RuntimeError('Incorrect JSON Streaming Format: expect a JSON Array to end at root, got object')

    def on_array_stream_start(self):
        self.logger.debug('Array Stream started')

    def on_array_stream_end(self):
        del self._obj_streamer
        self.logger.debug('Array Stream ended')

    def on_pair(self, pair):
        self.logger.debug('Pair {}'.format(pair))
        raise RuntimeError('Received a key-value pair object - expected elements only')
Пример #10
0
class JSONProtocol(asyncio.Protocol):

    logger = logging.getLogger(__name__)

    def __init__(self):
        self._pending_data = []
        self._connected = False
        self._transport = None
        self._obj_streamer = None

    def _make_frame(self, packet):
        string = json.dumps(packet) + ','
        return string

    def _write_pending_data(self):
        for packet in self._pending_data:
            frame = self._make_frame(packet)
            self._transport.write(frame.encode())
        self._pending_data.clear()

    def connection_made(self, transport):
        self._connected = True
        self._transport = transport
        self._obj_streamer = ObjectStreamer()
        self._obj_streamer.auto_listen(self, prefix='on_')

        self._transport.write('['.encode())  # start a json array
        self._write_pending_data()

    def connection_lost(self, exc):
        self._connected = False
        self.logger.info('Peer closed the connection')

    def send(self, packet: dict):
        string = json.dumps(packet) + ','
        if self._connected:
            self._transport.write(string.encode())
            self.logger.info('Data sent: {}'.format(string))
        else:
            self._pending_data.append(packet)
            self.logger.info('Appended data: {}'.format(self._pending_data))

    def close(self):
        self._transport.write('bye]'.encode())  # end the json array
        self._transport.close()

    def data_received(self, byte_data):
        string_data = byte_data.decode()
        self.logger.info('Data received: {}'.format(string_data))
        self._obj_streamer.consume(string_data)

    def on_object_stream_start(self):
        raise RuntimeError(
            'Incorrect JSON Streaming Format: expect a JSON Array to start at root, got object'
        )

    def on_object_stream_end(self):
        del self._obj_streamer
        raise RuntimeError(
            'Incorrect JSON Streaming Format: expect a JSON Array to end at root, got object'
        )

    def on_array_stream_start(self):
        self.logger.info('Array Stream started')

    def on_array_stream_end(self):
        del self._obj_streamer
        self.logger.info('Array Stream ended')

    def on_pair(self, pair):
        self.logger.info('Pair {}'.format(pair))
        raise RuntimeError(
            'Received a key-value pair object - expected elements only')
Пример #11
0
class JSONProtocol(asyncio.Protocol):
    logger = logging.getLogger(__name__)

    def __init__(self):
        self._send_q = None
        self._connected = False
        self._transport = None
        self._obj_streamer = None
        self._pending_data = []

    @staticmethod
    def _make_frame(packet):
        string = json.dumps(packet) + ","
        return string.encode()

    def is_connected(self):
        return self._connected

    def _write_pending_data(self):
        for packet in self._pending_data:
            frame = self._make_frame(packet)
            self._transport.write(frame.encode())
        self._pending_data.clear()

    def connection_made(self, transport):
        self._connected = True
        self._transport = transport
        self._obj_streamer = ObjectStreamer()
        self._obj_streamer.auto_listen(self, prefix="on_")

        self._transport.send = self._transport.write
        self._send_q = SendQueue(transport, self.is_connected)

        self._transport.write("[".encode())  # start a json array
        self._send_q.send()

    def connection_lost(self, exc):
        self._connected = False
        self.logger.info("Peer closed %s", self._transport.get_extra_info("peername"))

    def send(self, packet: dict):
        self._send_q.send(self._make_frame(packet))
        if "ping" in packet or "pong" in packet:
            if is_ping_logging_enabled():
                self.logger.debug("Data sent: {}".format(packet))
        else:
            self.logger.debug("Data sent: {}".format(packet))

    def close(self):
        self._transport.write("]".encode())  # end the json array
        self._transport.close()

    def data_received(self, byte_data):
        string_data = byte_data.decode()
        if "ping" in string_data or "pong" in string_data:
            if is_ping_logging_enabled():
                self.logger.debug("Data received: {}".format(string_data))
        else:
            self.logger.debug("Data received: {}".format(string_data))
        self._obj_streamer.consume(string_data)

    def on_object_stream_start(self):
        raise RuntimeError("Incorrect JSON Streaming Format: expect a JSON Array to start at root, got object")

    def on_object_stream_end(self):
        del self._obj_streamer
        raise RuntimeError("Incorrect JSON Streaming Format: expect a JSON Array to end at root, got object")

    def on_array_stream_start(self):
        self.logger.debug("Array Stream started")

    def on_array_stream_end(self):
        del self._obj_streamer
        self.logger.debug("Array Stream ended")

    def on_pair(self, pair):
        self.logger.debug("Pair {}".format(pair))
        raise RuntimeError("Received a key-value pair object - expected elements only")