コード例 #1
0
ファイル: chat_client.py プロジェクト: mwoinoski/crs1906
def main():
    chat_room = None
    try:
        # TODO: import ChatRoom from the chat_room module
        # from chat_room import ChatRoom

        # TODO: create a ChatRoom() object and assign it to a variable
        # named `chat_room`
        # chat_room = ChatRoom()

        # BONUS TODO: comment out the two statements above (the import of
        # ChatRoom and the setting of `chat_room`

        # BONUS TODO: import ChatRoomProxy from the chat_room_proxy module
        from chat_room_proxy import ChatRoomProxy

        # BONUS TODO: create a ChatRoomProxy() object and assign it to a variable
        # named `chat_room`
        chat_room = ChatRoomProxy()

        client_name = input("What's your name? ")

        # TODO: create a ChatClient object and assign it to a variable named
        # `chat_client`. The ChatClient constructor takes two arguments:
        # 1. client_name
        # 2. chat_room
        chat_client = ChatClient(client_name, chat_room)

        # TODO: note how we read an input line from the console
        # (no code change required)
        line = input(prompt)
        while line != "quit":
            # TODO: call the ChatClient's new_message() method, passing the
            # input line as the argument
            chat_client.new_message(line)

            line = input(prompt)

    except KeyboardInterrupt:  # user entered Ctrl-c
        pass
    except Exception as e:
        print("Exception:", e)
    finally:
        if chat_room:
            chat_room.shutdown()
コード例 #2
0
ファイル: chat_client.py プロジェクト: stekodyne/crs1906
def main():
    chat_room = None
    try:
        # TODO: import ChatRoom from the chat_room module
        # from chat_room import ChatRoom

        # TODO: create a ChatRoom() object and assign it to a variable
        # named `chat_room`
        # chat_room = ChatRoom()

        # BONUS TODO: comment out the two statements above (the import of
        # ChatRoom and the setting of `chat_room`

        # BONUS TODO: import ChatRoomProxy from the chat_room_proxy module
        from chat_room_proxy import ChatRoomProxy

        # BONUS TODO: create a ChatRoomProxy() object and assign it to a variable
        # named `chat_room`
        chat_room = ChatRoomProxy()

        client_name = input("What's your name? ")

        # TODO: create a ChatClient object and assign it to a variable named
        # `chat_client`. The ChatClient constructor takes two arguments:
        # 1. client_name
        # 2. chat_room
        chat_client = ChatClient(client_name, chat_room)

        # TODO: note how we read an input line from the console
        # (no code change required)
        line = input(prompt)
        while line != 'quit':
            # TODO: call the ChatClient's new_message() method, passing the
            # input line as the argument
            chat_client.new_message(line)

            line = input(prompt)

    except KeyboardInterrupt:  # user entered Ctrl-c
        pass
    except Exception as e:
        print('Exception:', e)
    finally:
        if chat_room:
            chat_room.shutdown()
コード例 #3
0
def main():
    chat_room = None
    try:
        # from chat_room import ChatRoom
        from chat_room_proxy import ChatRoomProxy
        chat_room = ChatRoomProxy()
        client_name = input("What's your name? ")
        chat_client = ChatClient(client_name, chat_room)
        line = input(prompt)
        while line != 'quit':
            chat_client.new_message(line)
            line = input(prompt)

    except KeyboardInterrupt:  # user entered Ctrl-c
        pass
    except Exception as e:
        print('Exception:', e)
    finally:
        if chat_room:
            chat_room.shutdown()
コード例 #4
0
ファイル: chat_client.py プロジェクト: mwoinoski/crs1906
def main():
    chat_room = None
    try:
        # from chat_room import ChatRoom
        from chat_room_proxy import ChatRoomProxy
        chat_room = ChatRoomProxy()
        client_name = input("What's your name? ")
        chat_client = ChatClient(client_name, chat_room)
        line = input(prompt)
        while line != 'quit':
            chat_client.new_message(line)
            line = input(prompt)

    except KeyboardInterrupt:  # user entered Ctrl-c
        pass
    except Exception as e:
        print('Exception:', e)
    finally:
        if chat_room:
            chat_room.shutdown()
コード例 #5
0
ファイル: test_chat_server.py プロジェクト: mwoinoski/crs1906
def test_three_chat_clients():
    # Because we're testing the server, we can skip the chat client instances
    # and let the ChatRoomProxy take the role of client.
    chat_client1 = ChatRoomProxy()

    # If the server is working correctly, the ChatRoomProxy will call
    # the update() method of its single Observer. To confirm that, we'll
    # define a Mock observer and use its assert_has_calls() to verify that
    # the update() method was called correctly.
    observer1 = Mock()

    # Attach the Mock observer to the ChatRoomProxy.
    chat_client1.observer_attach(observer1)
    time.sleep(0.1)  # pause a bit, otherwise clients may miss messages

    chat_client2 = ChatRoomProxy()
    observer2 = Mock()
    chat_client2.observer_attach(observer2)
    time.sleep(0.1)

    chat_client3 = ChatRoomProxy()
    observer3 = Mock()
    chat_client3.observer_attach(observer3)
    time.sleep(0.1)

    # Send chat messages to the server from each "client"
    chat_client1.add_message("client1", "message 1")
    chat_client2.add_message("client2", "message 2")
    chat_client3.add_message("client3", "message 3")
    chat_client3.add_message("client3", "message 4")
    chat_client2.add_message("client2", "message 5")
    chat_client1.add_message("client1", "message 6")
    time.sleep(0.2)

    chat_client1.shutdown()
    chat_client2.shutdown()
    chat_client3.shutdown()

    # Verify that the Mock observer's update() method was called correctly.
    # A chat client should receive messages sent by other clients but should
    # not receive messages that it sent.
    observer1.update.assert_has_calls(
        [
            call(ChatMessage("client2", "message 2")),
            call(ChatMessage("client3", "message 3")),
            call(ChatMessage("client3", "message 4")),
            call(ChatMessage("client2", "message 5")),
        ]
    )

    observer2.update.assert_has_calls(
        [
            call(ChatMessage("client1", "message 1")),
            call(ChatMessage("client3", "message 3")),
            call(ChatMessage("client3", "message 4")),
            call(ChatMessage("client1", "message 6")),
        ]
    )

    observer3.update.assert_has_calls(
        [
            call(ChatMessage("client1", "message 1")),
            call(ChatMessage("client2", "message 2")),
            call(ChatMessage("client2", "message 5")),
            call(ChatMessage("client1", "message 6")),
        ]
    )
コード例 #6
0
ファイル: test_chat_server.py プロジェクト: stekodyne/crs1906
def test_three_chat_clients():
    # Because we're testing the server, we can skip the chat client instances
    # and let the ChatRoomProxy take the role of client.
    chat_client1 = ChatRoomProxy()

    # If the server is working correctly, the ChatRoomProxy will call
    # the update() method of its single Observer. To confirm that, we'll
    # define a Mock observer and use its assert_has_calls() to verify that
    # the update() method was called correctly.
    observer1 = Mock()

    # Attach the Mock observer to the ChatRoomProxy.
    chat_client1.observer_attach(observer1)
    time.sleep(0.1)  # pause a bit, otherwise clients may miss messages

    chat_client2 = ChatRoomProxy()
    observer2 = Mock()
    chat_client2.observer_attach(observer2)
    time.sleep(0.1)

    chat_client3 = ChatRoomProxy()
    observer3 = Mock()
    chat_client3.observer_attach(observer3)
    time.sleep(0.1)

    # Send chat messages to the server from each "client"
    chat_client1.add_message('client1', 'message 1')
    chat_client2.add_message('client2', 'message 2')
    chat_client3.add_message('client3', 'message 3')
    chat_client3.add_message('client3', 'message 4')
    chat_client2.add_message('client2', 'message 5')
    chat_client1.add_message('client1', 'message 6')
    time.sleep(0.2)

    chat_client1.shutdown()
    chat_client2.shutdown()
    chat_client3.shutdown()

    # Verify that the Mock observer's update() method was called correctly.
    # A chat client should receive messages sent by other clients but should
    # not receive messages that it sent.
    observer1.update.assert_has_calls([
        call(ChatMessage('client2', 'message 2')),
        call(ChatMessage('client3', 'message 3')),
        call(ChatMessage('client3', 'message 4')),
        call(ChatMessage('client2', 'message 5')),
    ])

    observer2.update.assert_has_calls([
        call(ChatMessage('client1', 'message 1')),
        call(ChatMessage('client3', 'message 3')),
        call(ChatMessage('client3', 'message 4')),
        call(ChatMessage('client1', 'message 6')),
    ])

    observer3.update.assert_has_calls([
        call(ChatMessage('client1', 'message 1')),
        call(ChatMessage('client2', 'message 2')),
        call(ChatMessage('client2', 'message 5')),
        call(ChatMessage('client1', 'message 6')),
    ])