Esempio n. 1
0
    def _ping(self):
        user = chat.User()
        user.name = self.username

        while True:
            try:
                print_take_input_msg()
                for message in self.conn.Ping(user):
                    if message.destination == self.username:

                        if message.type == chat.File:
                            write_file_chunks(message)
                            print_file_info(message)
                            if message.origin == self.username:
                                print("round time for ",message.id," part ",message.seqnum,"/",message.seqmax," : ",time.time()-self.messageTimes[message.id])

                        if message.type == chat.Text:
                            print_msg(message)

                        print_take_input_msg()
                    elif message.origin == self.username:
                        log_info("destination " + message.destination + " not found..")
                    else:
                        log_forwarding_info(message)
                        # log_info(
                        #     "forwarding message \"" + str(message.id) + "\" from " + message.origin + " to " + message.destination + "....")
                        # time.sleep(1)
                        chats.append(message)

            except grpc.RpcError as e:
                log_error("Fail to connect...")
                time.sleep(1)
    def _ping(self):
        user = chat.User()
        user.name = self.username

        while True:
            try:
                for message in self.conn.Ping(user):
                    if message.destination == self.username:
                        if message.type == chat.File:
                            write_file_chunks(message)
                            print_file_info(message)

                        if message.type == chat.Text:
                            print_msg(message)

                    elif message.origin == self.username:
                        log_info("destination " + message.destination +
                                 " not found..")
                    else:
                        if message.hops != 0:
                            skip = False
                            for sent_message in chats:
                                if sent_message.origin == message.origin and sent_message.id == message.id and sent_message.seqnum == message.seqnum:
                                    print("duplicate message")
                                    skip = True
                            if not skip:
                                message.hops = message.hops - 1
                                log_forwarding_info(message)
                                chats.append(message)

            except grpc.RpcError as e:
                log_error("Fail to connect...")
                time.sleep(1)
Esempio n. 3
0
def main():
    """
    Run the chat client
    The listener is spawned in a thread because it can be exited via channel closure
    The post function is kept in the main thread because it is difficult to close a blocked thread (blocked by input())
    On ctrl-c, trigger the leave function, close the channel, and wait for the listener future to return
    """
    channel = grpc.insecure_channel("localhost:3000")
    stub = chat_pb2_grpc.ChatStub(channel)
    with concurrent.futures.ThreadPoolExecutor(1) as executor:
        future = executor.submit(listen, stub, args.name)
        try:
            post(stub, chat_pb2.User(name=args.name))
        except KeyboardInterrupt:
            pass
        finally:
            print("Leaving chat...")
            stub.Leave(chat_pb2.User(name=args.name))
            channel.close()
            future.result()
Esempio n. 4
0
def listen(stub: chat_pb2_grpc.ChatStub, name: str):
    """
    Listens on the stream for new server messages
    Will be spawned in a separate thread, so we need to wrap in try/except to catch the eventual exception from the channel closing on shutdown
    """
    try:
        for message in stub.Join(chat_pb2.User(name=name)):
            print(
                f"\r{message.user.name} | {message.timestamp} - {message.content}\n> ",
                end="")
    except Exception:
        # channel close (I hope)
        pass
Esempio n. 5
0
    def _ping(self):
        user = chat.User()
        user.name = self.username

        while True:
            try:
                print_take_input_msg()
                for message in self.conn.Ping(user):
                    print_msg(message)
                    print_take_input_msg()

            except grpc.RpcError as e:
                log_error("Fail to connect...")
                time.sleep(1)