Search in sources :

Example 86 with ChannelBuffer

use of org.jboss.netty.buffer.ChannelBuffer in project graphdb by neo4j-attic.

the class Client method sendRequest.

protected <R> Response<R> sendRequest(RequestType<M> type, SlaveContext context, Serializer serializer, Deserializer<R> deserializer) {
    Triplet<Channel, ChannelBuffer, ByteBuffer> channelContext = null;
    try {
        // Send 'em over the wire
        channelContext = getChannel();
        Channel channel = channelContext.first();
        channelContext.second().clear();
        ChunkingChannelBuffer chunkingBuffer = new ChunkingChannelBuffer(channelContext.second(), channel, Protocol.MAX_FRAME_LENGTH);
        chunkingBuffer.writeByte(type.id());
        writeContext(type, context, chunkingBuffer);
        serializer.write(chunkingBuffer, channelContext.third());
        chunkingBuffer.done();
        // Read the response
        @SuppressWarnings("unchecked") BlockingReadHandler<ChannelBuffer> reader = (BlockingReadHandler<ChannelBuffer>) channel.getPipeline().get("blockingHandler");
        final Triplet<Channel, ChannelBuffer, ByteBuffer> finalChannelContext = channelContext;
        DechunkingChannelBuffer dechunkingBuffer = new DechunkingChannelBuffer(reader, DEFAULT_READ_RESPONSE_TIMEOUT_SECONDS) {

            @Override
            protected ChannelBuffer readNext() {
                ChannelBuffer result = super.readNext();
                if (result == null) {
                    channelPool.dispose(finalChannelContext);
                    throw new ComException("Channel has been closed");
                }
                return result;
            }
        };
        R response = deserializer.read(dechunkingBuffer, channelContext.third());
        StoreId storeId = readStoreId(dechunkingBuffer, channelContext.third());
        if (shouldCheckStoreId(type)) {
            assertCorrectStoreId(storeId);
        }
        TransactionStream txStreams = readTransactionStreams(dechunkingBuffer);
        return new Response<R>(response, storeId, txStreams);
    } catch (ClosedChannelException e) {
        channelPool.dispose(channelContext);
        throw new ComException(e);
    } catch (IOException e) {
        throw new ComException(e);
    } catch (InterruptedException e) {
        throw new ComException(e);
    } catch (Exception e) {
        throw new ComException(e);
    } finally {
        releaseChannel();
    }
}
Also used : BlockingReadHandler(org.jboss.netty.handler.queue.BlockingReadHandler) ClosedChannelException(java.nio.channels.ClosedChannelException) Channel(org.jboss.netty.channel.Channel) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) ClosedChannelException(java.nio.channels.ClosedChannelException) IOException(java.io.IOException) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer) StoreId(org.neo4j.kernel.impl.nioneo.store.StoreId)

Example 87 with ChannelBuffer

use of org.jboss.netty.buffer.ChannelBuffer in project graphdb by neo4j-attic.

the class DechunkingChannelBuffer method readNextChunk.

private void readNextChunk() {
    ChannelBuffer readBuffer = readNext();
    more = readBuffer.readByte() == ChunkingChannelBuffer.CONTINUATION_MORE;
    if (!more && buffer == null) {
        // Optimization: this is the first chunk and it'll be the only chunk
        // in this message.
        buffer = readBuffer;
    } else {
        buffer = buffer == null ? ChannelBuffers.dynamicBuffer() : buffer;
        discardReadBytes();
        buffer.writeBytes(readBuffer);
    }
}
Also used : ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Example 88 with ChannelBuffer

use of org.jboss.netty.buffer.ChannelBuffer in project neo4j by neo4j.

the class TestBlockLogBuffer method canWriteLargestAtomAfterFillingBuffer.

@Test
public void canWriteLargestAtomAfterFillingBuffer() throws Exception {
    byte[] bytes = new byte[300];
    ChannelBuffer wrappedBuffer = ChannelBuffers.wrappedBuffer(bytes);
    wrappedBuffer.resetWriterIndex();
    BlockLogBuffer buffer = new BlockLogBuffer(wrappedBuffer, new Monitors().newMonitor(ByteCounterMonitor.class));
    byte[] bytesValue = new byte[255];
    bytesValue[0] = 1;
    bytesValue[254] = -1;
    long longValue = 123456;
    buffer.put(bytesValue, bytesValue.length);
    buffer.putLong(longValue);
    buffer.close();
    ByteBuffer verificationBuffer = ByteBuffer.wrap(bytes);
    assertEquals((byte) 0, verificationBuffer.get());
    byte[] actualBytes = new byte[bytesValue.length];
    verificationBuffer.get(actualBytes);
    assertThat(actualBytes, new ArrayMatches<byte[]>(bytesValue));
    assertEquals((byte) 8, verificationBuffer.get());
    assertEquals(longValue, verificationBuffer.getLong());
}
Also used : ByteCounterMonitor(org.neo4j.kernel.monitoring.ByteCounterMonitor) BlockLogBuffer(org.neo4j.com.BlockLogBuffer) Monitors(org.neo4j.kernel.monitoring.Monitors) ByteBuffer(java.nio.ByteBuffer) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer) Test(org.junit.Test)

Example 89 with ChannelBuffer

use of org.jboss.netty.buffer.ChannelBuffer in project neo4j by neo4j.

the class TestBlockLogBuffer method canWriteReallyLargeByteArray.

@Test
public void canWriteReallyLargeByteArray() throws Exception {
    byte[] bytes = new byte[650];
    ChannelBuffer wrappedBuffer = ChannelBuffers.wrappedBuffer(bytes);
    wrappedBuffer.resetWriterIndex();
    BlockLogBuffer buffer = new BlockLogBuffer(wrappedBuffer, new Monitors().newMonitor(ByteCounterMonitor.class));
    byte[] bytesValue = new byte[600];
    bytesValue[1] = 1;
    bytesValue[99] = 2;
    bytesValue[199] = 3;
    bytesValue[299] = 4;
    bytesValue[399] = 5;
    bytesValue[499] = 6;
    bytesValue[599] = 7;
    buffer.put(bytesValue, bytesValue.length);
    buffer.close();
    byte[] actual;
    ByteBuffer verificationBuffer = ByteBuffer.wrap(bytes);
    assertEquals((byte) 0, verificationBuffer.get());
    actual = new byte[255];
    verificationBuffer.get(actual);
    assertThat(actual, new ArrayMatches<byte[]>(Arrays.copyOfRange(bytesValue, 0, 255)));
    assertEquals((byte) 0, verificationBuffer.get());
    actual = new byte[255];
    verificationBuffer.get(actual);
    assertThat(actual, new ArrayMatches<byte[]>(Arrays.copyOfRange(bytesValue, 255, 510)));
    assertEquals((byte) 90, verificationBuffer.get());
    actual = new byte[90];
    verificationBuffer.get(actual);
    assertThat(actual, new ArrayMatches<byte[]>(Arrays.copyOfRange(bytesValue, 510, 600)));
}
Also used : ByteCounterMonitor(org.neo4j.kernel.monitoring.ByteCounterMonitor) BlockLogBuffer(org.neo4j.com.BlockLogBuffer) Monitors(org.neo4j.kernel.monitoring.Monitors) ByteBuffer(java.nio.ByteBuffer) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer) Test(org.junit.Test)

Example 90 with ChannelBuffer

use of org.jboss.netty.buffer.ChannelBuffer in project neo4j by neo4j.

the class TestBlockLogBuffer method readSmallPortions.

@Test
public void readSmallPortions() throws IOException {
    byte[] bytes = new byte[255];
    ChannelBuffer wrappedBuffer = ChannelBuffers.wrappedBuffer(bytes);
    wrappedBuffer.resetWriterIndex();
    BlockLogBuffer buffer = new BlockLogBuffer(wrappedBuffer, new Monitors().newMonitor(ByteCounterMonitor.class));
    byte byteValue = 5;
    int intValue = 1234;
    long longValue = 574853;
    buffer.put(byteValue);
    buffer.putInt(intValue);
    buffer.putLong(longValue);
    buffer.close();
    ReadableByteChannel reader = new BlockLogReader(wrappedBuffer);
    ByteBuffer verificationBuffer = ByteBuffer.wrap(new byte[1]);
    reader.read(verificationBuffer);
    verificationBuffer.flip();
    assertEquals(byteValue, verificationBuffer.get());
    verificationBuffer = ByteBuffer.wrap(new byte[4]);
    reader.read(verificationBuffer);
    verificationBuffer.flip();
    assertEquals(intValue, verificationBuffer.getInt());
    verificationBuffer = ByteBuffer.wrap(new byte[8]);
    reader.read(verificationBuffer);
    verificationBuffer.flip();
    assertEquals(longValue, verificationBuffer.getLong());
}
Also used : ByteCounterMonitor(org.neo4j.kernel.monitoring.ByteCounterMonitor) ReadableByteChannel(java.nio.channels.ReadableByteChannel) BlockLogBuffer(org.neo4j.com.BlockLogBuffer) BlockLogReader(org.neo4j.com.BlockLogReader) Monitors(org.neo4j.kernel.monitoring.Monitors) ByteBuffer(java.nio.ByteBuffer) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer) Test(org.junit.Test)

Aggregations

ChannelBuffer (org.jboss.netty.buffer.ChannelBuffer)494 DefaultHttpResponse (org.jboss.netty.handler.codec.http.DefaultHttpResponse)70 Test (org.junit.Test)67 DeviceSession (org.traccar.DeviceSession)64 Position (org.traccar.model.Position)62 HttpResponse (org.jboss.netty.handler.codec.http.HttpResponse)61 Test (org.testng.annotations.Test)49 HttpChunk (org.jboss.netty.handler.codec.http.HttpChunk)48 DefaultHttpChunk (org.jboss.netty.handler.codec.http.DefaultHttpChunk)44 HttpChunkTrailer (org.jboss.netty.handler.codec.http.HttpChunkTrailer)37 DefaultHttpChunkTrailer (org.jboss.netty.handler.codec.http.DefaultHttpChunkTrailer)34 ChannelFuture (org.jboss.netty.channel.ChannelFuture)28 Checkpoint (com.linkedin.databus.core.Checkpoint)27 ByteBuffer (java.nio.ByteBuffer)27 RecoverablePduException (com.cloudhopper.smpp.type.RecoverablePduException)26 UnrecoverablePduException (com.cloudhopper.smpp.type.UnrecoverablePduException)26 DateBuilder (org.traccar.helper.DateBuilder)26 BootstrapDatabaseTooOldException (com.linkedin.databus2.core.container.request.BootstrapDatabaseTooOldException)25 IOException (java.io.IOException)23 ArrayList (java.util.ArrayList)23