public void start(String ip, int port) { // Configure the client. System.setProperty("java.net.preferIPv4Stack", "true"); System.setProperty("java.net.preferIPv6Addresses", "false"); bootstrap = new ClientBootstrap( new NioClientSocketChannelFactory( Executors.newCachedThreadPool(), Executors.newCachedThreadPool())); // Set up the event pipeline factory. // bootstrap.setPipelineFactory(new MessageServerPipelineFactory()); bootstrap.getPipeline().addLast("decoder", new PackageDecoder()); bootstrap.getPipeline().addLast("encoder", new PackageEncoder()); bootstrap.getPipeline().addLast("handler", new ClientHandler()); bootstrap.setOption("tcpNoDelay", true); bootstrap.setOption("keepAlive", true); bootstrap.setOption("child.linger", 1); // Start the connection attempt. channelFuture = bootstrap.connect(new InetSocketAddress(ip, port)); // Wait until the connection is closed or the connection attempt fails. channelFuture.awaitUninterruptibly(); channel = channelFuture.awaitUninterruptibly().getChannel(); }
public void run() { // Configure the client. ChannelFactory factory = new NioClientSocketChannelFactory( Executors.newCachedThreadPool(), Executors.newCachedThreadPool(), 1, 1); ClientBootstrap bootstrap = new ClientBootstrap(factory); // Set up the pipeline factory. bootstrap.setPipelineFactory(setPipelineFactory()); bootstrap.setOption("tcpNoDelay", true); bootstrap.setOption("keepAlive", true); // Start the connection attempt. ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port)); if (oneShot) { // Wait until the connection is closed or the connection attempt fails. future.getChannel().getCloseFuture().awaitUninterruptibly(); // Shut down thread pools to exit. bootstrap.releaseExternalResources(); } }
/** * Starts the BGP peer. * * @param connectToSocket the socket to connect to */ private void connect(InetSocketAddress connectToSocket) throws InterruptedException { ChannelFactory channelFactory = new NioClientSocketChannelFactory( Executors.newCachedThreadPool(), Executors.newCachedThreadPool()); ChannelPipelineFactory pipelineFactory = () -> { ChannelPipeline pipeline = Channels.pipeline(); pipeline.addLast("BgpPeerFrameDecoderTest", peerFrameDecoder); pipeline.addLast("BgpPeerChannelHandlerTest", peerChannelHandler); return pipeline; }; peerBootstrap = new ClientBootstrap(channelFactory); peerBootstrap.setOption("child.keepAlive", true); peerBootstrap.setOption("child.tcpNoDelay", true); peerBootstrap.setPipelineFactory(pipelineFactory); peerBootstrap.connect(connectToSocket); }
private Channel connectFrom(InetSocketAddress connectToSocket, SocketAddress localAddress) throws InterruptedException { ChannelFactory channelFactory = new NioClientSocketChannelFactory( Executors.newCachedThreadPool(), Executors.newCachedThreadPool()); ChannelPipelineFactory pipelineFactory = () -> { ChannelPipeline pipeline = Channels.pipeline(); pipeline.addLast("BgpPeerFrameDecoderTest", peerFrameDecoder); pipeline.addLast("BgpPeerChannelHandlerTest", peerChannelHandler); return pipeline; }; peerBootstrap = new ClientBootstrap(channelFactory); peerBootstrap.setOption("child.keepAlive", true); peerBootstrap.setOption("child.tcpNoDelay", true); peerBootstrap.setPipelineFactory(pipelineFactory); Channel channel = peerBootstrap.connect(connectToSocket, localAddress).getChannel(); return channel; }
/** * Connect to remote servers. We'll initiate the connection to * any nodes with a lower ID so that there will be a single connection * between each pair of nodes which we'll use symmetrically */ protected void startClients(ChannelPipelineFactory pipelineFactory) { final ClientBootstrap bootstrap = new ClientBootstrap( new NioClientSocketChannelFactory(bossExecutor, workerExecutor)); bootstrap.setOption("child.reuseAddr", true); bootstrap.setOption("child.keepAlive", true); bootstrap.setOption("child.tcpNoDelay", true); bootstrap.setOption("child.sendBufferSize", SEND_BUFFER_SIZE); bootstrap.setOption("child.connectTimeoutMillis", CONNECT_TIMEOUT); bootstrap.setPipelineFactory(pipelineFactory); clientBootstrap = bootstrap; ScheduledExecutorService ses = syncManager.getThreadPool().getScheduledExecutor(); reconnectTask = new SingletonTask(ses, new ConnectTask()); reconnectTask.reschedule(0, TimeUnit.SECONDS); }
public void init() throws SyncException { cg = new DefaultChannelGroup("Cluster Bootstrap"); bossExecutor = Executors.newCachedThreadPool(); workerExecutor = Executors.newCachedThreadPool(); bootstrap = new ClientBootstrap(new NioClientSocketChannelFactory(bossExecutor, workerExecutor)); bootstrap.setOption("child.reuseAddr", true); bootstrap.setOption("child.keepAlive", true); bootstrap.setOption("child.tcpNoDelay", true); bootstrap.setOption("child.sendBufferSize", RPCService.SEND_BUFFER_SIZE); bootstrap.setOption("child.receiveBufferSize", RPCService.SEND_BUFFER_SIZE); bootstrap.setOption("child.connectTimeoutMillis", RPCService.CONNECT_TIMEOUT); pipelineFactory = new BootstrapPipelineFactory(this); bootstrap.setPipelineFactory(pipelineFactory); }
@Override public void startUp(FloodlightModuleContext context) throws FloodlightModuleException { shutdown = false; bossExecutor = Executors.newCachedThreadPool(); workerExecutor = Executors.newCachedThreadPool(); final ClientBootstrap bootstrap = new ClientBootstrap( new NioClientSocketChannelFactory(bossExecutor, workerExecutor)); bootstrap.setOption("child.reuseAddr", true); bootstrap.setOption("child.keepAlive", true); bootstrap.setOption("child.tcpNoDelay", true); bootstrap.setOption("child.sendBufferSize", RPCService.SEND_BUFFER_SIZE); bootstrap.setOption("child.receiveBufferSize", RPCService.SEND_BUFFER_SIZE); bootstrap.setOption("child.connectTimeoutMillis", RPCService.CONNECT_TIMEOUT); pipelineFactory = new RemoteSyncPipelineFactory(this); bootstrap.setPipelineFactory(pipelineFactory); clientBootstrap = bootstrap; }
/** * Create a bookkeeper client. A zookeeper client and a client socket factory * will be instantiated as part of this constructor. * * @param servers * A list of one of more servers on which zookeeper is running. The * client assumes that the running bookies have been registered with * zookeeper under the path * {@link BookieWatcher#BOOKIE_REGISTRATION_PATH} * @throws IOException * @throws InterruptedException * @throws KeeperException */ public BookKeeper(String servers) throws IOException, InterruptedException, KeeperException { this(new ZooKeeper(servers, 10000, new Watcher() { @Override public void process(WatchedEvent event) { // TODO: handle session disconnects and expires if (LOG.isDebugEnabled()) { LOG.debug("Process: " + event.getType() + " " + event.getPath()); } } }), new NioClientSocketChannelFactory(Executors.newCachedThreadPool(), Executors.newCachedThreadPool())); ownZKHandle = true; ownChannelFactory = true; }
public NiftyClient(NettyClientConfig nettyClientConfig, boolean local) { this.nettyClientConfig = nettyClientConfig; if (local) { log.warn("Using local client"); this.channelFactory = new DefaultLocalClientChannelFactory(); this.timer = null; this.bossExecutor = null; this.workerExecutor = null; this.defaultSocksProxyAddress = null; } else { this.timer = nettyClientConfig.getTimer(); this.bossExecutor = nettyClientConfig.getBossExecutor(); this.workerExecutor = nettyClientConfig.getWorkerExecutor(); this.defaultSocksProxyAddress = nettyClientConfig.getDefaultSocksProxyAddress(); int bossThreadCount = nettyClientConfig.getBossThreadCount(); int workerThreadCount = nettyClientConfig.getWorkerThreadCount(); NioWorkerPool workerPool = new NioWorkerPool(workerExecutor, workerThreadCount, ThreadNameDeterminer.CURRENT); NioClientBossPool bossPool = new NioClientBossPool(bossExecutor, bossThreadCount, timer, ThreadNameDeterminer.CURRENT); this.channelFactory = new NioClientSocketChannelFactory(bossPool, workerPool); } }
public synchronized void start() { final Executor bossPool = Executors.newCachedThreadPool(); final Executor workerPool = Executors.newCachedThreadPool(); bootstrap = new ServerBootstrap(new NioServerSocketChannelFactory(bossPool, workerPool)); final ClientSocketChannelFactory clientSocketChannelFactory = new NioClientSocketChannelFactory(bossPool, workerPool); bootstrap.setOption("child.tcpNoDelay", true); allChannels = new DefaultChannelGroup("handler"); bootstrap.setPipelineFactory(new ChannelPipelineFactory() { @Override public ChannelPipeline getPipeline() throws Exception { return Channels.pipeline(new FrontendHandler(allChannels, clientSocketChannelFactory, serverPool, statistics)); } }); log.info("Starting on port {}", port); acceptor = bootstrap.bind(new InetSocketAddress(port)); if (acceptor.isBound()) { log.info("Server started successfully"); } }
/** * Connect master server for message communication. */ private void connectMasterServer() { this.messageClient = new ClientBootstrap(new NioClientSocketChannelFactory(Executors.newSingleThreadExecutor(), Executors.newSingleThreadExecutor())); // Set up the pipeline factory. this.messageClient.setPipelineFactory(new ChannelPipelineFactory() { public ChannelPipeline getPipeline() throws Exception { return Channels.pipeline(new NettyBytableEncoder(), new NettyBytableDecoder(), new ClientHandler()); } }); String[] namePortGroup = this.masterServerAddress.split(":"); String masterServerName = namePortGroup[0]; int masterServerPort = NumberFormatUtils.getInt(namePortGroup[1]); // Start the connection attempt. ChannelFuture future = this.messageClient.connect(new InetSocketAddress(masterServerName, masterServerPort)); this.clientChannel = future.awaitUninterruptibly().getChannel(); LOG.info("Connect to {}:{}", masterServerName, masterServerPort); }
/** * Connect to app master for status RPC report. */ private void initRPCClient() { this.rpcClient = new ClientBootstrap(new NioClientSocketChannelFactory(Executors.newSingleThreadExecutor(), Executors.newSingleThreadExecutor())); // Set up the pipeline factory. this.rpcClient.setPipelineFactory(new ChannelPipelineFactory() { public ChannelPipeline getPipeline() throws Exception { return Channels.pipeline(new ObjectEncoder(), new ObjectDecoder(ClassResolvers.cacheDisabled(getClass().getClassLoader())), new ClientHandler()); } }); // Start the connection attempt. ChannelFuture future = this.rpcClient.connect(new InetSocketAddress(this.rpcHostName, this.rpcPort)); LOG.info("Connect to {}:{}", this.rpcHostName, this.rpcPort); this.rpcClientChannel = future.awaitUninterruptibly().getChannel(); }
public void connect() throws IOException { this.bootstrap = new ClientBootstrap( new NioClientSocketChannelFactory( Executors.newCachedThreadPool(), Executors.newCachedThreadPool())); this.bootstrap.setPipelineFactory(new NettyTCPWriterPipelineFactory(this.bootstrap, this.channel, this.timer)); //this.bootstrap.setOption("tcpNoDelay", true); // TODO: do some exception handling here bootstrap.setOption("remoteAddress", new InetSocketAddress(this.host, this.port)); this.future = this.bootstrap.connect(); this.channel = this.future.awaitUninterruptibly().getChannel(); this.channel.setReadable(false); if(!this.future.isSuccess()){ logger.info("NettyTCP: future unsuccessful"); } }