Search in sources :

Example 1 with FragmentedMessage

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);
}
Also used : DataInputStream(java.io.DataInputStream) Util(org.jgroups.util.Util) FragmentedMessage(org.jgroups.FragmentedMessage) SequenceInputStream(java.io.SequenceInputStream) Range(org.jgroups.util.Range) Objects(java.util.Objects) BytesMessage(org.jgroups.BytesMessage) List(java.util.List) Message(org.jgroups.Message) ByteArrayDataInputStream(org.jgroups.util.ByteArrayDataInputStream) DataInput(java.io.DataInput) Address(org.jgroups.Address) InputStream(java.io.InputStream) DataInput(java.io.DataInput) FragmentedMessage(org.jgroups.FragmentedMessage) SequenceInputStream(java.io.SequenceInputStream) FragmentedMessage(org.jgroups.FragmentedMessage) BytesMessage(org.jgroups.BytesMessage) Message(org.jgroups.Message) DataInputStream(java.io.DataInputStream) SequenceInputStream(java.io.SequenceInputStream) ByteArrayDataInputStream(org.jgroups.util.ByteArrayDataInputStream) InputStream(java.io.InputStream) ByteArrayDataInputStream(org.jgroups.util.ByteArrayDataInputStream) DataInputStream(java.io.DataInputStream) ByteArrayDataInputStream(org.jgroups.util.ByteArrayDataInputStream)

Example 2 with FragmentedMessage

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);
    }
}
Also used : FragmentedMessage(org.jgroups.FragmentedMessage) Address(org.jgroups.Address) FragmentedMessage(org.jgroups.FragmentedMessage) BytesMessage(org.jgroups.BytesMessage) Message(org.jgroups.Message) Range(org.jgroups.util.Range)

Aggregations

Address (org.jgroups.Address)2 BytesMessage (org.jgroups.BytesMessage)2 FragmentedMessage (org.jgroups.FragmentedMessage)2 Message (org.jgroups.Message)2 Range (org.jgroups.util.Range)2 DataInput (java.io.DataInput)1 DataInputStream (java.io.DataInputStream)1 InputStream (java.io.InputStream)1 SequenceInputStream (java.io.SequenceInputStream)1 List (java.util.List)1 Objects (java.util.Objects)1 ByteArrayDataInputStream (org.jgroups.util.ByteArrayDataInputStream)1 Util (org.jgroups.util.Util)1