def AddNewNode(self, request, context):
     others = self.server.AddNewNode(request, context)
     for tupla in others:  # each tupla is an id and a list of ports
         for node in tupla[1]:  # member 1 is the list of ports
             try:
                 channel = grpc.insecure_channel(self.server.address + ':' +
                                                 str(node))
                 conn = rpc.ChatSServerStub(
                     channel)  ## connection with the responsible server
                 conn.AddNewNode(
                     chat.NewNodeReq(n_id=request.n_id, port=request.port))
             except:
                 print("Fail on AddNewNode at", node)
     return chat.EmptyResponse()
    def go_online(self):
        # Colocar para os nos adicionarem o main server a sua table e requisitarem a adicao deles no main server table
        if self.id != 0:
            main_servers = [11904, 11936, 11968, 12000]
            for serv in main_servers:
                self.route_table.add_node(0, serv)
                channel = grpc.insecure_channel(self.address + ':' + str(serv))
                conn = rpc.ChatSServerStub(channel)
                print("send", self.id, self.Request_port)
                conn.AddNewNode(
                    chat.NewNodeReq(n_id=self.id, port=self.Request_port))

        server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
        rpc.add_ChatSServerServicer_to_server(ChatServer(self), server)
        print('Starting server, Listenning ...')
        server.add_insecure_port('[::]:' + str(self.Request_port))
        server.start()
        server.wait_for_termination()