public static void main(String[] args) throws Exception { // parse arguments if (args.length != 3 || !args[1].equals("-config")) { usage(); } // Determine type Type type = null; String software; if (args[0].equals("-w")) { type = Type.WELCOME; software = "TAISHI/WelcomeThreadServer"; } else if (args[0].equals("-b")) { type = Type.BUSY; software = "TAISHI/BusyWaitServer"; } else if (args[0].equals("-s")) { type = Type.SUSPENSION; software = "TAISHI/SuspensionServer"; } else { software = ""; usage(); } // Reading configuration file String config_file = args[2]; ReadConfig parse = new ReadConfig(config_file, software); HashMap<String, String> config_map = parse.getConfig(); Debug.debug(config_map.toString()); // check if config_map is valid // necessary to check the thread pool size is set if (config_map == null || !config_map.containsKey("ThreadPoolSize")) { throw new Exception("Incomplete configuration"); } // create cache Debug.debug("Create cache with size: " + config_map.get("CacheSize")); ThreadSafeCache cache = new ThreadSafeCache(Integer.parseInt(config_map.get("CacheSize"))); // create server ThreadPoolServer server = new ThreadPoolServer(type, config_map, cache); server.run(); }
public ThreadPoolServer(Type type, HashMap<String, String> config_map, ThreadSafeCache cache) throws Exception { try { this.concurrent_type = type; this.config_map = config_map; this.cache = cache; // making welcome socket int port = Integer.parseInt(config_map.get("port")); welcomeSocket = new ServerSocket(port); this.thread_count = Integer.parseInt(config_map.get("ThreadPoolSize")); System.out.println( "Starting the server with thread pool size of " + thread_count + " and listening at: " + welcomeSocket); } catch (Exception e) { Debug.debug(e.getMessage()); throw new Exception("ThreadPool Server initial construction failed."); } }
// Run using thread pool competing on welcome socket private void run_welcome(WServiceThread[] threads) throws InterruptedException { for (int i = 0; i < threads.length; i++) { threads[i].join(); } Debug.debug("All threads finished. Exit"); }