Java Code Examples for io.netty.bootstrap.ServerBootstrap#localAddress()

The following examples show how to use io.netty.bootstrap.ServerBootstrap#localAddress() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NettyNetworking.java    From Cleanstone with MIT License 6 votes vote down vote up
@Override
public void start() {
    bossGroup = epoll ? new EpollEventLoopGroup() : new NioEventLoopGroup();
    workerGroup = epoll ? new EpollEventLoopGroup() : new NioEventLoopGroup();
    ServerBootstrap bootstrap = new ServerBootstrap();
    bootstrap.group(bossGroup, workerGroup)
            .channel(epoll ? EpollServerSocketChannel.class : NioServerSocketChannel.class)
            .childHandler(new ServerChannelInitializer(this))
            .option(ChannelOption.SO_BACKLOG, socketBacklog)
            .childOption(ChannelOption.SO_KEEPALIVE, socketKeepAlive);
    bootstrap.localAddress(this.getAddress(), this.getPort());
    bootstrap.bind().addListener(future -> {
        if (future.isSuccess()) {
            log.info(CleanstoneServer.getMessage("net.netty.bind-successful",
                    protocol.getClass().getSimpleName(), getAddress(), getPort() + ""));
        } else {
            log.error(CleanstoneServer.getMessage("net.netty.bind-failure",
                    getAddress().getHostAddress(), getPort() + ""), future.cause());
        }
    });
    running = true;
}
 
Example 2
Source File: AbstractSocketTest.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Override
protected void configure(ServerBootstrap bootstrap, Bootstrap bootstrap2, ByteBufAllocator allocator) {
    bootstrap.localAddress(newSocketAddress());
    bootstrap.option(ChannelOption.ALLOCATOR, allocator);
    bootstrap.childOption(ChannelOption.ALLOCATOR, allocator);
    bootstrap2.option(ChannelOption.ALLOCATOR, allocator);
}
 
Example 3
Source File: AbstractSctpTest.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Override
protected void configure(ServerBootstrap serverBootstrap, Bootstrap bootstrap, ByteBufAllocator allocator) {
    serverBootstrap.localAddress(new InetSocketAddress(NetUtil.LOCALHOST, 0));
    serverBootstrap.option(ChannelOption.ALLOCATOR, allocator);
    serverBootstrap.childOption(ChannelOption.ALLOCATOR, allocator);
    bootstrap.option(ChannelOption.ALLOCATOR, allocator);
}
 
Example 4
Source File: EpollReuseAddrTest.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
private static ServerBootstrap createServerBootstrap() {
    ServerBootstrap bootstrap = new ServerBootstrap();
    bootstrap.group(EpollSocketTestPermutation.EPOLL_BOSS_GROUP, EpollSocketTestPermutation.EPOLL_WORKER_GROUP);
    bootstrap.channel(EpollServerSocketChannel.class);
    bootstrap.childHandler(new DummyHandler());
    InetSocketAddress address = new InetSocketAddress(NetUtil.LOCALHOST, 0);
    bootstrap.localAddress(address);
    return bootstrap;
}
 
Example 5
Source File: DefaultServer.java    From sailfish with Apache License 2.0 5 votes vote down vote up
public void start() throws SailfishException {
	ServerBootstrap boot = newServerBootstrap();
	EventLoopGroup accept = NettyPlatformIndependent.newEventLoopGroup(1,
			new DefaultThreadFactory(RemotingConstants.SERVER_ACCEPT_THREADNAME));
	if (null != config.getEventLoopGroup()) {
		boot.group(accept, config.getEventLoopGroup());
	} else {
		boot.group(accept, ServerEventGroup.INSTANCE.getLoopGroup());
	}
	final EventExecutorGroup executor = (null != config.getEventExecutorGroup() ? config.getEventExecutorGroup()
			: ServerEventGroup.INSTANCE.getExecutorGroup());
	boot.localAddress(config.address().host(), config.address().port());
	boot.childHandler(new ChannelInitializer<SocketChannel>() {
		@Override
		protected void initChannel(SocketChannel ch) throws Exception {
			ChannelPipeline pipeline = ch.pipeline();
			ch.attr(ChannelAttrKeys.OneTime.idleTimeout).set(config.idleTimeout());
			ch.attr(ChannelAttrKeys.maxIdleTimeout).set(config.maxIdleTimeout());
			ch.attr(ChannelAttrKeys.exchangeServer).set(DefaultServer.this);
			pipeline.addLast(executor, 
					RemotingEncoder.INSTANCE, 
					new RemotingDecoder(), 
					new IdleStateHandler(config.idleTimeout(), 0, 0), 
					HeartbeatChannelHandler.INSTANCE,
					NegotiateChannelHandler.INSTANCE,
					ConcreteRequestHandler.INSTANCE);
		}
	});
	try {
		channel = boot.bind().syncUninterruptibly().channel();
	} catch (Throwable cause) {
		throw new SailfishException(cause);
	}
}
 
Example 6
Source File: AbstractServerSocketTest.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
@Override
protected void configure(ServerBootstrap bootstrap, ByteBufAllocator allocator) {
    addr = newSocketAddress();
    bootstrap.localAddress(addr);
    bootstrap.option(ChannelOption.ALLOCATOR, allocator);
    bootstrap.childOption(ChannelOption.ALLOCATOR, allocator);
}
 
Example 7
Source File: AbstractSocketTest.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
@Override
protected void configure(ServerBootstrap bootstrap, Bootstrap bootstrap2, ByteBufAllocator allocator) {
    addr = newSocketAddress();
    bootstrap.localAddress(addr);
    bootstrap.option(ChannelOption.ALLOCATOR, allocator);
    bootstrap.childOption(ChannelOption.ALLOCATOR, allocator);
    bootstrap2.remoteAddress(addr);
    bootstrap2.option(ChannelOption.ALLOCATOR, allocator);
}
 
Example 8
Source File: AbstractSctpTest.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
@Override
protected void configure(ServerBootstrap serverBootstrap, Bootstrap bootstrap, ByteBufAllocator allocator) {
    addr = new InetSocketAddress(NetUtil.LOCALHOST, TestUtils.getFreePort());
    serverBootstrap.localAddress(addr);
    serverBootstrap.option(ChannelOption.ALLOCATOR, allocator);
    serverBootstrap.childOption(ChannelOption.ALLOCATOR, allocator);
    bootstrap.remoteAddress(addr);
    bootstrap.option(ChannelOption.ALLOCATOR, allocator);
}
 
Example 9
Source File: EpollReuseAddrTest.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
private static ServerBootstrap createServerBootstrap() {
    ServerBootstrap bootstrap = new ServerBootstrap();
    bootstrap.group(EpollSocketTestPermutation.EPOLL_BOSS_GROUP, EpollSocketTestPermutation.EPOLL_WORKER_GROUP);
    bootstrap.channel(EpollServerSocketChannel.class);
    bootstrap.childHandler(new DummyHandler());
    InetSocketAddress address = new InetSocketAddress(NetUtil.LOCALHOST, TestUtils.getFreePort());
    bootstrap.localAddress(address);
    return bootstrap;
}
 
Example 10
Source File: AbstractServerSocketTest.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
@Override
protected void configure(ServerBootstrap bootstrap, ByteBufAllocator allocator) {
    bootstrap.localAddress(newSocketAddress());
    bootstrap.option(ChannelOption.ALLOCATOR, allocator);
    bootstrap.childOption(ChannelOption.ALLOCATOR, allocator);
}
 
Example 11
Source File: SubServer.java    From consulo with Apache License 2.0 4 votes vote down vote up
public boolean bind(int port) {
  if (port == server.getPort() || port == -1) {
    return true;
  }

  if (channelRegistrar == null) {
    Disposer.register(server, this);
    channelRegistrar = new ChannelRegistrar();
  }

  ServerBootstrap bootstrap = serverBootstrap(server.getEventLoopGroup());
  Map<String, Object> xmlRpcHandlers = user.createXmlRpcHandlers();
  if (xmlRpcHandlers == null) {
    BuiltInServer.configureChildHandler(bootstrap, channelRegistrar, null);
  }
  else {
    final XmlRpcDelegatingHttpRequestHandler handler = new XmlRpcDelegatingHttpRequestHandler(xmlRpcHandlers);
    bootstrap.childHandler(new ChannelInitializer() {
      @Override
      protected void initChannel(Channel channel) throws Exception {
        channel.pipeline().addLast(channelRegistrar);
        NettyUtil.addHttpServerCodec(channel.pipeline());
        channel.pipeline().addLast(handler);
      }
    });
  }

  try {
    bootstrap.localAddress(user.isAvailableExternally() ? new InetSocketAddress(port) : NetUtils.loopbackSocketAddress(port));
    channelRegistrar.setServerChannel(bootstrap.bind().syncUninterruptibly().channel(), false);
    return true;
  }
  catch (Exception e) {
    try {
      NettyUtil.log(e, Logger.getInstance(BuiltInServer.class));
    }
    finally {
      user.cannotBind(e, port);
    }
    return false;
  }
}