Exemple #1
0
def Cliente2():
    print('Cliente2: Trying to Send a message')
    for i in range(100):
        room_conn.SendMessage(chat.Note(nickname='Tester2', message=str(i)))
        print('Cliente2: Message:' + str(i) + 'sent ...')

    while True:
        for message in room_conn.ReceiveMessage(chat.EmptyResponse()):
            print(message)
 def ReceiveMessage(self, request, context):
     print("Rcv")
     lastindex = 0
     aux = self.Validade_User(request.roomname, request.nickname)
     if aux != None:
         while True:
             while lastindex < len(aux.Chats):
                 n = aux.Chats[lastindex]
                 n = chat.Note(roomname=request.roomname,
                               nickname=n['nickname'],
                               message=n['message'])
                 lastindex += 1
                 yield n
 def Send_message(self, Message):
     if Message != '':
         n = chat.Note(roomname=self.Roomname,
                       nickname=self.Nickname,
                       message=Message)
         for i in range(trys):
             try:
                 self.conn.SendMessage(n)
                 break
             except Exception as e:
                 print("Send")
                 print(e)
                 self.new_channel()
    def ReceiveMessage(self, request, context):
        print("Send it all")
        resp_node = self.server.FindResponsible(request, context)
        room_name = request.roomname
        resp_serv = resp_node[1][1]

        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)
                except:
                    print("False Fai Receive Message at", serv)

        if self.Request_port() in resp_serv:
            lastindex = 0
            aux = None
            while not aux:
                aux = self.server.Validade_User(request.roomname,
                                                request.nickname)
            if aux != None:
                while True:
                    while lastindex < len(aux.Chats):
                        n = aux.Chats[lastindex]
                        n = chat.Note(roomname=request.roomname,
                                      nickname=n['nickname'],
                                      message=n['message'])
                        lastindex += 1
                        yield n
        print("What")
        for serv in resp_serv:
            try:
                channel = grpc.insecure_channel(self.server.address + ':' +
                                                str(serv))
                conn = rpc.ChatSServerStub(
                    channel)  ## connection with the server
                for note in conn.ReceiveMessage(
                        chat.First(roomname=request.roomname,
                                   nickname=request.nickname)):
                    yield note
                break
            except:
                print("Error Receive Message at", serv)
    def SendMessage(self, request, context):
        resp_node = self.server.FindResponsible(request, context)
        room_name = request.roomname
        resp_serv = resp_node[1][1]

        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 Error Send Message at", serv)

        if self.Request_port() in resp_serv:
            print("I handle")
            return self.server.SendMessage(request, context)

        for serv in resp_serv:
            try:
                channel = grpc.insecure_channel(self.server.address + ':' +
                                                str(serv))
                conn = rpc.ChatSServerStub(
                    channel)  ## connection with the server
                return conn.SendMessage(
                    chat.Note(roomname=request.roomname,
                              nickname=request.nickname,
                              message=request.message))
            except:
                print("True Error Send Message at", serv)