コード例 #1
0
    def CreateChat(self, request, context):
        print("Create chat")
        resp_node = self.server.FindResponsible(
            request,
            context)  # Fist - try to descover who will handle the request
        room_name = request.roomname  # the id of the room
        resp_serv = resp_node[1][
            1]  # list of severs that will/might know who handle
        print(resp_serv)

        # Talvez problema devido a atribuicoa resp_serv dentro do for
        if not resp_node[
                0]:  # Communicate with the server that might know who will respond the request
            for serv in resp_serv:
                try:
                    channel = grpc.insecure_channel(self.server.address + ':' +
                                                    str(serv))
                    conn = rpc.ChatSServerStub(
                        channel)  # connection with the responsible server
                    result = conn.FindResponsible(
                        chat.FindRRequest(roomname=room_name))
                    resp_serv = self.Str_to_list_ports(result.port)
                    break
                except:
                    print("False Fail Create Chat at", serv)

        # If this server is the one supposed to handle -----------------------------------------------------------------------------
        if self.Request_port() in resp_serv:
            print("I handle", request.roomname, request.password,
                  request.nickname)
            result = self.server.CreateChat(request.roomname, request.password,
                                            request.nickname)
            print(result)
            if result:
                return chat.JoinResponse(state='sucess', Port=0)
            else:
                return chat.JoinResponse(state='fail', Port=0)

        # Server knows who will handle --------------------------------------------------------------------------------------------
        print("I know who will handle")
        print("is : ", resp_serv)
        for serv in resp_serv:
            try:
                channel = grpc.insecure_channel(self.server.address + ':' +
                                                str(serv))
                conn = rpc.ChatSServerStub(
                    channel)  ## connection with the responsible server
                result = conn.CreateChat(
                    chat.CreateChatRequest(roomname=request.roomname,
                                           password=request.password,
                                           nickname=request.nickname))
                break
            except:
                print("False Fail Create Chat at", serv)

        return result
コード例 #2
0
    def Create_chatRoom(self, Roomname, Password, Nickname):
        for i in range(trys):
            try:
                response = self.conn.CreateChat(
                    chat.CreateChatRequest(roomname=Roomname,
                                           password=Password,
                                           nickname=Nickname))
                break
            except Exception as e:
                print("Create")
                print(e)
                self.new_channel()

        if response.state == 'sucess':
            print('Sucess')
            self.Nickname = Nickname
            self.Roomname = Roomname
            self.chats.clear()
            self.start_Listenner()
            print('Time to fail')
            return True
        else:
            return False