@Test public void addByteDecoderWhenFullReactorPipeline() throws Exception { channel.pipeline() .addLast(NettyPipeline.HttpCodec, new HttpServerCodec()) .addLast(NettyPipeline.HttpServerHandler, new ChannelDuplexHandler()) .addLast(NettyPipeline.ReactiveBridge, new ChannelHandlerAdapter() { }); ChannelHandler decoder = new LineBasedFrameDecoder(12); testContext.addHandlerLast("decoder", decoder) .addHandlerFirst("decoder$extract", NettyPipeline.inboundHandler(ADD_EXTRACTOR)); assertEquals(channel.pipeline() .names(), Arrays.asList(NettyPipeline.HttpCodec, NettyPipeline.HttpServerHandler, "decoder$extract", "decoder", NettyPipeline.ReactiveBridge, "DefaultChannelPipeline$TailContext#0")); }
@Test public void addNonByteDecoderWhenFullReactorPipeline() throws Exception { channel.pipeline() .addLast(NettyPipeline.HttpCodec, new HttpServerCodec()) .addLast(NettyPipeline.HttpServerHandler, new ChannelDuplexHandler()) .addLast(NettyPipeline.ReactiveBridge, new ChannelHandlerAdapter() { }); ChannelHandler decoder = new ChannelHandlerAdapter() { }; testContext.addHandlerLast("decoder", decoder); assertEquals(channel.pipeline() .names(), Arrays.asList(NettyPipeline.HttpCodec, NettyPipeline.HttpServerHandler, "decoder", NettyPipeline.ReactiveBridge, "DefaultChannelPipeline$TailContext#0")); }
@Test public void addByteEncoderWhenFullReactorPipeline() throws Exception { channel.pipeline() .addLast(NettyPipeline.HttpCodec, new HttpServerCodec()) .addLast(NettyPipeline.HttpServerHandler, new ChannelDuplexHandler()) .addLast(NettyPipeline.ReactiveBridge, new ChannelHandlerAdapter() { }); ChannelHandler encoder = new LineBasedFrameDecoder(12); testContext.addHandlerFirst("encoder", encoder); assertEquals(channel.pipeline() .names(), Arrays.asList(NettyPipeline.HttpCodec, NettyPipeline.HttpServerHandler, "encoder", NettyPipeline.ReactiveBridge, "DefaultChannelPipeline$TailContext#0")); }
@Test public void addNonByteEncoderWhenFullReactorPipeline() throws Exception { channel.pipeline() .addLast(NettyPipeline.HttpCodec, new HttpServerCodec()) .addLast(NettyPipeline.HttpServerHandler, new ChannelDuplexHandler()) .addLast(NettyPipeline.ReactiveBridge, new ChannelHandlerAdapter() { }); ChannelHandler encoder = new ChannelHandlerAdapter() { }; testContext.addHandlerFirst("encoder", encoder); assertEquals(channel.pipeline() .names(), Arrays.asList(NettyPipeline.HttpCodec, NettyPipeline.HttpServerHandler, "encoder", NettyPipeline.ReactiveBridge, "DefaultChannelPipeline$TailContext#0")); }
@Test public void addSeveralByteEncodersWhenCodec() throws Exception { ChannelHandler encoder1 = new LineBasedFrameDecoder(12); ChannelHandler encoder2 = new LineBasedFrameDecoder(13); channel.pipeline() .addLast(NettyPipeline.HttpCodec, new HttpServerCodec()) .addLast(NettyPipeline.HttpServerHandler, new ChannelDuplexHandler()) .addLast(NettyPipeline.ReactiveBridge, new ChannelHandlerAdapter() { }); testContext.addHandlerFirst("encoder1", encoder1) .addHandlerFirst("encoder2", encoder2); assertEquals(channel.pipeline() .names(), Arrays.asList(NettyPipeline.HttpCodec, NettyPipeline.HttpServerHandler, "encoder2", "encoder1", NettyPipeline.ReactiveBridge, "DefaultChannelPipeline$TailContext#0")); }
@Override protected void initChannel(Channel channel) throws Exception { ChannelPipeline pipeline = channel.pipeline(); pipeline.addLast(new ChannelDuplexHandler() { @Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { LOGGER.debug("Disconnected from: {} {}", address, ctx.channel()); outgoingChannels.remove(address); super.channelInactive(ctx); } }); pipeline.addLast(new ProtobufVarint32LengthFieldPrepender()); pipeline.addLast(serializerHandler); if (networkEmulatorHandler != null) { pipeline.addLast(networkEmulatorHandler); } pipeline.addLast(exceptionHandler); }
protected void handleInitChannel ( final SocketChannel ch ) { // add the APCI/APDU handler ch.pipeline ().addLast ( new APDUDecoder () ); ch.pipeline ().addLast ( new APDUEncoder () ); // add logging if ( Boolean.getBoolean ( "org.eclipse.scada.protocol.iec60870.trace" ) ) { ch.pipeline ().addLast ( new LoggingHandler ( LogLevel.TRACE ) ); } final MessageChannel messageChannel = new MessageChannel ( this.options, this.manager ); // message channel ch.pipeline ().addLast ( messageChannel ); // now add all server modules for ( final ServerModule module : this.modules ) { module.initializeChannel ( ch, messageChannel ); } // finally add the default exception catcher ch.pipeline ().addLast ( new ChannelDuplexHandler () { @Override public void exceptionCaught ( final ChannelHandlerContext ctx, final Throwable cause ) throws Exception { logger.warn ( "Close connection due to uncaught exception", cause ); ctx.close (); } } ); }
private void initChannel(ChannelPipeline pipeline) { pipeline.addBefore("handler", "mqttEncoder", MqttEncoder.INSTANCE); if (this.options.getMaxMessageSize() > 0) { pipeline.addBefore("handler", "mqttDecoder", new MqttDecoder(this.options.getMaxMessageSize())); } else { // max message size not set, so the default from Netty MQTT codec is used pipeline.addBefore("handler", "mqttDecoder", new MqttDecoder()); } // adding the idle state handler for timeout on CONNECT packet pipeline.addBefore("handler", "idle", new IdleStateHandler(this.options.timeoutOnConnect(), 0, 0)); pipeline.addBefore("handler", "timeoutOnConnect", new ChannelDuplexHandler() { @Override public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { if (evt instanceof IdleStateEvent) { IdleStateEvent e = (IdleStateEvent) evt; if (e.state() == IdleState.READER_IDLE) { // as MQTT 3.1.1 describes, if no packet is sent after a "reasonable" time (here CONNECT timeout) // the connection is closed ctx.channel().close(); } } } }); }
private void initChannel(ChannelPipeline pipeline) { // add into pipeline netty's (en/de)coder pipeline.addBefore("handler", "mqttEncoder", MqttEncoder.INSTANCE); if (this.options.getMaxMessageSize() > 0) { pipeline.addBefore("handler", "mqttDecoder", new MqttDecoder(this.options.getMaxMessageSize())); } else { // max message size not set, so the default from Netty MQTT codec is used pipeline.addBefore("handler", "mqttDecoder", new MqttDecoder()); } if (this.options.isAutoKeepAlive() && this.options.getKeepAliveTimeSeconds() != 0) { pipeline.addBefore("handler", "idle", new IdleStateHandler(0, this.options.getKeepAliveTimeSeconds(), 0)); pipeline.addBefore("handler", "keepAliveHandler", new ChannelDuplexHandler() { @Override public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { if (evt instanceof IdleStateEvent) { IdleStateEvent e = (IdleStateEvent) evt; if (e.state() == IdleState.WRITER_IDLE) { ping(); } } } }); } }
@Test public void addSeveralByteDecodersWhenCodec() throws Exception { ChannelHandler decoder1 = new LineBasedFrameDecoder(12); ChannelHandler decoder2 = new LineBasedFrameDecoder(13); channel.pipeline() .addLast(NettyPipeline.HttpCodec, new HttpServerCodec()) .addLast(NettyPipeline.HttpServerHandler, new ChannelDuplexHandler()) .addLast(NettyPipeline.ReactiveBridge, new ChannelHandlerAdapter() { }); testContext.addHandlerLast("decoder1$extract", NettyPipeline.inboundHandler(ADD_EXTRACTOR)) .addHandlerLast("decoder1", decoder1) .addHandlerLast("decoder2$extract", NettyPipeline.inboundHandler(ADD_EXTRACTOR)) .addHandlerLast("decoder2", decoder2); assertEquals(channel.pipeline() .names(), Arrays.asList(NettyPipeline.HttpCodec, NettyPipeline.HttpServerHandler, "decoder1$extract", "decoder1", "decoder2$extract", "decoder2", NettyPipeline.ReactiveBridge, "DefaultChannelPipeline$TailContext#0")); }
@Override public Subscriber<Long> createSubscriber() { // Embedded channel requires at least one handler when it's created, but HandlerSubscriber // needs the channels event loop in order to be created, so start with a dummy, then replace. ChannelHandler dummy = new ChannelDuplexHandler(); EmbeddedChannel channel = new EmbeddedChannel(dummy); HandlerSubscriber<Long> subscriber = new HandlerSubscriber<>(channel.eventLoop(), 2, 4); channel.pipeline().replace(dummy, "subscriber", subscriber); return new SubscriberWithChannel<>(channel, subscriber); }
@Override protected void initChannel(SocketChannel ch) throws Exception { // Initialize our session Object when the channel is initialized, attach // it to the channel. ch.attr(NetworkConstants.SESSION_KEY).setIfAbsent(new PlayerIO(ch)); // Initialize the pipeline channel handlers. ChannelDuplexHandler timeout = new IdleStateHandler(NetworkConstants.INPUT_TIMEOUT, 0, 0); ByteToMessageDecoder loginHandshakeHandler = new LoginHandshakeHandler(); ch.pipeline().addLast("login-handshake", loginHandshakeHandler); ch.pipeline().addLast("channel-handler", channelHandler); ch.pipeline().addLast("timeout", timeout); }
@Test public void testInboundChainNoChanges() throws Exception { List<Interceptor> interceptors = new ArrayList<>(); MockInterceptor interceptor1 = new MockInterceptor(); MockInterceptor interceptor2 = new MockInterceptor(); interceptors.add(interceptor1); interceptors.add(interceptor2); EmbeddedChannel channel = new EmbeddedChannel( new ChannelDuplexHandler() { @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { InterceptorChain chain = new InterceptorChain(ctx, interceptors, (ResourceRequest) msg); chain.fireInbound(); } } ); ResourceRequest request = new DefaultResourceRequest.Builder(RequestType.READ, new ResourcePath("/foo/bar")).build(); channel.writeInbound(request); ResourceRequest endRequest = (ResourceRequest) channel.readInbound(); assertThat(endRequest).isNotNull(); assertThat(interceptor1.requests()).hasSize(1); assertThat(interceptor1.requests()).contains(request); assertThat(interceptor2.requests()).hasSize(1); assertThat(interceptor2.requests()).contains(request); }
@Test public void testOutbound() throws Exception { List<Interceptor> interceptors = new ArrayList<>(); MockInterceptor interceptor1 = new MockInterceptor(); MockInterceptor interceptor2 = new MockInterceptor(); interceptors.add(interceptor1); interceptors.add(interceptor2); ResourceRequest request = new DefaultResourceRequest.Builder(RequestType.READ, new ResourcePath("/foo/bar")).build(); ResourceResponse response = new DefaultResourceErrorResponse(request, ResourceErrorResponse.ErrorType.NOT_AUTHORIZED); EmbeddedChannel channel = new EmbeddedChannel( new ChannelDuplexHandler() { @Override public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception { InterceptorChain chain = new InterceptorChain(ctx, interceptors, (ResourceResponse) msg); chain.fireOutbound(); } } ); channel.writeInbound(request); ResourceRequest endRequest = (ResourceRequest) channel.readInbound(); channel.writeAndFlush(response); ResourceResponse endResponse = (ResourceResponse) channel.readOutbound(); assertThat(endResponse).isNotNull(); assertThat(interceptor2.responses()).hasSize(1); assertThat(interceptor2.responses()).contains(response); assertThat(interceptor1.responses()).hasSize(1); assertThat(interceptor1.responses()).contains(response); }
@Test public void testInboundChainShortCircuit() throws Exception { List<Interceptor> interceptors = new ArrayList<>(); ResourceRequest request = new DefaultResourceRequest.Builder(RequestType.READ, new ResourcePath("/foo/bar")).build(); ResourceResponse response = new DefaultResourceErrorResponse(request, ResourceErrorResponse.ErrorType.NOT_AUTHORIZED); MockInterceptor interceptor1 = new MockInterceptor(); MockInterceptor interceptor2 = new MockInterceptor() { @Override public void onInbound(InboundInterceptorContext context) throws Exception { requests.add(context.request()); context.replyWith(response); } }; MockInterceptor interceptor3 = new MockInterceptor(); interceptors.add(interceptor1); interceptors.add(interceptor2); interceptors.add(interceptor3); EmbeddedChannel channel = new EmbeddedChannel( new ChannelDuplexHandler() { @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { InterceptorChain chain = new InterceptorChain(ctx, interceptors, (ResourceRequest) msg); chain.fireInbound(); } } ); channel.writeInbound(request); ResourceRequest endRequest = (ResourceRequest) channel.readInbound(); Object endResponse = channel.readOutbound(); assertThat(endRequest).isNull(); assertThat(interceptor1.requests()).hasSize(1); assertThat(interceptor1.requests()).contains(request); assertThat(interceptor2.requests()).hasSize(1); assertThat(interceptor2.requests()).contains(request); assertThat(interceptor3.requests()).isEmpty(); assertThat(interceptor3.responses()).isEmpty(); assertThat(interceptor2.responses()).isEmpty(); assertThat(interceptor1.responses()).hasSize(1); assertThat(interceptor1.responses()).contains(response); }
public MockChannelHandlerContext(Channel channel, String name, ChannelDuplexHandler handler) { this.channel = channel; this.name = name; this.handler = handler; }