List of usage examples for io.netty.channel ChannelFuture channel
Channel channel();
From source file:com.liusu.tcp.proxy.mine.server.ReadBackServer.java
License:Apache License
public void bind(int port) throws Exception { // ??NIO//from w ww. j ava2 s . com EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try { ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .option(ChannelOption.SO_BACKLOG, 1024).childHandler(new ChildChannelHandler()); // ??? ChannelFuture f = b.bind(port).sync(); // f.addListener(ChannelFutureListener.CLOSE); // ??? f.channel().closeFuture().sync(); } finally { // ? bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }
From source file:com.ltln.modules.ni.omc.system.simulator.AlmClient.java
License:Apache License
public static void main(String[] args) throws Exception { Constants.init();//from w ww. ja v a 2s . c o m final SslContext sslCtx; if (SSL) { sslCtx = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).build(); } else { sslCtx = null; } // Configure the client. EventLoopGroup group = new NioEventLoopGroup(); final EventExecutorGroup handlerGroup = new DefaultEventExecutorGroup(1); 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 { ChannelPipeline p = ch.pipeline(); if (sslCtx != null) { p.addLast(sslCtx.newHandler(ch.alloc(), HOST, PORT)); } p.addLast(new AlarmMsgDecoder(8192, 7, 2, 0, 0, false)); p.addLast(new AlarmMsgEncoder()); p.addLast(handlerGroup, new AlarmClientHandler()); } }); // Start the client. ChannelFuture f = b.connect(HOST, PORT).sync(); // Wait until the connection is closed. f.channel().closeFuture().sync(); } finally { // Shut down the event loop to terminate all threads. group.shutdownGracefully(); } }
From source file:com.magnet.yak.load.NettyClient.java
License:Apache License
public static void main(String[] args) throws Exception { String host = "54.148.43.16"; int port = 5222; EventLoopGroup workerGroup = new NioEventLoopGroup(); try {/*from w ww.j a v a2s.co m*/ Bootstrap b = new Bootstrap(); // (1) b.group(workerGroup); // (2) b.channel(NioSocketChannel.class); // (3) b.option(ChannelOption.SO_KEEPALIVE, true); // (4) b.handler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new XMPPHandler()); } }); ChannelFuture f = b.connect(host, port).sync(); // (5) f.await(); Channel channel = f.channel(); LOGGER.trace("main : Writing {}"); // Wait until the connection is closed. f.channel().closeFuture().sync(); } finally { workerGroup.shutdownGracefully(); } }
From source file:com.mapr.franz.netty.FranzClient.java
License:Apache License
public void run() throws Exception { // Configure the client. Bootstrap b = new Bootstrap(); try {// ww w . j a va 2s . c om b.group(new NioEventLoopGroup()).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true) .remoteAddress(new InetSocketAddress(host, port)) .handler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new LoggingHandler(LogLevel.INFO), new FranzClientHandler(firstMessageSize)); } }); // Start the client. ChannelFuture f = b.connect().sync(); // Wait until the connection is closed. f.channel().closeFuture().sync(); } finally { // Shut down the event loop to terminate all threads. b.shutdown(); } }
From source file:com.mapr.franz.netty.FranzServer.java
License:Apache License
public void run() throws Exception { // Configure the server. ServerBootstrap b = new ServerBootstrap(); try {// w w w .j a v a 2 s. c om b.group(new NioEventLoopGroup(), new NioEventLoopGroup()).channel(NioServerSocketChannel.class) .option(ChannelOption.SO_BACKLOG, 100).localAddress(new InetSocketAddress(port)) .childOption(ChannelOption.TCP_NODELAY, true).handler(new LoggingHandler(LogLevel.INFO)) .childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new LoggingHandler(LogLevel.INFO), new FranzServerHandler()); } }); // Start the server. ChannelFuture f = b.bind().sync(); // Wait until the server socket is closed. f.channel().closeFuture().sync(); } finally { // Shut down all event loops to terminate all threads. b.shutdown(); } }
From source file:com.mastfrog.acteur.io.FileWriter.java
License:Open Source License
@Override public void operationComplete(ChannelFuture f) throws Exception { if (!f.channel().isOpen()) { return;/* w w w. jav a2 s . c o m*/ } ByteBuf buf = f.channel().alloc().buffer(bufferSize); int bytes = buf.writeBytes(stream, bufferSize); if (bytes == -1) { stream.close(); f.channel().writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT).addListener(CLOSE); return; } f = f.channel().writeAndFlush(new DefaultHttpContent(buf)); f.addListener(this); }
From source file:com.mastfrog.netty.http.client.HttpClient.java
License:Open Source License
private void submit(final URL url, HttpRequest rq, final AtomicBoolean cancelled, final ResponseFuture handle, final ResponseHandler<?> r, RequestInfo info, Duration timeout, boolean noAggregate) { if (info != null && info.isExpired()) { cancelled.set(true);//from www. j a va2s . c o m } if (cancelled.get()) { handle.event(new State.Cancelled()); return; } try { for (RequestInterceptor i : interceptors) { rq = i.intercept(rq); } final HttpRequest req = rq; Bootstrap bootstrap; if (url.getProtocol().isSecure()) { bootstrap = startSsl(url.getHostAndPort()); } else { bootstrap = start(url.getHostAndPort()); } if (!url.isValid()) { throw new IllegalArgumentException(url.getProblems() + ""); } TimeoutTimerTask tt = null; if (info == null) { info = new RequestInfo(url, req, cancelled, handle, r, timeout, tt, noAggregate); if (timeout != null) { tt = new TimeoutTimerTask(cancelled, handle, r, info); timer.schedule(tt, timeout.getMillis()); } info.timer = tt; } if (info.isExpired()) { handle.event(new State.Timeout(info.age())); return; } handle.event(new State.Connecting()); //XXX who is escaping this? req.setUri(req.getUri().replaceAll("%5f", "_")); ChannelFuture fut = bootstrap.connect(url.getHost().toString(), url.getPort().intValue()); if (tt != null) { fut.channel().closeFuture().addListener(tt); } fut.channel().attr(KEY).set(info); handle.setFuture(fut); if (!monitors.isEmpty()) { for (ActivityMonitor m : monitors) { m.onStartRequest(url); } fut.channel().closeFuture().addListener(new AdapterCloseNotifier(url)); } fut.addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { if (!future.isSuccess()) { Throwable cause = future.cause(); if (cause == null) { cause = new ConnectException(url.getHost().toString()); } handle.event(new State.Error(cause)); if (r != null) { r.onError(cause); } cancelled.set(true); } if (cancelled.get()) { future.cancel(true); if (future.channel().isOpen()) { future.channel().close(); } for (ActivityMonitor m : monitors) { m.onEndRequest(url); } return; } handle.event(new State.Connected(future.channel())); handle.event(new State.SendRequest(req)); future = future.channel().writeAndFlush(req); future.addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { if (cancelled.get()) { future.cancel(true); future.channel().close(); } handle.event(new State.AwaitingResponse()); } }); } }); } catch (Exception ex) { Exceptions.chuck(ex); } }
From source file:com.mastfrog.netty.http.client.ResponseFuture.java
License:Open Source License
boolean cancel(Duration forTimeout) { // We need to send the timeout event before setting the cancelled flag if (forTimeout != null && !cancelled.get()) { event(new State.Timeout(forTimeout)); }/* w w w . ja v a 2s .c om*/ boolean result = cancelled.compareAndSet(false, true); if (result) { try { ChannelFuture fut = future; if (fut != null) { fut.cancel(true); } if (fut != null && fut.channel() != null && fut.channel().isOpen()) { fut.channel().close(); } } finally { if (forTimeout == null) { event(new State.Cancelled()); } } latch.countDown(); } return result; }
From source file:com.mastfrog.scamper.SctpClient.java
License:Open Source License
/** * Start the client, returning a ChannelFuture which can be waited on to * keep the client running (the returned future is the client SCTP socket's * channel's <code>closeFuture()</code> - call its <code>sync()</code> * method to block the current thread until the connection is closed. * * @return The close future for this client's connection * @throws InterruptedException if the connect process is interrupted *//*from w w w . j a v a 2 s . c o m*/ public ChannelFuture start() throws InterruptedException { // Configure the client. Bootstrap b = new Bootstrap(); configurer.init(b).handler(new LoggingHandler(LogLevel.INFO)); logger.log(Level.INFO, "Start for {0} on {1}", new Object[] { host, port }); // Start the client. ChannelFuture f = b.connect(host, port); if (logger.isLoggable(Level.FINE)) { f.addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { logger.log(Level.FINE, "Connect to {0}:{1}", new Object[] { host, port }); } }); } f.sync(); // Caller can until the connection is closed. return f.channel().closeFuture(); }
From source file:com.mastfrog.scamper.SctpServer.java
License:Open Source License
public ChannelFuture start(AtomicReference<ChannelFuture> connectFutureReceiver) throws InterruptedException { // Configure the server. ServerBootstrap b = new ServerBootstrap(); config.init(b);//from www . j a v a2 s . co m b.handler(new LoggingHandler(LogLevel.INFO)); logger.log(Level.FINE, "Start server on {0}", port); // Start the server. ChannelFuture f = b.bind(port); if (logger.isLoggable(Level.FINE)) { f.addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { logger.log(Level.FINE, "Listening for connections on {0}", port); } }); } f.sync(); logger.log(Level.FINER, "Thread proceeding", Thread.currentThread()); // For tests and things that need to delay execution until a connection // has been opened if (connectFutureReceiver != null) { connectFutureReceiver.set(f); } synchronized (this) { return future = f.channel().closeFuture(); } }