Search in sources :

Example 16 with Buffer

use of org.fusesource.hawtbuf.Buffer in project fabric8 by jboss-fuse.

the class ClientInvokerImpl method request.

protected Object request(ProxyInvocationHandler handler, final String address, final UTF8Buffer service, final ClassLoader classLoader, final Method method, final Object[] args) throws Exception {
    if (!running.get()) {
        throw new IllegalStateException("DOSGi Client stopped");
    }
    final long correlation = correlationGenerator.incrementAndGet();
    // Encode the request before we try to pass it onto
    // IO layers so that #1 we can report encoding error back to the caller
    // and #2 reduce CPU load done in the execution queue since it's
    // serially executed.
    DataByteArrayOutputStream baos = new DataByteArrayOutputStream((int) (handler.lastRequestSize * 1.10));
    // we don't know the size yet...
    baos.writeInt(0);
    baos.writeVarLong(correlation);
    writeBuffer(baos, service);
    MethodData methodData = getMethodData(method);
    writeBuffer(baos, methodData.signature);
    final ResponseFuture future = methodData.invocationStrategy.request(methodData.serializationStrategy, classLoader, method, args, baos);
    // toBuffer() is better than toByteArray() since it avoids an
    // array copy.
    final Buffer command = baos.toBuffer();
    // Update the field size.
    BufferEditor editor = command.buffer().bigEndianEditor();
    editor.writeInt(command.length);
    handler.lastRequestSize = command.length;
    queue().execute(new Runnable() {

        public void run() {
            try {
                TransportPool pool = transports.get(address);
                if (pool == null) {
                    pool = new InvokerTransportPool(address, queue());
                    transports.put(address, pool);
                    pool.start();
                }
                requests.put(correlation, future);
                pool.offer(command, correlation);
            } catch (Exception e) {
                LOGGER.info("Error while sending request", e);
                future.fail(e);
            }
        }
    });
    // TODO: make that configurable, that's only for tests
    return future.get(timeout, TimeUnit.MILLISECONDS);
}
Also used : Buffer(org.fusesource.hawtbuf.Buffer) UTF8Buffer(org.fusesource.hawtbuf.UTF8Buffer) DataByteArrayOutputStream(org.fusesource.hawtbuf.DataByteArrayOutputStream) BufferEditor(org.fusesource.hawtbuf.BufferEditor) IOException(java.io.IOException)

Example 17 with Buffer

use of org.fusesource.hawtbuf.Buffer in project fabric8 by jboss-fuse.

the class LengthPrefixedCodec method write.

public BufferState write(Object value) throws IOException {
    if (full()) {
        return BufferState.FULL;
    } else {
        boolean wasEmpty = empty();
        Buffer buffer = (Buffer) value;
        next_write_size += buffer.length;
        next_write_buffers.add(buffer.toByteBuffer());
        return wasEmpty ? BufferState.WAS_EMPTY : BufferState.NOT_EMPTY;
    }
}
Also used : Buffer(org.fusesource.hawtbuf.Buffer) ByteBuffer(java.nio.ByteBuffer)

Example 18 with Buffer

use of org.fusesource.hawtbuf.Buffer in project fabric8 by jboss-fuse.

the class LengthPrefixedCodec method read.

public Object read() throws IOException {
    while (true) {
        if (read_buffer.remaining() != 0) {
            // keep reading from the channel until we fill the read buffer..
            int count = read_channel.read(read_buffer);
            if (count == -1) {
                throw new EOFException("Peer disconnected");
            } else if (count == 0) {
                return null;
            }
            read_counter += count;
        } else {
            // read buffer is full.. interpret it..
            read_buffer.flip();
            if (read_buffer.capacity() == 4) {
                // Finding out the
                int size = read_buffer.getInt(0);
                if (size < 4) {
                    throw new ProtocolException("Expecting a size greater than 3");
                }
                if (size == 4) {
                    // weird.. empty frame.. guess it could happen.
                    Buffer rc = new Buffer(read_buffer);
                    read_buffer = ByteBuffer.allocate(4);
                    return rc;
                } else {
                    // Resize to the right size.. this resumes the reads..
                    ByteBuffer next = ByteBuffer.allocate(size);
                    next.putInt(size);
                    read_buffer = next;
                }
            } else {
                // finish loading the rest of the buffer..
                Buffer rc = new Buffer(read_buffer);
                read_buffer = ByteBuffer.allocate(4);
                return rc;
            }
        }
    }
}
Also used : Buffer(org.fusesource.hawtbuf.Buffer) ByteBuffer(java.nio.ByteBuffer) ProtocolException(java.net.ProtocolException) EOFException(java.io.EOFException) ByteBuffer(java.nio.ByteBuffer)

Example 19 with Buffer

use of org.fusesource.hawtbuf.Buffer in project fabric8 by jboss-fuse.

the class ActiveMQMapMessage method loadContent.

/**
 * Builds the message body from data
 *
 * @throws OpenwireException
 */
private void loadContent() throws OpenwireException {
    try {
        if (getContent() != null && map.isEmpty()) {
            Buffer content = getContent();
            InputStream is = new ByteArrayInputStream(content);
            if (isCompressed()) {
                is = new InflaterInputStream(is);
            }
            DataInputStream dataIn = new DataInputStream(is);
            map = MarshallingSupport.unmarshalPrimitiveMap(dataIn);
            dataIn.close();
        }
    } catch (IOException e) {
        throw new OpenwireException(e);
    }
}
Also used : Buffer(org.fusesource.hawtbuf.Buffer) OpenwireException(io.fabric8.gateway.handlers.detecting.protocol.openwire.support.OpenwireException) ByteArrayInputStream(org.fusesource.hawtbuf.ByteArrayInputStream) ByteArrayInputStream(org.fusesource.hawtbuf.ByteArrayInputStream) InflaterInputStream(java.util.zip.InflaterInputStream) InflaterInputStream(java.util.zip.InflaterInputStream)

Example 20 with Buffer

use of org.fusesource.hawtbuf.Buffer in project fabric8 by jboss-fuse.

the class ActiveMQObjectMessage method getObject.

/**
 * Gets the serializable object containing this message's data. The default
 * value is null.
 *
 * @return the serializable object containing this message's data
 * @throws OpenwireException
 */
public Serializable getObject() throws OpenwireException {
    if (object == null && getContent() != null) {
        // try {
        Buffer content = getContent();
        InputStream is = new ByteArrayInputStream(content);
        if (isCompressed()) {
            is = new InflaterInputStream(is);
        }
        DataInputStream dataIn = new DataInputStream(is);
        throw new UnsupportedOperationException();
    // ClassLoadingAwareObjectInputStream objIn = new ClassLoadingAwareObjectInputStream(dataIn);
    // try {
    // object = (Serializable)objIn.readObject();
    // } catch (ClassNotFoundException ce) {
    // throw new OpenwireException("Failed to build body from content. Serializable class not available to broker. Reason: " + ce, ce);
    // } finally {
    // dataIn.close();
    // }
    // } catch (IOException e) {
    // throw new OpenwireException("Failed to build body from bytes. Reason: " + e, e);
    // }
    }
    return this.object;
}
Also used : Buffer(org.fusesource.hawtbuf.Buffer) ByteArrayInputStream(org.fusesource.hawtbuf.ByteArrayInputStream) DataInputStream(java.io.DataInputStream) InflaterInputStream(java.util.zip.InflaterInputStream) ByteArrayInputStream(org.fusesource.hawtbuf.ByteArrayInputStream) InputStream(java.io.InputStream) InflaterInputStream(java.util.zip.InflaterInputStream) DataInputStream(java.io.DataInputStream)

Aggregations

Buffer (org.fusesource.hawtbuf.Buffer)45 IOException (java.io.IOException)17 ByteBuffer (java.nio.ByteBuffer)10 Exchange (org.apache.camel.Exchange)9 Test (org.junit.Test)9 Transaction (org.fusesource.hawtdb.api.Transaction)8 UTF8Buffer (org.fusesource.hawtbuf.UTF8Buffer)6 SortedIndex (org.fusesource.hawtdb.api.SortedIndex)6 InflaterInputStream (java.util.zip.InflaterInputStream)5 ByteArrayInputStream (org.fusesource.hawtbuf.ByteArrayInputStream)5 BufferState (io.fabric8.dosgi.io.ProtocolCodec.BufferState)4 OpenwireException (io.fabric8.gateway.handlers.detecting.protocol.openwire.support.OpenwireException)3 DataStructure (io.fabric8.gateway.handlers.detecting.protocol.openwire.command.DataStructure)2 RandomAccessFile (java.io.RandomAccessFile)2 DeflaterOutputStream (java.util.zip.DeflaterOutputStream)2 BufferEditor (org.fusesource.hawtbuf.BufferEditor)2 ByteArrayOutputStream (org.fusesource.hawtbuf.ByteArrayOutputStream)2 DataByteArrayOutputStream (org.fusesource.hawtbuf.DataByteArrayOutputStream)2 Index (org.fusesource.hawtdb.api.Index)2 ObjectSerializationStrategy (io.fabric8.dosgi.api.ObjectSerializationStrategy)1