@Override protected void doOpen() throws Throwable { NettyHelper.setNettyLoggerFactory(); bootstrap = new ClientBootstrap(channelFactory); // config // @see org.jboss.netty.channel.socket.SocketChannelConfig bootstrap.setOption("keepAlive", true); bootstrap.setOption("tcpNoDelay", true); bootstrap.setOption("connectTimeoutMillis", getTimeout()); final NettyHandler nettyHandler = new NettyHandler(getUrl(), this); bootstrap.setPipelineFactory(new ChannelPipelineFactory() { public ChannelPipeline getPipeline() { NettyCodecAdapter adapter = new NettyCodecAdapter(getCodec(), getUrl(), NettyClient.this); ChannelPipeline pipeline = Channels.pipeline(); pipeline.addLast("decoder", adapter.getDecoder()); pipeline.addLast("encoder", adapter.getEncoder()); pipeline.addLast("handler", nettyHandler); return pipeline; } }); }
NettyServerCnxnFactory() { bootstrap = new ServerBootstrap( new NioServerSocketChannelFactory( Executors.newCachedThreadPool(), Executors.newCachedThreadPool())); // parent channel bootstrap.setOption("reuseAddress", true); // child channels bootstrap.setOption("child.tcpNoDelay", true); /* set socket linger to off, so that socket close does not block */ bootstrap.setOption("child.soLinger", -1); bootstrap.setPipelineFactory(new ChannelPipelineFactory() { @Override public ChannelPipeline getPipeline() throws Exception { ChannelPipeline p = Channels.pipeline(); if (secure) { initSSL(p); } p.addLast("servercnxnfactory", channelHandler); return p; } }); }
private ServerBootstrap startHttpServer(int port, final Token<DelegationTokenIdentifier> token, final URI url) { ServerBootstrap bootstrap = new ServerBootstrap( new NioServerSocketChannelFactory(Executors.newCachedThreadPool(), Executors.newCachedThreadPool())); bootstrap.setPipelineFactory(new ChannelPipelineFactory() { @Override public ChannelPipeline getPipeline() throws Exception { return Channels.pipeline(new HttpRequestDecoder(), new HttpChunkAggregator(65536), new HttpResponseEncoder(), new CredentialsLogicHandler(token, url.toString())); } }); bootstrap.bind(new InetSocketAddress("localhost", port)); return bootstrap; }
/** * Tell controller that we're ready to accept switches loop. */ public void run() { try { final ServerBootstrap bootstrap = createServerBootStrap(); bootstrap.setOption("reuseAddr", true); bootstrap.setOption("child.keepAlive", true); bootstrap.setOption("child.tcpNoDelay", true); bootstrap.setOption("child.sendBufferSize", Controller.SEND_BUFFER_SIZE); ChannelPipelineFactory pfact = new OpenflowPipelineFactory(this, null, sslContext); bootstrap.setPipelineFactory(pfact); cg = new DefaultChannelGroup(); openFlowPorts.forEach(port -> { InetSocketAddress sa = new InetSocketAddress(port); cg.add(bootstrap.bind(sa)); log.info("Listening for switch connections on {}", sa); }); } catch (Exception e) { throw new RuntimeException(e); } }
/** * Tell controller that we're ready to accept pcc connections. */ public void run() { try { final ServerBootstrap bootstrap = createServerBootStrap(); bootstrap.setOption("reuseAddr", true); bootstrap.setOption("child.keepAlive", true); bootstrap.setOption("child.tcpNoDelay", true); bootstrap.setOption("child.sendBufferSize", Controller.SEND_BUFFER_SIZE); ChannelPipelineFactory pfact = new PcepPipelineFactory(this); bootstrap.setPipelineFactory(pfact); InetSocketAddress sa = new InetSocketAddress(pcepPort); cg = new DefaultChannelGroup(); cg.add(bootstrap.bind(sa)); log.info("Listening for PCC connection on {}", sa); } catch (Exception e) { throw new RuntimeException(e); } }
/** * 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); }
/** * Bootstraps netty, the server that handles all openflow connections */ public void bootstrapNetty() { try { final ServerBootstrap bootstrap = createServerBootStrap(); bootstrap.setOption("reuseAddr", true); bootstrap.setOption("child.keepAlive", true); bootstrap.setOption("child.tcpNoDelay", true); bootstrap.setOption("child.sendBufferSize", Controller.SEND_BUFFER_SIZE); ChannelPipelineFactory pfact = useSsl ? new OpenflowPipelineFactory(this, floodlightProvider.getTimer(), this, debugCounterService, keyStore, keyStorePassword) : new OpenflowPipelineFactory(this, floodlightProvider.getTimer(), this, debugCounterService); bootstrap.setPipelineFactory(pfact); InetSocketAddress sa = new InetSocketAddress(floodlightProvider.getOFPort()); final ChannelGroup cg = new DefaultChannelGroup(); cg.add(bootstrap.bind(sa)); log.info("Listening for switch connections on {}", sa); } catch (Exception e) { throw new RuntimeException(e); } }
/** * Startup a ServerBootstrap with NioServerSocketChannelFactory using the * portNo specified in the constructor. * * @return */ public ServerBootstrap connect() { bootstrap = new ServerBootstrap(new NioServerSocketChannelFactory( Executors.newCachedThreadPool(), Executors.newCachedThreadPool())); bootstrap.setPipelineFactory(new ChannelPipelineFactory() { @Override public ChannelPipeline getPipeline() throws Exception { return Channels.pipeline(new MessageFrameDecoder(), new MessageEventBagHandler(bagList)); } }); System.out.println("Binding to: localhost:" + portNo); bootstrap.bind(new InetSocketAddress("localhost", portNo)); return bootstrap; }
private ServerBootstrap connectServer(boolean simulateConflict, boolean simulateConflictErrorPointer) { ServerBootstrap bootstrap = new ServerBootstrap( new NioServerSocketChannelFactory( Executors.newCachedThreadPool(), Executors.newCachedThreadPool())); final MessageEventBagHandler messagEventBagHandler = new MessageEventBagHandler( bagList, simulateConflict, simulateConflictErrorPointer); bootstrap.setPipelineFactory(new ChannelPipelineFactory() { @Override public ChannelPipeline getPipeline() throws Exception { return Channels.pipeline(new MessageFrameDecoder(), messagEventBagHandler); } }); bootstrap.bind(new InetSocketAddress(testPort)); return bootstrap; }
private ServerBootstrap connectServer() { ServerBootstrap bootstrap = new ServerBootstrap( new NioServerSocketChannelFactory( Executors.newCachedThreadPool(), Executors.newCachedThreadPool())); bootstrap.setPipelineFactory(new ChannelPipelineFactory() { @Override public ChannelPipeline getPipeline() throws Exception { return Channels.pipeline(new MessageFrameDecoder(), new MessageEventBagHandler(bagList)); } }); bootstrap.bind(new InetSocketAddress(testPort)); return bootstrap; }
@Override public void connect() { workerService = createWorkerService(getThreadPoolType(CollectorProperties.WRITER.COLLECTOR_WORKER_THREAD_POOL)); workerbossService = createWorkderBossService(getThreadPoolType(CollectorProperties.WRITER.COLLECTOR_WORKERBOSS_THREAD_POOL)); channelFactory = new NioServerSocketChannelFactory(workerbossService, workerService); bootstrap = new ServerBootstrap(channelFactory); bootstrap.setPipelineFactory(new ChannelPipelineFactory() { @Override public ChannelPipeline getPipeline() throws Exception { return Channels.pipeline(ipFilterHandler, new MessageFrameDecoder(), new ReadTimeoutHandler( HashedWheelTimerFactory.getInstance(), readTimeout, TimeUnit.MILLISECONDS), metricsHandler, channelHandler); } }); bootstrap.bind(new InetSocketAddress(port)); }
/** * Startup a ServerBootstrap with NioServerSocketChannelFactory using the * portNo specified in the constructor. * */ private void connectLockBootstrap() { lockBootstrap = new ServerBootstrap(new NioServerSocketChannelFactory( Executors.newCachedThreadPool(), Executors.newCachedThreadPool())); lockBootstrap.setPipelineFactory(new ChannelPipelineFactory() { @Override public ChannelPipeline getPipeline() throws Exception { return Channels.pipeline(new MessageFrameDecoder(), metricHandler, lockHandler); } }); lockBootstrap.bind(new InetSocketAddress(lockPort)); }
/** * Startup a ServerBootstrap with NioServerSocketChannelFactory using the * portNo specified in the constructor. * */ private void connectUnlockBootstrap() { unlockBootstrap = new ServerBootstrap( new NioServerSocketChannelFactory( Executors.newCachedThreadPool(), Executors.newCachedThreadPool())); unlockBootstrap.setPipelineFactory(new ChannelPipelineFactory() { @Override public ChannelPipeline getPipeline() throws Exception { return Channels.pipeline(new MessageFrameDecoder(), unlockHandler); } }); unlockBootstrap.bind(new InetSocketAddress(releaseLockPort)); }
@Override protected void doOpen() throws Throwable { NettyHelper.setNettyLoggerFactory(); bootstrap = new ClientBootstrap(channelFactory); // config // @see org.jboss.netty.channel.socket.SocketChannelConfig bootstrap.setOption("keepAlive", true); bootstrap.setOption("tcpNoDelay", true); bootstrap.setOption("connectTimeoutMillis", getTimeout()); //下面才是正确的 //bootstrap.setOption("connectTimeoutMillis", getConnectTimeout()); //netty handler final NettyHandler nettyHandler = new NettyHandler(getUrl(), this); bootstrap.setPipelineFactory(new ChannelPipelineFactory() { public ChannelPipeline getPipeline() { NettyCodecAdapter adapter = new NettyCodecAdapter(getCodec(), getUrl(), NettyClient.this); ChannelPipeline pipeline = Channels.pipeline(); pipeline.addLast("decoder", adapter.getDecoder()); pipeline.addLast("encoder", adapter.getEncoder()); pipeline.addLast("handler", nettyHandler); return pipeline; } }); }
public void start(int listenPort, final ExecutorService threadPool) throws Exception { if (!startFlag.compareAndSet(false, true)) { return; } bootstrap.setPipelineFactory(new ChannelPipelineFactory() { public ChannelPipeline getPipeline() throws Exception { ChannelPipeline pipeline = new DefaultChannelPipeline(); pipeline.addLast("decoder", new NettyProtocolDecoder()); pipeline.addLast("encoder", new NettyProtocolEncoder()); pipeline.addLast("handler", new NettyServerHandler(threadPool)); return pipeline; } }); bootstrap.bind(new InetSocketAddress(listenPort)); LOGGER.warn("Server started,listen at: " + listenPort); }
@Override public void start() { org.jboss.netty.channel.ChannelFactory factory = new NioServerSocketChannelFactory( Executors.newCachedThreadPool(), Executors.newCachedThreadPool()); ServerBootstrap serverBootstrap = new ServerBootstrap(factory); serverBootstrap.setPipelineFactory(new ChannelPipelineFactory() { @Override public ChannelPipeline getPipeline() { EventHandler handler = new EventHandler(); final ChannelPipeline pipeline = Channels.pipeline(handler); pipeline.addFirst("decoder", new LineBasedFrameDecoder(1024)); pipeline.addLast("encoder", new StringEncoder(Charsets.UTF_8)); return pipeline; } }); logger.info("OpenTSDB Source starting..."); if (host == null) { nettyChannel = serverBootstrap.bind(new InetSocketAddress(port)); } else { nettyChannel = serverBootstrap.bind(new InetSocketAddress(host, port)); } super.start(); }
@Override protected void doOpen() throws Throwable { NettyHelper.setNettyLoggerFactory(); //设置线程池(但是线程池中的线程都是守护线程,为的就是当JVM退出时候不用考虑守护线程是否已经结束) ExecutorService boss = Executors.newCachedThreadPool(new NamedThreadFactory("NettyServerBoss", true)); ExecutorService worker = Executors.newCachedThreadPool(new NamedThreadFactory("NettyServerWorker", true)); ChannelFactory channelFactory = new NioServerSocketChannelFactory(boss, worker, getUrl().getPositiveParameter(Constants.IO_THREADS_KEY, Constants.DEFAULT_IO_THREADS)); bootstrap = new ServerBootstrap(channelFactory); //Netty启动类 //定义NettyHandler(这个应该是通用的Handler,只有在服务启动的时候生效一次) final NettyHandler nettyHandler = new NettyHandler(getUrl(), this); channels = nettyHandler.getChannels(); bootstrap.setPipelineFactory(new ChannelPipelineFactory() { public ChannelPipeline getPipeline() { NettyCodecAdapter adapter = new NettyCodecAdapter(getCodec() ,getUrl(), NettyServer.this); ChannelPipeline pipeline = Channels.pipeline(); pipeline.addLast("decoder", adapter.getDecoder()); //增加解码处理器 pipeline.addLast("encoder", adapter.getEncoder()); //增加编码处理器 pipeline.addLast("handler", nettyHandler); //增加具体操作的处理器 return pipeline; } }); // bind channel = bootstrap.bind(getBindAddress()); }
@Override public ChannelPipelineFactory getPipelineFactory() { executionHandler = new NaviExecutionHandler(); return new ChannelPipelineFactory() { public ChannelPipeline getPipeline() throws Exception { ChannelPipeline pipeline = Channels.pipeline(); pipeline.addLast("httpCodec", new NaviHttpServerCodec()); pipeline.addLast("inflater", new HttpContentDecompressor()); pipeline.addLast("GLOBAL_TRAFFIC_SHAPING", globalTcHandler); String chunkSize = ServerConfigure.get(NaviDefine.CHUNK_AGGR_SIZE); if (StringUtils.isNumeric(chunkSize)) { pipeline.addLast("aggregator", new HttpChunkAggregator(Integer.valueOf(chunkSize))); } // pipeline.addLast("encoder", new HttpResponseEncoder()); pipeline.addLast("deflater", new HttpContentCompressor()); pipeline.addLast("execution", executionHandler); pipeline.addLast("idleState", new IdleStateHandler(timer, getChildChannelIdleTime(), getChildChannelIdleTime(), getChildChannelIdleTime())); pipeline.addLast("handler", getNaviHttpHandler()); return pipeline; } }; }
public ChannelPipelineFactory getPipelineFactory() { executionHandler = new NaviExecutionHandler(); // execution = new ExecutionHandler(Executors.newCachedThreadPool()); return new ChannelPipelineFactory() { public ChannelPipeline getPipeline() throws Exception { ChannelPipeline pipeline = Channels.pipeline(); pipeline.addLast("idleState", new IdleStateHandler(timer, getChildChannelIdleTime(), getChildChannelIdleTime(), getChildChannelIdleTime())); //StateCheckChannelHandler加入心跳机制 读空闲 断开连接 写空闲发送心跳数据 // pipeline.addLast("idleHandler", new StateCheckChannelHandler()); pipeline.addLast("decoder", new DelimiterBasedFrameDecoder(getMaxPacketSize(), getDelimiter())); pipeline.addLast("execution", executionHandler); // pipeline.addLast("execution", execution); pipeline.addLast("handler", getNaviTCPHandler()); return pipeline; } }; }
@Override public void doOpen() throws Throwable { bootstrap = new ClientBootstrap(channelFactory); bootstrap.setOption("keepAlive", true); bootstrap.setOption("tcpNoDelay", true); bootstrap.setOption("connectTimeoutMillis", getConnectTimeout()); final NettyHandler nettyHandler = new NettyHandler(getConf(), this); bootstrap.setPipelineFactory(new ChannelPipelineFactory() { public ChannelPipeline getPipeline() { NettyCodecAdapter adapter = new NettyCodecAdapter(getConf(),getCodec(), NettyClient.this); ChannelPipeline pipeline = Channels.pipeline(); pipeline.addLast("decoder", adapter.getDecoder()); pipeline.addLast("encoder", adapter.getEncoder()); pipeline.addLast("handler", nettyHandler); return pipeline; } }); }
@Override public void doOpen() throws Throwable { ExecutorService boss = Executors.newCachedThreadPool(new NamedThreadFactory("NettyServerBoss", false)); ExecutorService worker = Executors.newCachedThreadPool(new NamedThreadFactory("NettyServerWorker", true)); int ioThread = conf.getInt(Constants.IO_THREADS,Constants.DEFAULT_IO_THREADS); ChannelFactory channelFactory = new NioServerSocketChannelFactory(boss, worker, ioThread); bootstrap = new ServerBootstrap(channelFactory); final NettyHandler nettyHandler = new NettyHandler(getConf(), this); channels = nettyHandler.getChannels(); bootstrap.setPipelineFactory(new ChannelPipelineFactory() { public ChannelPipeline getPipeline() { NettyCodecAdapter adapter = new NettyCodecAdapter(conf,getCodec(), NettyServer.this); ChannelPipeline pipeline = Channels.pipeline(); pipeline.addLast("decoder", adapter.getDecoder()); pipeline.addLast("encoder", adapter.getEncoder()); pipeline.addLast("handler", nettyHandler); return pipeline; } }); // bind channel = bootstrap.bind(getBindAddress()); }
/** * Tell controller that we're ready to accept switches loop. */ public void run() { try { final ServerBootstrap bootstrap = createServerBootStrap(); bootstrap.setOption("reuseAddr", true); bootstrap.setOption("child.keepAlive", true); bootstrap.setOption("child.tcpNoDelay", true); bootstrap.setOption("child.sendBufferSize", Controller.SEND_BUFFER_SIZE); ChannelPipelineFactory pfact = new OpenflowPipelineFactory(this, null); bootstrap.setPipelineFactory(pfact); InetSocketAddress sa = new InetSocketAddress(openFlowPort); cg = new DefaultChannelGroup(); cg.add(bootstrap.bind(sa)); log.info("Listening for switch connections on {}", sa); } catch (Exception e) { throw new RuntimeException(e); } }
public MasterServer(final ChannelHandler handler){ NioServerSocketChannelFactory channelFactory= new NioServerSocketChannelFactory(Executors.newCachedThreadPool(), Executors.newCachedThreadPool()); bootstrap=new ServerBootstrap(channelFactory); pipelineFactory=new ChannelPipelineFactory(){ private final ProtobufVarint32LengthFieldPrepender frameEncoder = new ProtobufVarint32LengthFieldPrepender(); private final ProtobufEncoder protobufEncoder = new ProtobufEncoder(); public ChannelPipeline getPipeline() throws Exception { ChannelPipeline p = pipeline(); p.addLast("frameDecoder", new ProtobufVarint32FrameDecoder()); p.addLast("protobufDecoder",new ProtobufDecoder(Protocol.SocketMessage.getDefaultInstance())); p.addLast("frameEncoder", frameEncoder); p.addLast("protobufEncoder", protobufEncoder); p.addLast("handler", handler); return p; } }; try { bootstrap.setPipeline(pipelineFactory.getPipeline()); } catch (Exception e) { e.printStackTrace(); } }