コード例 #1
0
ファイル: test_chat.py プロジェクト: Grulfen/pychat
    def setup_class(self):
        from listen import listen_thread
        self.host = Address(socket.gethostname(), 9898)
        self.remote = Address(socket.gethostname(), 9898)
        new_chat_pipe_out, new_chat_pipe_in = Pipe(False)

        self.state = State("test", self.host, TESTFILE)
        self.l_thread = GenericThread(listen_thread, self.host, self.state,
                                      new_chat_pipe_in)
        self.l_thread.start()
        time.sleep(0.2)
コード例 #2
0
ファイル: test_chat.py プロジェクト: Grulfen/pychat
class TestServer:
    def setup_class(self):
        from listen import listen_thread
        self.host = Address(socket.gethostname(), 9898)
        self.remote = Address(socket.gethostname(), 9898)
        new_chat_pipe_out, new_chat_pipe_in = Pipe(False)

        self.state = State("test", self.host, TESTFILE)
        self.l_thread = GenericThread(listen_thread, self.host, self.state,
                                      new_chat_pipe_in)
        self.l_thread.start()
        time.sleep(0.2)

    def teardown_class(self):
        self.l_thread.terminate()
        os.remove(TESTFILE)

    def test_ping_pong(self):
        ping_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        message = "<ping><null><null>"

        ping_socket.connect(self.remote)
        ping_socket.send(message.encode('latin1'))
        ping_socket.shutdown(socket.SHUT_WR)
        pong = ping_socket.recv(1024).decode('latin1')
        ping_socket.close()
        assert pong == "<pong><null><null>"

    def test_get_msg_hi(self):
        get_pipe, send_pipe = Pipe(False)
        self.state.pipes["test_sender"] = (send_pipe, None)

        hi_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        message_send = "<msg><test_sender><test>hi"

        hi_socket.connect(self.remote)
        hi_socket.send(message_send.encode('latin1'))
        hi_socket.close()

        while not get_pipe.poll():
            time.sleep(0.01)
        message_get = get_pipe.recv()
        assert message_get == "test_sender: hi"

        del self.state.pipes["test_sender"]
コード例 #3
0
def main():
    """ Main function, gets arguments from sys.argv and starts chat and listen
        thread """
    parser = ArgumentParser(description='Chat program')
    parser.add_argument('-l', '--local-port', default=12345, type=int)
    parser.add_argument('-n', '--name', default='apa', help='Your name')
    args = parser.parse_args()
    host = Address(socket.gethostname(), args.local_port)

    new_chat_pipe_out, new_chat_pipe_in = Pipe(False)
    state = State(args.name, host)
    input_control = InputControl(args.name, host, state)
    l_thread = GenericThread(listen_thread, host, state, new_chat_pipe_in)
    l_thread.start()

    print(">", end=" ")
    sys.stdout.flush()
    while True:
        try:
            input_ready, _, _ = select.select([sys.stdin,
                                              new_chat_pipe_out.fileno()],
                                              [], [], 0)
            if sys.stdin in input_ready:
                # Command from command line
                line = sys.stdin.readline()
                if line:
                    input_control.handle_input(line)
                else:
                    # Shutdown on EOF
                    input_control.shutdown()
            if new_chat_pipe_out.fileno() in input_ready:
                # Got message from friend, but chat is not open
                friend_name = new_chat_pipe_out.recv()
                state.connect_to(friend_name)
            # Check if any chat has been closed
            state.check_closed_chat()
            time.sleep(0.1)

        except (EOFError, KeyboardInterrupt):
            state.shutdown()
            l_thread.terminate()