@Override public ChannelPipeline getPipeline() throws Exception { BootstrapChannelHandler handler = new BootstrapChannelHandler(bootstrap); ChannelPipeline pipeline = Channels.pipeline(); pipeline.addLast("frameDecoder", new ThriftFrameDecoder(maxFrameSize)); pipeline.addLast("frameEncoder", new ThriftFrameEncoder()); pipeline.addLast("timeout", new BootstrapTimeoutHandler(timer, 10)); pipeline.addLast("handler", handler); return pipeline; }
/** * Sends this response to all the passed channels as a {@link TextWebSocketFrame} * @param listener A channel future listener to attach to each channel future. Ignored if null. * @param channels The channels to send this response to * @return An array of the futures for the write of this response to each channel written to */ public ChannelFuture[] send(ChannelFutureListener listener, Channel...channels) { if(channels!=null && channels.length>0) { Set<ChannelFuture> futures = new HashSet<ChannelFuture>(channels.length); if(opCode==null) { opCode = "ok"; } TextWebSocketFrame frame = new TextWebSocketFrame(this.toChannelBuffer()); for(Channel channel: channels) { if(channel!=null && channel.isWritable()) { ChannelFuture cf = Channels.future(channel); if(listener!=null) cf.addListener(listener); channel.getPipeline().sendDownstream(new DownstreamMessageEvent(channel, cf, frame, channel.getRemoteAddress())); futures.add(cf); } } return futures.toArray(new ChannelFuture[futures.size()]); } return EMPTY_CHANNEL_FUTURE_ARR; }
@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; } }); }
@Override public ChannelPipeline getPipeline() throws Exception { ChannelPipeline channelPipeline = Channels.pipeline(); SizeHeaderFrameDecoder sizeHeader = new SizeHeaderFrameDecoder(); if (nettyTransport.maxCumulationBufferCapacity != null) { if (nettyTransport.maxCumulationBufferCapacity.bytes() > Integer.MAX_VALUE) { sizeHeader.setMaxCumulationBufferCapacity(Integer.MAX_VALUE); } else { sizeHeader.setMaxCumulationBufferCapacity((int) nettyTransport.maxCumulationBufferCapacity.bytes()); } } if (nettyTransport.maxCompositeBufferComponents != -1) { sizeHeader.setMaxCumulationBufferComponents(nettyTransport.maxCompositeBufferComponents); } channelPipeline.addLast("size", sizeHeader); // using a dot as a prefix means, this cannot come from any settings parsed channelPipeline.addLast("dispatcher", new MessageChannelHandler(nettyTransport, nettyTransport.logger, ".client")); return channelPipeline; }
@Override public ChannelPipeline getPipeline() throws Exception { ChannelPipeline channelPipeline = Channels.pipeline(); channelPipeline.addLast("openChannels", nettyTransport.serverOpenChannels); SizeHeaderFrameDecoder sizeHeader = new SizeHeaderFrameDecoder(); if (nettyTransport.maxCumulationBufferCapacity != null) { if (nettyTransport.maxCumulationBufferCapacity.bytes() > Integer.MAX_VALUE) { sizeHeader.setMaxCumulationBufferCapacity(Integer.MAX_VALUE); } else { sizeHeader.setMaxCumulationBufferCapacity((int) nettyTransport.maxCumulationBufferCapacity.bytes()); } } if (nettyTransport.maxCompositeBufferComponents != -1) { sizeHeader.setMaxCumulationBufferComponents(nettyTransport.maxCompositeBufferComponents); } channelPipeline.addLast("size", sizeHeader); channelPipeline.addLast("dispatcher", new MessageChannelHandler(nettyTransport, nettyTransport.logger, name)); return channelPipeline; }
@Override public ChannelPipeline getPipeline() throws Exception { ChannelPipeline pipeline = Channels.pipeline(); if (sslFactory != null) { pipeline.addLast("ssl", new SslHandler(sslFactory.createSSLEngine())); } pipeline.addLast("decoder", new HttpRequestDecoder()); pipeline.addLast("aggregator", new HttpChunkAggregator(1 << 16)); pipeline.addLast("encoder", new HttpResponseEncoder()); pipeline.addLast("chunking", new ChunkedWriteHandler()); pipeline.addLast("shuffle", SHUFFLE); return pipeline; // TODO factor security manager into pipeline // TODO factor out encode/decode to permit binary shuffle // TODO factor out decode of index to permit alt. models }
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; }
public void run() { // Configure the client. DatagramChannelFactory f = new NioDatagramChannelFactory( Executors.newCachedThreadPool(), workerCount); server = new ConnectionlessBootstrap(f); server.setPipeline(Channels.pipeline(RpcUtil.STAGE_RPC_MESSAGE_PARSER, rpcProgram, RpcUtil.STAGE_RPC_UDP_RESPONSE)); server.setOption("broadcast", "false"); server.setOption("sendBufferSize", SEND_BUFFER_SIZE); server.setOption("receiveBufferSize", RECEIVE_BUFFER_SIZE); // Listen to the UDP port ch = server.bind(new InetSocketAddress(port)); InetSocketAddress socketAddr = (InetSocketAddress) ch.getLocalAddress(); boundPort = socketAddr.getPort(); LOG.info("Started listening to UDP requests at port " + boundPort + " for " + rpcProgram + " with workerCount " + workerCount); }
@Override public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception { ChannelBuffer buf = (ChannelBuffer) e.getMessage(); ByteBuffer b = buf.toByteBuffer().asReadOnlyBuffer(); XDR in = new XDR(b, XDR.State.READING); RpcInfo info = null; try { RpcCall callHeader = RpcCall.read(in); ChannelBuffer dataBuffer = ChannelBuffers.wrappedBuffer(in.buffer() .slice()); info = new RpcInfo(callHeader, dataBuffer, ctx, e.getChannel(), e.getRemoteAddress()); } catch (Exception exc) { LOG.info("Malformed RPC request from " + e.getRemoteAddress()); } if (info != null) { Channels.fireMessageReceived(ctx, info); } }
@Override public ChannelPipeline getPipeline() throws Exception { BgpChannelHandler handler = new BgpChannelHandler(bgpController); ChannelPipeline pipeline = Channels.pipeline(); pipeline.addLast("bgpmessagedecoder", new BgpMessageDecoder()); pipeline.addLast("bgpmessageencoder", new BgpMessageEncoder()); pipeline.addLast("holdTime", readTimeoutHandler); if (isBgpServ) { pipeline.addLast("PassiveHandler", handler); } else { pipeline.addLast("ActiveHandler", handler); } return pipeline; }
/** * 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; }
@Override public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception { Object message = e.getMessage(); if (message instanceof SyncMessage) { handleSyncMessage((SyncMessage)message, ctx.getChannel()); } else if (message instanceof List) { for (Object i : (List<?>)message) { if (i instanceof SyncMessage) { try { handleSyncMessage((SyncMessage)i, ctx.getChannel()); } catch (Exception ex) { Channels.fireExceptionCaught(ctx, ex); } } } } else { handleUnknownMessage(ctx, message); } }
@Override public ChannelPipeline getPipeline() throws Exception { RPCChannelHandler channelHandler = new RPCChannelHandler(syncManager, rpcService); IdleStateHandler idleHandler = new IdleStateHandler(timer, 5, 10, 0); ReadTimeoutHandler readTimeoutHandler = new ReadTimeoutHandler(timer, 30); ChannelPipeline pipeline = Channels.pipeline(); pipeline.addLast("idle", idleHandler); pipeline.addLast("timeout", readTimeoutHandler); pipeline.addLast("handshaketimeout", new HandshakeTimeoutHandler(channelHandler, timer, 10)); pipeline.addLast("frameDecoder", new ThriftFrameDecoder(maxFrameSize)); pipeline.addLast("frameEncoder", new ThriftFrameEncoder()); pipeline.addLast("handler", channelHandler); return pipeline; }
@Override public ChannelPipeline getPipeline() throws Exception { RemoteSyncChannelHandler channelHandler = new RemoteSyncChannelHandler(syncManager); ChannelPipeline pipeline = Channels.pipeline(); pipeline.addLast("frameDecoder", new ThriftFrameDecoder(maxFrameSize)); pipeline.addLast("frameEncoder", new ThriftFrameEncoder()); pipeline.addLast("timeout", new RSHandshakeTimeoutHandler(channelHandler, timer, 3)); pipeline.addLast("handler", channelHandler); return pipeline; }
@Override public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception { if (e.getMessage() instanceof List) { @SuppressWarnings("unchecked") List<OFMessage> msglist = (List<OFMessage>)e.getMessage(); for (OFMessage ofm : msglist) { try { // Do the actual packet processing state.processOFMessage(ofm); } catch (Exception ex) { // We are the last handler in the stream, so run the // exception through the channel again by passing in // ctx.getChannel(). Channels.fireExceptionCaught(ctx.getChannel(), ex); } } } else { Channels.fireExceptionCaught(ctx.getChannel(), new AssertionError("Message received from channel is not a list")); } }
/** * 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 public ChannelPipeline getPipeline() throws Exception { OFChannelHandler handler = new OFChannelHandler(controller); ChannelPipeline pipeline = Channels.pipeline(); pipeline.addLast("ofmessagedecoder", new OFMessageDecoder()); pipeline.addLast("ofmessageencoder", new OFMessageEncoder()); pipeline.addLast("idle", idleHandler); pipeline.addLast("timeout", readTimeoutHandler); pipeline.addLast("handshaketimeout", new HandshakeTimeoutHandler(handler, timer, 15)); if (pipelineExecutor != null) pipeline.addLast("pipelineExecutor", new ExecutionHandler(pipelineExecutor)); pipeline.addLast("handler", handler); return pipeline; }
@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; } }); }