Search in sources :

Example 1 with Update

use of im.actor.core.network.parser.Update in project actor-platform by actorapp.

the class SequenceActor method onUpdateReceived.

private void onUpdateReceived(final int seq, final byte[] state, int type, byte[] body, List<ApiUser> users, List<ApiGroup> groups) {
    // Checking sequence
    if (seq <= this.seq) {
        Log.d(TAG, "Ignored SeqUpdate {seq:" + seq + ", currentSeq: " + this.seq + "}");
        return;
    }
    Log.d(TAG, "SeqUpdate {seq:" + seq + "}");
    if (!isValidated) {
        Log.d(TAG, "Stashing update");
        stash();
        return;
    }
    if (seq != this.seq + 1) {
        stash();
        if (seq - this.seq > INVALIDATE_MAX_SEC_HOLE) {
            Log.w(TAG, "Out of sequence: Too big hole. Force invalidate immediately");
            forceInvalidate();
        }
        if (isTimerStarted) {
            Log.w(TAG, "Out of sequence: timer already started");
        } else {
            Log.w(TAG, "Out of sequence: starting timer for invalidation");
            startInvalidationTimer();
        }
        return;
    }
    Update update = null;
    try {
        update = updatesParser.read(type, body);
    } catch (IOException e) {
        e.printStackTrace();
    }
    if (update != null) {
        if ((users == null || users.size() == 0) && (groups == null || groups.size() == 0)) {
            if (validator.isCausesInvalidation(update)) {
                Log.w(TAG, "Invalidating: No Users of Groups available for update");
                forceInvalidate();
                return;
            }
        }
        Log.d(TAG, "Handling update #" + seq);
        startWakeLock();
        handler.onSeqUpdate(update, users, groups).then(aVoid -> {
            Log.d(TAG, "Handling update ended #" + seq);
            onUpdatesApplied(seq, state);
        });
    }
    // Saving memory-only state
    this.seq = seq;
    this.state = state;
    unstashAll();
    stopInvalidationTimer();
}
Also used : IOException(java.io.IOException) FatSeqUpdate(im.actor.core.api.base.FatSeqUpdate) WeakUpdate(im.actor.core.api.base.WeakUpdate) Update(im.actor.core.network.parser.Update) SeqUpdate(im.actor.core.api.base.SeqUpdate)

Example 2 with Update

use of im.actor.core.network.parser.Update in project actor-platform by actorapp.

the class GetDiffCombiner method buildDiff.

public static CombinedDifference buildDiff(List<Update> updates) {
    CombinedDifference res = new CombinedDifference();
    UpdateChatGroupsChanged chatGroupsChanged = null;
    for (Update u : updates) {
        if (u instanceof UpdateMessage) {
            res.putMessage((UpdateMessage) u);
        } else if (u instanceof UpdateMessageRead) {
            UpdateMessageRead read = (UpdateMessageRead) u;
            res.putRead(convert(read.getPeer()), read.getStartDate());
        } else if (u instanceof UpdateMessageReceived) {
            UpdateMessageReceived received = (UpdateMessageReceived) u;
            res.putReceived(convert(received.getPeer()), received.getStartDate());
        } else if (u instanceof UpdateMessageReadByMe) {
            UpdateMessageReadByMe readByMe = (UpdateMessageReadByMe) u;
            int counter = 0;
            if (readByMe.getUnreadCounter() != null) {
                counter = readByMe.getUnreadCounter();
            }
            res.putReadByMe(convert(readByMe.getPeer()), readByMe.getStartDate(), counter);
        } else if (u instanceof UpdateCountersChanged) {
        // Ignore
        } else if (u instanceof UpdateChatGroupsChanged) {
            chatGroupsChanged = (UpdateChatGroupsChanged) u;
        } else {
            res.getOtherUpdates().add(u);
        }
    }
    // Handling reordering of updates
    if (chatGroupsChanged != null) {
        ArrayList<ApiDialogGroup> dialogs = new ArrayList<>();
        for (ApiDialogGroup dg : chatGroupsChanged.getDialogs()) {
            ArrayList<ApiDialogShort> dialogShorts = new ArrayList<>();
            for (ApiDialogShort ds : dg.getDialogs()) {
                CombinedDifference.ReadByMeValue val = res.getReadByMe().get(convert(ds.getPeer()));
                if (val != null) {
                    dialogShorts.add(new ApiDialogShort(ds.getPeer(), val.getCounter(), ds.getDate()));
                } else {
                    dialogShorts.add(ds);
                }
            }
            dialogs.add(new ApiDialogGroup(dg.getTitle(), dg.getKey(), dialogShorts));
        }
        res.getOtherUpdates().add(new UpdateChatGroupsChanged(dialogs));
    }
    return res;
}
Also used : UpdateMessage(im.actor.core.api.updates.UpdateMessage) ArrayList(java.util.ArrayList) UpdateMessageReadByMe(im.actor.core.api.updates.UpdateMessageReadByMe) UpdateCountersChanged(im.actor.core.api.updates.UpdateCountersChanged) UpdateMessageReceived(im.actor.core.api.updates.UpdateMessageReceived) Update(im.actor.core.network.parser.Update) UpdateChatGroupsChanged(im.actor.core.api.updates.UpdateChatGroupsChanged) UpdateMessageRead(im.actor.core.api.updates.UpdateMessageRead) ApiDialogGroup(im.actor.core.api.ApiDialogGroup) ApiDialogShort(im.actor.core.api.ApiDialogShort)

Example 3 with Update

use of im.actor.core.network.parser.Update in project actor-platform by actorapp.

the class SequenceActor method invalidate.

private void invalidate() {
    if (!isValidated) {
        return;
    }
    isValidated = false;
    startWakeLock();
    if (seq < 0) {
        Log.d(TAG, "Loading fresh state...");
        api(new RequestGetState(ApiSupportConfiguration.OPTIMIZATIONS)).then(response -> {
            if (isValidated) {
                return;
            }
            Log.d(TAG, "State loaded {seq=" + seq + "}");
            seq = response.getSeq();
            state = response.getState();
            persistState(seq, state);
            stopWakeLock();
            onBecomeValid(response.getSeq(), response.getState());
        });
    } else {
        Log.d(TAG, "Loading difference...");
        onUpdateStarted();
        final long loadStart = im.actor.runtime.Runtime.getCurrentTime();
        api(new RequestGetDifference(seq, state, ApiSupportConfiguration.OPTIMIZATIONS)).then(response -> {
            if (isValidated) {
                return;
            }
            long parsingStart = Runtime.getCurrentTime();
            ArrayList<Update> updates = new ArrayList<>();
            for (ApiUpdateContainer container : response.getUpdates()) {
                try {
                    updates.add(updatesParser.read(container.getUpdateHeader(), container.getUpdate()));
                } catch (IOException e) {
                    e.printStackTrace();
                // Ignore
                }
            }
            Log.d(TAG, "Difference loaded {seq=" + response.getSeq() + "} in " + (Runtime.getCurrentTime() - loadStart) + " ms, " + "parsed in " + (Runtime.getCurrentTime() - parsingStart) + " ms, " + "userRefs: " + response.getUsersRefs().size() + ", " + "groupRefs: " + response.getGroupsRefs().size());
            handler.onDifferenceUpdate(response.getUsers(), response.getGroups(), response.getUsersRefs(), response.getGroupsRefs(), updates).then(updateProcessed -> onUpdatesApplied(response.getSeq(), response.getState()));
            onBecomeValid(response.getSeq(), response.getState());
            if (response.needMore()) {
                invalidate();
            } else {
                onUpdateEnded();
            }
        });
    }
}
Also used : ApiUpdateContainer(im.actor.core.api.ApiUpdateContainer) RequestGetDifference(im.actor.core.api.rpc.RequestGetDifference) RequestGetState(im.actor.core.api.rpc.RequestGetState) ArrayList(java.util.ArrayList) IOException(java.io.IOException) FatSeqUpdate(im.actor.core.api.base.FatSeqUpdate) WeakUpdate(im.actor.core.api.base.WeakUpdate) Update(im.actor.core.network.parser.Update) SeqUpdate(im.actor.core.api.base.SeqUpdate)

Example 4 with Update

use of im.actor.core.network.parser.Update in project actor-platform by actorapp.

the class SequenceActor method onWeakUpdateReceived.

private void onWeakUpdateReceived(int type, byte[] body, long date) {
    Update update;
    try {
        update = updatesParser.read(type, body);
    } catch (IOException e) {
        e.printStackTrace();
        return;
    }
    handler.onWeakUpdate(update, date);
}
Also used : IOException(java.io.IOException) FatSeqUpdate(im.actor.core.api.base.FatSeqUpdate) WeakUpdate(im.actor.core.api.base.WeakUpdate) Update(im.actor.core.network.parser.Update) SeqUpdate(im.actor.core.api.base.SeqUpdate)

Example 5 with Update

use of im.actor.core.network.parser.Update in project actor-platform by actorapp.

the class SequenceHandlerActor method onDifferenceUpdate.

private Promise<Void> onDifferenceUpdate(@NotNull List<ApiUser> users, @NotNull List<ApiGroup> groups, @NotNull List<ApiUserOutPeer> userOutPeers, @NotNull List<ApiGroupOutPeer> groupOutPeers, @NotNull List<Update> updates) {
    if (updates.size() > 0) {
        String command = "Difference updates:";
        for (Update u : updates) {
            command += "\n| " + u;
        }
        Log.d(TAG, command);
    }
    beginUpdates();
    // Related Users
    Promise<Void> currentPromise = updates().applyRelatedData(users, groups);
    // Loading missing peers
    currentPromise = currentPromise.chain(v -> updates().loadRequiredPeers(userOutPeers, groupOutPeers));
    // Apply Diff
    long applyStart = im.actor.runtime.Runtime.getCurrentTime();
    currentPromise = currentPromise.chain(v -> processor.applyDifferenceUpdate(updates)).then(v -> {
        Log.d(TAG, "Difference applied in " + (im.actor.runtime.Runtime.getCurrentTime() - applyStart) + " ms");
        endUpdates();
    });
    return currentPromise;
}
Also used : HandlerDifferenceUpdates(im.actor.core.modules.sequence.internal.HandlerDifferenceUpdates) ModuleContext(im.actor.core.modules.ModuleContext) ActorCreator(im.actor.runtime.actors.ActorCreator) Promise(im.actor.runtime.promise.Promise) ApiUser(im.actor.core.api.ApiUser) PromiseFunc(im.actor.runtime.promise.PromiseFunc) ArrayList(java.util.ArrayList) UpdateProcessor(im.actor.core.modules.sequence.processor.UpdateProcessor) HandlerRelatedResponse(im.actor.core.modules.sequence.internal.HandlerRelatedResponse) PromisesArray(im.actor.runtime.promise.PromisesArray) HandlerWeakUpdate(im.actor.core.modules.sequence.internal.HandlerWeakUpdate) RequestGetReferencedEntitites(im.actor.core.api.rpc.RequestGetReferencedEntitites) ApiGroup(im.actor.core.api.ApiGroup) Void(im.actor.runtime.actors.messages.Void) Runtime(im.actor.runtime.Runtime) Constructor(im.actor.runtime.function.Constructor) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) ModuleActor(im.actor.core.modules.ModuleActor) HandlerSeqUpdate(im.actor.core.modules.sequence.internal.HandlerSeqUpdate) ApiGroupOutPeer(im.actor.core.api.ApiGroupOutPeer) ApiUserOutPeer(im.actor.core.api.ApiUserOutPeer) Log(im.actor.runtime.Log) NotNull(org.jetbrains.annotations.NotNull) Function(im.actor.runtime.function.Function) Update(im.actor.core.network.parser.Update) Promises(im.actor.runtime.promise.Promises) HandlerWeakUpdate(im.actor.core.modules.sequence.internal.HandlerWeakUpdate) HandlerSeqUpdate(im.actor.core.modules.sequence.internal.HandlerSeqUpdate) Update(im.actor.core.network.parser.Update) Void(im.actor.runtime.actors.messages.Void)

Aggregations

Update (im.actor.core.network.parser.Update)6 ArrayList (java.util.ArrayList)4 FatSeqUpdate (im.actor.core.api.base.FatSeqUpdate)3 SeqUpdate (im.actor.core.api.base.SeqUpdate)3 WeakUpdate (im.actor.core.api.base.WeakUpdate)3 IOException (java.io.IOException)3 UpdateMessageRead (im.actor.core.api.updates.UpdateMessageRead)2 UpdateMessageReadByMe (im.actor.core.api.updates.UpdateMessageReadByMe)2 UpdateMessageReceived (im.actor.core.api.updates.UpdateMessageReceived)2 Void (im.actor.runtime.actors.messages.Void)2 ApiDialogGroup (im.actor.core.api.ApiDialogGroup)1 ApiDialogShort (im.actor.core.api.ApiDialogShort)1 ApiGroup (im.actor.core.api.ApiGroup)1 ApiGroupOutPeer (im.actor.core.api.ApiGroupOutPeer)1 ApiUpdateContainer (im.actor.core.api.ApiUpdateContainer)1 ApiUser (im.actor.core.api.ApiUser)1 ApiUserOutPeer (im.actor.core.api.ApiUserOutPeer)1 RequestGetDifference (im.actor.core.api.rpc.RequestGetDifference)1 RequestGetReferencedEntitites (im.actor.core.api.rpc.RequestGetReferencedEntitites)1 RequestGetState (im.actor.core.api.rpc.RequestGetState)1