Пример #1
0
def join_other_room(client_socket, client_address, desired_room):
    """
	- Changes room of user to desired_room
	- Sends "BAD" to client if desired_room doesn't exist else "OK"
	"""
    with print_lock and get_user_name_lock and get_room_no_lock:
        print(
            f">>> (user: {get_user_name[client_address]}) requested room change from (room: {get_room_no[client_address]}) to (room: {desired_room})."
        )
    if desired_room not in rooms:
        with print_lock:
            print(f"(room: {desired_room}) doesn't exists.")
        socket_interact.send_message(client_socket, 2, "BAD")
    else:
        with get_room_no_lock:
            prv_room = get_room_no[client_address]
        with rooms_lock:
            rooms[prv_room].remove(client_address)
            if len(rooms[prv_room]) == 0:
                rooms.pop(prv_room)
            try:
                rooms[desired_room] += [client_address]
            except:
                rooms[desired_room] = [client_address
                                       ]  # Loopback (connect to current room)
        with get_room_no_lock:
            get_room_no[client_address] = desired_room
        socket_interact.send_message(client_socket, 2, "OK")
        with print_lock and get_user_name_lock:
            print(
                f"(user: {get_user_name[client_address]}) moved to (room: {desired_room})."
            )
    print_vars()
Пример #2
0
def register_client(client_socket, client_address, user_name):
    """
    - Registers user_name with server
        - Maps: client_address -> user_name
        - Sends "BAD" to client if user_name already exists else room_no of the user
    """
    global rooms_cnt
    with print_lock:
        print(f">>> {client_address} requested (user-name: {user_name}).")
    if user_name in active_users:
        with print_lock:
            print(f"(user-name: {user_name}) already exists.")
        socket_interact.send_message(client_socket, 1, "BAD")
    else:
        with active_users_lock:
            active_users.add(user_name)
        with get_user_name_lock:
            get_user_name[client_address] = user_name
        with rooms_cnt_lock:
            rooms_cnt += 1
        with rooms_lock:
            rooms[rooms_cnt] = [client_address]
        with get_room_no_lock:
            get_room_no[client_address] = rooms_cnt
        with get_client_socket_lock:
            get_client_socket[client_address] = client_socket
        socket_interact.send_message(client_socket, 1, str(rooms_cnt))
        with print_lock:
            print(f"(user-name: {user_name}) registered.")
    print_vars()
Пример #3
0
def disconnect_room(client_socket, client_address):
    """
    - Disconnects user from current room (with several users)
        - Moves him to new empty room
    """
    global rooms_cnt
    with print_lock and get_user_name_lock and get_room_no_lock:
        print(
            f">>> (user: {get_user_name[client_address]}) requested to disconnect from (room: {get_room_no[client_address]})."
        )
    with get_room_no_lock:
        prv_room = get_room_no[client_address]
    with rooms_lock:
        if len(rooms[prv_room]) == 1:
            with print_lock:
                print(f"Single user in room. Failed disconnect request.")
                socket_interact.send_message(client_socket, 3, "BAD")
                return
        else:
            rooms[prv_room].remove(client_address)
    with rooms_cnt_lock:
        rooms_cnt += 1
        new_room = rooms_cnt
    with rooms_lock:
        rooms[new_room] = [client_address]
    with get_room_no_lock:
        get_room_no[client_address] = new_room
    socket_interact.send_message(client_socket, 3,
                                 str(prv_room) + " " + str(new_room))
    with print_lock and get_user_name_lock:
        print(
            f"(user: {get_user_name[client_address]}) disconnected from (room: {prv_room}) and moved to (room: {new_room})."
        )
    print_vars()
Пример #4
0
def join_other_room(conn, room_no):
    """
	- Takes user to some other room (Lobby where his friends maybe present :P)
	"""
    socket_interact.send_message(conn, 2, str(room_no))
    p_no, data_sz = socket_interact.receive_header(conn)
    message = socket_interact.receive_message(conn, data_sz)
    if message == "OK":
        print(f"Joined (room: {room_no}).")
    else:
        print(f"(room: {room_no}) doesn't exist.")
Пример #5
0
def user_name_taken(conn, user_name):
    """
    - Return True if user_name is already registered with server else False
    """
    socket_interact.send_message(conn, 1, user_name)
    p_no, data_sz, file_name_size = socket_interact.receive_header(conn)
    message = socket_interact.receive_message(conn, data_sz)
    if message == "BAD":
        return True
    else:
        print(f"Joined (room: {message}).")
        return False
Пример #6
0
def disconnect_room(conn):
    """
	- Disconnects user from current room (with several users)
	- Moves him to new empty room
	"""
    socket_interact.send_message(conn, 3, "")
    p_no, data_sz = socket_interact.receive_header(conn)
    message = socket_interact.receive_message(conn, data_sz)
    if message == "BAD":
        print(f"You can't disconnect from a room with single user.")
    else:
        prv_room, new_room = map(int, message.split())
        print(f"Disconnected from (room: {prv_room}).")
        print(f"Joined (room: {new_room}).")
Пример #7
0
def send_file(conn, file_no):
    """
    - Sends file to all users in room
    """
    files = shell_interact.run_command("ls " + CLIENT_FILES_LOC)
    files = files.split("\n")
    files.pop()
    if file_no > len(files):
        add_log_message("File doesn't exist")
    else:
        file_name = files[file_no - 1]
        file_data = open(f"{CLIENT_FILES_LOC}/{file_name}", "rb").read()
        socket_interact.send_message(conn, 7,
                                     file_name.encode() + file_data,
                                     len(file_name))
Пример #8
0
def send_message(client_socket, client_address, message):
    """
    - Sends message to all users in room
    """
    with print_lock and get_user_name_lock and get_room_no_lock:
        print(
            f">>> (user: {get_user_name[client_address]}) broadcasted a message in (room: {get_room_no[client_address]})"
        )
    with get_room_no_lock:
        room_no = get_room_no[client_address]
    with rooms_lock and get_client_socket_lock:
        for client in rooms[room_no]:
            if client == client_address:
                continue
            socket_interact.send_message(get_client_socket[client], 6, message)
    print_vars()
Пример #9
0
def send_file(client_socket, client_address, file_name_size, file_info):
    """
    - Sends file to all users in room
    """
    with print_lock and get_user_name_lock and get_room_no_lock:
        print(
            f">>> (user: {get_user_name[client_address]}) sent a file in (room: {get_room_no[client_address]})"
        )
    with get_room_no_lock:
        room_no = get_room_no[client_address]
    with rooms_lock and get_client_socket_lock:
        for client in rooms[room_no]:
            if client == client_address:
                continue
            socket_interact.send_message(get_client_socket[client], 7,
                                         file_info, file_name_size)
    print_vars()
Пример #10
0
def send_room_info(client_socket, client_address):
    """
    - Sends room info to user
    """
    with print_lock and get_user_name_lock:
        print(
            f">>> (user: {get_user_name[client_address]}) requested room info."
        )
    with get_room_no_lock:
        room_no = get_room_no[client_address]
    room_info = []
    with rooms_lock and get_user_name_lock:
        for client in rooms[room_no]:
            room_info += [get_user_name[client]]
    socket_interact.send_message(client_socket, 5,
                                 str(room_no) + " " + " ".join(room_info))
    print_vars()
Пример #11
0
def send_message(conn, message):
    """
    - Sends message in room
    """
    message = "[ " + user_name + " ]: " + message
    socket_interact.send_message(conn, 6, message)
Пример #12
0
def get_info(conn, user_name):
    """
    - Asks server for room info
    - Displays files present on system to user
    """
    socket_interact.send_message(conn, 5, "")
Пример #13
0
def quit_app(conn):
    """
    - Erases user from server
    """
    socket_interact.send_message(conn, 4, "")
Пример #14
0
def disconnect_room(conn):
    """
    - Disconnects user from current room (with several users)
    - Moves him to new empty room
    """
    socket_interact.send_message(conn, 3, "")
Пример #15
0
def join_other_room(conn, room_no):
    """
    - Takes user to some other room (Lobby where his friends maybe present :P)
    """
    socket_interact.send_message(conn, 2, str(room_no))