summaryrefslogtreecommitdiff
path: root/src/Server
diff options
context:
space:
mode:
authorDor Bivas <dor1_b@walla.com>2013-11-02 17:58:09 +0200
committerDor Bivas <dor1_b@walla.com>2013-11-02 17:58:09 +0200
commitbb802317abad8c55f205a2649194d3c0fc9b0da8 (patch)
tree123424065ccd5e405214ddba3bd31bd5698699b3 /src/Server
parent46c9497652bc2852b4a9bc632358e20bcae7a139 (diff)
support multiple connections and JOIN/PRIVMSG
* A separate thread per connection. * Basic handling of JOIN and PRIVMSG. Now simple IRC clients can talk to us.
Diffstat (limited to 'src/Server')
-rw-r--r--src/Server/ChatServer.java36
-rw-r--r--src/Server/Client.java14
-rw-r--r--src/Server/CommandsTable.java51
3 files changed, 66 insertions, 35 deletions
diff --git a/src/Server/ChatServer.java b/src/Server/ChatServer.java
index b475500..a3d300a 100644
--- a/src/Server/ChatServer.java
+++ b/src/Server/ChatServer.java
@@ -1,5 +1,4 @@
package Server;
-import java.util.*;
import java.net.*;
import java.io.*;
@@ -8,20 +7,10 @@ public class ChatServer
//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 line;
ServerSocket Service;
- CommandsTable commandsTable = new CommandsTable();
-
+
try
{
Service = new ServerSocket(portNum);
@@ -34,32 +23,15 @@ public class ChatServer
Socket soc = null;
try
{
- 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)
{
- 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");
+ soc = Service.accept();
+ Connection con = new Connection(soc);
+ con.start();
}
} catch (IOException e)
{
- if (soc != null)
- soc.close();
System.out.println(e);
- Service.close();
}
}
diff --git a/src/Server/Client.java b/src/Server/Client.java
index f8cc503..6b1bc3a 100644
--- a/src/Server/Client.java
+++ b/src/Server/Client.java
@@ -6,8 +6,9 @@ import java.net.Socket;
public class Client
{
String nick;
+ String username;
Socket soc;
- BufferedWriter output;
+ BufferedWriter output;;
public Client(Socket soc, BufferedWriter output)
{
@@ -24,6 +25,16 @@ public class Client
{
this.nick = newNick;
}
+
+ public String getUsername()
+ {
+ return this.username;
+ }
+
+ public void setUsername(String newUsername)
+ {
+ this.username = newUsername;
+ }
public void disconect ()
@@ -44,5 +55,6 @@ public class Client
output.write(line, 0, line.length());
output.newLine();
output.flush();
+ System.out.println(this.soc.getPort() + ": " + line);
}
}
diff --git a/src/Server/CommandsTable.java b/src/Server/CommandsTable.java
index 38d1799..93949a8 100644
--- a/src/Server/CommandsTable.java
+++ b/src/Server/CommandsTable.java
@@ -11,6 +11,8 @@ public class CommandsTable
this.table.put("NICK", new CommandNick());
this.table.put("USER", new CommandUser());
this.table.put("QUIT", new CommandQuit());
+ this.table.put("JOIN", new CommandJoin());
+ this.table.put("PRIVMSG", new CommandPrivmsg());
}
public void runCommand (Client client , String commandName , String args)
@@ -32,7 +34,7 @@ public class CommandsTable
abstract class Command
{
abstract public void run(Client client, String args);
-
+
public void println(Client client, String str)
{
try {
@@ -41,23 +43,68 @@ abstract class Command
System.err.println("Failed to print to client socket: <" + str + "> (" + e + ")");
}
}
+
+ public void printUser(Client client, String str)
+ {
+ try {
+ client.println(":" + client.getNick() + "!bla@me " + str);
+ } catch (IOException e) {
+ System.err.println("Failed to print to client socket: <" + str + "> (" + e + ")");
+ }
+ }
+
+ public static String RemoveFirst(String st)
+ {
+ return st.substring(1 , st.length());
+ }
}
+
+
+class CommandPrivmsg extends Command
+{
+ public CommandPrivmsg(){}
+
+ public void run(Client client, String args)
+ {
+ String [] starr = args.split("[ \t]+" , 2);
+ String line = RemoveFirst(starr[1]);
+ }
+}
+
+
class CommandNick extends Command
{
public CommandNick() {}
public void run(Client client, String args)
{
- client.setNick(args);
+ String nick = RemoveFirst(args);
+ client.setNick(nick);
// FIXME: print reply
}
}
+class CommandJoin extends Command
+{
+ public CommandJoin() {}
+
+ public void run(Client client, String args)
+ {
+ // FIXME: parse args to channel names and save client state
+ // but right now everybody is on a single channel
+ this.printUser(client, "JOIN " + ":" + args);
+ this.println(client, "332 " + client.getNick() + " " + args + " :Welcome to the single channel");
+ //this.println(client, "333 " + client.getNick()) + args + "someone!somewhere";
+ }
+}
+
class CommandUser extends Command
{
public void run(Client client, String args)
{
+ String[] argsArray = args.split("[ \t]+");
+ client.setUsername(argsArray[0]);
//client.setNick(args);
this.println(client, "001 " + client.getNick() + " :Welcome to Dor's ircd");
}