/** * ForumConnectionHandler.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 ForumConnectionHandler extends Thread { // 1.0 requests static final int LOAD_ALL_THREADS = 1; static final int LOAD_THREAD_ARTICLES = 2; static final int POST_ARTICLE = 3; // new requests for 1.1 static final int LOAD_THREAD_SUBJECTS = 4; static final int LOAD_SUBJECT_ARTICLES = 5; static final int LOAD_USER_ARTICLES = 6; long id; Socket client; Vector basicThreads; InputStream in; OutputStream out; DataInputStream dIn; DataOutputStream dOut; public ForumConnectionHandler (long i, Socket c, ThreadGroup h, Vector bt) { super (h, "Forum Connection Handler 1.1 " + i); id = i; client = c; basicThreads = bt; } public void run() { try { in = new BufferedInputStream (client.getInputStream()); out = new BufferedOutputStream (client.getOutputStream()); dIn = new DataInputStream (in); dOut = new DataOutputStream (out); ForumCommInterface comm = new ForumConnectionHandlerComm (); String t, type = ""; int request = -1; Vector threadArts; request = dIn.readInt(); switch (request) { // 1.0 requests case LOAD_ALL_THREADS: Hashtable threads = comm.loadAllThreads(); threadSetup (threads, basicThreads); Enumeration en = threads.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 = comm.loadThreadArticles (t); 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: t = dIn.readUTF(); String art = dIn.readUTF(); comm.postArticle (art, t); type = "POST_ARTICLE for thread " + t; break; // stubs for 1.1 requests case LOAD_THREAD_SUBJECTS: // Vector subjects = comm.loadThreadSubjects (String t); break; case LOAD_SUBJECT_ARTICLES: // Vector sa = loadSubjectArticles (String t, String s); break; case LOAD_USER_ARTICLES: // Vector ua = loadUserArticles (String t, String u); break; default: type = "unknown request: " + request; } // end switch System.out.println ("#" + id + ": " + type + " from " + client.getInetAddress()); } catch (Exception ex) { ex.printStackTrace (); } finally { try { client.close(); } catch (IOException ex) { System.out.println ("Socket close for connection #" + id + " failed."); } } } void threadSetup (Hashtable h, Vector t) { // make sure that the basic threads are in place Enumeration en = t.elements (); while (en.hasMoreElements ()) { String k = (String) en.nextElement (); if (!h.containsKey (k)) h.put (k, new Vector ()); } } }