use of org.jgroups.FragmentedMessage in project JGroups by belaban.
the class FRAG4 method assembleMessage.
@Override
protected Message assembleMessage(Message[] fragments, boolean needs_deserialization, FragHeader hdr) throws Exception {
if (fragments[0] instanceof FragmentedMessage) {
if (Objects.equals(local_addr, fragments[0].getSrc()))
return ((FragmentedMessage) fragments[0]).getOriginalMessage();
InputStream seq = new SequenceInputStream(Util.enumerate(fragments, 0, fragments.length, m -> new ByteArrayDataInputStream(m.getArray(), m.getOffset(), m.getLength())));
DataInput in = new DataInputStream(seq);
Message retval = msg_factory.create(hdr.getOriginalType());
retval.readFrom(in);
return retval;
}
int combined_length = 0, index = 0;
for (Message fragment : fragments) combined_length += fragment.getLength();
byte[] combined_buffer = new byte[combined_length];
// doesn't copy the payload, but copies the headers
Message retval = fragments[0].copy(false, true);
for (int i = 0; i < fragments.length; i++) {
Message fragment = fragments[i];
// help garbage collection a bit
fragments[i] = null;
byte[] tmp = fragment.getArray();
int length = fragment.getLength(), offset = fragment.getOffset();
System.arraycopy(tmp, offset, combined_buffer, index, length);
index += length;
}
return retval.setArray(combined_buffer, 0, combined_buffer.length);
}
use of org.jgroups.FragmentedMessage in project JGroups by belaban.
the class FRAG4 method fragment.
protected void fragment(Message msg) {
try {
if (msg.getSrc() == null && local_addr != null)
msg.setSrc(local_addr);
int offset = msg.hasArray() ? msg.getOffset() : 0, length = msg.hasArray() ? msg.getLength() : msg.size();
final List<Range> fragments = Util.computeFragOffsets(offset, length, frag_size);
int num_frags = fragments.size();
// used as a seqno
final long frag_id = getNextId();
num_frags_sent.add(num_frags);
if (log.isTraceEnabled()) {
Address dest = msg.getDest();
log.trace("%s: fragmenting message to %s (size=%d) into %d fragment(s) [frag_size=%d]", local_addr, dest != null ? dest : "<all>", msg.getLength(), num_frags, frag_size);
}
for (int i = 0; i < num_frags; i++) {
Range r = fragments.get(i);
Message frag_msg = msg.hasArray() ? msg.copy(false, i == 0).setArray(msg.getArray(), (int) r.low, (int) r.high).putHeader(this.id, new FragHeader(frag_id, i, num_frags)) : new FragmentedMessage(msg, (int) r.low, (int) r.high).setDest(msg.getDest()).setSrc(msg.getSrc()).setFlag(msg.getFlags(true), true).putHeader(this.id, new FragHeader(frag_id, i, num_frags).setOriginalType(msg.getType()));
down_prot.down(frag_msg);
}
} catch (Exception e) {
log.error("%s: fragmentation failure: %s", local_addr, e);
}
}
Aggregations