diff options
author | Dor Bivas <dor1_b@walla.com> | 2014-03-29 17:15:26 +0300 |
---|---|---|
committer | Dor Bivas <dor1_b@walla.com> | 2014-03-29 17:15:26 +0300 |
commit | 3e6edd995e1e705f6470f77bfe07c914511e2f49 (patch) | |
tree | c05477ec7e2dabaffdf6ad123fbff5e77d66119d /src/Client | |
parent | d008f730a318bc1daf603ac6520293c808a054b2 (diff) |
Chat: deleted white spaces prettify the code, and fixm's and unneeded
notes and tasks.
Diffstat (limited to 'src/Client')
-rw-r--r-- | src/Client/ChatClient.java | 75 | ||||
-rw-r--r-- | src/Client/ChatClientReader.java | 19 | ||||
-rw-r--r-- | src/Client/ChatClientWriter.java | 3 | ||||
-rw-r--r-- | src/Client/ParsedLine.java | 12 | ||||
-rw-r--r-- | src/Client/UserInterface.java | 14 |
5 files changed, 44 insertions, 79 deletions
diff --git a/src/Client/ChatClient.java b/src/Client/ChatClient.java index 6499b75..fe70742 100644 --- a/src/Client/ChatClient.java +++ b/src/Client/ChatClient.java @@ -2,18 +2,6 @@ package Client; import java.io.*; import java.net.Socket; - - - - - - - - - -import Server.ChatServer; -import Server.Client; - public class ChatClient { private String nick; @@ -23,7 +11,7 @@ public class ChatClient private Socket soc; private boolean isConnected = false; - private static final String CHANNEL = "#Dor-chat"; //FIXME: should be public. But it's interesting if the server could be ignorant of it + private static final String CHANNEL = "#Dor-chat"; ChatClient() { this.soc = null; @@ -39,7 +27,8 @@ public class ChatClient { return this.realname; } - /** changing the nick name */ + /** changing the nick name * + * @param nick name the name to set*/ public void setNick (String nick) { this.nick = nick; @@ -57,6 +46,7 @@ public class ChatClient this.isConnected = true; } + /** returning connection */ public boolean getConnected() { return this.isConnected; @@ -71,12 +61,14 @@ public class ChatClient writer.sendLine(line); } + /** send line to server */ private void earlySendline(String line) { writer.sendLine(line); } - /** */ + /** if the line is special command privmsg will handle it, else it will send the lien as a privmsg. + * @param the line.*/ public void PRIVMSG (String line) { String[] words = line.split("[ \t]", 2); @@ -97,12 +89,14 @@ public class ChatClient } } + /** checking for nick Collisions and handling them. */ public void fixNickCollision() { - String newNick = getNick() + new Integer((int) (Math.random()*1000)).toString(); + String newNick = getNick() + new Integer((int)(Math.random()*1000)).toString(); earlySendline("NICK " + newNick); } + /** sending nick and user to the writer.*/ public void startup() { earlySendline("NICK " + this.getNick()); earlySendline("USER " + this.getNick() + " 0 * :" + this.getRealname()); @@ -116,6 +110,7 @@ public class ChatClient this.setConnected(); // FIXME: Not sure yet if client is connected. } + /** closing the socket*/ public void shutdown() { if (soc != null) @@ -128,14 +123,16 @@ public class ChatClient } } + /** opening new connection + * @param + * host name. + * port name. */ public boolean connect (String host, int port) { try { soc = new Socket(host, port); - ChatClientReader reader = new ChatClientReader(this, soc); - - reader.setDisplay(ui); + ChatClientReader reader = new ChatClientReader(this, soc, ui); this.writer = new ChatClientWriter(soc,this); @@ -158,44 +155,4 @@ public class ChatClient client.setRealname("Dor bivas"); client.ui = new UserInterface(client); } - } - - - -/** 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? - * * In the end: - * * Close socket - * * Send QUIT before that (QUIT :leaving) - * * 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. - */ diff --git a/src/Client/ChatClientReader.java b/src/Client/ChatClientReader.java index 1734d18..487676a 100644 --- a/src/Client/ChatClientReader.java +++ b/src/Client/ChatClientReader.java @@ -1,10 +1,7 @@ package Client; import java.net.Socket; -import java.util.*; import java.io.*; -import javax.swing.JTextArea; - public class ChatClientReader extends Thread { private ChatClient client; @@ -13,14 +10,10 @@ public class ChatClientReader extends Thread /** constructor * @param the socket */ - public ChatClientReader (ChatClient client, Socket soc) + public ChatClientReader (ChatClient client, Socket soc, UserInterface ui) { this.client = client; this.soc = soc; - } - - public void setDisplay (UserInterface ui) - { this.ui = ui; } @@ -29,13 +22,13 @@ public class ChatClientReader extends Thread { try { - String line; InputStreamReader inputTemp = new InputStreamReader(this.soc.getInputStream()); BufferedReader input = new BufferedReader(inputTemp); - do { + while (true) + { line = input.readLine(); if (line == null) { @@ -43,8 +36,7 @@ public class ChatClientReader extends Thread System.err.println("The line is empty"); break; } - - //System.out.println(""); + System.out.println("Got line: [" + line + "]"); ParsedLine parsed = new ParsedLine(line); @@ -76,8 +68,7 @@ public class ChatClientReader extends Thread { ui.outputWriter("[" + line + "]"); } - } while (line != null); - // FIXME: close socket here? + } } catch (IOException e) { System.err.println(e); return; diff --git a/src/Client/ChatClientWriter.java b/src/Client/ChatClientWriter.java index 924e683..9d89cea 100644 --- a/src/Client/ChatClientWriter.java +++ b/src/Client/ChatClientWriter.java @@ -22,6 +22,8 @@ public class ChatClientWriter extends Thread this.queue = new LinkedBlockingQueue<String>(); } + /** add message to queue. + * @param message */ public void sendLine (String line) { try @@ -56,7 +58,6 @@ public class ChatClientWriter extends Thread OutputStreamWriter outTemp; String line; - outTemp = new OutputStreamWriter(this.soc.getOutputStream()); this.output = new BufferedWriter(outTemp); diff --git a/src/Client/ParsedLine.java b/src/Client/ParsedLine.java index 807f289..ebc45b1 100644 --- a/src/Client/ParsedLine.java +++ b/src/Client/ParsedLine.java @@ -6,6 +6,16 @@ public class ParsedLine { private String line; private Hashtable <String, String> dict; + + /** constructor, returning dictionary. + * @param text line + * for example input: ":sweetmorn.cohens.org.il 352 tzafrir #chat-dor ~tzafrir localhost" + * output: a dictionary with the following entries: + * - server: sweetmorn.cohens.org.il + * - command: 352 + * - nick: tzafrir + * - message: #chat-dor ~tzafrir localhost + */ public ParsedLine(String line) { this.line = line; @@ -68,6 +78,8 @@ public class ParsedLine System.err.println("Notice: failed to parse message message: [" + line + "]"); } + /** getting the results from the dictionary + @param property */ public String get(String property) { return dict.get(property); diff --git a/src/Client/UserInterface.java b/src/Client/UserInterface.java index fefab23..df56ec3 100644 --- a/src/Client/UserInterface.java +++ b/src/Client/UserInterface.java @@ -29,12 +29,13 @@ public class UserInterface extends JFrame implements ActionListener, KeyListener private JButton connect; private JButton send; - + /**showing your message on the output text area. */ public void outputWriter (String line) { output.append(line + "\n"); } + /**showing the message on the output text area. */ private void msgSendRun () { if (message.getText().equals("")) @@ -47,14 +48,16 @@ public class UserInterface extends JFrame implements ActionListener, KeyListener output.setCaretPosition(output.getText().length()); } + /**sending connection request and and verify it happened only one time */ private void msgConnectRun () { - output.append("You've connect to the server succesufully" + "\n"); + output.append("Connecting..." + "\n"); counterClick++; this.send.setEnabled(true); this.connect.setEnabled(false); } + /** clearing the output*/ public void Clear () { output.setText(""); @@ -105,7 +108,6 @@ public class UserInterface extends JFrame implements ActionListener, KeyListener this.send.setEnabled(false); vertPanel.setLayout(new BoxLayout(vertPanel, BoxLayout.PAGE_AXIS)); - //vertPanel.setMinimumSize(new Dimension(645, 120)); FIXME: Set minimal Size vert panel. Dimension MaximumSize = new Dimension (800 , send.getHeight()); @@ -159,10 +161,10 @@ public class UserInterface extends JFrame implements ActionListener, KeyListener msgConnectRun (); } } - - } + /** Syncing the the action that the event caused to the perform + * @param event key press */ @Override public void keyPressed(KeyEvent e) { @@ -187,6 +189,8 @@ public class UserInterface extends JFrame implements ActionListener, KeyListener } + /** Syncing the the action closing window to the shutdown + * @param event window closing */ public void windowClosing(WindowEvent e) { client.shutdown(); |