received.add(x, new AtomicBoolean(false));
final int pos = x;
try {
final LongPart lp = new LongPart(longMessageId, x, segments.size(), segments.get(x));
final ByteArraySegmentBuilder builder = ByteArraySegment.builder();
PrimitiveMessageType.SHORT.write(builder);
final int messageId = TrUtils.rand.nextInt();
builder.writeInt(messageId);
ShortMessageType.LONG_PART.write(builder);
TrSerializer.serializeTo(lp, builder);
final Resender resender = new Resender(messageId, TrConstants.UDP_SHORT_MESSAGE_RETRY_ATTEMPTS, new TrSentReceivedListener() {
boolean failureReported = false;
public void failure() {
if (!failureReported) {
failureReported = true;
sentListener.failure();
}
}
public void received() {
received.get(pos).set(true);
// logger.debug("Longpart " + pos +
// " receive confirmation: " +
// Arrays.toString(received));
for (final AtomicBoolean r : received) {
if (!r.get())
return;
}
sentListener.received();
}
public void sent() {
sent.get(pos).set(true);
for (final AtomicBoolean s : sent) {
if (!s.get())
return;
}
sentListener.sent();
}
}, encryptOutbound(builder.build()), this, priority);
resenders.put(messageId, resender);
resender.run();
} catch (final TrSerializableException e) {
throw new RuntimeException(e);
}