List of usage examples for io.netty.channel ChannelFuture sync
@Override
ChannelFuture sync() throws InterruptedException;
From source file:org.acmsl.katas.antlr4netty.InterpreterServer.java
License:Open Source License
/** * Runs the interpreter from the command line. * @param args the arguments.// w w w.j a va 2 s . c o m */ public static void main(@NotNull final String[] args) { final int port = Integer.parseInt(args[0]); @NotNull ChannelFuture future = new InterpreterServer().listen(port); try { future.channel().closeFuture().sync(); future.sync(); System.out.println("Finishing..."); } catch (@NotNull final InterruptedException interrupted) { System.err.println("Error: " + interrupted.getMessage()); } }
From source file:org.acmsl.queryj.debugging.netty.NettyServerDebuggingService.java
License:Open Source License
/** * {@inheritDoc}/*from www .ja v a2 s .com*/ */ @Override @NotNull public TemplateDebuggingCommand debugTemplate(@NotNull final ST template, @NotNull final C context, @NotNull final String output) throws DevelopmentModeException { try { final ChannelFuture future = launchServer(); future.sync(); } catch (@NotNull final InterruptedException | IOException interruption) { throw new DevelopmentModeException(template.groupThatCreatedThisInstance); } return this.m__Command; }
From source file:org.betawares.jorre.Client.java
License:Open Source License
/** * Connect to a {@link Server} using the specified {@link Connection} settings. This call * will block until successful or an exception is thrown. * /*from w w w.ja v a 2 s . c o m*/ * @param connection a {@link Connection} instance specifying the connection settings * @return true if the connection was successful, false otherwise */ public boolean connect(Connection connection) { clientMessageHandler = new ClientMessageHandler(this, connection.getMaxResponseAge()); group = new NioEventLoopGroup(); try { if (connection.isSSL()) { sslCtx = SslContextBuilder.forClient().build(); } else { sslCtx = null; } Bootstrap bootstrap = new Bootstrap(); bootstrap.group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { if (sslCtx != null) { ch.pipeline() .addLast(sslCtx.newHandler(ch.alloc(), connection.getHost(), connection.getPort())); } ch.pipeline().addLast(new ObjectDecoder(10 * 1024 * 1024, ClassResolvers.cacheDisabled(null))); ch.pipeline().addLast(new ObjectEncoder()); // ch.pipeline().addLast("messageInspector", new ClientMessageInspector()); ch.pipeline().addLast("idleStateHandler", new IdleStateHandler(connection.getIdleTimeout(), connection.getIdlePingTime(), 0, TimeUnit.MILLISECONDS)); ch.pipeline().addLast("heartbeatHandler", new ClientHeartbeatHandler(Client.this)); ch.pipeline().addLast("pingMessageHandler", new PingMessageHandler()); ch.pipeline().addLast("pongMessageHandler", new PongMessageHandler()); ch.pipeline().addLast("clientMessageHandler", clientMessageHandler); ch.pipeline().addLast("exceptionHandler", new ChannelHandlerAdapter() { @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { logger.error("Communications error", cause); Client.this.disconnect(DisconnectReason.IOError, true); } }); } }); bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, connection.getConnectionTimeout()); ChannelFuture future = bootstrap.connect(connection.getHost(), connection.getPort()); channel = future.sync().channel(); return true; } catch (SSLException | InterruptedException ex) { logger.fatal("Error connecting", ex); disconnect(DisconnectReason.IOError, true); } return false; }
From source file:org.elasticsearch.http.netty4.Netty4HttpClient.java
License:Apache License
private synchronized Collection<FullHttpResponse> sendRequests(final SocketAddress remoteAddress, final Collection<HttpRequest> requests) throws InterruptedException { final CountDownLatch latch = new CountDownLatch(requests.size()); final Collection<FullHttpResponse> content = Collections.synchronizedList(new ArrayList<>(requests.size())); clientBootstrap.handler(new CountDownLatchHandler(latch, content)); ChannelFuture channelFuture = null; try {/*w ww .j a va 2 s .c o m*/ channelFuture = clientBootstrap.connect(remoteAddress); channelFuture.sync(); for (HttpRequest request : requests) { channelFuture.channel().writeAndFlush(request); } latch.await(10, TimeUnit.SECONDS); } finally { if (channelFuture != null) { channelFuture.channel().close().sync(); } } return content; }
From source file:org.elasticsearch.http.nio.Netty4HttpClient.java
License:Apache License
private synchronized Collection<FullHttpResponse> sendRequests(final SocketAddress remoteAddress, final Collection<HttpRequest> requests) throws InterruptedException { final CountDownLatch latch = new CountDownLatch(requests.size()); final Collection<FullHttpResponse> content = Collections.synchronizedList(new ArrayList<>(requests.size())); clientBootstrap.handler(new CountDownLatchHandler(latch, content)); ChannelFuture channelFuture = null; try {// w w w.j av a 2 s .c om channelFuture = clientBootstrap.connect(remoteAddress); channelFuture.sync(); for (HttpRequest request : requests) { channelFuture.channel().writeAndFlush(request); } latch.await(30, TimeUnit.SECONDS); } finally { if (channelFuture != null) { channelFuture.channel().close().sync(); } } return content; }
From source file:org.jboss.aerogear.simplepush.server.netty.NettySimplePushSockJSServerTest.java
License:Apache License
@Test public void withoutTLS() throws Exception { final URI uri = new URI("ws://127.0.0.1:" + port + "/simplepush/websocket"); final EventLoopGroup group = new NioEventLoopGroup(); try {//from ww w . ja v a 2 s .c o m final Bootstrap b = new Bootstrap(); final HttpHeaders customHeaders = new DefaultHttpHeaders(); final WebSocketClientHandler handler = new WebSocketClientHandler(WebSocketClientHandshakerFactory .newHandshaker(uri, WebSocketVersion.V13, null, false, customHeaders)); b.group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); pipeline.addLast("http-codec", new HttpClientCodec()); pipeline.addLast("aggregator", new HttpObjectAggregator(8192)); pipeline.addLast("ws-handler", handler); } }); final Channel ch = b.connect(uri.getHost(), uri.getPort()).sync().channel(); handler.handshakeFuture().sync(); final String uaid = UUIDUtil.newUAID(); final String json = JsonUtil.toJson(new HelloMessageImpl(uaid.toString())); final ChannelFuture future = ch.writeAndFlush(new TextWebSocketFrame(json)); future.sync(); final TextWebSocketFrame textFrame = handler.getTextFrame(); final HelloResponse fromJson = JsonUtil.fromJson(textFrame.text(), HelloResponseImpl.class); assertThat(fromJson.getMessageType(), equalTo(MessageType.Type.HELLO)); assertThat(fromJson.getUAID(), equalTo(uaid)); textFrame.release(); final String channelId = UUID.randomUUID().toString(); final String register = JsonUtil.toJson(new RegisterMessageImpl(channelId)); final ChannelFuture registerFuture = ch.writeAndFlush(new TextWebSocketFrame(register)); registerFuture.sync(); final TextWebSocketFrame registerFrame = handler.getTextFrame(); final RegisterResponseImpl registerResponse = JsonUtil.fromJson(registerFrame.text(), RegisterResponseImpl.class); assertThat(registerResponse.getMessageType(), equalTo(MessageType.Type.REGISTER)); assertThat(registerResponse.getChannelId(), equalTo(channelId)); ch.writeAndFlush(new CloseWebSocketFrame()); ch.closeFuture().sync(); } finally { group.shutdownGracefully(); } }
From source file:org.jboss.aerogear.simplepush.server.netty.NettySimplePushSockJSServerTest.java
License:Apache License
@Test public void userAgentReaper() throws Exception { final URI uri = new URI("ws://127.0.0.1:" + port + "/simplepush/websocket"); final EventLoopGroup group = new NioEventLoopGroup(); try {/* ww w .ja v a2 s . c o m*/ final Bootstrap b = new Bootstrap(); final HttpHeaders customHeaders = new DefaultHttpHeaders(); final WebSocketClientHandler handler = new WebSocketClientHandler(WebSocketClientHandshakerFactory .newHandshaker(uri, WebSocketVersion.V13, null, false, customHeaders)); b.group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); pipeline.addLast("http-codec", new HttpClientCodec()); pipeline.addLast("aggregator", new HttpObjectAggregator(8192)); pipeline.addLast("ws-handler", handler); } }); final Channel ch = b.connect(uri.getHost(), uri.getPort()).sync().channel(); handler.handshakeFuture().sync(); final String uaid = UUIDUtil.newUAID(); final String json = JsonUtil.toJson(new HelloMessageImpl(uaid.toString())); final ChannelFuture future = ch.writeAndFlush(new TextWebSocketFrame(json)); future.sync(); final TextWebSocketFrame textFrame = handler.getTextFrame(); final HelloResponse fromJson = JsonUtil.fromJson(textFrame.text(), HelloResponseImpl.class); assertThat(fromJson.getMessageType(), equalTo(MessageType.Type.HELLO)); assertThat(fromJson.getUAID(), equalTo(uaid)); textFrame.release(); Thread.sleep(3000); final String channelId = UUID.randomUUID().toString(); final String register = JsonUtil.toJson(new RegisterMessageImpl(channelId)); final ChannelFuture registerFuture = ch.writeAndFlush(new TextWebSocketFrame(register)); registerFuture.sync(); ch.writeAndFlush(new CloseWebSocketFrame()); ch.closeFuture().sync(); } finally { group.shutdownGracefully(); } }
From source file:org.jboss.aerogear.webpush.WebPushClient.java
License:Apache License
private void writeRequest(final Http2Headers headers) throws Exception { handler.outbound(headers);//from w ww. j a va 2 s . com ChannelFuture requestFuture = channel.writeAndFlush(new WebPushMessage(headers)).sync(); requestFuture.sync(); }
From source file:org.jboss.aerogear.webpush.WebPushClient.java
License:Apache License
private void writeRequest(final Http2Headers headers, final ByteBuf payload) throws Exception { handler.outbound(headers);/*from w w w. j a va2 s . co m*/ ChannelFuture requestFuture = channel.writeAndFlush(new WebPushMessage(headers, payload)).sync(); requestFuture.sync(); }
From source file:org.jmqtt.broker.acceptor.NettyAcceptor.java
License:Open Source License
private void initFactory(String host, int port, final PipelineInitializer pipeliner) { ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .childHandler(new ChannelInitializer<SocketChannel>() { @Override/*from w w w.j a v a 2 s . c o m*/ public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); try { pipeliner.init(pipeline); } catch (Throwable th) { LOG.error("Severe error during pipeline creation", th); throw th; } } }).option(ChannelOption.SO_BACKLOG, 128).option(ChannelOption.SO_REUSEADDR, true) .option(ChannelOption.TCP_NODELAY, true).childOption(ChannelOption.SO_KEEPALIVE, true); try { // Bind and start to accept incoming connections. ChannelFuture f = b.bind(host, port); LOG.info("Server binded host: {}, port: {}", host, port); f.sync(); } catch (InterruptedException ex) { LOG.error(null, ex); } }