Search in sources :

Example 1 with ServerTransportListener

use of io.grpc.internal.ServerTransportListener in project grpc-java by grpc.

the class NettyServerTest method getPort.

@Test
public void getPort() throws Exception {
    InetSocketAddress addr = new InetSocketAddress(0);
    NettyServer ns = new NettyServer(addr, NioServerSocketChannel.class, // no boss group
    null, // no event group
    null, new ProtocolNegotiators.PlaintextNegotiator(), // ignore
    1, // ignore
    1, // ignore
    1, // ignore
    1);
    ns.start(new ServerListener() {

        @Override
        public ServerTransportListener transportCreated(ServerTransport transport) {
            return null;
        }

        @Override
        public void serverShutdown() {
        }
    });
    // Check that we got an actual port.
    assertThat(ns.getPort()).isGreaterThan(0);
    // Cleanup
    ns.shutdown();
}
Also used : ServerTransportListener(io.grpc.internal.ServerTransportListener) ServerTransport(io.grpc.internal.ServerTransport) InetSocketAddress(java.net.InetSocketAddress) ServerListener(io.grpc.internal.ServerListener) Test(org.junit.Test)

Example 2 with ServerTransportListener

use of io.grpc.internal.ServerTransportListener in project grpc-java by grpc.

the class NettyServer method start.

@Override
public void start(ServerListener serverListener) throws IOException {
    listener = checkNotNull(serverListener, "serverListener");
    // If using the shared groups, get references to them.
    allocateSharedGroups();
    ServerBootstrap b = new ServerBootstrap();
    b.group(bossGroup, workerGroup);
    b.channel(channelType);
    if (NioServerSocketChannel.class.isAssignableFrom(channelType)) {
        b.option(SO_BACKLOG, 128);
        b.childOption(SO_KEEPALIVE, true);
    }
    b.childHandler(new ChannelInitializer<Channel>() {

        @Override
        public void initChannel(Channel ch) throws Exception {
            NettyServerTransport transport = new NettyServerTransport(ch, protocolNegotiator, maxStreamsPerConnection, flowControlWindow, maxMessageSize, maxHeaderListSize);
            ServerTransportListener transportListener;
            // This is to order callbacks on the listener, not to guard access to channel.
            synchronized (NettyServer.this) {
                if (channel != null && !channel.isOpen()) {
                    // Server already shutdown.
                    ch.close();
                    return;
                }
                // `channel` shutdown can race with `ch` initialization, so this is only safe to increment
                // inside the lock.
                eventLoopReferenceCounter.retain();
                transportListener = listener.transportCreated(transport);
            }
            transport.start(transportListener);
            ch.closeFuture().addListener(new ChannelFutureListener() {

                @Override
                public void operationComplete(ChannelFuture future) {
                    eventLoopReferenceCounter.release();
                }
            });
        }
    });
    // Bind and start to accept incoming connections.
    ChannelFuture future = b.bind(address);
    try {
        future.await();
    } catch (InterruptedException ex) {
        Thread.currentThread().interrupt();
        throw new RuntimeException("Interrupted waiting for bind");
    }
    if (!future.isSuccess()) {
        throw new IOException("Failed to bind", future.cause());
    }
    channel = future.channel();
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) ServerTransportListener(io.grpc.internal.ServerTransportListener) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) ServerChannel(io.netty.channel.ServerChannel) Channel(io.netty.channel.Channel) IOException(java.io.IOException) ChannelFutureListener(io.netty.channel.ChannelFutureListener) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) IOException(java.io.IOException)

Aggregations

ServerTransportListener (io.grpc.internal.ServerTransportListener)2 ServerListener (io.grpc.internal.ServerListener)1 ServerTransport (io.grpc.internal.ServerTransport)1 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)1 Channel (io.netty.channel.Channel)1 ChannelFuture (io.netty.channel.ChannelFuture)1 ChannelFutureListener (io.netty.channel.ChannelFutureListener)1 ServerChannel (io.netty.channel.ServerChannel)1 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)1 IOException (java.io.IOException)1 InetSocketAddress (java.net.InetSocketAddress)1 Test (org.junit.Test)1