use of im.actor.core.network.mtp.entity.SessionHello in project actor-platform by actorapp.
the class PusherActor method onReceive.
@Override
public void onReceive(Object message) {
if (message instanceof SendMessage) {
if (isEnableLog) {
Log.d(TAG, "Received SendMessage #" + ((SendMessage) message).mid);
}
SendMessage sendMessage = (SendMessage) message;
ProtoMessage holder = new ProtoMessage(sendMessage.mid, sendMessage.message);
unsentPackages.put(holder.getMessageId(), holder);
doSend(holder);
} else if (message instanceof ConnectionCreated) {
if (isEnableLog) {
Log.d(TAG, "Received ConnectionCreated");
}
// Marking all pending confirms as unsent
confirm.addAll(pendingConfirm);
pendingConfirm.clear();
// Resending unsent messages
ArrayList<ProtoMessage> toSend = new ArrayList<>();
for (ProtoMessage unsentPackage : unsentPackages.values()) {
if (isEnableLog) {
Log.d(TAG, "ReSending #" + unsentPackage.getMessageId());
}
toSend.add(unsentPackage);
}
// Sending SessionHello if there is no packages to sent
if (toSend.size() == 0) {
if (isEnableLog) {
Log.d(TAG, "Sending SessionHello");
}
toSend.add(new ProtoMessage(MTUids.nextId(), new SessionHello().toByteArray()));
}
doSend(toSend);
} else if (message instanceof SessionLost) {
if (isEnableLog) {
Log.d(TAG, "Sending SessionHello");
}
doSend(new ProtoMessage(MTUids.nextId(), new SessionHello().toByteArray()));
} else if (message instanceof ForgetMessage) {
if (isEnableLog) {
Log.d(TAG, "Received ForgetMessage #" + ((ForgetMessage) message).mid);
}
unsentPackages.remove(((ForgetMessage) message).mid);
} else if (message instanceof ConfirmMessage) {
if (isEnableLog) {
Log.d(TAG, "Confirming message #" + ((ConfirmMessage) message).mid);
}
confirm.add(((ConfirmMessage) message).mid);
if (confirm.size() >= ACK_THRESHOLD) {
if (askCancellable != null) {
askCancellable.cancel();
askCancellable = null;
}
askCancellable = schedule(new ForceAck(), 0);
} else if (confirm.size() == 1) {
if (askCancellable != null) {
askCancellable.cancel();
askCancellable = null;
}
askCancellable = schedule(new ForceAck(), ACK_DELAY);
}
} else if (message instanceof ForceAck) {
if (confirm.size() == 0) {
return;
}
MessageAck messageAck = buildAck();
doSend(new ProtoMessage(MTUids.nextId(), messageAck.toByteArray()));
} else if (message instanceof NewSession) {
NewSession newSession = (NewSession) message;
Log.w(TAG, "Received NewSessionCreated");
// Clearing pending acks because of session die
pendingConfirm.clear();
confirm.clear();
// Resending all required messages
ArrayList<ProtoMessage> toSend = new ArrayList<>();
for (ProtoMessage unsentPackage : unsentPackages.values()) {
if (unsentPackage.getMessageId() < newSession.getMessageId()) {
if (isEnableLog) {
Log.d(TAG, "ReSending #" + unsentPackage.getMessageId());
}
toSend.add(unsentPackage);
}
}
doSend(toSend);
} else if (message instanceof ReadPackageFromConnection) {
// Clearing pending confirmation
if (pendingConfirm.size() > 0) {
pendingConfirm.clear();
}
}
}
Aggregations