diff options
Diffstat (limited to 'src/Server/Connection.java')
-rw-r--r-- | src/Server/Connection.java | 45 |
1 files changed, 26 insertions, 19 deletions
diff --git a/src/Server/Connection.java b/src/Server/Connection.java index bcc3ac7..c5c06cf 100644 --- a/src/Server/Connection.java +++ b/src/Server/Connection.java @@ -3,7 +3,6 @@ package Server; import java.net.*; import java.io.BufferedReader; import java.io.BufferedWriter; -import java.io.DataInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; @@ -13,7 +12,9 @@ class ParsedLine private String line; private String command; private String args; - + + /**splits the line by white spaces, to fetch the command and arguments + * @param line */ public ParsedLine(String line) { this.line = line; @@ -22,18 +23,20 @@ class ParsedLine this.args = parsedLine[1]; else this.args = ""; - + if (parsedLine.length > 0) this.command = parsedLine[0]; else this.command = ""; } - + + /** returning the command. */ public String getCommand() { return this.command; } - + + /** returning arguments */ public String getArgs() { return this.args; @@ -45,43 +48,48 @@ public class Connection extends Thread private Socket soc; private Client client; + /** constructor */ public Connection(Socket soc) { this.soc = soc; } - + + /** returning the client. */ public Client getClient() { return this.client; } - - public void shutdown() + + /** removing the connection from the list and the nick from the dictionary */ + public void shutdown() { - + ChatServer.server.connectionRemove(this); ChatServer.server.removeNick(client.getNick()); try { if (!soc.isClosed()) soc.close(); - - } catch (IOException e) {} + + } catch (IOException e) {} } - - public String toString () + + /** pretty print (the port) */ + public String toString() { return "[" + soc.getPort() + "]"; } + /** main loop of connection */ @SuppressWarnings("deprecation") public void run() { try { CommandsTable commandsTable = new CommandsTable(); - InputStreamReader InTemp = new InputStreamReader(soc.getInputStream() ); + InputStreamReader InTemp = new InputStreamReader(soc.getInputStream()); BufferedReader input = new BufferedReader(InTemp); - + OutputStreamWriter outTemp = new OutputStreamWriter(soc.getOutputStream()); BufferedWriter output = new BufferedWriter(outTemp); client = new Client(soc, output, this); @@ -93,10 +101,11 @@ public class Connection extends Thread try { - line = input.readLine(); // FIXME: more then 1 line + line = input.readLine(); } catch (java.net.SocketException e) { - break; // connection was lost / disconnected + shutdown(); + break; } if (line == null) { @@ -107,11 +116,9 @@ public class Connection extends Thread 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) { |