import java.io.BufferedReader; import java.io.IOException; import java.io.PrintWriter; public class ClientListenerThread extends Thread { private final MainServerThread owner; private final BufferedReader from_client; final PrintWriter to_client; private static int instances = 0; private int number = ++instances; private final SimpleLogger logger = new SimpleLogger("ConnectionWaiterThread " + number); private String user_name = null; private boolean quit = false; public ClientListenerThread(MainServerThread owner, BufferedReader from_client, PrintWriter to_client) { this.owner = owner; this.from_client = from_client; this.to_client = to_client; } public void run() { while (! quit) { String line_from_client; try { line_from_client = from_client.readLine(); logger.log("Från klienten: " + line_from_client); if (line_from_client == null || line_from_client.equals("LOGOUT")) { owner.client_has_disconneted(this); quit = true; } else if (line_from_client.equals("SHUTDOWN")) { System.exit(0); // quit = true; } else if (line_from_client.startsWith("LOGIN ")) { user_name = line_from_client.substring(6); } else { owner.received_from_client(this, line_from_client); } } catch (IOException e) { logger.log("Tappat kontakten med klienten."); e.printStackTrace(); quit = true; } } // while owner.client_has_disconneted(this); } // run public String getUserName() { if (user_name == null) return "Namnlös chattare nummer " + number; else return user_name; } public void send(String line) { to_client.println(line); } } // class ClientListenerThread