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 } }
public void send(String msg) { msgOut.println(msg); }