private void handleShortMessage(final DataInputStream dis, final int maxLength) throws IOException,
TrSerializableException {
final int messageId = dis.readInt();
{
// Construct and send an ack message
final ByteArraySegmentBuilder ackMessage = ByteArraySegment.builder();
PrimitiveMessageType.ACK.write(ackMessage);
ackMessage.writeInt(messageId);
logger.debug("Sending ACK");
iface.sendTo(remoteAddress, encryptOutbound(ackMessage.build()),
TrNetworkInterface.CONNECTION_MAINTAINANCE_PRIORITY);
}
final ShortMessageType type = ShortMessageType.forBytes.get(dis.readByte());
if (recentlyReceivedShortMessages.contains(messageId))
// Seen this message before, disregard
return;
recentlyReceivedShortMessages.add(messageId);
switch (type) {
case SIMPLE:
listener.received(iface, remoteAddress, ByteArraySegment.from(dis, maxLength));
break;
case LONG_PART:
final LongPart lh = TrSerializer.deserializeFrom(LongPart.class, dis);
// logger.debug("Received " + lh);
PendingLongMessage plm = pendingReceivedLongMessages.get(lh.longMessageId);
if (plm == null) {
plm = new PendingLongMessage(lh.totalParts);
pendingReceivedLongMessages.put(lh.longMessageId, plm);
}
plm.parts[lh.partNumber] = lh.data;
if (plm.isComplete()) {
// logger.debug("LongPart " + lh.longMessageId +
// " received in its entirity");
pendingReceivedLongMessages.remove(lh.longMessageId);
final ByteArraySegmentBuilder longMessage = ByteArraySegment.builder();
for (final ByteArraySegment bas : plm.parts) {
longMessage.write(bas);
}
listener.received(iface, remoteAddress, longMessage.build());
}
}
}