예제 #1
0
    def handle_home_screen(self, client_socket, address, username_logged):
        logged_loop = True
        while logged_loop is True:

            client_reply = utilities.decode_utf_8(client_socket.recv(1024))

            if self.is_the_reply_ctrlc(client_reply):
                self.list_of_logs.append("	({}) {}:{} --> CRASHED".format(
                    utilities.get_date_and_hour(), address[0], address[1]))
                self.remove_connection(address[0], address[1])
                self.remove_user_from_online_list(username_logged)
                logged_loop = False

            else:
                self.list_of_logs.append(
                    "	({}) {}:{} --> {} requested.".format(
                        utilities.get_date_and_hour(), address[0], address[1],
                        client_reply))

                if client_reply == UIText.PLAY_NEW_GAME.value:
                    ack = utilities.ServerResponseStatus.ACK.value
                    client_socket.send(utilities.encode_utf_8(ack))
                    self.list_of_logs.append("	({}) {}:{} <-- ACK {}.".format(
                        utilities.get_date_and_hour(), address[0], address[1],
                        client_reply))

                if client_reply == UIText.PLAY_COOP_GAME.value:
                    ack = utilities.ServerResponseStatus.ACK.value
                    client_socket.send(utilities.encode_utf_8(ack))
                    self.list_of_logs.append("	({}) {}:{} <-- ACK {}.".format(
                        utilities.get_date_and_hour(), address[0], address[1],
                        client_reply))

                if client_reply == UIText.CONTINUE_GAME.value:
                    ack = utilities.ServerResponseStatus.ACK.value
                    client_socket.send(utilities.encode_utf_8(ack))
                    self.list_of_logs.append("	({}) {}:{} <-- ACK {}.".format(
                        utilities.get_date_and_hour(), address[0], address[1],
                        client_reply))

                if client_reply == UIText.PROFILE.value:

                    need_to_disconnect_the_client_or_quit_the_loop = self.handle_profile_screen(
                        client_reply, client_socket, address, username_logged)
                    if need_to_disconnect_the_client_or_quit_the_loop:
                        self.remove_user_from_online_list(username_logged)
                        logged_loop = False

                if client_reply == UIText.LOGOUT.value:
                    ack = utilities.ServerResponseStatus.ACK.value
                    client_socket.send(utilities.encode_utf_8(ack))
                    self.list_of_logs.append("	({}) {}:{} <-- ACK {}.".format(
                        utilities.get_date_and_hour(), address[0], address[1],
                        client_reply))

                    self.remove_user_from_online_list(username_logged)
                    logged_loop = False
예제 #2
0
    def handle_client_connection(self, client_socket, address):
        current_client_running = True
        try:
            while current_client_running:

                client_reply = utilities.decode_utf_8(client_socket.recv(1024))

                if self.is_the_reply_ctrlc(client_reply):
                    self.handle_client_ctrlc_request(address)
                    current_client_running = False

                elif self.is_the_reply_exit(client_reply):
                    self.handle_client_exit_request(address)
                    ack = utilities.ServerResponseStatus.ACK.value
                    client_socket.send(utilities.encode_utf_8(ack))
                    current_client_running = False

                elif client_reply == UIText.SIGN_UP.value:
                    self.handle_sign_up_screen(client_reply, client_socket,
                                               address)

                elif client_reply == UIText.SIGN_IN.value:
                    username = self.handle_sign_in_screen(
                        client_reply, client_socket, address)
                    if username:
                        self.handle_home_screen(client_socket, address,
                                                username)

        except socket.error as sock_er:
            self.handle_any_exception(address, sock_er)

        except IOError as ioe:
            if ioe.errno == errno.EPIPE:
                self.handle_any_exception(address, ioe)
            else:
                self.handle_any_exception(address, ioe)
        finally:
            self.remove_connection(address[0], address[1])
            return
예제 #3
0
def send_nicer_good_bye_and_close():
    stdscr.addstr("CTRL+C detected. Server has been informed.")
    client.socket.send(utilities.encode_utf_8("CTRL+C"))
    client.socket.close()
    client.print_message_and_press_enter_to_continue("\nPress ENTER to close")
예제 #4
0
 def send_encoded_message_via_socket(self, message):
     self.socket.send(utilities.encode_utf_8(message))
예제 #5
0
    def handle_sign_in_screen(self, client_reply, client_socket, address):
        self.list_of_logs.append("	({}) {}:{} --> {} requested.".format(
            utilities.get_date_and_hour(), address[0], address[1],
            client_reply))

        ack = utilities.ServerResponseStatus.ACK.value
        client_socket.send(utilities.encode_utf_8(ack))
        self.list_of_logs.append("	({}) {}:{} <-- ACK {}.".format(
            utilities.get_date_and_hour(), address[0], address[1],
            client_reply))

        client_reply = utilities.decode_utf_8(client_socket.recv(1024))
        self.list_of_logs.append(
            "	({}) {}:{} --> Sign in credentials sent.".format(
                utilities.get_date_and_hour(), address[0], address[1]))

        if self.is_the_reply_ctrlc(client_reply):
            self.list_of_logs.append(
                "	({}) {}:{} has disconnected: CTRL+C detected.".format(
                    utilities.get_date_and_hour(), address[0], address[1]))
            self.remove_connection(address[0], address[1])
            self.close_client_connection(client_socket)

            msg = None

        else:

            splitted_credential = client_reply.split(";")
            username = splitted_credential[0]
            password = splitted_credential[1]

            if self.validate_user(username, password):

                if self.is_user_online(username):
                    rejected = utilities.ServerResponseStatus.USER_ALREADY_ONLINE.value
                    client_socket.send(utilities.encode_utf_8(rejected))
                    self.list_of_logs.append(
                        "	({}) {}:{} <-- Sign in REJECTED - User already online."
                        .format(utilities.get_date_and_hour(), address[0],
                                address[1]))

                    msg = None

                else:
                    ack = utilities.ServerResponseStatus.ACK.value
                    client_socket.send(utilities.encode_utf_8(ack))
                    self.list_of_logs.append(
                        "	({}) {}:{} <-- Sign in VALIDATED.".format(
                            utilities.get_date_and_hour(), address[0],
                            address[1]))
                    self.add_user_to_online_list(username)

                    msg = username
            else:
                deny = utilities.ServerResponseStatus.DENY.value
                client_socket.send(utilities.encode_utf_8(deny))
                self.list_of_logs.append(
                    "	({}) {}:{} <-- Sign in DENIED.".format(
                        utilities.get_date_and_hour(), address[0], address[1]))

                msg = None

        return msg
예제 #6
0
    def handle_sign_up_screen(self, client_reply, client_socket, address):
        self.list_of_logs.append("	({}) {}:{} --> {} requested.".format(
            utilities.get_date_and_hour(), address[0], address[1],
            client_reply))

        ack = utilities.ServerResponseStatus.ACK.value

        client_socket.send(utilities.encode_utf_8(ack))

        self.list_of_logs.append("	({}) {}:{} <-- ACK {}.".format(
            utilities.get_date_and_hour(), address[0], address[1],
            client_reply))

        sign_up_loop = True
        while sign_up_loop is True:

            client_reply = utilities.decode_utf_8(client_socket.recv(1024))
            self.list_of_logs.append(
                "	({}) {}:{} --> Sign up details sent.".format(
                    utilities.get_date_and_hour(), address[0], address[1]))

            if self.is_the_reply_ctrlc(client_reply):
                self.list_of_logs.append("	({}) {}:{} --> CRASHED.".format(
                    utilities.get_date_and_hour(), address[0], address[1]))
                self.remove_connection(address[0], address[1])
                sign_up_loop = False

            else:
                splitted_details = client_reply.split(";")

                username = splitted_details[0]
                password = splitted_details[1]
                gender = splitted_details[2]
                age = splitted_details[3]
                email = splitted_details[4]

                sign_up_loop = False
                indexs_wrong_field = []
                if utilities.is_valid_format(username) or len(username) == 0:
                    sign_up_loop = True
                    indexs_wrong_field.append(0)
                if len(password) == 0:
                    sign_up_loop = True
                    indexs_wrong_field.append(1)
                if utilities.is_valid_format(gender) or gender.isdigit():
                    sign_up_loop = True
                    indexs_wrong_field.append(2)
                if not age.isdigit() and len(age) is not 0:
                    sign_up_loop = True
                    indexs_wrong_field.append(3)
                if not utilities.is_valid_email(email):
                    sign_up_loop = True
                    indexs_wrong_field.append(4)

                if sign_up_loop is True:
                    self.list_of_logs.append(
                        "	({}) {}:{} <-- Wrong sign up details received.".
                        format(utilities.get_date_and_hour(), address[0],
                               address[1]))

                    wrongdetails = utilities.ServerResponseStatus.WRONG_USER_DETAILS.value
                    formatted_indexs_wrong_field = str("-".join(
                        map(str, indexs_wrong_field)))
                    msg = []
                    msg.append(wrongdetails)
                    msg.append(formatted_indexs_wrong_field)
                    serverMessage = str(";".join(map(str, msg)))
                    client_socket.send(utilities.encode_utf_8(serverMessage))

                else:
                    self.list_of_logs.append(
                        "	({}) {}:{} <-- Correct sign up details received.".
                        format(utilities.get_date_and_hour(), address[0],
                               address[1]))

                    new_user_details = {
                        "username": splitted_details[0],
                        "password": splitted_details[1],
                        "gender": splitted_details[2],
                        "age": splitted_details[3],
                        "email": splitted_details[4]
                    }

                    if not self.is_already_signed(splitted_details[0],
                                                  splitted_details[4]):
                        self.save_user_credentials(new_user_details)

                        signed = utilities.ServerResponseStatus.SIGNED.value
                        client_socket.send(utilities.encode_utf_8(signed))
                        self.list_of_logs.append(
                            "	({}) {}:{} <-- Sign up completed.".format(
                                utilities.get_date_and_hour(), address[0],
                                address[1]))

                    else:
                        already_signed = utilities.ServerResponseStatus.EMAIL_OR_USER_ALREADY_EXISTS.value
                        client_socket.send(
                            utilities.encode_utf_8(already_signed))
                        self.list_of_logs.append(
                            "	({}) {}:{} <-- Sign up denied - User or email already exists."
                            .format(utilities.get_date_and_hour(), address[0],
                                    address[1]))

                        sign_up_loop = True
예제 #7
0
    def handle_profile_screen(self, client_reply, client_socket, address,
                              username_logged):
        ack = utilities.ServerResponseStatus.ACK.value
        client_socket.send(utilities.encode_utf_8(ack))
        self.list_of_logs.append("	({}) {}:{} <-- ACK {}.".format(
            utilities.get_date_and_hour(), address[0], address[1],
            client_reply))

        need_to_disconnect_the_client = False
        client_is_crashed = False
        profile_loop = True

        while profile_loop is True:

            client_reply = utilities.decode_utf_8(client_socket.recv(1024))
            client_reply_split = client_reply.split("-")

            if self.is_the_reply_ctrlc(client_reply_split):
                self.list_of_logs.append("	({}) {}:{} --> CRASHED.".format(
                    utilities.get_date_and_hour(), address[0], address[1]))
                self.remove_connection(address[0], address[1])
                profile_loop = False
                client_is_crashed = True

            elif client_reply_split[0] == UIText.SAVE.value:

                split_details = client_reply_split[1].split(";")

                username = split_details[0]
                password = split_details[1]
                gender = split_details[2]
                age = split_details[3]
                email = split_details[4]

                new_user_details = {
                    "username": username,
                    "password": password,
                    "gender": gender,
                    "age": age,
                    "email": email
                }

                profile_loop = False
                indexs_wrong_field = []
                if utilities.is_valid_format(username) or len(username) == 0:
                    profile_loop = True
                    indexs_wrong_field.append(0)
                if len(password) == 0:
                    profile_loop = True
                    indexs_wrong_field.append(1)
                if utilities.is_valid_format(gender) or gender.isdigit():
                    profile_loop = True
                    indexs_wrong_field.append(2)
                if not age.isdigit() and len(age) is not 0:
                    profile_loop = True
                    indexs_wrong_field.append(3)
                if not utilities.is_valid_email(email):
                    profile_loop = True
                    indexs_wrong_field.append(4)

                if profile_loop is True:
                    self.list_of_logs.append(
                        "	({}) {}:{} <-- Wrong new credentials received.".
                        format(utilities.get_date_and_hour(), address[0],
                               address[1]))

                    wrongdetails = utilities.ServerResponseStatus.WRONG_USER_DETAILS.value
                    formatted_indexs_wrong_field = str("-".join(
                        map(str, indexs_wrong_field)))
                    msg = []
                    msg.append(wrongdetails)
                    msg.append(formatted_indexs_wrong_field)
                    serverMessage = str(";".join(map(str, msg)))
                    client_socket.send(utilities.encode_utf_8(serverMessage))

                elif not self.is_already_signed(
                        new_user_details.get("username"),
                        new_user_details.get("password")):
                    self.save_existing_user_new_credentials(
                        new_user_details, username_logged)

                    ack = utilities.ServerResponseStatus.ACK.value
                    client_socket.send(utilities.encode_utf_8(ack))
                    self.list_of_logs.append(
                        "	({}) {}:{} <-- New credentials saved.".format(
                            utilities.get_date_and_hour(),
                            address[0],
                            address[1],
                        ))

                    profile_loop = False
                    need_to_disconnect_the_client = True
                else:
                    already_signed = utilities.ServerResponseStatus.EMAIL_OR_USER_ALREADY_EXISTS.value
                    client_socket.send(utilities.encode_utf_8(already_signed))
                    self.list_of_logs.append(
                        "	({}) {}:{} <-- New credential not saved - Username or email already exists."
                        .format(utilities.get_date_and_hour(), address[0],
                                address[1]))

                    profile_loop = True

            elif client_reply_split[0] == UIText.BACK.value:

                ack = utilities.ServerResponseStatus.ACK.value
                client_socket.send(utilities.encode_utf_8(ack))
                self.list_of_logs.append(
                    "	({}) {}:{} <-- No changes applied. Profile closed.".
                    format(utilities.get_date_and_hour(), address[0],
                           address[1]))
                profile_loop = False

        if need_to_disconnect_the_client or client_is_crashed:
            return True
        else:
            return False