/** * ForumConnectionHandler.java 1.00 1.15.97 Michael Shoffner * * Copyright (c) 1997 Prominence Dot Com, Inc. 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 ForumConnectionHandler extends Thread { // possible client requests static final int LOAD_ALL_THREADS = 1; static final int LOAD_THREAD_ARTICLES = 2; static final int POST_ARTICLE = 3; long id; Socket client; long memoryLimit; Hashtable articles; InputStream in; OutputStream out; DataInputStream dIn; DataOutputStream dOut; public ForumConnectionHandler (long i, Socket c, long m, Hashtable ar, ThreadGroup h) { super (h, "Forum Connection Handler " + i); id = i; client = c; memoryLimit = m; articles = ar; } public void run() { try { in = new BufferedInputStream (client.getInputStream()); out = new BufferedOutputStream (client.getOutputStream()); dIn = new DataInputStream (in); dOut = new DataOutputStream (out); String t, type = ""; int request = -1; Vector threadArts; request = dIn.readInt(); switch (request) { case LOAD_ALL_THREADS: Enumeration en = articles.keys(); while (en.hasMoreElements()) dOut.writeUTF ((String) en.nextElement()); dOut.writeUTF(""); dOut.flush(); type = "LOAD_ALL_THREADS"; break; case LOAD_THREAD_ARTICLES: t = dIn.readUTF(); threadArts = (Vector) articles.get (t); if (threadArts != null && threadArts.size() > 0) { Enumeration en2 = threadArts.elements(); while (en2.hasMoreElements()) dOut.writeUTF ((String) en2.nextElement()); } dOut.writeUTF (""); dOut.flush(); type = "LOAD_THREAD_ARTICLES for thread " + t; break; case POST_ARTICLE: // ignores posts to threads that don't exist t = dIn.readUTF(); String art = dIn.readUTF(); threadArts = (Vector) articles.get (t); if (threadArts != null && memoryLimitCheck (art)) threadArts.addElement (art); type = "POST_ARTICLE for thread " + t; break; default: type = "unknown request: " + request; } // end switch System.out.println ("#" + id + ": " + type + " from " + client.getInetAddress()); } catch (IOException ex) { System.out.println ("Client request processing failed for connection #" + id + "."); } try { client.close(); } catch (IOException ex) { System.out.println ("Socket close for connection #" + id + " failed."); } } static boolean memoryLimitCheck (String art) { // checks attempted post to see if it will exceed memory return true; } }