/** * This method waits for input from the console. Once it is received, it sends it to the client's * message handler. */ public void accept() { try { BufferedReader fromConsole = new BufferedReader(new InputStreamReader(System.in)); String message; while (true) { message = fromConsole.readLine(); if (client == null) { client = new ChatClient(this); } client.handleMessageFromClientUI(message); } } catch (Exception ex) { System.out.println("Unexpected error while reading from console!"); } }
public void run() { try { msgOut = new PrintWriter(socket.getOutputStream(), true); msgIn = new BufferedReader(new InputStreamReader(socket.getInputStream())); } catch (Exception e) { e.printStackTrace(); } try { String input; String output; TextTransferProtocol ttp = new TextTransferProtocol(this); // set up ttp parsey thing String first = msgIn.readLine(); if (first.startsWith(ChatServer.REQ)) { if (first.indexOf("/message") > -1) { isPing = true; msgOut.println(ChatServer.getMotd()); // ChatServer.pushToChat("ping"); } else if (first.indexOf("/login") > -1) { // ChatServer.pushToChat("login"); if (name == null) { name = first.substring(8, first.length()); // ChatServer.pushToChat("setting name to " + name); // msgOut.println("setting name to " + name); if (ChatServer.users.contains(name)) { ChatServer.pushToChat( Colors.RED + "User " + name + " already exists, changing to " + name + "_" + ChatServer.users.size() + 1); name = name + "_" + ChatServer.users.size(); } ChatServer.addUser(name); ChatServer.restoreClientRank(name); ChatServer.updateUsers(); } ChatServer.sendAll(Colors.YELLOW + name + " has joined the server"); ChatServer.sendOne(clientID, ChatServer.getLoginMessage()); while ((input = msgIn.readLine()) != null) { // continue doing that until ends] // ChatServer.pushToChat(input); output = ttp.processIn(input); if (output != null) ChatServer.sendAll(getRankColor() + name + Colors.WHITE + ": " + output); } } } else { msgOut.println("400: PLEASE START ANY CONNECTION WITH " + ChatServer.REQ + "/fn"); } socket.close(); // ChatServer.pushToChat("closed socket"); } catch (Exception e) { e.printStackTrace(msgOut); if (!isPing) { leave(); p.nl("Lost connection to user: "******"[WARNING]" + Colors.WHITE + " Lost connection to user: "******"got pinged by: " + socket); ChatServer.pushToChat(Colors.YELLOW + "[INFO]" + Colors.WHITE + " Pinged by: " + socket); } // ChatServer.updateUsers(); // ChatServer.removeClient(ChatServer.getClientID(name)); //figure out what to do if the // client exits without doing /logout } }