package com.amebame.triton.server;

import java.net.InetSocketAddress;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.ChannelPipelineFactory;

import com.beust.jcommander.JCommander;
import com.beust.jcommander.Parameter;
import com.google.inject.Guice;
import com.google.inject.Injector;

public class TritonServer {
	
	private static final Logger log = LogManager.getLogger(TritonServer.class);
	
	@Parameter(names = {"-p","--port"}, description="server port")
	private int port = 4848;
	
	@Parameter(names = {"-c","--config"}, description="config path")
	private String configPath = null;
	
	// Guice injector
	private Injector injector;
	
	public TritonServer() {
	}
	
	public void setPort(int port) {
		this.port = port;
	}
	
	public void setConfigPath(String configPath) {
		this.configPath = configPath;
	}
	
	public void start() {
		log.info("starting triton server");
		try {
			injector = Guice.createInjector(new TritonModule(configPath));
			ServerBootstrap bootstrap = injector.getInstance(ServerBootstrap.class);
			ChannelPipelineFactory pipelineFactory = injector.getInstance(TritonServerPipelineFactory.class);
			bootstrap.setPipelineFactory(pipelineFactory);
			bootstrap.bind(new InetSocketAddress(port));
			log.info("triton server started on port {}", port);
		} catch (Exception e) {
			log.error("failed to start the server {}", e.getMessage(), e);
		}
	}
	
	public void stop() {
		log.info("stopping triton server");
		if (injector == null) {
			return;
		}
		ServerBootstrap bootstrap = injector.getInstance(ServerBootstrap.class);
		if (bootstrap != null) {
			bootstrap.shutdown();
		}
	}
	
	public static void main(String[] args) {
		
		TritonServer server = new TritonServer();
		new JCommander(server, args);
		server.start();
		Runtime.getRuntime().addShutdownHook(new ShutdownHook(server));
	}
	
	private static class ShutdownHook extends Thread {
		private TritonServer server;
		public ShutdownHook(TritonServer server) {
			setName("shutdown-hook");
			this.server = server;
		}
		@Override
		public void run() {
			// shutdown with cleaner
			TritonServerCleaner cleaner = server.injector.getInstance(TritonServerCleaner.class);
			cleaner.clean();
			// stop the server
			server.stop();
		}
	}

}

@@ -706,16 +706,117 @@
 null;%0A%09%0A
+%09@Parameter(names = %7B%22-h%22,%22--help%22%7D, description=%22print this help%22)%0A%09private boolean help = false;%0A%09%0A
 %09// Guic
@@ -1923,16 +1923,33 @@
 er();%0A%09%09
+JCommander cmd = 
 new JCom
@@ -1970,16 +1970,68 @@
  args);%0A
+%09%09if (server.help) %7B%0A%09%09%09cmd.usage();%0A%09%09%09return;%0A%09%09%7D%0A
 %09%09server

Enable help out with --h argument