summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDor Bivas <dor1_b@walla.com>2014-03-29 17:15:26 +0300
committerDor Bivas <dor1_b@walla.com>2014-03-29 17:15:26 +0300
commit3e6edd995e1e705f6470f77bfe07c914511e2f49 (patch)
treec05477ec7e2dabaffdf6ad123fbff5e77d66119d
parentd008f730a318bc1daf603ac6520293c808a054b2 (diff)
Chat: deleted white spaces prettify the code, and fixm's and unneeded
notes and tasks.
-rw-r--r--src/Client/ChatClient.java75
-rw-r--r--src/Client/ChatClientReader.java19
-rw-r--r--src/Client/ChatClientWriter.java3
-rw-r--r--src/Client/ParsedLine.java12
-rw-r--r--src/Client/UserInterface.java14
-rw-r--r--src/Server/ChatServer.java4
-rw-r--r--src/Server/Client.java3
-rw-r--r--src/Server/CommandsTable.java30
-rw-r--r--src/Server/Connection.java45
9 files changed, 93 insertions, 112 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();
diff --git a/src/Server/ChatServer.java b/src/Server/ChatServer.java
index 363a747..6d58c49 100644
--- a/src/Server/ChatServer.java
+++ b/src/Server/ChatServer.java
@@ -14,16 +14,20 @@ public class ChatServer
public static final int portNum = 6667;
public static ChatServer server;
+ /** removing connection from the iterator.
+ * @param connection */
public void connectionRemove(Connection con)
{
this.connections.remove(con);
}
+ /** returning ConnectionIterator. */
public Iterator<Connection> getConnectionIterator()
{
return this.connections.iterator();
}
+ /** constructor of the server , building socket , connection list and nicks dictionary.*/
ChatServer() throws IOException {
this.service = new ServerSocket(portNum);
this.connections = new LinkedList<Connection>();
diff --git a/src/Server/Client.java b/src/Server/Client.java
index 4ab36dd..35cfefc 100644
--- a/src/Server/Client.java
+++ b/src/Server/Client.java
@@ -10,7 +10,8 @@ public class Client
private Socket soc;
private BufferedWriter output;
private Connection con;
-
+
+ /** constructor*/
public Client(Socket soc, BufferedWriter output, Connection con)
{
this.soc = soc;
diff --git a/src/Server/CommandsTable.java b/src/Server/CommandsTable.java
index 65bbe03..966b67f 100644
--- a/src/Server/CommandsTable.java
+++ b/src/Server/CommandsTable.java
@@ -17,7 +17,7 @@ public class CommandsTable
this.table.put("PING", new CommandPing());
this.table.put("PRIVMSG", new CommandPrivmsg());
this.table.put("MODE", new CommandIgnored());
- }
+ }
public void runCommand (Client client , String commandName , String args)
{
@@ -32,11 +32,14 @@ public class CommandsTable
}
}
-
+/** parent class for all command handling classes.*/
abstract class Command
{
abstract public void run(Client client, String args);
+ /** print line to the client
+ * @param client destination client
+ * @param str the line */
public void println(Client client, String str)
{
try {
@@ -46,17 +49,22 @@ abstract class Command
}
}
-
+ /** send message to a client
+ * @param client
+ * @param str message */
public void printUser(Client client, String str)
{
printUser (client ,client, str);
}
-
+ /** print to client a message from sender.
+ * @param client destination client
+ * @param sender client
+ * @param str message */
public void printUser(Client client,Client sender, String str)
{
try {
- client.println(":" + sender .getNick() + "!" + client.getUsername() + "@" + client.getHostname() + " " + str);
+ client.println(":" + sender.getNick() + "!" + client.getUsername() + "@" + client.getHostname() + " " + str);
} catch (IOException e) {
System.err.println("Failed to print to client socket: <" + str + "> (" + e + ")");
}
@@ -69,7 +77,8 @@ abstract class Command
}
}
-
+/** sending message from a client to all other clients.
+ * @param client, arguments */
class CommandPrivmsg extends Command
{
public CommandPrivmsg(){}
@@ -108,10 +117,7 @@ class CommandNick extends Command
if (!client.setNick(nick))
{
this.println(client, "433 * " + nick + " :Nick alredy in use.");
- }
-
-
-
+ }
}
}
@@ -163,9 +169,7 @@ class CommandBad extends Command
/** A command to ignore */
class CommandIgnored extends Command
{
- public void run(Client client, String args)
- {
- }
+ public void run(Client client, String args) {}
}
/** Pinging each few seconds to keep the connection alive*/
diff --git a/src/Server/Connection.java b/src/Server/Connection.java
index bcc3ac7..c5c06cf 100644
--- a/src/Server/Connection.java
+++ b/src/Server/Connection.java
@@ -3,7 +3,6 @@ package Server;
import java.net.*;
import java.io.BufferedReader;
import java.io.BufferedWriter;
-import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
@@ -13,7 +12,9 @@ class ParsedLine
private String line;
private String command;
private String args;
-
+
+ /**splits the line by white spaces, to fetch the command and arguments
+ * @param line */
public ParsedLine(String line)
{
this.line = line;
@@ -22,18 +23,20 @@ class ParsedLine
this.args = parsedLine[1];
else
this.args = "";
-
+
if (parsedLine.length > 0)
this.command = parsedLine[0];
else
this.command = "";
}
-
+
+ /** returning the command. */
public String getCommand()
{
return this.command;
}
-
+
+ /** returning arguments */
public String getArgs()
{
return this.args;
@@ -45,43 +48,48 @@ public class Connection extends Thread
private Socket soc;
private Client client;
+ /** constructor */
public Connection(Socket soc)
{
this.soc = soc;
}
-
+
+ /** returning the client. */
public Client getClient()
{
return this.client;
}
-
- public void shutdown()
+
+ /** removing the connection from the list and the nick from the dictionary */
+ public void shutdown()
{
-
+
ChatServer.server.connectionRemove(this);
ChatServer.server.removeNick(client.getNick());
try
{
if (!soc.isClosed())
soc.close();
-
- } catch (IOException e) {}
+
+ } catch (IOException e) {}
}
-
- public String toString ()
+
+ /** pretty print (the port) */
+ public String toString()
{
return "[" + soc.getPort() + "]";
}
+ /** main loop of connection */
@SuppressWarnings("deprecation")
public void run()
{
try
{
CommandsTable commandsTable = new CommandsTable();
- InputStreamReader InTemp = new InputStreamReader(soc.getInputStream() );
+ InputStreamReader InTemp = new InputStreamReader(soc.getInputStream());
BufferedReader input = new BufferedReader(InTemp);
-
+
OutputStreamWriter outTemp = new OutputStreamWriter(soc.getOutputStream());
BufferedWriter output = new BufferedWriter(outTemp);
client = new Client(soc, output, this);
@@ -93,10 +101,11 @@ public class Connection extends Thread
try
{
- line = input.readLine(); // FIXME: more then 1 line
+ line = input.readLine();
} catch (java.net.SocketException e)
{
- break; // connection was lost / disconnected
+ shutdown();
+ break;
}
if (line == null)
{
@@ -107,11 +116,9 @@ public class Connection extends Thread
parsedLine = new ParsedLine(line);
String s = soc.getPort() + ": <" + line + ">";
System.out.println(s);
-
s = soc.getPort() + ": <" + line + ">, command: <" + parsedLine.getCommand() + ">, args: <" + parsedLine.getArgs() + ">.";
commandsTable.runCommand(client, parsedLine.getCommand(), parsedLine.getArgs());
System.out.println(s);
- // System.out.println("Done");
}
} catch (IOException e)
{