use of org.jboss.netty.handler.queue.BlockingReadHandler 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();
}
}
use of org.jboss.netty.handler.queue.BlockingReadHandler in project neo4j by neo4j.
the class Client method getPipeline.
@Override
public ChannelPipeline getPipeline() throws Exception {
ChannelPipeline pipeline = Channels.pipeline();
pipeline.addLast(MONITORING_CHANNEL_HANDLER_NAME, new MonitorChannelHandler(byteCounterMonitor));
addLengthFieldPipes(pipeline, frameLength);
BlockingReadHandler<ChannelBuffer> reader = new BlockingReadHandler<>(new ArrayBlockingQueue<>(100, false));
pipeline.addLast(BLOCKING_CHANNEL_HANDLER_NAME, reader);
return pipeline;
}
use of org.jboss.netty.handler.queue.BlockingReadHandler in project graphdb by neo4j-attic.
the class Client method getPipeline.
public ChannelPipeline getPipeline() throws Exception {
ChannelPipeline pipeline = Channels.pipeline();
addLengthFieldPipes(pipeline);
BlockingReadHandler<ChannelBuffer> reader = new BlockingReadHandler<ChannelBuffer>(new ArrayBlockingQueue<ChannelEvent>(3, false));
pipeline.addLast("blockingHandler", reader);
return pipeline;
}
use of org.jboss.netty.handler.queue.BlockingReadHandler in project neo4j by neo4j.
the class Protocol method deserializeResponse.
public <PAYLOAD> Response<PAYLOAD> deserializeResponse(BlockingReadHandler<ChannelBuffer> reader, ByteBuffer input, long timeout, Deserializer<PAYLOAD> payloadDeserializer, ResourceReleaser channelReleaser, final LogEntryReader<ReadableClosablePositionAwareChannel> entryReader) throws IOException {
final DechunkingChannelBuffer dechunkingBuffer = new DechunkingChannelBuffer(reader, timeout, internalProtocolVersion, applicationProtocolVersion);
PAYLOAD response = payloadDeserializer.read(dechunkingBuffer, input);
StoreId storeId = readStoreId(dechunkingBuffer, input);
// Response type is what previously was a byte saying how many data sources there were in the
// coming transaction stream response. For backwards compatibility we keep it as a byte and we introduce
// the transaction obligation response type as -1
byte responseType = dechunkingBuffer.readByte();
if (responseType == TransactionObligationResponse.RESPONSE_TYPE) {
// It is a transaction obligation response
long obligationTxId = dechunkingBuffer.readLong();
return new TransactionObligationResponse<>(response, storeId, obligationTxId, channelReleaser);
}
// It's a transaction stream in this response
TransactionStream transactions = visitor -> {
NetworkReadableClosableChannel channel = new NetworkReadableClosableChannel(dechunkingBuffer);
try (PhysicalTransactionCursor<ReadableClosablePositionAwareChannel> cursor = new PhysicalTransactionCursor<>(channel, entryReader)) {
while (cursor.next() && !visitor.visit(cursor.get())) {
}
}
};
return new TransactionStreamResponse<>(response, storeId, transactions, channelReleaser);
}
Aggregations