Search in sources :

Example 1 with Range

use of org.jgroups.util.Range in project JGroups by belaban.

the class MessageTest method testComputeFragOffsetsWithOffsets.

public void testComputeFragOffsetsWithOffsets() {
    Range r;
    // byte[] buf={'p', 'a', 'd', 0,1,2,3,4,5,6,7,8,9, 'p', 'a', 'd', 'd', 'i', 'e'};
    java.util.List<Range> retval = Util.computeFragOffsets(3, 10, 4);
    System.out.println("list is " + retval);
    Assert.assertEquals(3, retval.size());
    r = retval.get(0);
    Assert.assertEquals(3, r.low);
    Assert.assertEquals(4, r.high);
    r = retval.get(1);
    Assert.assertEquals(7, r.low);
    Assert.assertEquals(4, r.high);
    r = retval.get(2);
    Assert.assertEquals(11, r.low);
    Assert.assertEquals(2, r.high);
}
Also used : Range(org.jgroups.util.Range)

Example 2 with Range

use of org.jgroups.util.Range in project JGroups by belaban.

the class MessageTest method testComputeFragOffsets4.

public void testComputeFragOffsets4() {
    byte[] buf = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    java.util.List<Range> retval = Util.computeFragOffsets(buf, 5);
    System.out.println("list is " + retval);
    Assert.assertEquals(2, retval.size());
    Range r = retval.get(0);
    Assert.assertEquals(0, r.low);
    Assert.assertEquals(5, r.high);
    r = retval.get(1);
    Assert.assertEquals(5, r.low);
    Assert.assertEquals(5, r.high);
}
Also used : Range(org.jgroups.util.Range)

Example 3 with Range

use of org.jgroups.util.Range in project JGroups by belaban.

the class FRAG2 method fragment.

/**
 * Send all fragments as separate messages (with same ID !).
 *     Example:
 *     <pre>
 *     Given the generated ID is 2344, number of fragments=3, message {dst,src,buf}
 *     would be fragmented into:
 *
 *     [2344,3,0]{dst,src,buf1},
 *     [2344,3,1]{dst,src,buf2} and
 *     [2344,3,2]{dst,src,buf3}
 *     </pre>
 */
protected void fragment(Message msg) {
    try {
        byte[] buffer = msg.getRawBuffer();
        final List<Range> fragments = Util.computeFragOffsets(msg.getOffset(), msg.getLength(), frag_size);
        int num_frags = fragments.size();
        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);
        }
        // used as a seqno
        long frag_id = getNextId();
        for (int i = 0; i < num_frags; i++) {
            Range r = fragments.get(i);
            // don't copy the buffer, only src, dest and headers. Only copy the headers one time !
            Message frag_msg = msg.copy(false, i == 0);
            frag_msg.setBuffer(buffer, (int) r.low, (int) r.high);
            FragHeader hdr = new FragHeader(frag_id, i, num_frags);
            frag_msg.putHeader(this.id, hdr);
            down_prot.down(frag_msg);
        }
    } catch (Exception e) {
        log.error("%s: fragmentation failure: %s", local_addr, e);
    }
}
Also used : Address(org.jgroups.Address) Message(org.jgroups.Message) Range(org.jgroups.util.Range)

Example 4 with Range

use of org.jgroups.util.Range 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)

Example 5 with Range

use of org.jgroups.util.Range in project JGroups by belaban.

the class MessageTest method testComputeFragOffsets2.

public void testComputeFragOffsets2() {
    byte[] buf = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    java.util.List<Range> retval = Util.computeFragOffsets(buf, 10);
    System.out.println("list is " + retval);
    Assert.assertEquals(1, retval.size());
    Range r = retval.get(0);
    Assert.assertEquals(0, r.low);
    Assert.assertEquals(10, r.high);
}
Also used : Range(org.jgroups.util.Range)

Aggregations

Range (org.jgroups.util.Range)7 Address (org.jgroups.Address)2 Message (org.jgroups.Message)2 BytesMessage (org.jgroups.BytesMessage)1 FragmentedMessage (org.jgroups.FragmentedMessage)1