def consumer(self): while True: try: payload = yield from self.ws_protocol.recv() logging.info('Received message.') msg = message_pb2.PrexMessage() msg.ParseFromString(payload) if msg.type == message_pb2.PrexMessage.IO: io = message_pb2.Io() io.ParseFromString(msg.payload) print(io.data.decode()) except websockets.exceptions.ConnectionClosed: return
def run(self): try: websocket = yield from websockets.connect('ws://localhost:43000') self.ws_protocol = websocket message = message_pb2.PrexMessage() message.type = message_pb2.PrexMessage.LOAD_PROGRAM message_program = message_pb2.LoadProgram() message_program.filename = 'hello.py' message_program.code = code message.payload = message_program.SerializeToString() yield from websocket.send(message.SerializeToString()) asyncio.ensure_future(self.delayed_terminate()) yield from self.consumer() websocket.close() except Exception as exc: raise
def consumer(self): while True: try: payload = yield from self.ws_protocol.recv() logging.info('Received message.') msg = message_pb2.PrexMessage() msg.ParseFromString(payload) if msg.type == message_pb2.PrexMessage.IO: io = message_pb2.Io() io.ParseFromString(msg.payload) print(io.data.decode(), end='', flush=True) if msg.type == message_pb2.PrexMessage.IMAGE: image = message_pb2.Image() image.ParseFromString(msg.payload) print('Received {} bytes of image data.'.format( len(image.payload))) if msg.type == message_pb2.PrexMessage.TERMINATE: # The remote process terminated return except websockets.exceptions.ConnectionClosed: return
def terminate(self): message = message_pb2.PrexMessage() message.type = message_pb2.PrexMessage.TERMINATE yield from self.ws_protocol.send(message.SerializeToString())