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
172 changes: 172 additions & 0 deletions code/simplechat2/ChatClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
// 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.*;

/**
* 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.
*/
private ChatIF clientUI;
private String loginID = "";


//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.
* @param loginID User entered loginID through cmd-line args
*/



public ChatClient(String host, int port, ChatIF clientUI, String loginID) throws IOException{
super(host, port); //Call the superclass constructor
this.clientUI = clientUI; //assign rest of instance variables
this.loginID = loginID;
if (loginID.equals("")){ //if login ID is blank, refuse connection
clientUI.display("Error - No login ID specified. Connection aborted.");
quit();
}
else{ //if login ID was entered, connect, and send loginID to server

try{
openConnection();
sendToServer("#login "+loginID);
clientUI.display(loginID+" has logged on.");
}
catch(Exception ex){clientUI.display("Cannot open connection. Awaiting command");}
}
}



//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{
//series of commands user can enter through the ClientUI
if (message.equals("#quit")){
quit();
}

else if ( message.equals("#logoff") ){
//if already not connected, display error message
if (!isConnected()){
clientUI.display("You are not connected to the server.");
}
else{
closeConnection();
clientUI.display("You have been disconnected from "+getHost()+".");
}
}

else if (message.equals("#login")){
//if you are already logged in, display error message.
if (isConnected()){
clientUI.display("You are already connected.");
}
else{
openConnection();
clientUI.display("You have connected to "+getHost()+".");
}
}

else if (message.equals("#gethost")){
clientUI.display("Current host: "+getHost());
}

else if (message.equals("#getport")){
clientUI.display("Current port: "+getPort());
}

else if (message.contains("#setport")){
if (!isConnected()){
int newPort = Integer.parseInt( message.substring(9) );
setPort(newPort);
clientUI.display("Port set to: "+getPort());
}
else{
clientUI.display("Cannot set port while connected to server.");
}
}


else if (message.contains("#sethost")){
if (!isConnected()){
String newHost = new String();
newHost = message.substring(9) ;
setHost(newHost);
clientUI.display("Host set to: "+getHost());
}
else{
clientUI.display("Cannot set host while connected to server.");
}
}
//if it is not a command, treat as a regular message to server
else{
sendToServer(message);}
}

//catch exceptions and disconnect from server
catch(IOException e){
clientUI.display("Could not send message to server. Terminating client.");
quit();
}
}


//disconnect from server if server shuts down while connected.
public void connectionException(Exception exception){
String exceptionString = exception.toString();
System.out.println("Abnormal termination of connection.");

}

/**
* This method terminates the client.
*/
public void quit(){
try{
closeConnection();
}
catch(IOException e) {}
System.exit(0);
}
}
//End of ChatClient class
127 changes: 127 additions & 0 deletions code/simplechat2/ClientConsole.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
// 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.*;

/**
* 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.
* @param loginID The login ID to connect with.
*/
public ClientConsole(String host, int port, String loginID)
{
try{
client = new ChatClient(host, port, this, loginID);
}

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();
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);
}


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

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

try{
loginID = args[0];
host = args[1];
port = Integer.parseInt(args[2]);

}
catch(ArrayIndexOutOfBoundsException e)
{
host = "localhost";
port = DEFAULT_PORT;
}



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