use of org.jboss.netty.channel.Channel 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.channel.Channel in project graphdb by neo4j-attic.
the class Server method checkForDeadChannels.
private void checkForDeadChannels() {
synchronized (connectedSlaveChannels) {
Collection<Channel> channelsToRemove = new ArrayList<Channel>();
for (Map.Entry<Channel, SlaveContext> entry : connectedSlaveChannels.entrySet()) {
if (!channelIsOpen(entry.getKey())) {
msgLog.logMessage("Found dead channel " + entry.getKey() + ", " + entry.getValue());
finishOffConnection(entry.getKey(), entry.getValue());
msgLog.logMessage("Removed " + entry.getKey() + ", " + entry.getValue());
channelsToRemove.add(entry.getKey());
}
}
for (Channel channel : channelsToRemove) {
connectedSlaveChannels.remove(channel);
channelBuffers.remove(channel);
partialRequests.remove(channel);
}
}
}
use of org.jboss.netty.channel.Channel in project neo4j by neo4j.
the class NetworkReceiverTest method testMessageReceivedOriginFix.
@Test
public void testMessageReceivedOriginFix() throws Exception {
LogProvider logProvider = mock(LogProvider.class);
when(logProvider.getLog(NetworkReceiver.class)).thenReturn(mock(Log.class));
NetworkReceiver networkReceiver = new NetworkReceiver(mock(NetworkReceiver.Monitor.class), mock(NetworkReceiver.Configuration.class), logProvider);
// This defines where message is coming from
final InetSocketAddress inetSocketAddress = new InetSocketAddress("localhost", PORT);
final Channel channel = mock(Channel.class);
when(channel.getLocalAddress()).thenReturn(inetSocketAddress);
when(channel.getRemoteAddress()).thenReturn(inetSocketAddress);
ChannelHandlerContext ctx = mock(ChannelHandlerContext.class);
when(ctx.getChannel()).thenReturn(channel);
final Message message = Message.to(new MessageType() {
@Override
public String name() {
return "test";
}
}, new URI("cluster://anywhere"));
MessageEvent messageEvent = mock(MessageEvent.class);
when(messageEvent.getRemoteAddress()).thenReturn(inetSocketAddress);
when(messageEvent.getMessage()).thenReturn(message);
when(messageEvent.getChannel()).thenReturn(channel);
// the original FROM header should be ignored
message.setHeader(Message.FROM, "cluster://someplace:1234");
networkReceiver.new MessageReceiver().messageReceived(ctx, messageEvent);
assertEquals("FROM header should have been changed to visible ip address: " + message.getHeader(Message.FROM), "cluster://127.0.0.1:1234", message.getHeader(Message.FROM));
}
use of org.jboss.netty.channel.Channel in project neo4j by neo4j.
the class Server method start.
@Override
public void start() throws Throwable {
String className = getClass().getSimpleName();
ExecutorService bossExecutor = newCachedThreadPool(daemon("Boss-" + className));
ExecutorService workerExecutor = newCachedThreadPool(daemon("Worker-" + className));
bootstrap = new ServerBootstrap(new NioServerSocketChannelFactory(bossExecutor, workerExecutor, config.getMaxConcurrentTransactions()));
bootstrap.setPipelineFactory(this);
PortRangeSocketBinder portRangeSocketBinder = new PortRangeSocketBinder(bootstrap);
try {
Connection connection = portRangeSocketBinder.bindToFirstAvailablePortInRange(config.getServerAddress());
Channel channel = connection.getChannel();
socketAddress = connection.getSocketAddress();
channelGroup = new DefaultChannelGroup();
channelGroup.add(channel);
msgLog.info(className + " communication server started and bound to " + socketAddress);
} catch (Exception ex) {
msgLog.error("Failed to bind server to " + socketAddress, ex);
bootstrap.releaseExternalResources();
targetCallExecutor.shutdownNow();
unfinishedTransactionExecutor.shutdownNow();
silentChannelExecutor.shutdownNow();
throw new IOException(ex);
}
}
use of org.jboss.netty.channel.Channel in project neo4j by neo4j.
the class IdleChannelReaperTest method shouldNotCloseAChannelThatHasBeenIdleForMoreThanHalfThresholdButIsStillOpenConnectedAndBound.
@Test
public void shouldNotCloseAChannelThatHasBeenIdleForMoreThanHalfThresholdButIsStillOpenConnectedAndBound() {
// given
FakeClock clock = Clocks.fakeClock();
ChannelCloser channelCloser = mock(ChannelCloser.class);
IdleChannelReaper idleChannelReaper = new IdleChannelReaper(channelCloser, NO_LOGGING, clock, THRESHOLD);
Channel channel = mock(Channel.class);
idleChannelReaper.add(channel, dummyRequestContext());
when(channel.isOpen()).thenReturn(true);
when(channel.isConnected()).thenReturn(true);
when(channel.isBound()).thenReturn(true);
// when
clock.forward(THRESHOLD / 2 + 10, TimeUnit.MILLISECONDS);
idleChannelReaper.run();
// then
verifyNoMoreInteractions(channelCloser);
}
Aggregations