Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added code/Testcases_Fortin_6338858.docx
Binary file not shown.
62 changes: 52 additions & 10 deletions code/simplechat1/ClientConsole.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,16 @@ 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 clientId, String host, int port)
{
try
{
client= new ChatClient(host, port, this);
client= new ChatClient(clientId, host, port, this);
}
catch(IOException exception)
{
System.out.println("Error: Can't setup connection!"
+ " Terminating client.");
System.exit(1);
System.out.println("Cannot open connection."
+ " Awaiting command.");
}
}

Expand Down Expand Up @@ -91,7 +90,18 @@ public void accept()
*/
public void display(String message)
{
System.out.println("> " + message);
//For a message coming from the server to not have a > prepended
try{
if (message.charAt(10)=='>'){
System.out.println(message);
}
else{
System.out.println("> " + message);
}
}
catch(IndexOutOfBoundsException e){
System.out.println("> " + message);
}
}


Expand All @@ -106,16 +116,48 @@ public static void main(String[] args)
{
String host = "";
int port = 0; //The port number

String clientId = ""; //The user's loginId


//Question 7a
//Assigns the first argument as the loginId
try {
clientId=args[0];
}
//If no loginId is provided an error message is printed and the user is disconnected
catch(ArrayIndexOutOfBoundsException e0){
System.out.println("Cannot connect to server without login Id.");
System.exit(0);
}

try
{
host = args[0];
//Question 7 a
//changed to second arg since adding clientId
host = args[1];
}
catch(ArrayIndexOutOfBoundsException e)
catch(ArrayIndexOutOfBoundsException e1)
{
host = "localhost";
}
ClientConsole chat= new ClientConsole(host, DEFAULT_PORT);

//Question 5b
//This try loop checks to see if there is a second argument
//If that's the case, it will assign it as the port number
try
{
//Question 7 a
//changed to third arg since adding clientId
port = Integer.valueOf(args[2]);
}
//If there's no 2nd argument, it will use the DEFAULT_PORT
catch(ArrayIndexOutOfBoundsException e2)
{
port = DEFAULT_PORT;
}
//will use the port variable defined above instead of DEFAULT_PORT
//added the loginId to the constructor (Question 7a)
ClientConsole chat= new ClientConsole(clientId, host, port);
chat.accept(); //Wait for console data
}
}
Expand Down
224 changes: 221 additions & 3 deletions code/simplechat1/EchoServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public class EchoServer extends AbstractServer
*/
final public static int DEFAULT_PORT = 5555;



//Constructors ****************************************************

/**
Expand All @@ -34,11 +36,59 @@ public class EchoServer extends AbstractServer
public EchoServer(int port)
{
super(port);

}


//Instance methods ************************************************

/**
* Hook method called each time an exception is thrown in a
* ConnectionToClient thread.
* The method may be overridden by subclasses but should remains
* synchronized.
*
* @param client the client that raised the exception.
* @param Throwable the exception thrown.
*/

//Question 5c
//when an exception is thrown, it calls the clientDisconnected hook method to print the appropriate message.
synchronized protected void clientException(
ConnectionToClient client, Throwable exception) {
clientDisconnected(client);
}

/**
* Hook method called each time a new client connection is
* accepted. The default implementation does nothing.
* @param client the connection connected to the client.
*/

//Question 5c
//prints a message when the client logs on.
protected void clientConnected(ConnectionToClient client) {
System.out.println("A new client is attempting to connect to the server.");
}

/**
* Hook method called each time a client disconnects.
* The default implementation does nothing. The method
* may be overridden by subclasses but should remains synchronized.
*
* @param client the connection with the client.
*/

//Question 5c
//prints a message when the client logs off
//changed to comply with the testcases
synchronized protected void clientDisconnected(ConnectionToClient client) {
this.sendToAllClients(client.getInfo("loginId").toString() + " has logged off ");
System.out.println(client.getInfo("loginId").toString()+" has logged off");

}


/**
* This method handles any messages received from the client.
*
Expand All @@ -48,8 +98,63 @@ public EchoServer(int port)
public void handleMessageFromClient
(Object msg, ConnectionToClient client)
{
System.out.println("Message received: " + msg + " from " + client);
this.sendToAllClients(msg);
//converts the message Object into a string
String message = msg.toString();
//splits the string into words
String[] idArray = message.split(" ");

//Question 7 c i
//This if statement recognizes the #login command
if(idArray[0].equals("#login")){
try{
//Question 7 iv
//Tries to get loginId, if there is none, then (#login <loginId>) is the first message
// it would then jump to the catch Exception e
String test = client.getInfo("loginId").toString();
try{
//An error message would be printed if the loginId already exists
client.sendToClient("Error: you have already given us a login Id");
}
catch(IOException e){}
}
catch(Exception f){
//Question 7ii
//identifies the client by adding the loginId to the user info hashmap.
client.setInfo("loginId", idArray[1]);
//Question 5c
//Message that tells all users when someone logs onto the server
this.sendToAllClients(client.getInfo("loginId").toString() + " has logged on ");
System.out.println(client.getInfo("loginId").toString()+" has logged on");

}
}
else{
try{
//Tries to get loginId, if there is none, then the first message (#login <loginId>) was not recieved
// it would then jump to the catch Exception g
String test = client.getInfo("loginId").toString();
//Question 7 c iii
//Prints message in server and sends the message out to all users with the loginId prepended.
System.out.println("Message received: " + msg + " from " + client.getInfo("loginId").toString());
this.sendToAllClients(client.getInfo("loginId").toString() + " " + msg);
}
catch(Exception g){
//Question 7 c v
//If the loginId has not already been recieved as 1st message it prints and error message
//and closes the connection to that client.
try{
client.sendToClient("Error: no login Id provided by the system");
}
catch(IOException h){}
finally{
try{
client.close();
}
catch(IOException i){}
}
}
}

}

/**
Expand All @@ -69,7 +174,120 @@ protected void serverStarted()
protected void serverStopped()
{
System.out.println
("Server has stopped listening for connections.");
("Server has stopped listening for new connections.");
}


/**
* Hook method called when the server is clased.
* The default implementation does nothing. This method may be
* overriden by subclasses. When the server is closed while still
* listening, serverStopped() will also be called.
*/
protected void serverClosed(){
System.out.println
("Server has closed all connections."); }


/**
*
*This method handles the case when a user enters a string starting with a #
*It parses the message to see what command it was then it executes said command
*
* @param message The message from the UI.
*/

//Question 6 c
//Method that recieves a message that begins with # (a command)
//and parses which command was issued and what to do
public void commandHelperMethod(String message){

//seperates the command from the arguments (if pertinent - for #setport)
String[] argumentsArray = message.split(" ");

switch(argumentsArray[0]){
//Question 6 c i
case "#quit": //closes the server then quits the system
try{
close();
}
catch(IOException e){

}
System.out.print("Shutting down server");
System.exit(0);
break;
//Question 6 c ii
case "#stop": //stops listening for new clients
stopListening();
break;
//Question 6 c iii
case "#close": //closes the server (stops listening + kicks the current users off)
try{
close();
}
catch(IOException e){

}
break;
//Question 6 c iv
case "#setport": //allows the user to change the port (only available when not connected to a server)
if(!isListening()&&getNumberOfClients()==0){
try{
setPort(Integer.parseInt(argumentsArray[1]));
}
catch(ArrayIndexOutOfBoundsException e){
System.out.println("Port number not specified. Please use the following format: #setport <portnumber> ");
}
}
else{
System.out.println("The server is currently open. You must first close the server using #close");
}
break;
//Question 6 c v
case "#start": //opens the server
try{
listen();
}
catch(IOException e){

}
break;
//Question 6 c vi
case "#getport": // prints the port number
System.out.println("The port number is: "+Integer.toString(getPort()));
break;

default: //if someone typed the wrong command they would get an error message and a list of accepted commands
System.out.println("This is not a recognised command");
System.out.println("Please use one of the following:");
System.out.println("#quit, #stop, #close, #setport, #start or #getport");
break;
}

}

/**
* This method handles all data coming from the UI
*
* @param message The message from the UI.
*/


public void handleMessageFromServerUI(String message)
{
//Checks if the message is a command and redirects it to the helper method
if(message.charAt(0)=='#'){
commandHelperMethod(message);
}
else{
//Question 6b ii
//makes the messages comming from the server begin with 'SERVER MSG>'

sendToAllClients("SERVER MSG> "+message);

System.out.println("SERVER MSG> " + message);
}
}

//Class methods ***************************************************
Expand Down
Loading