summaryrefslogtreecommitdiff
path: root/src/Server/ChatServer.java
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/ChatServer.java
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/ChatServer.java')
-rw-r--r--src/Server/ChatServer.java55
1 files changed, 32 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();
}