diff options
author | Dor Bivas <dor1_b@walla.com> | 2014-03-29 17:15:26 +0300 |
---|---|---|
committer | Dor Bivas <dor1_b@walla.com> | 2014-03-29 17:15:26 +0300 |
commit | 3e6edd995e1e705f6470f77bfe07c914511e2f49 (patch) | |
tree | c05477ec7e2dabaffdf6ad123fbff5e77d66119d /src/Server | |
parent | d008f730a318bc1daf603ac6520293c808a054b2 (diff) |
Chat: deleted white spaces prettify the code, and fixm's and unneeded
notes and tasks.
Diffstat (limited to 'src/Server')
-rw-r--r-- | src/Server/ChatServer.java | 4 | ||||
-rw-r--r-- | src/Server/Client.java | 3 | ||||
-rw-r--r-- | src/Server/CommandsTable.java | 30 | ||||
-rw-r--r-- | src/Server/Connection.java | 45 |
4 files changed, 49 insertions, 33 deletions
diff --git a/src/Server/ChatServer.java b/src/Server/ChatServer.java index 363a747..6d58c49 100644 --- a/src/Server/ChatServer.java +++ b/src/Server/ChatServer.java @@ -14,16 +14,20 @@ public class ChatServer public static final int portNum = 6667; public static ChatServer server; + /** removing connection from the iterator. + * @param connection */ public void connectionRemove(Connection con) { this.connections.remove(con); } + /** returning ConnectionIterator. */ public Iterator<Connection> getConnectionIterator() { return this.connections.iterator(); } + /** constructor of the server , building socket , connection list and nicks dictionary.*/ ChatServer() throws IOException { this.service = new ServerSocket(portNum); this.connections = new LinkedList<Connection>(); diff --git a/src/Server/Client.java b/src/Server/Client.java index 4ab36dd..35cfefc 100644 --- a/src/Server/Client.java +++ b/src/Server/Client.java @@ -10,7 +10,8 @@ public class Client private Socket soc; private BufferedWriter output; private Connection con; - + + /** constructor*/ public Client(Socket soc, BufferedWriter output, Connection con) { this.soc = soc; diff --git a/src/Server/CommandsTable.java b/src/Server/CommandsTable.java index 65bbe03..966b67f 100644 --- a/src/Server/CommandsTable.java +++ b/src/Server/CommandsTable.java @@ -17,7 +17,7 @@ public class CommandsTable this.table.put("PING", new CommandPing()); this.table.put("PRIVMSG", new CommandPrivmsg()); this.table.put("MODE", new CommandIgnored()); - } + } public void runCommand (Client client , String commandName , String args) { @@ -32,11 +32,14 @@ public class CommandsTable } } - +/** parent class for all command handling classes.*/ abstract class Command { abstract public void run(Client client, String args); + /** print line to the client + * @param client destination client + * @param str the line */ public void println(Client client, String str) { try { @@ -46,17 +49,22 @@ abstract class Command } } - + /** send message to a client + * @param client + * @param str message */ public void printUser(Client client, String str) { printUser (client ,client, str); } - + /** print to client a message from sender. + * @param client destination client + * @param sender client + * @param str message */ public void printUser(Client client,Client sender, String str) { try { - client.println(":" + sender .getNick() + "!" + client.getUsername() + "@" + client.getHostname() + " " + str); + client.println(":" + sender.getNick() + "!" + client.getUsername() + "@" + client.getHostname() + " " + str); } catch (IOException e) { System.err.println("Failed to print to client socket: <" + str + "> (" + e + ")"); } @@ -69,7 +77,8 @@ abstract class Command } } - +/** sending message from a client to all other clients. + * @param client, arguments */ class CommandPrivmsg extends Command { public CommandPrivmsg(){} @@ -108,10 +117,7 @@ class CommandNick extends Command if (!client.setNick(nick)) { this.println(client, "433 * " + nick + " :Nick alredy in use."); - } - - - + } } } @@ -163,9 +169,7 @@ class CommandBad extends Command /** A command to ignore */ class CommandIgnored extends Command { - public void run(Client client, String args) - { - } + public void run(Client client, String args) {} } /** Pinging each few seconds to keep the connection alive*/ 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) { |