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();
}
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;
}
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();
}
});
}
}
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);
}
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;
}
Aggregations