private void stopConfiguration() throws Exception { if (configuration == null) { // abort received but script not prepared, therefore entire script failed if (progress == null) { progress = new ScriptProgress(newSequential(0, 0), ""); } RegionInfo scriptInfo = progress.getScriptInfo(); progress.addScriptFailure(scriptInfo); } else { // stopping the configuration will implicitly trigger the script complete listener // to handle incomplete script that is being aborted by canceling the finish future // clear out the pipelines for new connections to avoid impacting the observed script for (ServerBootstrapResolver serverResolver : configuration.getServerResolvers()) { ServerBootstrap server = serverResolver.resolve(); server.setPipelineFactory(pipelineFactory(pipeline(closeOnExceptionHandler))); } for (ClientBootstrapResolver clientResolver : configuration.getClientResolvers()) { ClientBootstrap client = clientResolver.resolve(); client.setPipelineFactory(pipelineFactory(pipeline(closeOnExceptionHandler))); } // remove each handler from the configuration pipelines // this will trigger failures for any handlers on a pipeline for an incomplete stream // including pipelines not yet associated with any channel for (ChannelPipeline pipeline : configuration.getClientAndServerPipelines()) { stopStream(pipeline); } // cancel any pending binds and connects for (ChannelFuture bindFuture : bindFutures) { bindFuture.cancel(); } for (ChannelFuture connectFuture : connectFutures) { connectFuture.cancel(); } // close server and client channels final ChannelGroupFuture closeFuture = serverChannels.close(); closeFuture.addListener( new ChannelGroupFutureListener() { @Override public void operationComplete(final ChannelGroupFuture future) { clientChannels.close(); } }); for (AutoCloseable resource : configuration.getResources()) { try { resource.close(); } catch (Exception e) { // ignore } } } }
private void connectClient(ClientBootstrapResolver clientResolver) throws Exception { final RegionInfo regionInfo = clientResolver.getRegionInfo(); ClientBootstrap client = clientResolver.resolve(); if (LOGGER.isDebugEnabled()) { LOGGER.debug("[id: ] connect " + client.getOption("remoteAddress")); } ChannelFuture connectFuture = client.connect(); connectFutures.add(connectFuture); clientChannels.add(connectFuture.getChannel()); connectFuture.addListener(createConnectCompleteListener(regionInfo)); }
private <In, Out> Channel listen(int port, NetworkEndpointFactory<In, Out> endpointFactory) { ServerBootstrap bootstrap = new ServerBootstrap(channelFactory); bootstrap.setPipelineFactory( () -> Channels.pipeline( new ObjectEncoder(), new ObjectDecoder(ClassResolvers.softCachingResolver(getClass().getClassLoader())), new LoggingHandler(NettyNetworkServer.class, logLevel), new NettyNetworkEndpointAdapter<>(endpointFactory.createEndpoint()), new AddToChannelGroupHandler(allChannels))); bootstrap.setOption("child.tcpNoDelay", true); bootstrap.setOption("child.keepAlive", true); Channel channel = bootstrap.bind(new InetSocketAddress(port)); allChannels.add(channel); return channel; }
private ChannelFuture prepareServers() throws Exception { /* Accept's ... Robot acting as a server */ for (ServerBootstrapResolver serverResolver : configuration.getServerResolvers()) { ServerBootstrap server = serverResolver.resolve(); if (LOGGER.isDebugEnabled()) { LOGGER.debug("Binding to address " + server.getOption("localAddress")); } /* Keep track of the client channels */ server.setParentHandler( new SimpleChannelHandler() { @Override public void childChannelOpen(ChannelHandlerContext ctx, ChildChannelStateEvent e) throws Exception { clientChannels.add(e.getChildChannel()); } @Override public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception { Channel channel = ctx.getChannel(); channel.close(); } }); // Bind Asynchronously ChannelFuture bindFuture = server.bindAsync(); // Add to out serverChannel Group serverChannels.add(bindFuture.getChannel()); // Add to our list of bindFutures so we can cancel them later on a possible abort bindFutures.add(bindFuture); // Listen for the bindFuture. RegionInfo regionInfo = (RegionInfo) server.getOption("regionInfo"); bindFuture.addListener( createBindCompleteListener(regionInfo, serverResolver.getNotifyBarrier())); } return new CompositeChannelFuture<>(channel, bindFutures); }
@Override public void close() { allChannels.close().awaitUninterruptibly(); channelFactory.releaseExternalResources(); }