Esempio n. 1
0
def run():
    with grpc.insecure_channel('localhost:5002') as channel:
        stub = birdwiki_pb2_grpc.BirdWikiStub(channel)
        # login(birdwiki_pb2_grpc.LoginUserStub(channel))

        while True:
            try:

                bird = chooseBird(stub)  # LISTA AS AVES E ESCOLHE UMA
                print(
                    "Escolha uma das opcoes: \n[1] Abrir para leitura \n[2] Editar \n[3] Deletar \n[0] Sair"
                )
                option = int(input())
                if (option == 2):
                    # VERIFICA SE PODE EDITAR AVE ESCOLHIDA
                    checkBirdAvailability(stub, bird)
                elif (option == 1):
                    readBird(stub, bird)
                elif (option == 3):
                    deleteBird(stub, bird)
                else:
                    raise KeyboardInterrupt

            except KeyboardInterrupt:
                print("Goodbye")
                channel.unsubscribe(close)
                exit()
            except grpc.RpcError:
                print("Server failed")
                channel.unsubscribe(close)
                exit()
def deleteBird(request, addrs):
    name = request.name
    port = getServer(name, addrs)

    with grpc.insecure_channel("localhost:" + str(port)) as channel:
        print(f"DELEGATING READ BIRD {name} TO SERVER {port}")
        stub = birdwiki_pb2_grpc.BirdWikiStub(channel)
        response = stub.deleteBird(birdwiki_pb2.BirdName(name=name))
        return response
def broadcast():
    list_neigh = []
    for i in range(NODE_QT):
        if i == SERVER_ID:
            continue
        port = BASE_SERVER + i
        try:
            with grpc.insecure_channel("localhost:" + str(port)) as channel:
                stub = birdwiki_pb2_grpc.BirdWikiStub(channel)
                response = stub.greeting(
                    birdwiki_pb2.ServerInfo(serverId=SERVER_ID))

                if response.flag == True:
                    print("SERVER", port, "RESPONDED GREETING")
                    list_neigh.append(port)

                else:
                    raise Exception

        except Exception as e:
            print(f"Server witth port {port} not found.")

    print(f"My neighbours: {list_neigh}")
    return list_neigh