package mmo.server;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.util.HashedWheelTimer;
import io.netty.util.concurrent.Future;

import javax.inject.Inject;
import javax.inject.Provider;

public class Server {
    private final Provider<Handler> handlerProvider;
    private final HashedWheelTimer timer;
    private NioEventLoopGroup parentGroup;
    private NioEventLoopGroup childGroup;


    @Inject
    public Server(Provider<Handler> handlerProvider, HashedWheelTimer timer) {
        this.handlerProvider = handlerProvider;
        this.timer = timer;
    }

    public void run() {
        parentGroup = new NioEventLoopGroup();
        childGroup = new NioEventLoopGroup();
        new ServerBootstrap()
                .group(parentGroup, childGroup)
                .channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    protected void initChannel(SocketChannel ch)
                            throws Exception {
                        ch.pipeline()
                                .addLast(new HttpServerCodec())
                                .addLast(handlerProvider.get());
                    }
                })
                .option(ChannelOption.TCP_NODELAY, true)
                .childOption(ChannelOption.TCP_NODELAY, true)
                .bind(8080);
    }

    public void shutdown() throws InterruptedException {
        timer.stop();
        Future<?> parentShutdown = parentGroup.shutdownGracefully();
        Future<?> childShutdown = childGroup.shutdownGracefully();
        parentShutdown.await();
        childShutdown.await();
    }
}
@@ -596,24 +596,61 @@
 imer timer;%0A
+    private final GameLoop gameLoop;%0A
     private 
@@ -723,17 +723,16 @@
 Group;%0A%0A
-%0A
     @Inj
@@ -810,16 +810,53 @@
 er timer
+,%0A                  GameLoop gameLoop
 ) %7B%0A    
@@ -923,24 +923,58 @@
 er = timer;%0A
+        this.gameLoop = gameLoop;%0A
     %7D%0A%0A    p
@@ -1972,32 +1972,100 @@
 wnGracefully();%0A
+        Future%3C?%3E gameLoopShutdown = gameLoop.shutdownGracefully();%0A
         parentSh
@@ -2107,19 +2107,53 @@
 wn.await();%0A
+        gameLoopShutdown.await();%0A
     %7D%0A%7D

fix error where server did not shut down