package Client; import java.io.*; import java.net.Socket; import Server.ChatServer; import Server.Client; public class ChatClient { private String nick; private String realname; private ChatClientWriter writer; private 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 void runSendline (String line) { writer.sendLine(line); } public void PRIVMSG (String line) { if (line.substring(0,6) == "/QUOTE") { runSendline(line.substring(6, line.length()-1)); } else { runSendline("PRIVMSG " + "#chat-dor : " + line); } } public boolean connect (String host, int port) { Socket soc; try { soc = new Socket(host, port); ChatClientReader reader = new ChatClientReader(soc); reader.setDisplay(ui); this.writer = new ChatClientWriter(soc,this); 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: 5. When we get from the server a PRIVMSG, parse it and show who sent it. * Print other messages (Server messages) differently. * * Example: * * Instead of: :tzafrir!~tzafrir@localhost PRIVMSG #the-chan :Hello There * print: [#the-chan] Hello There * * TODO: 6. 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: 7. Don't allow sending text until we joined a channel. */ /** Later * * * Fix or remove all the fixme-s. * * Closing the program (Close, ALT-F4) does not end the process * * Keep channels up: PING occasionally * * The output buffer does not have a scroll bar. * * Pressing Enter in the message line does not send text. * * Configuration file to save the nick? * * History log? * * 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 * * Command-line client - useful for testing. */