Search in sources :

Example 1 with SessionLost

use of im.actor.core.network.mtp.entity.SessionLost in project actor-platform by actorapp.

the class ReceiverActor method onReceive.

private void onReceive(ProtoMessage message) {
    sender.send(new PusherActor.ReadPackageFromConnection());
    boolean disableConfirm = false;
    try {
        if (receivedMessages.size() >= MAX_RECEIVED_BUFFER) {
            receivedMessages.remove(0);
        }
        receivedMessages.add(message.getMessageId());
        ProtoStruct obj;
        try {
            obj = ProtoSerializer.readMessagePayload(message.getPayload());
        } catch (IOException e) {
            Log.w(TAG, "Unable to parse message: ignoring");
            e.printStackTrace();
            return;
        }
        if (obj instanceof NewSessionCreated) {
            NewSessionCreated newSessionCreated = (NewSessionCreated) obj;
            sender.send(new PusherActor.NewSession(newSessionCreated.getMessageId()));
            proto.getCallback().onSessionCreated();
        } else if (obj instanceof Container) {
            Container container = (Container) obj;
            for (ProtoMessage m : container.getMessages()) {
                self().send(m, sender());
            }
        } else if (obj instanceof SessionLost) {
            sender.send(new PusherActor.SessionLost());
        } else if (obj instanceof MTRpcResponse) {
            MTRpcResponse responseBox = (MTRpcResponse) obj;
            // Forget messages
            sender.send(new PusherActor.ForgetMessage(responseBox.getMessageId(), true));
            proto.getCallback().onRpcResponse(responseBox.getMessageId(), responseBox.getPayload());
        } else if (obj instanceof MessageAck) {
            MessageAck ack = (MessageAck) obj;
            for (long ackMsgId : ack.messagesIds) {
                sender.send(new PusherActor.ForgetMessage(ackMsgId, false));
            }
        } else if (obj instanceof MTPush) {
            MTPush box = (MTPush) obj;
            proto.getCallback().onUpdate(box.getPayload());
        } else if (obj instanceof UnsentResponse) {
            UnsentResponse unsent = (UnsentResponse) obj;
            if (!receivedMessages.contains(unsent.getResponseMessageId())) {
                disableConfirm = true;
                sender.send(new PusherActor.SendMessage(MTUids.nextId(), new RequestResend(unsent.getMessageId()).toByteArray(), true));
            }
        } else if (obj instanceof UnsentMessage) {
            UnsentMessage unsent = (UnsentMessage) obj;
            if (!receivedMessages.contains(unsent.getMessageId())) {
                disableConfirm = true;
                sender.send(new PusherActor.SendMessage(MTUids.nextId(), new RequestResend(unsent.getMessageId()).toByteArray(), false));
            }
        } else if (obj instanceof AuthIdInvalid) {
            proto.getCallback().onAuthKeyInvalidated(proto.getAuthId());
            proto.stopProto();
        } else {
            Log.w(TAG, "Unsupported package " + obj);
        }
    } catch (Exception e) {
        Log.w(TAG, "Parsing error");
    } finally {
        if (!disableConfirm) {
            sender.send(new PusherActor.ConfirmMessage(message.getMessageId()));
        }
    }
}
Also used : MTRpcResponse(im.actor.core.network.mtp.entity.MTRpcResponse) MTPush(im.actor.core.network.mtp.entity.MTPush) NewSessionCreated(im.actor.core.network.mtp.entity.NewSessionCreated) Container(im.actor.core.network.mtp.entity.Container) ProtoStruct(im.actor.core.network.mtp.entity.ProtoStruct) AuthIdInvalid(im.actor.core.network.mtp.entity.AuthIdInvalid) ProtoMessage(im.actor.core.network.mtp.entity.ProtoMessage) UnsentResponse(im.actor.core.network.mtp.entity.UnsentResponse) MessageAck(im.actor.core.network.mtp.entity.MessageAck) UnsentMessage(im.actor.core.network.mtp.entity.UnsentMessage) IOException(java.io.IOException) IOException(java.io.IOException) RequestResend(im.actor.core.network.mtp.entity.RequestResend) SessionLost(im.actor.core.network.mtp.entity.SessionLost)

Aggregations

AuthIdInvalid (im.actor.core.network.mtp.entity.AuthIdInvalid)1 Container (im.actor.core.network.mtp.entity.Container)1 MTPush (im.actor.core.network.mtp.entity.MTPush)1 MTRpcResponse (im.actor.core.network.mtp.entity.MTRpcResponse)1 MessageAck (im.actor.core.network.mtp.entity.MessageAck)1 NewSessionCreated (im.actor.core.network.mtp.entity.NewSessionCreated)1 ProtoMessage (im.actor.core.network.mtp.entity.ProtoMessage)1 ProtoStruct (im.actor.core.network.mtp.entity.ProtoStruct)1 RequestResend (im.actor.core.network.mtp.entity.RequestResend)1 SessionLost (im.actor.core.network.mtp.entity.SessionLost)1 UnsentMessage (im.actor.core.network.mtp.entity.UnsentMessage)1 UnsentResponse (im.actor.core.network.mtp.entity.UnsentResponse)1 IOException (java.io.IOException)1