summaryrefslogtreecommitdiff
path: root/src/Server
diff options
context:
space:
mode:
authorDor Bivas <dor1_b@walla.com>2013-11-02 00:12:44 +0200
committerDor Bivas <dor1_b@walla.com>2013-11-02 00:12:44 +0200
commitb9052256e628bdc900ab39f288f3604e623ff707 (patch)
treedef8b099f0a50364c351592f3f5d0d868327ff32 /src/Server
parent3055f8949d34ddc1e32ccb6fbf9f9aedae7851c7 (diff)
fix command handling; QUIT
* Commands lookup always "failed". * Implement Client.disconnect * Use it for implementing the QUIT command
Diffstat (limited to 'src/Server')
-rw-r--r--src/Server/ChatServer.java55
-rw-r--r--src/Server/Client.java48
-rw-r--r--src/Server/CommandsTable.java84
3 files changed, 164 insertions, 23 deletions
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<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.");
+ }
+}
+