List of usage examples for io.netty.bootstrap ChannelFactory ChannelFactory
ChannelFactory
From source file:com.linkedin.mitm.proxy.ProxyServer.java
License:Open Source License
/** * Start proxy server/*from w ww. j a va2s . c o m*/ * */ public void start() throws InterruptedException { ServerBootstrap serverBootstrap = new ServerBootstrap(); serverBootstrap.group(_acceptorGroup, _upstreamWorkerGroup); serverBootstrap.channelFactory(new ChannelFactory<ServerChannel>() { @Override public ServerChannel newChannel() { return new NioServerSocketChannel(); } }); serverBootstrap.childHandler(new ProxyInitializer(this)); //bind ChannelFuture future = serverBootstrap.bind(_host, _port); //wait for the future future.awaitUninterruptibly(); if (!future.isSuccess()) { future.channel().closeFuture().awaitUninterruptibly(); throw new ChannelException(String.format("Failed to bind to: %s:%d", _host, _port), future.cause()); } else { _allChannels.add(future.channel()); } }
From source file:com.whizzosoftware.hobson.ssdp.SSDPPlugin.java
License:Open Source License
public void createSockets() { try {// w w w .j a va2 s. c om logger.debug("Using network interface: {}; local address: {}", nic, localAddress); if (nic == null) { logger.error("Unable to determine local NIC; discovery may not work properly"); } if (nic != null) { Bootstrap clientBootstrap = new Bootstrap().group(eventLoopGroup) .channelFactory(new ChannelFactory<Channel>() { @Override public Channel newChannel() { return new NioDatagramChannel(InternetProtocolFamily.IPv4); } }).localAddress(groupAddress).option(ChannelOption.IP_MULTICAST_IF, nic) .option(ChannelOption.SO_REUSEADDR, true).handler(new SSDPInboundHandler(this)); clientBootstrap.bind().addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture channelFuture) throws Exception { multicastChannel = (NioDatagramChannel) channelFuture.channel(); multicastChannel.joinGroup(groupAddress, nic); } }); } Bootstrap serverBootstrap = new Bootstrap().group(eventLoopGroup) .channelFactory(new ChannelFactory<Channel>() { @Override public Channel newChannel() { return new NioDatagramChannel(InternetProtocolFamily.IPv4); } }).localAddress(localAddress).option(ChannelOption.IP_MULTICAST_IF, nic) .option(ChannelOption.SO_REUSEADDR, true).handler(new SSDPInboundHandler(this)); serverBootstrap.bind().addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture channelFuture) throws Exception { localChannel = (NioDatagramChannel) channelFuture.channel(); sendDiscoveryPacket(); } }); } catch (Exception e) { e.printStackTrace(); } }
From source file:io.viewserver.network.netty.ipc.NettyIpcEndpoint.java
License:Apache License
@Override public ServerBootstrap getServerBootstrap(EventLoopGroup parentGroup, EventLoopGroup childGroup, ChannelHandler handler) {/* ww w . j a v a 2 s .c o m*/ ServerBootstrap server = new ServerBootstrap() { @Override public ServerBootstrap channelFactory(ChannelFactory<? extends ServerChannel> channelFactory) { return super.channelFactory(new ChannelFactory<ServerChannel>() { @Override public ServerChannel newChannel() { try { return channelFactory.newChannel(); } catch (Throwable ex) { return null; } } }); } }; server.group(parentGroup, childGroup).channel(EpollServerDomainSocketChannel.class).childHandler(handler); try { server.bind(new DomainSocketAddress(name)); } catch (Throwable ex) { log.warn("Could not listen on IPC socket '" + name + "'"); return null; } return server; }
From source file:org.apache.hadoop.hdfs.server.datanode.web.DatanodeHttpServer.java
License:Apache License
public DatanodeHttpServer(final Configuration conf, final InetSocketAddress jettyAddr, final ServerSocketChannel externalHttpChannel) throws IOException { this.conf = conf; this.confForCreate = new Configuration(conf); confForCreate.set(FsPermission.UMASK_LABEL, "000"); this.bossGroup = new NioEventLoopGroup(); this.workerGroup = new NioEventLoopGroup(); this.externalHttpChannel = externalHttpChannel; HttpConfig.Policy policy = DFSUtil.getHttpPolicy(conf); if (policy.isHttpEnabled()) { this.httpServer = new ServerBootstrap().group(bossGroup, workerGroup) .childHandler(new ChannelInitializer<SocketChannel>() { @Override/*from w ww. j a v a2 s.c o m*/ protected void initChannel(SocketChannel ch) throws Exception { ChannelPipeline p = ch.pipeline(); p.addLast(new HttpRequestDecoder(), new HttpResponseEncoder(), new ChunkedWriteHandler(), new URLDispatcher(jettyAddr, conf, confForCreate)); } }); if (externalHttpChannel == null) { httpServer.channel(NioServerSocketChannel.class); } else { httpServer.channelFactory(new ChannelFactory<NioServerSocketChannel>() { @Override public NioServerSocketChannel newChannel() { return new NioServerSocketChannel(externalHttpChannel) { // The channel has been bounded externally via JSVC, // thus bind() becomes a no-op. @Override protected void doBind(SocketAddress localAddress) throws Exception { } }; } }); } } else { this.httpServer = null; } if (policy.isHttpsEnabled()) { this.sslFactory = new SSLFactory(SSLFactory.Mode.SERVER, conf); try { sslFactory.init(); } catch (GeneralSecurityException e) { throw new IOException(e); } this.httpsServer = new ServerBootstrap().group(bossGroup, workerGroup) .channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ChannelPipeline p = ch.pipeline(); p.addLast(new SslHandler(sslFactory.createSSLEngine()), new HttpRequestDecoder(), new HttpResponseEncoder(), new ChunkedWriteHandler(), new URLDispatcher(jettyAddr, conf, confForCreate)); } }); } else { this.httpsServer = null; this.sslFactory = null; } }
From source file:picoview.collectd.CollectClient.java
License:Apache License
@Override public void initialize() throws RuntimeException { NetworkInterface nic;// w w w . jav a 2 s .c o m try { if (StringUtils.isBlank(nicName)) { nic = NetworkInterface.getByIndex(0); } else { nic = NetworkInterface.getByName(nicName); } } catch (SocketException exep) { throw new RuntimeException("unable to determine network interface to use", exep); } Bootstrap bs = new Bootstrap(); bs.option(ChannelOption.SO_BROADCAST, true); bs.option(ChannelOption.SO_REUSEADDR, true); bs.option(ChannelOption.IP_MULTICAST_LOOP_DISABLED, false); bs.option(ChannelOption.SO_RCVBUF, 2048); bs.option(ChannelOption.IP_MULTICAST_TTL, 255); bs.group(new NioEventLoopGroup()); bs.channelFactory(new ChannelFactory<Channel>() { public Channel newChannel() { return new NioDatagramChannel(InternetProtocolFamily.IPv4); } }); bs.handler(new ChannelInitializer<DatagramChannel>() { @Override public void initChannel(DatagramChannel channel) throws Exception { channel.pipeline().addLast(new CollectChannelHandler()); } }); if (StringUtils.isBlank(multicastHost)) { multicastHost = "239.192.74.66"; } if (multicastPort <= 0) { multicastPort = 25826; } try { DatagramChannel dch = (DatagramChannel) bs.bind(multicastPort).sync().channel(); ChannelFuture cf = dch.joinGroup(new InetSocketAddress(multicastHost, multicastPort), nic).sync(); if (!cf.isSuccess()) { throw new RuntimeException("unable to join multicast group"); } } catch (InterruptedException exep) { throw new RuntimeException("unable to setup network for collect client", exep); } }
From source file:reactor.io.net.impl.netty.udp.NettyDatagramServer.java
License:Apache License
public NettyDatagramServer(@Nonnull Environment env, @Nonnull Dispatcher dispatcher, @Nullable InetSocketAddress listenAddress, @Nullable final NetworkInterface multicastInterface, @Nonnull final ServerSocketOptions options, @Nullable Codec<Buffer, IN, OUT> codec) { super(env, dispatcher, listenAddress, multicastInterface, options, codec); if (options instanceof NettyServerSocketOptions) { this.nettyOptions = (NettyServerSocketOptions) options; } else {// w w w . jav a 2 s. c om this.nettyOptions = null; } if (null != nettyOptions && null != nettyOptions.eventLoopGroup()) { this.ioGroup = nettyOptions.eventLoopGroup(); } else { int ioThreadCount = getDefaultEnvironment().getIntProperty("reactor.udp.ioThreadCount", Environment.PROCESSORS); this.ioGroup = new NioEventLoopGroup(ioThreadCount, new NamedDaemonThreadFactory("reactor-udp-io")); } final InternetProtocolFamily family = toNettyFamily(options.protocolFamily()); this.bootstrap = new Bootstrap().group(ioGroup).option(ChannelOption.SO_RCVBUF, options.rcvbuf()) .option(ChannelOption.SO_SNDBUF, options.sndbuf()) .option(ChannelOption.SO_REUSEADDR, options.reuseAddr()).option(ChannelOption.AUTO_READ, false) .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, options.timeout()) .channelFactory(new ChannelFactory<Channel>() { @Override public Channel newChannel() { return new NioDatagramChannel(family); } }); if (null != listenAddress) { bootstrap.localAddress(listenAddress); } else { bootstrap.localAddress(NetUtil.LOCALHOST, 3000); } if (null != multicastInterface) { bootstrap.option(ChannelOption.IP_MULTICAST_IF, multicastInterface); } }
From source file:reactor.io.net.netty.udp.NettyDatagramServer.java
License:Apache License
public NettyDatagramServer(@Nonnull Environment env, @Nonnull EventBus reactor, @Nullable InetSocketAddress listenAddress, @Nullable final NetworkInterface multicastInterface, @Nonnull final ServerSocketOptions options, @Nullable Codec<Buffer, IN, OUT> codec, Collection<Consumer<NetChannel<IN, OUT>>> consumers) { super(env, reactor, listenAddress, multicastInterface, options, codec, consumers); if (options instanceof NettyServerSocketOptions) { this.nettyOptions = (NettyServerSocketOptions) options; } else {/*from w w w . j av a 2 s . co m*/ this.nettyOptions = null; } if (null != nettyOptions && null != nettyOptions.eventLoopGroup()) { this.ioGroup = nettyOptions.eventLoopGroup(); } else { int ioThreadCount = env.getProperty("reactor.udp.ioThreadCount", Integer.class, Environment.PROCESSORS); this.ioGroup = new NioEventLoopGroup(ioThreadCount, new NamedDaemonThreadFactory("reactor-udp-io")); } final NettyNetChannelInboundHandler inboundHandler = new NettyNetChannelInboundHandler() { @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { super.channelRead(ctx, ((DatagramPacket) msg).content()); } }; this.bootstrap = new Bootstrap().group(ioGroup).option(ChannelOption.SO_RCVBUF, options.rcvbuf()) .option(ChannelOption.SO_SNDBUF, options.sndbuf()) .option(ChannelOption.SO_REUSEADDR, options.reuseAddr()) .channelFactory(new ChannelFactory<Channel>() { @Override public Channel newChannel() { final NioDatagramChannel ch = new NioDatagramChannel(); DatagramChannelConfig config = ch.config(); config.setReceiveBufferSize(options.rcvbuf()); config.setSendBufferSize(options.sndbuf()); config.setReuseAddress(options.reuseAddr()); if (null != multicastInterface) { config.setNetworkInterface(multicastInterface); } if (null != nettyOptions && null != nettyOptions.pipelineConfigurer()) { nettyOptions.pipelineConfigurer().accept(ch.pipeline()); } ch.closeFuture().addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { if (log.isInfoEnabled()) { log.info("CLOSE {}", ch); } close(ch); } }); netChannel = (NettyNetChannel<IN, OUT>) select(ch); inboundHandler.setNetChannel(netChannel); ch.pipeline().addLast(new ChannelOutboundHandlerAdapter() { @Override public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception { super.write(ctx, msg, promise); } }); return ch; } }).handler(inboundHandler); if (null != listenAddress) { bootstrap.localAddress(listenAddress); } else { bootstrap.localAddress(NetUtil.LOCALHOST, 3000); } if (null != multicastInterface) { bootstrap.option(ChannelOption.IP_MULTICAST_IF, multicastInterface); } }