diff options
author | Dor Bivas <dor1_b@walla.com> | 2013-11-02 00:12:44 +0200 |
---|---|---|
committer | Dor Bivas <dor1_b@walla.com> | 2013-11-02 00:12:44 +0200 |
commit | b9052256e628bdc900ab39f288f3604e623ff707 (patch) | |
tree | def8b099f0a50364c351592f3f5d0d868327ff32 /src/Server/CommandsTable.java | |
parent | 3055f8949d34ddc1e32ccb6fbf9f9aedae7851c7 (diff) |
fix command handling; QUIT
* Commands lookup always "failed".
* Implement Client.disconnect
* Use it for implementing the QUIT command
Diffstat (limited to 'src/Server/CommandsTable.java')
-rw-r--r-- | src/Server/CommandsTable.java | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/src/Server/CommandsTable.java b/src/Server/CommandsTable.java new file mode 100644 index 0000000..38d1799 --- /dev/null +++ b/src/Server/CommandsTable.java @@ -0,0 +1,84 @@ +package Server;
+import java.io.IOException;
+import java.util.Hashtable;
+
+public class CommandsTable
+{
+ private Hashtable<String, Command> table;
+ public CommandsTable()
+ {
+ this.table = new Hashtable<String, Command>();
+ this.table.put("NICK", new CommandNick());
+ this.table.put("USER", new CommandUser());
+ this.table.put("QUIT", new CommandQuit());
+ }
+
+ public void runCommand (Client client , String commandName , String args)
+ {
+ //FIXME: convert to upper case
+ Command command = this.table.get(commandName);
+
+ if (command == null)
+ {
+ System.err.println("Missing command <" + commandName + ">.");
+ command = new CommandBad();
+ }
+ command.run(client, args);
+ }
+
+}
+
+
+abstract class Command
+{
+ abstract public void run(Client client, String args);
+
+ public void println(Client client, String str)
+ {
+ try {
+ client.println(":me " + str);
+ } catch (IOException e) {
+ System.err.println("Failed to print to client socket: <" + str + "> (" + e + ")");
+ }
+ }
+}
+
+class CommandNick extends Command
+{
+ public CommandNick() {}
+
+ public void run(Client client, String args)
+ {
+ client.setNick(args);
+ // FIXME: print reply
+ }
+}
+
+class CommandUser extends Command
+{
+ public void run(Client client, String args)
+ {
+ //client.setNick(args);
+ this.println(client, "001 " + client.getNick() + " :Welcome to Dor's ircd");
+ }
+}
+
+class CommandQuit extends Command
+{
+ public CommandQuit() {}
+
+ public void run(Client client, String args)
+ {
+ client.disconect();
+
+ }
+}
+
+class CommandBad extends Command
+{
+ public void run(Client client, String args)
+ {
+ this.println(client, "421 " + client.getNick() + " " + args + " Unknown command.");
+ }
+}
+
|