List of usage examples for io.netty.channel ChannelInitializer ChannelInitializer
ChannelInitializer
From source file:com.spotify.netty4.handler.codec.zmtp.EndToEndTest.java
License:Apache License
private Channel bind(final SocketAddress address, final ChannelHandler codec, final ChannelHandler handler) { final ServerBootstrap bootstrap = new ServerBootstrap(); bootstrap.group(new NioEventLoopGroup(1), new NioEventLoopGroup()); bootstrap.channel(NioServerSocketChannel.class); bootstrap.childHandler(new ChannelInitializer<NioSocketChannel>() { @Override//from w w w .ja va2 s .c om protected void initChannel(final NioSocketChannel ch) throws Exception { ch.pipeline().addLast(codec, handler); } }); return bootstrap.bind(address).awaitUninterruptibly().channel(); }
From source file:com.spotify.netty4.handler.codec.zmtp.EndToEndTest.java
License:Apache License
private Channel connect(final SocketAddress address, final ChannelHandler codec, final ChannelHandler handler) { final Bootstrap bootstrap = new Bootstrap(); bootstrap.group(new NioEventLoopGroup()); bootstrap.channel(NioSocketChannel.class); bootstrap.handler(new ChannelInitializer<NioSocketChannel>() { @Override//from w ww . ja v a2 s . c o m protected void initChannel(final NioSocketChannel ch) throws Exception { ch.pipeline().addLast(codec, handler); } }); return bootstrap.connect(address).awaitUninterruptibly().channel(); }
From source file:com.spotify.netty4.handler.codec.zmtp.ProtocolViolationTests.java
License:Apache License
@Before public void setup() { final ServerBootstrap serverBootstrap = new ServerBootstrap(); serverBootstrap.channel(NioServerSocketChannel.class); bossGroup = new NioEventLoopGroup(1); group = new NioEventLoopGroup(); serverBootstrap.group(bossGroup, group); serverBootstrap.childHandler(new ChannelInitializer<NioSocketChannel>() { @Override//from w w w .j a v a2 s .co m protected void initChannel(final NioSocketChannel ch) throws Exception { ch.pipeline().addLast( ZMTPCodec.builder().protocol(ZMTP20).socketType(ROUTER).localIdentity(identity).build(), mockHandler); } }); serverChannel = serverBootstrap.bind(new InetSocketAddress("localhost", 0)).awaitUninterruptibly() .channel(); serverAddress = (InetSocketAddress) serverChannel.localAddress(); }
From source file:com.spotify.netty4.handler.codec.zmtp.ProtocolViolationTests.java
License:Apache License
@Theory public void protocolErrorsCauseException(@TestedOn(ints = { 16, 17, 27, 32, 48, 53 }) final int payloadSize) throws Exception { final Bootstrap b = new Bootstrap(); b.group(new NioEventLoopGroup()); b.channel(NioSocketChannel.class); b.handler(new ChannelInitializer<NioSocketChannel>() { @Override//from ww w. j ava 2s . c om protected void initChannel(final NioSocketChannel ch) throws Exception { ch.pipeline().addLast(new MockHandler()); } }); final Channel channel = b.connect(serverAddress).awaitUninterruptibly().channel(); final ByteBuf payload = Unpooled.buffer(payloadSize); for (int i = 0; i < payloadSize; i++) { payload.writeByte(0); } channel.writeAndFlush(payload); mockHandler.active.get(5, SECONDS); mockHandler.exception.get(5, SECONDS); mockHandler.inactive.get(5, SECONDS); assertFalse(mockHandler.handshaked); assertFalse(mockHandler.read); }
From source file:com.springapp.mvc.netty.example.http.websocketx.client.WebSocketClient.java
License:Apache License
public static void main(String[] args) throws Exception { URI uri = new URI(URL); String scheme = uri.getScheme() == null ? "http" : uri.getScheme(); final String host = uri.getHost() == null ? "127.0.0.1" : uri.getHost(); final int port; if (uri.getPort() == -1) { if ("http".equalsIgnoreCase(scheme)) { port = 80;/*from www. j a v a2s. co m*/ } else if ("https".equalsIgnoreCase(scheme)) { port = 443; } else { port = -1; } } else { port = uri.getPort(); } if (!"ws".equalsIgnoreCase(scheme) && !"wss".equalsIgnoreCase(scheme)) { System.err.println("Only WS(S) is supported."); return; } final boolean ssl = "wss".equalsIgnoreCase(scheme); final SslContext sslCtx; if (ssl) { sslCtx = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).build(); } else { sslCtx = null; } EventLoopGroup group = new NioEventLoopGroup(); try { // Connect with V13 (RFC 6455 aka HyBi-17). You can change it to V08 or V00. // If you change it to V00, ping is not supported and remember to change // HttpResponseDecoder to WebSocketHttpResponseDecoder in the pipeline. final WebSocketClientHandler handler = new WebSocketClientHandler(WebSocketClientHandshakerFactory .newHandshaker(uri, WebSocketVersion.V13, null, false, new DefaultHttpHeaders())); Bootstrap b = new Bootstrap(); b.group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) { ChannelPipeline p = ch.pipeline(); if (sslCtx != null) { p.addLast(sslCtx.newHandler(ch.alloc(), host, port)); } p.addLast(new HttpClientCodec(), new HttpObjectAggregator(8192), handler); } }); Channel ch = b.connect(uri.getHost(), port).sync().channel(); handler.handshakeFuture().sync(); BufferedReader console = new BufferedReader(new InputStreamReader(System.in)); while (true) { String msg = console.readLine(); if (msg == null) { break; } else if ("bye".equals(msg.toLowerCase())) { ch.writeAndFlush(new CloseWebSocketFrame()); ch.closeFuture().sync(); break; } else if ("ping".equals(msg.toLowerCase())) { WebSocketFrame frame = new PingWebSocketFrame( Unpooled.wrappedBuffer(new byte[] { 8, 1, 8, 1 })); ch.writeAndFlush(frame); } else { WebSocketFrame frame = new TextWebSocketFrame(msg); ch.writeAndFlush(frame); } } } finally { group.shutdownGracefully(); } }
From source file:com.springapp.mvc.netty.example.localecho.LocalEcho.java
License:Apache License
public static void main(String[] args) throws Exception { // Address to bind on / connect to. final LocalAddress addr = new LocalAddress(PORT); EventLoopGroup serverGroup = new LocalEventLoopGroup(); EventLoopGroup clientGroup = new NioEventLoopGroup(); // NIO event loops are also OK try {//from w w w . j av a 2 s .com // Note that we can use any event loop to ensure certain local channels // are handled by the same event loop thread which drives a certain socket channel // to reduce the communication latency between socket channels and local channels. ServerBootstrap sb = new ServerBootstrap(); sb.group(serverGroup).channel(LocalServerChannel.class) .handler(new ChannelInitializer<LocalServerChannel>() { @Override public void initChannel(LocalServerChannel ch) throws Exception { ch.pipeline().addLast(new LoggingHandler(LogLevel.INFO)); } }).childHandler(new ChannelInitializer<LocalChannel>() { @Override public void initChannel(LocalChannel ch) throws Exception { ch.pipeline().addLast(new LoggingHandler(LogLevel.INFO), new LocalEchoServerHandler()); } }); Bootstrap cb = new Bootstrap(); cb.group(clientGroup).channel(LocalChannel.class).handler(new ChannelInitializer<LocalChannel>() { @Override public void initChannel(LocalChannel ch) throws Exception { ch.pipeline().addLast(new LoggingHandler(LogLevel.INFO), new LocalEchoClientHandler()); } }); // Start the server. sb.bind(addr).sync(); // Start the client. Channel ch = cb.connect(addr).sync().channel(); // Read commands from the stdin. System.out.println("Enter text (quit to end)"); ChannelFuture lastWriteFuture = null; BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); for (;;) { String line = in.readLine(); if (line == null || "quit".equalsIgnoreCase(line)) { break; } // Sends the received line to the server. lastWriteFuture = ch.writeAndFlush(line); } // Wait until all messages are flushed before closing the channel. if (lastWriteFuture != null) { lastWriteFuture.awaitUninterruptibly(); } } finally { serverGroup.shutdownGracefully(); clientGroup.shutdownGracefully(); } }
From source file:com.squareup.okhttp.benchmarks.NettyHttpClient.java
License:Apache License
@Override public void prepare(final Benchmark benchmark) { this.concurrencyLevel = benchmark.concurrencyLevel; this.targetBacklog = benchmark.targetBacklog; ChannelInitializer<SocketChannel> channelInitializer = new ChannelInitializer<SocketChannel>() { @Override//from w w w.ja v a2 s. co m public void initChannel(SocketChannel channel) throws Exception { ChannelPipeline pipeline = channel.pipeline(); if (benchmark.tls) { SSLContext sslContext = SslContextBuilder.localhost(); SSLEngine engine = sslContext.createSSLEngine(); engine.setUseClientMode(true); pipeline.addLast("ssl", new SslHandler(engine)); } pipeline.addLast("codec", new HttpClientCodec()); pipeline.addLast("inflater", new HttpContentDecompressor()); pipeline.addLast("handler", new HttpChannel(channel)); } }; bootstrap = new Bootstrap(); bootstrap.group(new NioEventLoopGroup(concurrencyLevel)) .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT).channel(NioSocketChannel.class) .handler(channelInitializer); }
From source file:com.srotya.linea.network.InternalTCPTransportServer.java
License:Apache License
public void init() throws Exception { final SslContext sslCtx; if (SSL) {/* w w w. j a va2 s .com*/ SelfSignedCertificate ssc = new SelfSignedCertificate(); sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build(); } else { sslCtx = null; } EventLoopGroup bossGroup = new NioEventLoopGroup(1); EventLoopGroup workerGroup = new NioEventLoopGroup(1); ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .handler(new LoggingHandler(LogLevel.INFO)).childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ChannelPipeline p = ch.pipeline(); if (sslCtx != null) { p.addLast(sslCtx.newHandler(ch.alloc())); } p.addLast(new LoggingHandler(LogLevel.INFO)); p.addLast(new KryoObjectEncoder()); p.addLast(new KryoObjectDecoder()); p.addLast(new IWCHandler(null)); //TODO add router } }).bind(Inet4Address.getByName("localhost"), 9999).sync().channel().closeFuture().await(); }
From source file:com.srotya.linea.network.InternalUDPTransportClient.java
License:Apache License
public void init() throws Exception { NetworkInterface iface = NetworkUtils.selectDefaultIPAddress(false); Inet4Address address = NetworkUtils.getIPv4Address(iface); // logger.info("Selected default interface:" + iface.getName() + "\twith // address:" + address.getHostAddress()); EventLoopGroup workerGroup = new NioEventLoopGroup(2); Bootstrap b = new Bootstrap(); channel = b.group(workerGroup).channel(NioDatagramChannel.class).handler(new ChannelInitializer<Channel>() { @Override// w w w . ja v a2s . co m protected void initChannel(Channel ch) throws Exception { ch.pipeline(); } }).bind(address, port).sync().channel(); // workerGroup.shutdownGracefully().sync(); }
From source file:com.srotya.linea.network.InternalUDPTransportServer.java
License:Apache License
public void start() throws Exception { NetworkInterface iface = NetworkUtils.selectDefaultIPAddress(false); Inet4Address address = NetworkUtils.getIPv4Address(iface); EventLoopGroup workerGroup = new NioEventLoopGroup(4); Bootstrap b = new Bootstrap(); channel = b.group(workerGroup).channel(NioDatagramChannel.class).option(ChannelOption.SO_RCVBUF, 1024 * 10) .handler(new ChannelInitializer<Channel>() { @Override/*from www.j a va 2s .c om*/ protected void initChannel(Channel ch) throws Exception { ch.pipeline().addLast(new KryoDatagramDecoderWrapper()).addLast(new IWCHandler(router)); } }).bind(address, port).sync().channel(); }