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);
}
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;
}
}
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;
}
}
}
}
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);
}
}
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;
}
Aggregations