List of usage examples for io.netty.channel ChannelInitializer ChannelInitializer
ChannelInitializer
From source file:cn.scujcc.bug.bitcoinplatformandroid.util.socket.websocket.WebSocketBase.java
License:Apache License
private void connect() { try {//from w w w . j a v a 2 s. c o m final URI uri = new URI(url); if (uri == null) { return; } if (uri.getHost().contains("com")) { siteFlag = 1; } group = new NioEventLoopGroup(1); bootstrap = new Bootstrap(); final SslContext sslCtx = SslContext.newClientContext(); final WebSocketClientHandler handler = new WebSocketClientHandler( WebSocketClientHandshakerFactory.newHandshaker(uri, WebSocketVersion.V13, null, false, new DefaultHttpHeaders(), Integer.MAX_VALUE), service, moniter); bootstrap.group(group).option(ChannelOption.TCP_NODELAY, true).channel(NioSocketChannel.class) .handler(new ChannelInitializer<SocketChannel>() { protected void initChannel(SocketChannel ch) { ChannelPipeline p = ch.pipeline(); if (sslCtx != null) { p.addLast(sslCtx.newHandler(ch.alloc(), uri.getHost(), uri.getPort())); } p.addLast(new HttpClientCodec(), new HttpObjectAggregator(8192), handler); } }); future = bootstrap.connect(uri.getHost(), uri.getPort()); future.addListener(new ChannelFutureListener() { public void operationComplete(final ChannelFuture future) throws Exception { } }); channel = future.sync().channel(); handler.handshakeFuture().sync(); this.setStatus(true); } catch (Exception e) { Log.e(TAG, "WebSocketClient start error " + e.getLocalizedMessage()); group.shutdownGracefully(); this.setStatus(false); } }
From source file:cn.yesway.demo.book.protocol.http.fileServer.HttpFileServer.java
License:Apache License
public void run(final int port, final String url) throws Exception { EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try {/*from www . jav a 2s .c om*/ ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast("http-decoder", new HttpRequestDecoder()); ch.pipeline().addLast("http-aggregator", new HttpObjectAggregator(65536)); ch.pipeline().addLast("http-encoder", new HttpResponseEncoder()); ch.pipeline().addLast("http-chunked", new ChunkedWriteHandler()); ch.pipeline().addLast("fileServerHandler", new HttpFileServerHandler(url)); } }); ChannelFuture future = b.bind("127.0.0.1", port).sync(); System.out.println("HTTP??? : " + "http://127.0.0.1:" + port + url); future.channel().closeFuture().sync(); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }
From source file:cn.yesway.demo.book.protocol.http.xml.client.HttpXmlClient.java
License:Apache License
public void connect(int port) throws Exception { // ?NIO/*from w w w . j av a 2 s . com*/ EventLoopGroup group = new NioEventLoopGroup(); try { Bootstrap b = new Bootstrap(); b.group(group).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true) .handler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast("http-decoder", new HttpResponseDecoder()); ch.pipeline().addLast("http-aggregator", new HttpObjectAggregator(65536)); // XML? ch.pipeline().addLast("xml-decoder", new HttpXmlResponseDecoder(Order.class, true)); ch.pipeline().addLast("http-encoder", new HttpRequestEncoder()); ch.pipeline().addLast("xml-encoder", new HttpXmlRequestEncoder()); ch.pipeline().addLast("xmlClientHandler", new HttpXmlClientHandle()); } }); // ?? ChannelFuture f = b.connect(new InetSocketAddress(port)).sync(); // f.channel().closeFuture().sync(); } finally { // NIO group.shutdownGracefully(); } }
From source file:cn.yesway.demo.book.protocol.http.xml.server.HttpXmlServer.java
License:Apache License
public void run(final int port) throws Exception { EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try {/*from w w w .j a va2s . c om*/ ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast("http-decoder", new HttpRequestDecoder()); ch.pipeline().addLast("http-aggregator", new HttpObjectAggregator(65536)); ch.pipeline().addLast("xml-decoder", new HttpXmlRequestDecoder(Order.class, true)); ch.pipeline().addLast("http-encoder", new HttpResponseEncoder()); ch.pipeline().addLast("xml-encoder", new HttpXmlResponseEncoder()); ch.pipeline().addLast("xmlServerHandler", new HttpXmlServerHandler()); } }); ChannelFuture future = b.bind(new InetSocketAddress(port)).sync(); System.out.println("HTTP??? : " + "http://localhost:" + port); future.channel().closeFuture().sync(); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }
From source file:cn.zyf.ObjectStorageServer.java
License:Open Source License
private void run(String host, int port, int packageSize) { EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); EventLoopGroup backendGroup = new NioEventLoopGroup(); try {//from w w w . ja va 2 s. c o m ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .childHandler(new ChannelInitializer<SocketChannel>() { protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast("http-decoder", new HttpRequestDecoder()); ch.pipeline().addLast("http-aggregator", new HttpObjectAggregator(packageSize)); ch.pipeline().addLast("http-chunked", new ChunkedWriteHandler()); ch.pipeline().addLast("http-encoder", new HttpResponseEncoder()); ch.pipeline().addLast("oss-decoder", new ParseRequestHandler()); ch.pipeline().addLast("oss-filter", new FilterRequestHandler()); ch.pipeline().addLast("oss-authen", new AuthenticationHandler()); ch.pipeline().addLast("oss-author", new AuthorizationHandler()); ch.pipeline().addLast(backendGroup, "oss-backend", new ObjectStorageHandler()); } }); ChannelFuture future = b.bind(host, port).sync(); LOG.info("start Http Server with hostname=" + host + ":" + port); future.channel().closeFuture().sync(); } catch (InterruptedException e) { LOG.error("occur InterruptedException ", e); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }
From source file:co.paralleluniverse.comsat.webactors.netty.WebActorTest.java
License:Open Source License
@Before public void setUp() throws InterruptedException, IOException { System.out.println("Clearing sessions"); WebActorHandler.sessions.clear();//from w w w. ja v a2 s .c o m group = new NioEventLoopGroup(); final ServerBootstrap b = new ServerBootstrap(); b.group(group).channel(NioServerSocketChannel.class).handler(new LoggingHandler(LogLevel.INFO)) .childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); pipeline.addLast(new LoggingHandler(LogLevel.INFO)); pipeline.addLast(new HttpRequestDecoder()); pipeline.addLast(new LoggingHandler(LogLevel.INFO)); pipeline.addLast(HTTP_RESPONSE_ENCODER_KEY, new HttpResponseEncoder()); pipeline.addLast(new LoggingHandler(LogLevel.INFO)); pipeline.addLast(new HttpObjectAggregator(65536)); pipeline.addLast(new LoggingHandler(LogLevel.INFO)); pipeline.addLast(webActorHandlerCreatorInEffect.call()); } }); ch = b.bind(INET_PORT).sync(); AbstractEmbeddedServer.waitUrlAvailable("http://localhost:" + INET_PORT); System.err.println("Server is up"); }
From source file:co.rsk.net.discovery.UDPServer.java
License:Open Source License
private Bootstrap createBootstrap(EventLoopGroup group) { return new Bootstrap().group(group).channel(NioDatagramChannel.class) .handler(new ChannelInitializer<NioDatagramChannel>() { @Override//from www . java2 s . c om public void initChannel(NioDatagramChannel ch) throws Exception { ch.pipeline().addLast(new PacketDecoder()); UDPChannel udpChannel = new UDPChannel(ch, peerExplorer); peerExplorer.setUDPChannel(udpChannel); ch.pipeline().addLast(udpChannel); } }); }
From source file:co.rsk.rpc.netty.Web3WebSocketServer.java
License:Open Source License
public void start() throws InterruptedException { ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .childHandler(new ChannelInitializer<SocketChannel>() { @Override//from ww w . j a va2 s . c o m protected void initChannel(SocketChannel ch) throws Exception { ChannelPipeline p = ch.pipeline(); p.addLast(new HttpServerCodec()); p.addLast(new HttpObjectAggregator(1024 * 1024 * 5)); p.addLast(new WebSocketServerProtocolHandler("/websocket")); p.addLast(jsonRpcHandler); p.addLast(web3ServerHandler); p.addLast(new Web3ResultWebSocketResponseHandler()); } }); webSocketChannel = b.bind(host, port); webSocketChannel.sync(); }
From source file:code.google.nfs.rpc.netty.client.NettyClientFactory.java
License:Apache License
protected Client createClient(String targetIP, int targetPort, int connectTimeout, String key) throws Exception { Bootstrap bootstrap = new Bootstrap(); bootstrap.group(workerGroup).channel(NioSocketChannel.class) .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) .option(ChannelOption.TCP_NODELAY, Boolean.parseBoolean(System.getProperty("nfs.rpc.tcp.nodelay", "true"))) .option(ChannelOption.SO_REUSEADDR, Boolean.parseBoolean(System.getProperty("nfs.rpc.tcp.reuseaddress", "true"))); if (connectTimeout < 1000) { bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 1000); } else {/*from www . j av a2s .c o m*/ bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, connectTimeout); } final NettyClientHandler handler = new NettyClientHandler(this, key); bootstrap.handler(new ChannelInitializer<SocketChannel>() { protected void initChannel(SocketChannel channel) throws Exception { ChannelPipeline pipeline = channel.pipeline(); pipeline.addLast("decoder", new NettyProtocolDecoder()); pipeline.addLast("encoder", new NettyProtocolEncoder()); pipeline.addLast("handler", handler); } }); ChannelFuture future = bootstrap.connect(new InetSocketAddress(targetIP, targetPort)).sync(); future.awaitUninterruptibly(connectTimeout); if (!future.isDone()) { LOGGER.error("Create connection to " + targetIP + ":" + targetPort + " timeout!"); throw new Exception("Create connection to " + targetIP + ":" + targetPort + " timeout!"); } if (future.isCancelled()) { LOGGER.error("Create connection to " + targetIP + ":" + targetPort + " cancelled by user!"); throw new Exception("Create connection to " + targetIP + ":" + targetPort + " cancelled by user!"); } if (!future.isSuccess()) { LOGGER.error("Create connection to " + targetIP + ":" + targetPort + " error", future.cause()); throw new Exception("Create connection to " + targetIP + ":" + targetPort + " error", future.cause()); } NettyClient client = new NettyClient(future, key, connectTimeout); handler.setClient(client); return client; }
From source file:code.google.nfs.rpc.netty.server.NettyServer.java
License:Apache License
public void start(int listenPort, final ExecutorService threadPool) throws Exception { if (!startFlag.compareAndSet(false, true)) { return;/*from w w w . j a va2 s. co m*/ } bootstrap.childHandler(new ChannelInitializer<SocketChannel>() { protected void initChannel(SocketChannel channel) throws Exception { ChannelPipeline pipeline = channel.pipeline(); pipeline.addLast("decoder", new NettyProtocolDecoder()); pipeline.addLast("encoder", new NettyProtocolEncoder()); pipeline.addLast("handler", new NettyServerHandler(threadPool)); } }); bootstrap.bind(new InetSocketAddress(listenPort)).sync(); LOGGER.warn("Server started,listen at: " + listenPort); }