diff options
author | Tzafrir Cohen <tzafrir@cohens.org.il> | 2013-12-04 10:50:36 +0200 |
---|---|---|
committer | Tzafrir Cohen <tzafrir@cohens.org.il> | 2013-12-04 10:50:36 +0200 |
commit | 3bbbd95c8d2d6f3185b0f3e48263f36ba4bb0e75 (patch) | |
tree | b336db15d08ab911658a646a6b7f3e6bf011be28 | |
parent | d57f9416a9d4bb92d4111f73af278f607bbe78b9 (diff) |
Convert source to NL-terminated
-rw-r--r-- | src/Client/ChatClient.java | 220 | ||||
-rw-r--r-- | src/Client/ChatClientReader.java | 130 | ||||
-rw-r--r-- | src/Client/ChatClientWriter.java | 130 | ||||
-rw-r--r-- | src/Client/UserInterface.java | 246 | ||||
-rw-r--r-- | src/Server/ChatServer.java | 92 | ||||
-rw-r--r-- | src/Server/Client.java | 132 | ||||
-rw-r--r-- | src/Server/CommandsTable.java | 294 |
7 files changed, 622 insertions, 622 deletions
diff --git a/src/Client/ChatClient.java b/src/Client/ChatClient.java index a1727e9..a3807f2 100644 --- a/src/Client/ChatClient.java +++ b/src/Client/ChatClient.java @@ -1,110 +1,110 @@ -package Client;
-import java.io.*;
-import java.net.Socket;
-
-
-
-import Server.ChatServer;
-
-public class ChatClient
-{
- String nick;
- String realname;
- BufferedWriter output;
- UserInterface ui;
-
- /** returning the nick name */
- public String getNick ()
- {
- return this.nick;
- }
- /** returning the real name */
- public String getRealname ()
- {
- return this.realname;
- }
- /** changing the nick name */
- public void setNick (String nick)
- {
- this.nick = nick;
- }
- /** changing the real name */
- public void setRealname (String realname)
- {
- this.realname = realname;
- }
-
- public boolean connect (String host, int port)
- {
- Socket soc;
- ChatClient client = new ChatClient();
-
- try
- {
- soc = new Socket(host, port);
- ChatClientReader reader = new ChatClientReader(soc);
-
- reader.setDisplay(client.ui);
-
- ChatClientWriter writer = new ChatClientWriter(soc,client);
-
- reader.start();
- writer.start();
-
- return true;
-
- } catch (IOException e)
- {
- System.err.println("Failed connecting to server: " + host + ":" + port + " (" + e + ")");
- return false;
- }
- }
-
- public static void main(String[] args) throws IOException
- {
- ChatClient client = new ChatClient();
- client.setNick("dor");
- client.setRealname("Dor bivas");
- client.ui = new UserInterface(client);
-
-
-
- //soc.close();
- }
-
-}
-
-
-
-/** Tasks
- * TODO: 1. Data received from the network will be written to the output window.
- *
- * TODO: 2. When the user presses "Connect", a message will be sent to a channel:
- * use PRIVMSG to channel #chat.
- *
- * TODO: 3. But we do want to allow sending commands to the channel. A
- * message line that begins with the word "/QUOTE", will be sent as is
- * (except the word "/QUOTE") to the server.
- *
- * TODO: 4. Join a channel at startup: For any server besides our own we need to send
- * a JOIN command (see how it is handled in the server).
- *
- * TODO: 5. Don't allow sending text until we joined a channel.
- *
- * TODO: 6. When we get from the server a PRIVMSG, parse it and show who sent it.
- * Print other messages (Server messages) differently.
- */
-
-/** Later
- *
- * * Fix or remove all the fixme-s.
- * * Closing the program (Close, ALT-F4) does not end the process
- * * The output buffer does not have a scroll bar.
- * * Pressing Enter in the message line does not send text.
- * * It may be handy to implement other client commands:
- * - /NICK - changes the nick
- * - /CLEAR - clears the output window
- * - /SAY - print a message directly to output window
- * - // - sends just a leading '/' (in case you want to send '/NICK whatever' to the channel
- * - /HELP - print a help message to the user
- */
+package Client; +import java.io.*; +import java.net.Socket; + + + +import Server.ChatServer; + +public class ChatClient +{ + String nick; + String realname; + BufferedWriter output; + UserInterface ui; + + /** returning the nick name */ + public String getNick () + { + return this.nick; + } + /** returning the real name */ + public String getRealname () + { + return this.realname; + } + /** changing the nick name */ + public void setNick (String nick) + { + this.nick = nick; + } + /** changing the real name */ + public void setRealname (String realname) + { + this.realname = realname; + } + + public boolean connect (String host, int port) + { + Socket soc; + ChatClient client = new ChatClient(); + + try + { + soc = new Socket(host, port); + ChatClientReader reader = new ChatClientReader(soc); + + reader.setDisplay(client.ui); + + ChatClientWriter writer = new ChatClientWriter(soc,client); + + reader.start(); + writer.start(); + + return true; + + } catch (IOException e) + { + System.err.println("Failed connecting to server: " + host + ":" + port + " (" + e + ")"); + return false; + } + } + + public static void main(String[] args) throws IOException + { + ChatClient client = new ChatClient(); + client.setNick("dor"); + client.setRealname("Dor bivas"); + client.ui = new UserInterface(client); + + + + //soc.close(); + } + +} + + + +/** Tasks + * TODO: 1. Data received from the network will be written to the output window. + * + * TODO: 2. When the user presses "Connect", a message will be sent to a channel: + * use PRIVMSG to channel #chat. + * + * TODO: 3. But we do want to allow sending commands to the channel. A + * message line that begins with the word "/QUOTE", will be sent as is + * (except the word "/QUOTE") to the server. + * + * TODO: 4. Join a channel at startup: For any server besides our own we need to send + * a JOIN command (see how it is handled in the server). + * + * TODO: 5. Don't allow sending text until we joined a channel. + * + * TODO: 6. When we get from the server a PRIVMSG, parse it and show who sent it. + * Print other messages (Server messages) differently. + */ + +/** Later + * + * * Fix or remove all the fixme-s. + * * Closing the program (Close, ALT-F4) does not end the process + * * The output buffer does not have a scroll bar. + * * Pressing Enter in the message line does not send text. + * * It may be handy to implement other client commands: + * - /NICK - changes the nick + * - /CLEAR - clears the output window + * - /SAY - print a message directly to output window + * - // - sends just a leading '/' (in case you want to send '/NICK whatever' to the channel + * - /HELP - print a help message to the user + */ diff --git a/src/Client/ChatClientReader.java b/src/Client/ChatClientReader.java index 2183d90..7cc7f07 100644 --- a/src/Client/ChatClientReader.java +++ b/src/Client/ChatClientReader.java @@ -1,65 +1,65 @@ -package Client;
-import java.net.Socket;
-import java.util.*;
-import java.io.*;
-
-import javax.swing.JTextArea;
-
-public class ChatClientReader extends Thread
-{
- public static Scanner reader = new Scanner(System.in);
- private Socket soc;
- private JTextArea display;
- UserInterface ui;
-
- /** constructor
- * @param the socket */
- public ChatClientReader (Socket soc)
- {
- this.soc = soc;
- }
-
- public void setDisplay (UserInterface ui)
- {
- this.ui = ui;
- }
-
- /** Running the object and translate the input from bytes to letters*/
- public void run()
- {
- try
- {
-
- String line ;
-
- InputStreamReader inputTemp = new InputStreamReader(this.soc.getInputStream());
- BufferedReader input = new BufferedReader(inputTemp);
-
- do {
- line = input.readLine(); // FIXME: more then 1 line
- if (line == null)
- {
- soc.close();
- }
- line = "[" + line + "]";
- System.out.println("");
- System.out.println(line);
-
- ui.outputWriter(line);
- this.display.append(line + "\n");
-
-
-
- } while (line != null);
-
-
- } catch (IOException e) {
- System.err.println(e);
- return;
- }
-
-
-
- }
-
-}
+package Client; +import java.net.Socket; +import java.util.*; +import java.io.*; + +import javax.swing.JTextArea; + +public class ChatClientReader extends Thread +{ + public static Scanner reader = new Scanner(System.in); + private Socket soc; + private JTextArea display; + UserInterface ui; + + /** constructor + * @param the socket */ + public ChatClientReader (Socket soc) + { + this.soc = soc; + } + + public void setDisplay (UserInterface ui) + { + this.ui = ui; + } + + /** Running the object and translate the input from bytes to letters*/ + public void run() + { + try + { + + String line ; + + InputStreamReader inputTemp = new InputStreamReader(this.soc.getInputStream()); + BufferedReader input = new BufferedReader(inputTemp); + + do { + line = input.readLine(); // FIXME: more then 1 line + if (line == null) + { + soc.close(); + } + line = "[" + line + "]"; + System.out.println(""); + System.out.println(line); + + ui.outputWriter(line); + this.display.append(line + "\n"); + + + + } while (line != null); + + + } catch (IOException e) { + System.err.println(e); + return; + } + + + + } + +} diff --git a/src/Client/ChatClientWriter.java b/src/Client/ChatClientWriter.java index 378008f..8e16655 100644 --- a/src/Client/ChatClientWriter.java +++ b/src/Client/ChatClientWriter.java @@ -1,65 +1,65 @@ -package Client;
-import java.io.*;
-import java.net.Socket;
-import java.util.*;
-
-public class ChatClientWriter extends Thread
-{
- private ChatClient client;
- private Socket soc;
- private BufferedWriter output;
-
- public static Scanner reader = new Scanner(System.in);
-
- /** constructor
- * @param the socket and the client info */
- public ChatClientWriter(Socket soc ,ChatClient client)
- {
- this.soc = soc;
- this.client = client;
- }
-
- /** writing the line and making sure the package will be pushed on the stream
- * @param the line and the client info */
- public void println(String line)
- {
- try
- {
- this.output.write(line, 0, line.length());
- this.output.newLine();
- this.output.flush();
-
- } catch (IOException e) {System.err.print("failed to print line" + e);}
-
- }
-
- /** Running the object and translate the output from bytes to letters*/
- public void run()
- {
- try
- {
- OutputStreamWriter outTemp;
- String line;
-
-
- outTemp = new OutputStreamWriter(this.soc.getOutputStream());
- this.output = new BufferedWriter(outTemp);
-
- println("NICK " + client.getNick());
- // 0: mode (nothing special). *: unused in RFC 2812, server name in RFC 1459
- println("USER " + client.getNick() + " 0 * :" + client.getRealname());
-
- while (true)
- {
- System.out.print("Write here: > ");
- line = reader.nextLine();
- this.output.write(line, 0, line.length());
- this.output.newLine();
- this.output.flush();
- }
-
- } catch (IOException e) {}
-
- }
-
-}
+package Client; +import java.io.*; +import java.net.Socket; +import java.util.*; + +public class ChatClientWriter extends Thread +{ + private ChatClient client; + private Socket soc; + private BufferedWriter output; + + public static Scanner reader = new Scanner(System.in); + + /** constructor + * @param the socket and the client info */ + public ChatClientWriter(Socket soc ,ChatClient client) + { + this.soc = soc; + this.client = client; + } + + /** writing the line and making sure the package will be pushed on the stream + * @param the line and the client info */ + public void println(String line) + { + try + { + this.output.write(line, 0, line.length()); + this.output.newLine(); + this.output.flush(); + + } catch (IOException e) {System.err.print("failed to print line" + e);} + + } + + /** Running the object and translate the output from bytes to letters*/ + public void run() + { + try + { + OutputStreamWriter outTemp; + String line; + + + outTemp = new OutputStreamWriter(this.soc.getOutputStream()); + this.output = new BufferedWriter(outTemp); + + println("NICK " + client.getNick()); + // 0: mode (nothing special). *: unused in RFC 2812, server name in RFC 1459 + println("USER " + client.getNick() + " 0 * :" + client.getRealname()); + + while (true) + { + System.out.print("Write here: > "); + line = reader.nextLine(); + this.output.write(line, 0, line.length()); + this.output.newLine(); + this.output.flush(); + } + + } catch (IOException e) {} + + } + +} diff --git a/src/Client/UserInterface.java b/src/Client/UserInterface.java index 20b235c..882534f 100644 --- a/src/Client/UserInterface.java +++ b/src/Client/UserInterface.java @@ -1,123 +1,123 @@ -package Client;
-import java.awt.Color;
-import java.awt.event.ActionEvent;
-import java.awt.event.ActionListener;
-
-import javax.swing.BoxLayout;
-import javax.swing.JButton;
-import javax.swing.JFrame;
-import javax.swing.JLabel;
-import javax.swing.JPanel;
-import javax.swing.JTextArea;
-import javax.swing.JTextField;
-import javax.xml.soap.Text;
-
-public class UserInterface extends JFrame implements ActionListener
-{
- private JTextField message;
- private JTextArea output;
- private ChatClient client;
- private ChatClientReader read;
- private JTextField host = new JTextField("localhost", 15);
- private JTextField port = new JTextField("6667", 4);
-
- public void outputWriter (String line)
- {
- output.append(line + "\n");
- }
-
- /** Graphic interface recipient the client*/
- public UserInterface(ChatClient client)
- {
- this.client = client;
-
- this.setIconImage(getIconImage());
- this.setSize(800, 400);
-
- output = new JTextArea(10, 20);
- output.setBackground(Color.gray);
-
- JPanel msgPanel = new JPanel();
- JPanel conPanel = new JPanel();
- JPanel vertPanel = new JPanel();
-
- message = new JTextField(40);
- JButton stop = new JButton("Stop");
- stop.setActionCommand("stop");
- stop.addActionListener(this);
- stop.setToolTipText("Click this button to exit.");
-
- JButton send = new JButton("Send");
- send.setActionCommand("send");
- send.addActionListener(this);
- send.setToolTipText("Click this button to send message.");
-
- JButton connect = new JButton("Connect");
- connect.setActionCommand("connect");
- connect.addActionListener(this);
- connect.setToolTipText("Click this button to connect the server.");
-
- vertPanel.setLayout(new BoxLayout(vertPanel, BoxLayout.PAGE_AXIS));
-
-
-
- msgPanel.add(connect);
- msgPanel.add(send);
- msgPanel.add(new JLabel("Message:"));
- msgPanel.add(message);
- msgPanel.add(stop);
-
-
-
- conPanel.add(new JLabel("host:"));
- conPanel.add(host);
- conPanel.add(new JLabel("port:"));
- conPanel.add(port);
-
- vertPanel.add(output);
- vertPanel.add(conPanel);
- vertPanel.add(msgPanel);
-
- add(vertPanel);
-
- setVisible(true);
- }
-
-
-
- /** Syncing the the action that the button send to the perform
- * @param name of the action */
- int counterClick = 0;
- public void actionPerformed(ActionEvent e)
- {
-
- System.out.println(e.getActionCommand()); //FIXME: delete.
- String command = e.getActionCommand();
-
- if (command == "stop")
- {
- System.exit(0);
- }
-
- if (command == "send")
- {
- output.append(message.getText() + "\n");
- message.setText("");
- }
-
- if (counterClick>0 && command == "connect")
- {
- output.append("You've connected to the server alredy" + "\n");
- }
-
- if (command == "connect" && counterClick == 0 )
- {
- if (client.connect("localhost",(Integer.parseInt("6667"))))
- {
- output.append("You've connect to the server succesufully" + "\n");
- counterClick++;
- }
- }
-
- }
-}
+package Client; +import java.awt.Color; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +import javax.swing.BoxLayout; +import javax.swing.JButton; +import javax.swing.JFrame; +import javax.swing.JLabel; +import javax.swing.JPanel; +import javax.swing.JTextArea; +import javax.swing.JTextField; +import javax.xml.soap.Text; + +public class UserInterface extends JFrame implements ActionListener +{ + private JTextField message; + private JTextArea output; + private ChatClient client; + private ChatClientReader read; + private JTextField host = new JTextField("localhost", 15); + private JTextField port = new JTextField("6667", 4); + + public void outputWriter (String line) + { + output.append(line + "\n"); + } + + /** Graphic interface recipient the client*/ + public UserInterface(ChatClient client) + { + this.client = client; + + this.setIconImage(getIconImage()); + this.setSize(800, 400); + + output = new JTextArea(10, 20); + output.setBackground(Color.gray); + + JPanel msgPanel = new JPanel(); + JPanel conPanel = new JPanel(); + JPanel vertPanel = new JPanel(); + + message = new JTextField(40); + JButton stop = new JButton("Stop"); + stop.setActionCommand("stop"); + stop.addActionListener(this); + stop.setToolTipText("Click this button to exit."); + + JButton send = new JButton("Send"); + send.setActionCommand("send"); + send.addActionListener(this); + send.setToolTipText("Click this button to send message."); + + JButton connect = new JButton("Connect"); + connect.setActionCommand("connect"); + connect.addActionListener(this); + connect.setToolTipText("Click this button to connect the server."); + + vertPanel.setLayout(new BoxLayout(vertPanel, BoxLayout.PAGE_AXIS)); + + + + msgPanel.add(connect); + msgPanel.add(send); + msgPanel.add(new JLabel("Message:")); + msgPanel.add(message); + msgPanel.add(stop); + + + + conPanel.add(new JLabel("host:")); + conPanel.add(host); + conPanel.add(new JLabel("port:")); + conPanel.add(port); + + vertPanel.add(output); + vertPanel.add(conPanel); + vertPanel.add(msgPanel); + + add(vertPanel); + + setVisible(true); + } + + + + /** Syncing the the action that the button send to the perform + * @param name of the action */ + int counterClick = 0; + public void actionPerformed(ActionEvent e) + { + + System.out.println(e.getActionCommand()); //FIXME: delete. + String command = e.getActionCommand(); + + if (command == "stop") + { + System.exit(0); + } + + if (command == "send") + { + output.append(message.getText() + "\n"); + message.setText(""); + } + + if (counterClick>0 && command == "connect") + { + output.append("You've connected to the server alredy" + "\n"); + } + + if (command == "connect" && counterClick == 0 ) + { + if (client.connect("localhost",(Integer.parseInt("6667")))) + { + output.append("You've connect to the server succesufully" + "\n"); + counterClick++; + } + } + + } +} 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 |