/** * ForumServer.java 1.1 5.1.97 Michael Shoffner * * Copyright (c) 1997 Michael Shoffner. All Rights Reserved. * * Permission to use, copy, modify, and distribute this software * for non-commercial purposes and without fee is hereby granted * provided that this copyright notice appears in all copies. * * http://prominence.com/ shoffner@prominence.com */ import java.net.*; import java.util.*; import java.io.*; public class ForumServer extends Thread { static final String DEFAULT_FORUM_CONFIGFILE = "forum.cfg"; static final String CONFIGFILE_THREADS = "basicThreads"; // port for server to listen for Forum clients static final int FORUM_PORT = 5000; String databaseName; long connections = 0; Vector threads, acl; ThreadGroup handlers; ServerSocket listener; String argv[]; public ForumServer (String argv[]) { handlers = new ThreadGroup ("Forum Connection Handlers"); this.argv = argv; threads = new Vector (); } public void run() { try { setup (argv); listener = new ServerSocket (FORUM_PORT); while (true) { Socket c = listener.accept(); if (aclCheck (c)) { ForumConnectionHandler h = new ForumConnectionHandler (++ connections, c, handlers, threads); h.start(); } else { // could send client a "denied" message here InetAddress denied = c.getInetAddress(); System.out.println ("Denied connection from: " + denied + ".\n"); try { c.close(); } catch (IOException ex) { System.out.println ("Denied close failed: " + ex + ".\n"); } } } } catch (IOException ex) { // this is to ignore bug when closing down ServerSocket if (!(ex instanceof SocketException)) ex.printStackTrace(); } finally { try { if (listener != null) listener.close(); } catch (IOException ex) { System.out.println ("ServerSocket close failed.\n"); } handlers.stop(); System.out.println ("The server has shut down."); } } void setup (String argv[]) throws IOException { // read from config file String configFileName = DEFAULT_FORUM_CONFIGFILE; if (argv.length > 0) configFileName = argv[0]; File configFile = new File (configFileName); if (! configFile.exists()) { throw new IOException ("Config file " + configFileName + " not found.\nSpecify a an alternate config file or provide a file named " + DEFAULT_FORUM_CONFIGFILE + ".\n"); } Properties config = getConfig (configFile); // load database name and driver name could go here // load threads from config file String t = config.getProperty (CONFIGFILE_THREADS); StringTokenizer strtok = new StringTokenizer (t, ","); while (strtok.hasMoreElements ()) threads.addElement (strtok.nextElement ()); // load in memoryLimit, // load in inbound connection ACL here // print config to console System.out.println ("FORUM SERVER CONFIGURATION\n"); System.out.println ("Discussion threads:\n"); Enumeration en = threads.elements (); while (en.hasMoreElements()) System.out.println (en.nextElement()); System.out.println (""); // System.out.println ("IP restrictions: "); System.out.println ("Client connection listing:\n"); } Properties getConfig (File configFile) throws IOException { Properties props = new Properties(); FileInputStream f = new FileInputStream (configFile); DataInputStream dIn = null; try { dIn = new DataInputStream (f); String line; int lineNo = 0; // readLine () is deprecated while ((line = dIn.readLine()) != null) { ++ lineNo; line = line.trim(); if ((line.length() > 0) && (!line.startsWith ("#"))) { int idx = line.indexOf ("="); if (idx >= 0) props.put (line.substring (0, idx).trim(), line.substring (idx + 1).trim()); else throw new IOException ("line " + lineNo + " not understood: " + line + "\n"); } } } catch (IOException ex) { throw new IOException ("Error reading " + configFile + ": " + ex.getMessage()); } finally { if (dIn != null) dIn.close(); } return props; } boolean aclCheck (Socket c) { // unimplemented return true; } public static void main (String argv[]) throws IOException { ForumServer accept = new ForumServer (argv); accept.start(); } }