package Server; import java.net.*; import java.io.BufferedWriter; import java.io.DataInputStream; import java.io.IOException; import java.io.OutputStreamWriter; class ParsedLine { String line; String command; String args; public ParsedLine(String line) { this.line = line; String[] parsedLine = this.line.split("[ \t]+", 2); if (parsedLine.length > 1) this.args = parsedLine[1]; else this.args = ""; if (parsedLine.length > 0) this.command = parsedLine[0]; else this.command = ""; } public String getCommand() { return this.command; } public String getArgs() { return this.args; } } public class Connection extends Thread { private Socket soc; public Connection(Socket soc) { this.soc = soc; } @SuppressWarnings("deprecation") public void run() { try { Client client; CommandsTable commandsTable = new CommandsTable(); DataInputStream input = new DataInputStream(soc.getInputStream()); OutputStreamWriter outTemp = new OutputStreamWriter(soc.getOutputStream()); BufferedWriter output = new BufferedWriter(outTemp); client = new Client(soc, output); while (true) { ParsedLine parsedLine; String line; try { line = input.readLine(); // FIXME: more then 1 line } catch (java.net.SocketException e) { break; // connection was lost / disconnected } if (line == null) { System.err.println("Got null line"); soc.close(); break; } parsedLine = new ParsedLine(line); String s = soc.getPort() + ": <" + line + ">"; System.out.println(s); s = soc.getPort() + ": <" + line + ">, command: <" + parsedLine.getCommand() + ">, args: <" + parsedLine.getArgs() + ">."; commandsTable.runCommand(client, parsedLine.getCommand(), parsedLine.getArgs()); System.out.println(s); // System.out.println("Done"); } } catch (IOException e) { if (soc != null) try { soc.close(); } catch (IOException e1) { e1.printStackTrace(); } e.printStackTrace(); } } }