diff --git a/.classpath b/.classpath
new file mode 100644
index 00000000..2c592c7f
--- /dev/null
+++ b/.classpath
@@ -0,0 +1,12 @@
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 00000000..c57768e3
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,4 @@
+/ClientConsole.class
+/EchoServer.class
+/ServerConsole.class
+/ServerConsoleTest.class
diff --git a/.project b/.project
new file mode 100644
index 00000000..3305e447
--- /dev/null
+++ b/.project
@@ -0,0 +1,17 @@
+
+
+ simpleChat
+
+
+
+
+
+ org.eclipse.jdt.core.javabuilder
+
+
+
+
+
+ org.eclipse.jdt.core.javanature
+
+
diff --git a/ClientConsole.class b/ClientConsole.class
new file mode 100644
index 00000000..5c5f76d7
Binary files /dev/null and b/ClientConsole.class differ
diff --git a/ClientConsole.java b/ClientConsole.java
index 5a8261e2..859507f5 100644
--- a/ClientConsole.java
+++ b/ClientConsole.java
@@ -50,18 +50,21 @@ public class ClientConsole implements ChatIF
* @param host The host to connect to.
* @param port The port to connect on.
*/
- public ClientConsole(String host, int port)
- {
+
+ public ClientConsole(String loginID,String host, int port)
+{
try
{
- client= new ChatClient(host, port, this);
-
-
+ client= new ChatClient(loginID, host, port, this);
}
catch(IOException exception)
{
- System.out.println("Error: Can't setup connection!"
- + " Terminating client.");
+ System.out.println("Error: Can't setup connection!"+ " Terminating client.");
+
+
+ client.connectionClosed(); //when connection is not possible
+
+
System.exit(1);
}
@@ -118,18 +121,40 @@ public void display(String message)
public static void main(String[] args)
{
String host = "";
-
-
- try
- {
- host = args[0];
+
+ //the port number
+ int port=0;
+
+ //the login id
+ String loginID="";
+ try {
+ loginID = args[0];
+ }
+ catch (ArrayIndexOutOfBoundsException e) {
+ System.err.println("No login ID provided!");
+ System.exit(1);
}
- catch(ArrayIndexOutOfBoundsException e)
- {
- host = "localhost";
+
+ try {
+ host = args[1];
+
+ } catch (ArrayIndexOutOfBoundsException e) {
+ host = "localhost";
+
}
- ClientConsole chat= new ClientConsole(host, DEFAULT_PORT);
- chat.accept(); //Wait for console data
+ try{
+ port= Integer.parseInt(args[2]); //convert argument in int
+ }catch(ArrayIndexOutOfBoundsException e){
+ port=DEFAULT_PORT; //if exception from using arguments, use default port
+ }
+ catch(NumberFormatException ne) { //we check that it works for any other format
+ port=DEFAULT_PORT;
+ }
+
+ ClientConsole chat=new ClientConsole (loginID, host,port);
+ chat.accept(); //Wait for console data
+
}
+
}
//End of ConsoleChat class
diff --git a/EchoServer.class b/EchoServer.class
new file mode 100644
index 00000000..e3ea1c22
Binary files /dev/null and b/EchoServer.class differ
diff --git a/EchoServer.java b/EchoServer.java
index 39579b5e..0730fcf8 100644
--- a/EchoServer.java
+++ b/EchoServer.java
@@ -3,8 +3,11 @@
// license found at www.lloseng.com
+import java.io.*;
+import client.ChatClient;
import ocsf.server.*;
+
/**
* This class overrides some of the methods in the abstract
* superclass in order to give more functionality to the server.
@@ -18,12 +21,18 @@
public class EchoServer extends AbstractServer
{
//Class variables *************************************************
+
+ final private String key="loginKey";
/**
* The default port to listen on.
*/
final public static int DEFAULT_PORT = 5555;
+ //I add
+ ChatClient client;
+
+
//Constructors ****************************************************
/**
@@ -34,6 +43,14 @@ public class EchoServer extends AbstractServer
public EchoServer(int port)
{
super(port);
+
+ try{
+ listen(); //Start listening for connections
+ }
+ catch (Exception ex){
+ System.out.println("ERROR - Could not listen for clients!");
+ }
+
}
@@ -45,13 +62,82 @@ public EchoServer(int port)
* @param msg The message received from the client.
* @param client The connection from which the message originated.
*/
- public void handleMessageFromClient
- (Object msg, ConnectionToClient client)
- {
- System.out.println("Message received: " + msg + " from " + client);
- this.sendToAllClients(msg);
+ public void handleMessageFromClient(Object msg, ConnectionToClient client) {
+ System.out.println("Message received: " + msg + " from " + client);
+ String msgStr=(String)msg;
+ if(msgStr.startsWith("#login")) {
+ String loginID="";
+ client.setInfo(key, loginID);
+ }
+ else {
+ this.sendToAllClients(msg);
+ }
+
+
+ String message = msg.toString();
+ if (message.startsWith("#")) {
+ String[] params = message.substring(1).split(" ");
+ if (params[0].equalsIgnoreCase("login") && params.length > 1) {
+ if (client.getInfo("username") == null) {
+ client.setInfo("username", params[1]);
+ } else {
+ try {
+ client.sendToClient(("Your username has already been set!"));
+ } catch (IOException e) {
+ //error sending
+ }
+ }
+ }
+ } else {
+ if (client.getInfo("username") == null) {
+ try {
+ client.sendToClient(("Please set a username before messaging the server!"));
+ client.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ } else {
+ System.out.println("Message received: " + msg + " from " +
+ client.getInfo("username"));
+ this.sendToAllClients((client.getInfo("username") + " > " + message));
+ }
+ }
+ }
+
+
+
+ /*if you want to read the information saved in the client
+ we do client.getId(key) for return the login id*/
+
+
+
+ protected void kickAllClients(){
+ Thread [] clientThreadList = getClientConnections();
+ for(int i=0; i " + message);
+ }
+
+
+
+/**
+ * This method is responsible for the creation of
+ * the server instance (there is no UI in this phase).
+ *
+ * @param args[0] The port number to listen on. Defaults to 5555
+ * if no argument is entered.
+ */
+ public static void main(String[] args) {
+ int port = 0; //Port to listen on
+
+ try
+ {
+ port = Integer.parseInt(args[0]); //Get port from command line
+ }
+ catch(Throwable t)
+ {
+ port = DEFAULT_PORT; //Set port to 5555
+ }
+
+ EchoServer sv = new EchoServer(port);
+
+ try
+ {
+ sv.listen(); //Start listening for connections
+ }
+ catch (Exception ex)
+ {
+ System.out.println("ERROR - Could not listen for clients!");
+ }
+ }
+}
diff --git a/client/.gitignore b/client/.gitignore
new file mode 100644
index 00000000..8d8b1f45
--- /dev/null
+++ b/client/.gitignore
@@ -0,0 +1 @@
+/ChatClient.class
diff --git a/client/ChatClient.class b/client/ChatClient.class
new file mode 100644
index 00000000..b231034b
Binary files /dev/null and b/client/ChatClient.class differ
diff --git a/client/ChatClient.java b/client/ChatClient.java
index fe1401ed..977bb340 100644
--- a/client/ChatClient.java
+++ b/client/ChatClient.java
@@ -5,6 +5,7 @@
package client;
import ocsf.client.*;
+
import common.*;
import java.io.*;
@@ -38,7 +39,7 @@ public class ChatClient extends AbstractClient
* @param clientUI The interface type variable.
*/
- public ChatClient(String host, int port, ChatIF clientUI)
+ public ChatClient(String loginID, String host, int port, ChatIF clientUI)
throws IOException
{
super(host, port); //Call the superclass constructor
@@ -66,17 +67,91 @@ public void handleMessageFromServer(Object msg)
*/
public void handleMessageFromClientUI(String message)
{
- try
- {
- sendToServer(message);
+ try{
+ if(message.startsWith("#")) {
+ handleCommand(message);
+ }
+ else {
+ sendToServer(message);
+ }
}
- catch(IOException e)
- {
- clientUI.display
- ("Could not send message to server. Terminating client.");
- quit();
+ catch(IOException e){
+ clientUI.display("Could not send message to server. Terminating client.");
+ quit();
}
}
+ //we create a method that implements the different commands
+ private void handleCommand (String cmd) {
+ if(cmd.equals("#quit")) {
+ clientUI.display("client will quit");
+ quit();
+ }
+ else if(cmd.equals("#logoff")) {
+ try {
+ if(this.isConnected()) {
+ this.closeConnection();
+ }
+ else {
+ System.out.println("the client is already disconnected");
+ }
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ System.out.println("there is a problem with the disconnection");
+ }
+ }
+ else if(cmd.equals("#login")) {
+ if(this.isConnected()) {
+ System.out.println("the client is already connected");
+ }
+ else {
+ try {
+ this.openConnection();
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ System.out.println("there is a problem with the connection");
+ }
+ }
+ }
+ else if(cmd.equals("#sethost")) {
+ if(this.isConnected()) {
+ System.out.println("the client is still connected");
+ }
+ else {
+ super.setHost((this.getHost()));
+ System.out.println("Port set to " + getHost());
+ }
+ }
+ else if(cmd.equals("#setport")) {
+ if(this.isConnected()) {
+ System.out.println("the client is still connected");
+ }
+ else {
+ super.setPort((this.getPort()));
+ System.out.println("Port set to " + getPort());
+ }
+
+ }
+ else if(cmd.equals("#getport")) {
+ System.out.println("Current port is " + this.getPort());
+
+ }
+ else if(cmd.equals("#gethost")) {
+ System.out.println("Current port is " + this.getHost());
+ }
+
+ }
+
+
+ //methods connectionClosed() and connectionException()
+ @Override
+ public void connectionClosed() {
+ clientUI.display("connection closed");
+ }
+
+ public void connectionException(Exception exception) {
+ clientUI.display("the server has stopped");
+ System.exit(0);
+ }
/**
* This method terminates the client.
diff --git a/common/.gitignore b/common/.gitignore
new file mode 100644
index 00000000..59994316
--- /dev/null
+++ b/common/.gitignore
@@ -0,0 +1,2 @@
+/ChatIF.class
+/ServerConsole.class
diff --git a/common/ChatIF.class b/common/ChatIF.class
new file mode 100644
index 00000000..857b9ae4
Binary files /dev/null and b/common/ChatIF.class differ