List of usage examples for io.netty.channel ChannelFuture sync
@Override
ChannelFuture sync() throws InterruptedException;
From source file:sas.systems.imflux.session.rtsp.SimpleRtspSession.java
License:Apache License
/** * {@inheritDoc}/*from w ww.ja v a 2s . c o m*/ */ @Override public synchronized boolean init() { if (this.running.get()) { return true; } // create bootstrap Class<? extends ServerChannel> channelType; if (useNio) { this.workerGroup = new NioEventLoopGroup(); this.bossGroup = new NioEventLoopGroup(); channelType = NioServerSocketChannel.class; } else { this.workerGroup = new OioEventLoopGroup(); this.bossGroup = new OioEventLoopGroup(); channelType = OioServerSocketChannel.class; } bootstrap = new ServerBootstrap(); bootstrap.group(this.bossGroup, this.workerGroup).option(ChannelOption.SO_SNDBUF, this.sendBufferSize) .option(ChannelOption.SO_RCVBUF, this.receiveBufferSize).channel(channelType) .handler(new LoggingHandler(LogLevel.INFO)).childHandler(new ChannelInitializer<Channel>() { // is used to initialize the ChannelPipeline @Override protected void initChannel(Channel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); pipeline.addLast("encoder", new RtspEncoder()); pipeline.addLast("decoder", new RtspDecoder()); pipeline.addLast("aggregator", new HttpObjectAggregator(64 * 1024)); pipeline.addLast("handler", new RtspHandler(SimpleRtspSession.this)); } }); // create channel try { ChannelFuture future = bootstrap.bind(this.localAddress); this.channel = future.sync().channel(); // wait for future to complete and retrieve channel } catch (Exception e) { LOG.error("Failed to bind RTSP channel for session with id " + this.id, e); this.workerGroup.shutdownGracefully(); this.bossGroup.shutdownGracefully(); this.workerGroup.terminationFuture().syncUninterruptibly(); this.bossGroup.terminationFuture().syncUninterruptibly(); return false; } LOG.debug("RTSP channel bound for RtspSession with id {}.", this.id); this.running.set(true); return true; }
From source file:test.httpupload.HttpUploadClient.java
License:Apache License
/** * Standard post without multipart but already support on Factory (memory management) * * @return the list of HttpData object (attribute and file) to be reused on next post *//*from w w w . jav a 2 s .com*/ private static List<InterfaceHttpData> formpost(Bootstrap bootstrap, String host, int port, URI uriSimple, File file, HttpDataFactory factory, List<Entry<String, String>> headers) throws Exception { // XXX /formpost // Start the connection attempt. ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port)); // Wait until the connection attempt succeeds or fails. Channel channel = future.sync().channel(); // Prepare the HTTP request. HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, uriSimple.toASCIIString()); // Use the PostBody encoder HttpPostRequestEncoder bodyRequestEncoder = new HttpPostRequestEncoder(factory, request, false); // false => not multipart // it is legal to add directly header or cookie into the request until finalize for (Entry<String, String> entry : headers) { request.headers().set(entry.getKey(), entry.getValue()); } // add Form attribute bodyRequestEncoder.addBodyAttribute("getform", "POST"); bodyRequestEncoder.addBodyAttribute("info", "first value"); bodyRequestEncoder.addBodyAttribute("secondinfo", "secondvalue &"); bodyRequestEncoder.addBodyAttribute("thirdinfo", textArea); bodyRequestEncoder.addBodyAttribute("fourthinfo", textAreaLong); bodyRequestEncoder.addBodyFileUpload("myfile", file, "application/x-zip-compressed", false); // finalize request request = bodyRequestEncoder.finalizeRequest(); // Create the bodylist to be reused on the last version with Multipart support List<InterfaceHttpData> bodylist = bodyRequestEncoder.getBodyListAttributes(); // send request channel.write(request); // test if request was chunked and if so, finish the write if (bodyRequestEncoder.isChunked()) { // could do either request.isChunked() // either do it through ChunkedWriteHandler channel.write(bodyRequestEncoder).sync(); } // Do not clear here since we will reuse the InterfaceHttpData on the next request // for the example (limit action on client side). Take this as a broadcast of the same // request on both Post actions. // // On standard program, it is clearly recommended to clean all files after each request // bodyRequestEncoder.cleanFiles(); // Wait for the server to close the connection. channel.closeFuture().sync(); return bodylist; }
From source file:test.httpupload.HttpUploadClient.java
License:Apache License
/** * Multipart example/*from w w w . jav a 2 s.c o m*/ */ private static void formpostmultipart(Bootstrap bootstrap, String host, int port, URI uriFile, HttpDataFactory factory, List<Entry<String, String>> headers, List<InterfaceHttpData> bodylist) throws Exception { // XXX /formpostmultipart // Start the connection attempt. ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port)); // Wait until the connection attempt succeeds or fails. Channel channel = future.sync().channel(); // Prepare the HTTP request. HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, uriFile.toASCIIString()); // Use the PostBody encoder HttpPostRequestEncoder bodyRequestEncoder = new HttpPostRequestEncoder(factory, request, true); // true => multipart // it is legal to add directly header or cookie into the request until finalize for (Entry<String, String> entry : headers) { request.headers().set(entry.getKey(), entry.getValue()); } // add Form attribute from previous request in formpost() bodyRequestEncoder.setBodyHttpDatas(bodylist); // finalize request bodyRequestEncoder.finalizeRequest(); // send request channel.write(request); // test if request was chunked and if so, finish the write if (bodyRequestEncoder.isChunked()) { channel.write(bodyRequestEncoder).sync(); } // Now no more use of file representation (and list of HttpData) bodyRequestEncoder.cleanFiles(); // Wait for the server to close the connection. channel.closeFuture().sync(); }
From source file:uidclient.UIDClient.java
public static void main(String[] args) throws Exception { // Configure SSL. final SslContext sslCtx = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE) .build();/* w w w . j a v a 2 s .c o m*/ EventLoopGroup group = new NioEventLoopGroup(); try { Bootstrap b = new Bootstrap(); b.group(group).channel(NioSocketChannel.class).handler(new UIDClientInitializer(sslCtx)); // Start the connection attempt. Channel ch = b.connect(Config.HOST, Config.PORT).sync().channel(); // Read commands from the stdin. ChannelFuture lastWriteFuture = null; BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); for (;;) { String line = in.readLine(); if (line == null) { break; } // Sends the received line to the server. lastWriteFuture = ch.writeAndFlush(line + "\r\n"); // If user typed the 'bye' command, wait until the server closes // the connection. if ("bye".equals(line.toLowerCase())) { ch.closeFuture().sync(); break; } } // Wait until all messages are flushed before closing the channel. if (lastWriteFuture != null) { lastWriteFuture.sync(); } } finally { // The connection is closed automatically on shutdown. group.shutdownGracefully(); } }