def _on_diplomacy_message(self, in_message): """ Handle a diplomacy message """ messages = [] request = RequestBuilder.from_bytes(in_message.content) try: LOGGER.info('[%d] request:[%s]', self._socket_no, bytes_to_str(in_message.content)) request.game_id = self.game_id message_responses = yield request_managers.handle_request( self.server, request, self) except exceptions.ResponseException: message_responses = [responses.REJ(bytes(request))] if message_responses: for response in message_responses: response_bytes = bytes(response) LOGGER.info('[%d] response:[%s]', self._socket_no, bytes_to_str(response_bytes) \ if response_bytes else None) message = DiplomacyMessage() message.content = response_bytes messages.append(message) return messages
def write_message(self, message, binary=True): """ Write a message into the stream """ if binary and isinstance(message, bytes): future = self.stream.write(message) else: if isinstance(message, notifications.DaideNotification): LOGGER.info('[%d] notification:[%s]', self._socket_no, bytes_to_str(bytes(message))) notification = message message = DiplomacyMessage() message.content = bytes(notification) if isinstance(message, DaideMessage): future = self.stream.write(bytes(message)) else: future = Future() future.set_result(None) return future
def validate_resp_notifs(self, expected_resp_notifs): """ Validate that expected response / notifications are received regardless of the order :param expected_resp_notifs: the response / notifications to receive """ while expected_resp_notifs: resp_notif_message = yield messages.DaideMessage.from_stream(self._stream) resp_notif = bytes_to_str(resp_notif_message.content) if Token(from_bytes=resp_notif_message.content[:2]) == tokens.HLO: resp_notif = resp_notif.split(' ') resp_notif[5] = expected_resp_notifs[0].split(' ')[5] resp_notif = ' '.join(resp_notif) self._is_game_joined = True LOGGER.info('[%d:%d] Received reply [%s]', self._id, self.stream.socket.fileno() + 1, str(resp_notif)) LOGGER.info('[%d:%d] Replies in buffer [%s]', self._id, self.stream.socket.fileno() + 1, ','.join(expected_resp_notifs)) assert resp_notif in expected_resp_notifs expected_resp_notifs.remove(resp_notif)
def __str__(self): """ Returning the string representation of the response """ return bytes_to_str(self._bytes)