Search in sources :

Example 6 with ByteArray

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

the class MessageDispatcherSpeedTest method sendMessages.

void sendMessages(int num) throws Exception {
    long start, stop;
    int show = num / 10;
    if (show <= 0)
        show = 1;
    start = System.currentTimeMillis();
    RequestOptions opts = new RequestOptions(ResponseMode.GET_ALL, TIMEOUT).flags(Message.Flag.DONT_BUNDLE, Message.Flag.NO_FC);
    byte[] data = "bla".getBytes();
    ByteArray buf = new ByteArray(data, 0, data.length);
    System.out.println("-- sending " + num + " messages");
    for (int i = 1; i <= num; i++) {
        disp.castMessage(null, new BytesMessage(null, buf), opts);
        if (i % show == 0)
            System.out.println("-- sent " + i);
    }
    stop = System.currentTimeMillis();
    printStats(stop - start, num);
}
Also used : RequestOptions(org.jgroups.blocks.RequestOptions) ByteArray(org.jgroups.util.ByteArray)

Example 7 with ByteArray

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

the class FRAG 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:
 * <p/>
 * [2344,3,0]{dst,src,buf1},
 * [2344,3,1]{dst,src,buf2} and
 * [2344,3,2]{dst,src,buf3}
 * </pre>
 */
private void fragment(Message msg) {
    Address dest = msg.getDest(), src = msg.getSrc();
    // used as seqnos
    long frag_id = curr_id.getAndIncrement();
    int num_frags;
    try {
        // write message into a byte buffer and fragment it
        ByteArray tmp = Util.messageToBuffer(msg);
        byte[] buffer = tmp.getArray();
        byte[][] fragments = Util.fragmentBuffer(buffer, frag_size, tmp.getLength());
        num_frags = fragments.length;
        num_frags_sent.add(num_frags);
        if (log.isTraceEnabled()) {
            StringBuilder sb = new StringBuilder();
            sb.append("fragmenting packet to ").append(dest != null ? dest.toString() : "<all members>").append(" (size=").append(buffer.length).append(") into ").append(num_frags).append(" fragment(s) [frag_size=").append(frag_size).append(']');
            log.trace(sb.toString());
        }
        for (int i = 0; i < num_frags; i++) {
            Message frag_msg = new BytesMessage(dest, fragments[i]).setSrc(src).setFlag(msg.getFlags(true), true).putHeader(this.id, new FragHeader(frag_id, i, num_frags));
            down_prot.down(frag_msg);
        }
    } catch (Exception e) {
        log.error(Util.getMessage("ExceptionOccurredTryingToFragmentMessage"), e);
    }
}
Also used : ByteArray(org.jgroups.util.ByteArray)

Example 8 with ByteArray

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

the class COMPRESS method down.

/**
 * We compress the payload if it is larger than {@code min_size}. In this case we add a header containing
 * the original size before compression. Otherwise we add no header.<p>
 * Note that we compress either the entire buffer (if offset/length are not used), or a subset (if offset/length
 * are used)
 */
public Object down(Message msg) {
    // takes offset/length (if set) into account
    int length = msg.getLength();
    if (length >= min_size) {
        boolean serialize = !msg.hasArray();
        ByteArray tmp = null;
        byte[] payload = serialize ? (tmp = messageToByteArray(msg)).getArray() : msg.getArray();
        int offset = serialize ? tmp.getOffset() : msg.getOffset();
        length = serialize ? tmp.getLength() : msg.getLength();
        byte[] compressed_payload = new byte[length];
        Deflater deflater = null;
        try {
            deflater = deflater_pool.take();
            deflater.reset();
            deflater.setInput(payload, offset, length);
            deflater.finish();
            deflater.deflate(compressed_payload);
            int compressed_size = deflater.getTotalOut();
            if (compressed_size < length) {
                // JGRP-1000
                Message copy = null;
                if (serialize)
                    copy = new BytesMessage(msg.getDest());
                else
                    copy = msg.copy(false, true);
                copy.setArray(compressed_payload, 0, compressed_size).putHeader(this.id, new CompressHeader(length).needsDeserialization(serialize));
                if (log.isTraceEnabled())
                    log.trace("compressed payload from %d bytes to %d bytes", length, compressed_size);
                num_compressions.increment();
                return down_prot.down(copy);
            } else {
                if (log.isTraceEnabled())
                    log.trace("skipping compression since the compressed message (%d) is not " + "smaller than the original (%d)", compressed_size, length);
            }
        } catch (InterruptedException e) {
            // set interrupt flag again
            Thread.currentThread().interrupt();
            throw new RuntimeException(e);
        } finally {
            if (deflater != null)
                deflater_pool.offer(deflater);
        }
    }
    return down_prot.down(msg);
}
Also used : Deflater(java.util.zip.Deflater) ByteArray(org.jgroups.util.ByteArray)

Example 9 with ByteArray

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

the class NioMessage method setObject.

/**
 * Takes an object and uses Java serialization to generate the byte[] buffer which is set in the
 * message. Parameter 'obj' has to be serializable (e.g. implementing Serializable,
 * Externalizable or Streamable, or be a basic type (e.g. Integer, Short etc)).
 */
public NioMessage setObject(Object obj) {
    clearFlag(Flag.SERIALIZED);
    if (obj == null) {
        buf = null;
        return this;
    }
    if (obj instanceof byte[])
        return setArray((byte[]) obj, 0, ((byte[]) obj).length);
    if (obj instanceof ByteArray)
        return setArray((ByteArray) obj);
    if (obj instanceof ByteBuffer)
        return setBuf((ByteBuffer) obj);
    try {
        ByteArray tmp = Util.objectToBuffer(obj);
        setFlag(Flag.SERIALIZED);
        return setArray(tmp);
    } catch (Exception ex) {
        throw new IllegalArgumentException(ex);
    }
}
Also used : ByteArray(org.jgroups.util.ByteArray) ByteBuffer(java.nio.ByteBuffer) IOException(java.io.IOException)

Example 10 with ByteArray

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

the class BytesMessage method setObject.

/**
 * Takes an object and uses Java serialization to generate the byte array which is set in the
 * message. Parameter 'obj' has to be serializable (e.g. implementing Serializable,
 * Externalizable or Streamable, or be a basic type (e.g. Integer, Short etc)).
 */
public BytesMessage setObject(Object obj) {
    clearFlag(Flag.SERIALIZED);
    if (obj == null) {
        array = null;
        offset = length = 0;
        return this;
    }
    if (obj instanceof byte[])
        return setArray((byte[]) obj, 0, ((byte[]) obj).length);
    if (obj instanceof ByteArray)
        return setArray((ByteArray) obj);
    if (obj instanceof ByteBuffer) {
        ByteBuffer bb = (ByteBuffer) obj;
        if (bb.isDirect())
            return (BytesMessage) setArray(Util.bufferToArray(bb));
        else
            return setArray(bb.array(), bb.arrayOffset() + bb.position(), bb.remaining());
    }
    try {
        ByteArray tmp = Util.objectToBuffer(obj);
        setFlag(Flag.SERIALIZED);
        return setArray(tmp);
    } catch (Exception ex) {
        throw new IllegalArgumentException(ex);
    }
}
Also used : ByteArray(org.jgroups.util.ByteArray) ByteBuffer(java.nio.ByteBuffer) IOException(java.io.IOException)

Aggregations

ByteArray (org.jgroups.util.ByteArray)15 IOException (java.io.IOException)2 ByteBuffer (java.nio.ByteBuffer)2 RequestOptions (org.jgroups.blocks.RequestOptions)2 GMS (org.jgroups.protocols.pbcast.GMS)2 DataInput (java.io.DataInput)1 LinkedHashSet (java.util.LinkedHashSet)1 Deflater (java.util.zip.Deflater)1 BytesMessage (org.jgroups.BytesMessage)1 Message (org.jgroups.Message)1 MessageDispatcher (org.jgroups.blocks.MessageDispatcher)1 PingData (org.jgroups.protocols.PingData)1 PingHeader (org.jgroups.protocols.PingHeader)1 NAKACK2 (org.jgroups.protocols.pbcast.NAKACK2)1 STABLE (org.jgroups.protocols.pbcast.STABLE)1 IpAddress (org.jgroups.stack.IpAddress)1 ByteArrayDataInputStream (org.jgroups.util.ByteArrayDataInputStream)1