From b9052256e628bdc900ab39f288f3604e623ff707 Mon Sep 17 00:00:00 2001 From: Dor Bivas Date: Sat, 2 Nov 2013 00:12:44 +0200 Subject: fix command handling; QUIT * Commands lookup always "failed". * Implement Client.disconnect * Use it for implementing the QUIT command --- src/Server/ChatServer.java | 55 ++++++++++++++++------------ src/Server/Client.java | 48 +++++++++++++++++++++++++ src/Server/CommandsTable.java | 84 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 164 insertions(+), 23 deletions(-) create mode 100644 src/Server/Client.java create mode 100644 src/Server/CommandsTable.java diff --git a/src/Server/ChatServer.java b/src/Server/ChatServer.java index 6269550..b475500 100644 --- a/src/Server/ChatServer.java +++ b/src/Server/ChatServer.java @@ -5,14 +5,22 @@ import java.io.*; public class ChatServer { - public static Scanner reader = new Scanner(System.in); - - public static int portNum = 1001; + //private static Scanner reader = new Scanner(System.in); + public static final int portNum = 6667; + + private static String[] parseLine(String line) + { + String[] parsedLine = line.split("[ \t]+", 2); + return parsedLine; + + } + @SuppressWarnings("deprecation") public static void main(String[] args) throws IOException { - String phrase; + String line; ServerSocket Service; + CommandsTable commandsTable = new CommandsTable(); try { @@ -22,33 +30,34 @@ public class ChatServer System.err.println("Failed listening on port " + portNum + " (" + e + ")"); return; } - - - Socket Soc = null; + + Socket soc = null; try { - Soc = Service.accept(); + Client client; + + soc = Service.accept(); + DataInputStream input = new DataInputStream(soc.getInputStream()); + OutputStreamWriter outTemp = new OutputStreamWriter(soc.getOutputStream()); + BufferedWriter output = new BufferedWriter(outTemp); + client = new Client(soc, output); + while (true) { - DataInputStream input = new DataInputStream( Soc.getInputStream() ); - //InputStreamReader inputTemp = new InputStreamReader(Soc.getInputStream()); - //BufferedReader input = new BufferedReader(inputTemp); - phrase = input.readLine(); // FIXME: more then 1 line - //System.out.println("Acc"); - OutputStreamWriter outTemp = new OutputStreamWriter(Soc.getOutputStream()); - BufferedWriter output = new BufferedWriter(outTemp); - phrase = "<" + phrase + ">"; - System.out.println(phrase); - output.write(phrase, 0, phrase.length()); - output.newLine(); - output.flush(); + String[] parsedLine; + + line = input.readLine(); // FIXME: more then 1 line + parsedLine = parseLine(line); + commandsTable.runCommand(client, parsedLine[0], parsedLine[1]); + line = "<" + line + ">, command: <" + parsedLine[0] + ">, args: <" + parsedLine[1] + ">."; + System.out.println(line); - //System.out.println("Done"); + // System.out.println("Done"); } } catch (IOException e) { - if (Soc != null) - Soc.close(); + if (soc != null) + soc.close(); System.out.println(e); Service.close(); } diff --git a/src/Server/Client.java b/src/Server/Client.java new file mode 100644 index 0000000..f8cc503 --- /dev/null +++ b/src/Server/Client.java @@ -0,0 +1,48 @@ +package Server; +import java.io.BufferedWriter; +import java.io.IOException; +import java.net.Socket; + +public class Client +{ + String nick; + Socket soc; + BufferedWriter output; + + public Client(Socket soc, BufferedWriter output) + { + this.soc = soc; + this.output = output; + } + + public String getNick() + { + return this.nick; + } + + public void setNick(String newNick) + { + this.nick = newNick; + } + + + public void disconect () + { + try { + this.soc.close(); + } + catch (IOException e) + { + System.err.println("The socket failed to closed: " + e); + } + + } + + + public void println(String line) throws IOException + { + output.write(line, 0, line.length()); + output.newLine(); + output.flush(); + } +} 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 table; + public CommandsTable() + { + this.table = new Hashtable(); + 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."); + } +} + -- cgit v1.2.3