示例#1
0
    def _send_list_clients_request(self):
        payload = builder.build_action_payload(
            protocol.action.LIST_CLIENTS_FIELD)
        self._sock.sendall(payload)

        response = read_response(self._sock)
        active_clients = response[protocol.action.LIST_CLIENTS_FIELD]
        print("Clientes ativos:")
        print_nicknames(active_clients, self._nick)
    def _keep_alive(self, c_sock, nicknames):
        # deal with information received from this connection
        while True:
            try:
                response = read_response(c_sock)
            except SocketClosedError:
                logging.debug(f'Socket {c_sock.getsockname()} has been closed')
                break

            # decode byte encoded response
            logging.debug(f'Client sent: {response}')

            self._handle_response(c_sock, nicknames, response)
示例#3
0
 def _handle_first_interaction_response(self):
     try:
         response = read_response(self._sock)
     except SocketClosedError:
         print("Socket fechado, tente novamente")
     else:
         if response[protocol.status.ok]:
             logging.debug(f'Setting nickname as {response[protocol.nick]}')
             self._nick = response[protocol.nick]
             print_colored(colors.OKBLUE, "Nickname criado com sucesso!")
         else:
             print_colored(colors.FAIL,
                           "Nickname já existente, tente novamente")
             self.first_interaction()
示例#4
0
    def ready(self, open_connections):
        # received connection from the server
        response = read_response(self._sock)

        # should only receive connection from the server when other client
        # wishes to send a message
        # if that's the case, open a p2p server to handle future incoming connections
        if (protocol.role_field in response
                and response[protocol.role_field] == protocol.role.server):
            self._create_p2p_server(open_connections)
        else:
            logging.debug(
                f'Received unexpected request from the server: {response}')
        return False, ""
示例#5
0
    def _send_connect_client_request(self, open_connections, nickname,
                                     can_read_stdin):
        payload = builder.build_action_payload(
            protocol.action.CONNECT_CLIENT_FIELD, nickname)
        self._sock.sendall(payload)

        response = read_response(self._sock)

        if protocol.connection.ip in response \
            and protocol.connection.port in response:
            self._create_p2p_client(open_connections, response, can_read_stdin)
        elif protocol.status.ok in response \
            and not response[protocol.status.ok]:
            self._handle_p2p_inactive_client(response)
    def init_p2p_server(self):
        """ Initialize p2p server interaction by accepting a connection
            and reading its response.
            Does *not* send any data back.
        """
        c_sock, addr = self._sock.accept()
        logging.debug(f'Connection accepted from: {addr}')

        # receive message from the connected client
        response = read_response(c_sock)

        # and print it
        print_received_message(response[protocol.nick],
                               response[protocol.message])

        # close both the client socket and itself
        c_sock.close()
        self.close()