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
210 changes: 210 additions & 0 deletions ChatClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
// This file contains material supporting section 3.7 of the textbook:
// "Object Oriented Software Engineering" and is issued under the open-source
// license found at www.lloseng.com

package client;

import ocsf.client.*;
import common.*;
import java.io.*;
import java.util.Arrays;
import java.util.List;

/**
* This class overrides some of the methods defined in the abstract
* superclass in order to give more functionality to the client.
*
* @author Dr Timothy C. Lethbridge
* @author Dr Robert Laganiè
* @author François Bélanger
* @version July 2000
*/
public class ChatClient extends AbstractClient
{
//Instance variables **********************************************

/**
* The interface type variable. It allows the implementation of
* the display method in the client.
*/
ChatIF clientUI;


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

/**
* Constructs an instance of the chat client.
*
* @param host The server to connect to.
* @param port The port number to connect on.
* @param clientUI The interface type variable.
*/

public ChatClient(String host, int port, ChatIF clientUI)
throws IOException
{
super(host, port); //Call the superclass constructor
this.clientUI = clientUI;
openConnection();
}


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

/**
* This method handles all data that comes in from the server.
*
* @param msg The message from the server.
*/
public void handleMessageFromServer(Object msg)
{
clientUI.display(msg.toString());
}

/**
* This method handles all data coming from the UI
*
* @param message The message from the UI.
*/
public void handleMessageFromClientUI(String message)
{
try
{
sendToServer(message);
}
catch(IOException e)
{
clientUI.display
("Could not send message to server. Terminating client.");
quit();
}
}

/**
* This method terminates the client.
*/
public void quit()
{
try
{
closeConnection();
}
catch(IOException e) {}
System.exit(0);
}

/**
* Hook method called after the connection has been closed. The default
* implementation does nothing. The method may be overriden by subclasses to
* perform special processing such as cleaning up and terminating, or
* attempting to reconnect.
*/
protected void connectionClosed() {
System.out.println("SYSTEM ::: Connection to the Server Terminated");
}

/**
* Hook method called each time an exception is thrown by the client's
* thread that is waiting for messages from the server. The method may be
* overridden by subclasses.
*
* @param exception
* the exception raised.
*/
protected void connectionException(Exception exception) {
System.out.println("SYSTEM ::: Server Shutdown");
quit();
}

/**
* This method contains all the client commands.
* Every commands begin with '#'.
*
* @param command word that needs to be actioned.
*/
public void clientCommand(String message){

//Make the message received into an array (necessary for setHost/setPort)
String[] messageArray = message.split(" ");
String[] commandArray = new String[]{"#quit","#login","#logoff","#gethost","#sethost","#getport","#setport"};

//If the command isn't in the 'known' list, give error message and command list.
if (!Arrays.asList(commandArray).contains(messageArray[0])){
System.out.println("The command line you entered is 'INVALID', here is a list of all current commands.");
System.out.println("#quit // #login // #logoff // #gethost // #sethost // #getport // #setport");
}

switch(messageArray[0]) {
case "#quit":
quit();

case "#login":
//what happens in #login
if (isConnected()){
System.out.println("SYSTEM ::: The client is already connected");
return;
}
else {
try
{
openConnection();
System.out.println("SYSTEM ::: Client connected");
}
catch (IOException ex){System.out.println("SYSTEM ::: Failed to connect to Server");}
return;
}

case "#logoff":
try
{
System.out.println("SYSTEM ::: Shutting down client");
closeConnection();
return;
}
catch(IOException e) {}
return;

case "#gethost":
System.out.println("SYSTEM ::: The Host name is: " + getHost());
return;

case "#sethost":

try
{
setHost(messageArray[1]);
System.out.println("SYSTEM ::: The new Host has been set to: " + getHost());
} catch (Exception ex2)
{
System.out.println("SYSTEM ::: Array Out of Bound: Please try again with the new Host name.");
} return;

case "#getport":

try
{
System.out.println("SYSTEM ::: The current Port is: " + getPort());
}

catch (Exception a)
{
System.out.println("SYSTEM ::: There was an error in the getPort attempt");
}
return;

case "#setport":

try {
int newPort = Integer.parseInt(messageArray[1]);

setPort(newPort);
System.out.println("SYSTEM ::: The new Port has been set to: " + getPort());
} catch (Exception ex2)
{
System.out.println("SYSTEM ::: Array Out-of-Bound or your Port was not a valid Integer between 1 and 65535.");
} return;
}
}

}
//End of ChatClient class
177 changes: 177 additions & 0 deletions ClientConsole.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
// This file contains material supporting section 3.7 of the textbook:
// "Object Oriented Software Engineering" and is issued under the open-source
// license found at www.lloseng.com

import java.io.*;
import client.*;
import common.*;
import java.util.Arrays;
import java.util.List;

/**
* This class constructs the UI for a chat client. It implements the
* chat interface in order to activate the display() method.
* Warning: Some of the code here is cloned in ServerConsole
*
* @author François Bélanger
* @author Dr Timothy C. Lethbridge
* @author Dr Robert Laganière
* @version July 2000
*/
public class ClientConsole implements ChatIF
{
//Class variables *************************************************

/**
* The default port to connect on.
*/
final public static int DEFAULT_PORT = 5555;

//Instance variables **********************************************

/**
* The instance of the client that created this ConsoleChat.
*/
ChatClient client;


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

/**
* Constructs an instance of the ClientConsole UI.
*
* @param host The host to connect to.
* @param port The port to connect on.
*/
public ClientConsole(String host, int port)
{
try
{
client= new ChatClient(host, port, this);
}
catch(IOException exception)
{
System.out.println("Error: Can't setup connection!"
+ " Terminating client.");
System.exit(1);
}
}


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

/**
* This method waits for input from the console. Once it is
* received, it sends it to the client's message handler.
*/
public void accept()
{
try
{
BufferedReader fromConsole =
new BufferedReader(new InputStreamReader(System.in));
String message;

while (true)
{
message = fromConsole.readLine();


//This is where I decide what to do with the code.

if ( message.charAt(0) == '#' ){
client.clientCommand(message);}

else {client.handleMessageFromClientUI(message);}

}
}
catch (Exception ex)
{
System.out.println
("Unexpected error while reading from console!");
}
}

/**
* This method overrides the method in the ChatIF interface. It
* displays a message onto the screen.
*
* @param message The string to be displayed.
*/
public void display(String message)
{
System.out.println("> " + message);
}

/**
* Hook method called after the connection has been closed. The default
* implementation does nothing. The method may be overriden by subclasses to
* perform special processing such as cleaning up and terminating, or
* attempting to reconnect.
*/
protected void connectionClosed() {
System.out.println("The server has shutdown.");
}

/**
* Hook method called each time an exception is thrown by the client's
* thread that is waiting for messages from the server. The method may be
* overridden by subclasses.
*
* @param exception
* the exception raised.
*/
protected void connectionException(Exception exception) {
connectionClosed();
}


//Class methods ***************************************************

/**
* This method is responsible for the creation of the Client UI.
*
* @param args[0] The host to connect to.
*/
public static void main(String[] args)
{
String host = "";
int port = 0; //The port number

try
{
host = args[0];
port = Integer.parseInt(args[1]);
}
catch(ArrayIndexOutOfBoundsException e)
{
port = DEFAULT_PORT;

try
{
host = args[0];
}
catch(ArrayIndexOutOfBoundsException f)
{
host = "localhost";
}
}

if (args.length == 0)
{
System.out.println("Host Name: " + host + " (default)");
}
else { System.out.println("Host Name: " + host);}

if (port == DEFAULT_PORT)
{
System.out.println("Port Number: " + port + " (default)");
}
else {System.out.println("Port Number: " + port);}

ClientConsole chat= new ClientConsole(host, port);
chat.accept(); //Wait for console data
}
}
//End of ConsoleChat class
Loading