diff options
Diffstat (limited to 'src/Server')
-rw-r--r-- | src/Server/ChatServer.java | 92 | ||||
-rw-r--r-- | src/Server/Client.java | 132 | ||||
-rw-r--r-- | src/Server/CommandsTable.java | 294 |
3 files changed, 259 insertions, 259 deletions
diff --git a/src/Server/ChatServer.java b/src/Server/ChatServer.java index e2d98f4..f9a233f 100644 --- a/src/Server/ChatServer.java +++ b/src/Server/ChatServer.java @@ -1,46 +1,46 @@ -package Server;
-import java.net.*;
-import java.io.*;
-import java.util.LinkedList;
-
-public class ChatServer
-{
- private ServerSocket service;
- private LinkedList<Connection> connections;
-
- public static final int portNum = 6667;
-
- ChatServer() throws IOException {
- this.service = new ServerSocket(portNum);
- this.connections = new LinkedList<Connection>();
- }
- /** Listening to port and opening a socket */
- public static void main(String[] args) throws IOException
- {
- ChatServer server;
- try
- {
- server = new ChatServer();
- } catch (IOException e)
- {
- System.err.println("Failed listening on port " + portNum + " (" + e + ")");
- return;
- }
-
- Socket soc = null;
- try
- {
- while (true)
- {
- soc = server.service.accept();
- Connection con = new Connection(soc);
- server.connections.add(con);
- con.start();
- }
- } catch (IOException e)
- {
- System.out.println(e);
- }
-
- }
-}
+package Server; +import java.net.*; +import java.io.*; +import java.util.LinkedList; + +public class ChatServer +{ + private ServerSocket service; + private LinkedList<Connection> connections; + + public static final int portNum = 6667; + + ChatServer() throws IOException { + this.service = new ServerSocket(portNum); + this.connections = new LinkedList<Connection>(); + } + /** Listening to port and opening a socket */ + public static void main(String[] args) throws IOException + { + ChatServer server; + try + { + server = new ChatServer(); + } catch (IOException e) + { + System.err.println("Failed listening on port " + portNum + " (" + e + ")"); + return; + } + + Socket soc = null; + try + { + while (true) + { + soc = server.service.accept(); + Connection con = new Connection(soc); + server.connections.add(con); + con.start(); + } + } catch (IOException e) + { + System.out.println(e); + } + + } +} diff --git a/src/Server/Client.java b/src/Server/Client.java index 34941cb..7538882 100644 --- a/src/Server/Client.java +++ b/src/Server/Client.java @@ -1,66 +1,66 @@ -package Server;
-import java.io.BufferedWriter;
-import java.io.IOException;
-import java.net.Socket;
-
-public class Client
-{
- String nick;
- String username;
- Socket soc;
- BufferedWriter output;;
-
- public Client(Socket soc, BufferedWriter output)
- {
- this.soc = soc;
- this.output = output;
- }
- /** returning the nick name */
- public String getNick()
- {
- return this.nick;
- }
- /** changing the nick name */
- public void setNick(String newNick)
- {
- this.nick = newNick;
- }
- /** returning the host name */
- public String getHostname()
- {
- return "me";
- }
- /** returning the user name */
- public String getUsername()
- {
- return this.username;
- }
- /** changing the user name */
- public void setUsername(String newUsername)
- {
- this.username = newUsername;
- }
-
- /** closing the socket */
- public void disconect ()
- {
- try {
- this.soc.close();
- }
- catch (IOException e)
- {
- System.err.println("The socket failed to closed: " + e);
- }
-
- }
-
- /** prints a line to the client socket
- * @param line the text to print */
- public void println(String line) throws IOException
- {
- output.write(line, 0, line.length());
- output.newLine();
- output.flush();
- System.out.println(this.soc.getPort() + ": " + line);
- }
-}
+package Server; +import java.io.BufferedWriter; +import java.io.IOException; +import java.net.Socket; + +public class Client +{ + String nick; + String username; + Socket soc; + BufferedWriter output;; + + public Client(Socket soc, BufferedWriter output) + { + this.soc = soc; + this.output = output; + } + /** returning the nick name */ + public String getNick() + { + return this.nick; + } + /** changing the nick name */ + public void setNick(String newNick) + { + this.nick = newNick; + } + /** returning the host name */ + public String getHostname() + { + return "me"; + } + /** returning the user name */ + public String getUsername() + { + return this.username; + } + /** changing the user name */ + public void setUsername(String newUsername) + { + this.username = newUsername; + } + + /** closing the socket */ + public void disconect () + { + try { + this.soc.close(); + } + catch (IOException e) + { + System.err.println("The socket failed to closed: " + e); + } + + } + + /** prints a line to the client socket + * @param line the text to print */ + public void println(String line) throws IOException + { + 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 2d580fc..6edac37 100644 --- a/src/Server/CommandsTable.java +++ b/src/Server/CommandsTable.java @@ -1,148 +1,148 @@ -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());
- this.table.put("JOIN", new CommandJoin());
- this.table.put("PING", new CommandPing());
- this.table.put("PRIVMSG", new CommandPrivmsg());
- }
-
- 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(":" + client.getHostname() +" " + str);
- } catch (IOException e) {
- System.err.println("Failed to print to client socket: <" + str + "> (" + e + ")");
- }
- }
-
- public void printUser(Client client, String str)
- {
- try {
- client.println(":" + client.getNick() + "!" + client.getUsername() + "@" + client.getHostname() + str);
- } catch (IOException e) {
- System.err.println("Failed to print to client socket: <" + str + "> (" + e + ")");
- }
- }
-
- /** Remove the first char from the input (this char is irelevant)*/
- public static String RemoveFirst(String st)
- {
- return st.substring(1 , st.length());
- }
-}
-
-
-//FIXME:
-class CommandPrivmsg extends Command
-{
- public CommandPrivmsg(){}
-
- public void run(Client client, String args)
- {
- String [] starr = args.split("[ \t]+" , 2);
- String line = RemoveFirst(starr[1]);
- }
-}
-
-/** Command that setting the nick name of the client*/
-class CommandNick extends Command
-{
- public CommandNick() {}
-
- public void run(Client client, String args)
- {
- String nick = args;
- client.setNick(nick);
- // FIXME: print reply
- }
-}
-
-/** Command that connecting the client to the server */
-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";
- }
-}
-
-/** Command that recipient the client to the irc*/
-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");
- }
-}
-
-/** Command that disconnect the client from the server*/
-class CommandQuit extends Command
-{
- public CommandQuit() {}
-
- public void run(Client client, String args)
- {
- client.disconect();
-
- }
-}
-
-/**Input of unknown command*/
-class CommandBad extends Command
-{
- public void run(Client client, String args)
- {
- this.println(client, "421 " + client.getNick() + " " + args + " Unknown command.");
- }
-}
-
-/** Pinging each few seconds to keep the connection alive*/
-class CommandPing extends Command
-{
- public CommandPing() {}
-
- public void run(Client client, String args)
- {
- this.println(client , "PONG " + args + " :" + client.getHostname()); //FIXME: needed massge return
-
- }
+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()); + this.table.put("JOIN", new CommandJoin()); + this.table.put("PING", new CommandPing()); + this.table.put("PRIVMSG", new CommandPrivmsg()); + } + + 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(":" + client.getHostname() +" " + str); + } catch (IOException e) { + System.err.println("Failed to print to client socket: <" + str + "> (" + e + ")"); + } + } + + public void printUser(Client client, String str) + { + try { + client.println(":" + client.getNick() + "!" + client.getUsername() + "@" + client.getHostname() + str); + } catch (IOException e) { + System.err.println("Failed to print to client socket: <" + str + "> (" + e + ")"); + } + } + + /** Remove the first char from the input (this char is irelevant)*/ + public static String RemoveFirst(String st) + { + return st.substring(1 , st.length()); + } +} + + +//FIXME: +class CommandPrivmsg extends Command +{ + public CommandPrivmsg(){} + + public void run(Client client, String args) + { + String [] starr = args.split("[ \t]+" , 2); + String line = RemoveFirst(starr[1]); + } +} + +/** Command that setting the nick name of the client*/ +class CommandNick extends Command +{ + public CommandNick() {} + + public void run(Client client, String args) + { + String nick = args; + client.setNick(nick); + // FIXME: print reply + } +} + +/** Command that connecting the client to the server */ +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"; + } +} + +/** Command that recipient the client to the irc*/ +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"); + } +} + +/** Command that disconnect the client from the server*/ +class CommandQuit extends Command +{ + public CommandQuit() {} + + public void run(Client client, String args) + { + client.disconect(); + + } +} + +/**Input of unknown command*/ +class CommandBad extends Command +{ + public void run(Client client, String args) + { + this.println(client, "421 " + client.getNick() + " " + args + " Unknown command."); + } +} + +/** Pinging each few seconds to keep the connection alive*/ +class CommandPing extends Command +{ + public CommandPing() {} + + public void run(Client client, String args) + { + this.println(client , "PONG " + args + " :" + client.getHostname()); //FIXME: needed massge return + + } }
\ No newline at end of file |