diff options
author | Dor Bivas <dor1_b@walla.com> | 2013-11-06 00:03:28 +0200 |
---|---|---|
committer | Dor Bivas <dor1_b@walla.com> | 2013-11-06 00:03:28 +0200 |
commit | 1a71609cda1be1e7e1deb4e3adf247f82cc8d258 (patch) | |
tree | e2ed2fc486438cccfa4653bb5add20009be225f9 /src/Server | |
parent | bb802317abad8c55f205a2649194d3c0fc9b0da8 (diff) |
Connection.java was not included
Diffstat (limited to 'src/Server')
-rw-r--r-- | src/Server/Connection.java | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/src/Server/Connection.java b/src/Server/Connection.java new file mode 100644 index 0000000..d232616 --- /dev/null +++ b/src/Server/Connection.java @@ -0,0 +1,73 @@ +package Server; + +import java.net.*; +import java.io.BufferedWriter; +import java.io.DataInputStream; +import java.io.IOException; +import java.io.OutputStreamWriter; + +public class Connection extends Thread +{ + private Socket soc; + + public Connection (Socket soc) + { + + this.soc = soc; + } + + + private static String[] parseLine(String line) + { + String[] parsedLine = line.split("[ \t]+", 2); + return parsedLine; + + } + + @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) + { + String[] 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 = parseLine(line); + commandsTable.runCommand(client, parsedLine[0], parsedLine[1]); + line = soc.getPort() + ": <" + line + ">, command: <" + parsedLine[0] + ">, args: <" + parsedLine[1] + ">."; + System.out.println(line); + + // System.out.println("Done"); + } + } catch (IOException e) + { + if (soc != null) + try { + soc.close(); + } catch (IOException e1) { + e1.printStackTrace(); + } + e.printStackTrace(); + } + } +} |