package com.manning.nettyinaction.chapter12;
import com.manning.nettyinaction.util.BogusSslContextFactory;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import javax.net.ssl.SSLContext;
import java.net.InetSocketAddress;
/**
* @author <a href="mailto:nmaurer@redhat.com">Norman Maurer</a>
*/
public class SpdyServer {
private final NioEventLoopGroup group = new NioEventLoopGroup();
private final SSLContext context;
private Channel channel;
public SpdyServer(SSLContext context) {
this.context = context;
}
public ChannelFuture start(InetSocketAddress address) {
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(group)
.channel(NioServerSocketChannel.class)
.childHandler(new SpdyChannelInitializer(context));
ChannelFuture future = bootstrap.bind(address);
future.syncUninterruptibly();
channel = future.channel();
return future;
}
public void destroy() {
if (channel != null) {
channel.close();
}
group.shutdownGracefully();
}
public static void main(String[] args) {
if (args.length != 1) {
System.err.println("Please give port as argument");
System.exit(1);
}
int port = Integer.parseInt(args[0]);
SSLContext context = BogusSslContextFactory.getServerContext();
final SpdyServer endpoint = new SpdyServer(context);
ChannelFuture future = endpoint.start(new InetSocketAddress(port));
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
endpoint.destroy();
}
});
future.channel().closeFuture().syncUninterruptibly();
}
}