import java.io.*; import java.net.*; import java.util.*; import java.text.*; public class ThreadModule { //Display a message, preceded by the name of the current thread static DateFormat dateFormLong = new SimpleDateFormat("M-d-yyyy"); static DateFormat dateFormShort = new SimpleDateFormat("[k:m:s]"); static PrintStream logFile = null; static ServerSocket ssock = null; static maxSlots = 5; static void log(String message) { message = "-"+Thread.currentThread().getName()+": "+message; System.out.println(message); logFile.println(message); } private static class Server implements Runnable { public void run() { Socket localsock = null; PrintWriter localwriter = null; BufferedReader localreader = null; initializeSockets(localsock, localwriter, localreader); int slotCount = 0; } } private static class Client implements Runnable { public void run() { //where the user enters what he wants to be done while(true)log("Client"); } } public static void main(String args[]) throws Exception { Date date = new Date(); String dateLong = dateFormLong.format(date); File logDir = new File("logs/"+dateLong); if(!(new File("logs")).exists()) (new File("logs")).mkdir(); if(!logDir.exists()) logDir.mkdir(); logFile = new PrintStream(new FileOutputStream("./logs/"+dateLong+"/"+InetAddress.getLocalHost().getHostName()+dateFormShort.format(date))); //Delay, in milliseconds before we interrupt MessageLoop //thread (default one hour). long patience = 1000 * 60 * 60; //If command line argument present, gives patience in seconds. log("Starting Peer Server"); long startTime = System.currentTimeMillis(); /* Thread client = new Thread(new Client()); Thread server = new Thread(new Server()); client.start(); server.start();*/ Thread serverThread = new Thread(new Server()); serverThread.start(); // Thread[] taskSlots = new Thread[maxTasks]; // for(int n = 0 ; n < maxTasks; n++){ // taskSlots[n]=new Thread(new Server()); // taskSlots[n].start(); // } /* threadMessage("Waiting for MessageLoop thread to finish"); //loop until MessageLoop thread exits while (threadsAlive(client,server)) { threadMessage("Still waiting..."); //Wait maximum of 1 second for MessageLoop thread to //finish. client.join(1000); server.join(1000); if (((System.currentTimeMillis() - startTime) > patience) && threadsAlive(client,server)) { threadMessage("Tired of waiting!"); client.interrupt(); server.interupt(); //Shouldn't be long now -- wait indefinitely client.join(); server.join(); } } threadMessage("Finally!"); } private static boolean threadsDone(Thread a, Thread b) { return a.isAlive() || b.isAlive();*/ } static void initializeSockets(Socket sock, PrintWriter writer, BufferedReader reader) { try { if (ssock != null) ssock.close(); if (reader != null) reader.close(); if (writer != null) writer.close(); ssock = new ServerSocket(1337); ssock.setSoTimeout(1000); log("Initializing sockets: before accepts"); sock = ssock.accept(); log("Initializing sockets: after accepts"); reader = new BufferedReader( new InputStreamReader(sock.getInputStream())); writer = new PrintWriter(sock.getOutputStream(), true); } catch (Exception e) { System.err.println("Initialize Socket failed: " + e.getMessage()); } } }