Пример #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 SendMessage(self, request, context):
        aux = self.Validade_User_Index(request.roomname, request.nickname)
        print(aux)
        if aux < len(self.ChatRooms):
            print('Message;' + request.nickname + ";" + request.roomname +
                  ";" + request.message)
            self.AuxSendMessage(aux, request.nickname, request.roomname,
                                request.message)
            self.state_file.stack_log('Message;' + request.nickname + ";" +
                                      request.roomname + ";" + request.message)

        return chat.EmptyResponse()
    def Quit(self, request, context):
        aux = self.Validade_User(request.roomname, request.nickname)
        if aux != None:
            print('LeftChat;' + request.nickname + ";" + request.roomname)
            self.state_file.stack_log('LeftChat;' + request.nickname + ";" +
                                      request.roomname)
            aux.Chats.append({
                'nickname': request.nickname,
                'message': request.nickname + ' quited chat room;'
            })
            aux.Nicknames.remove(request.nickname)

            return chat.EmptyResponse()
 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()