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 Test Cases.zip
Binary file not shown.
46 changes: 29 additions & 17 deletions code/simplechat1/ClientConsole.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,20 @@ 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.exit(1);
System.out.println("Cannot open connection. Awaiting command.");
System.exit(1); // Added because commands don't work..
}
}


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

/**
Expand All @@ -78,8 +77,7 @@ public void accept()
}
catch (Exception ex)
{
System.out.println
("Unexpected error while reading from console!");
System.out.println("Unexpected error while reading from console!");
}
}

Expand All @@ -100,22 +98,36 @@ public void display(String message)
/**
* This method is responsible for the creation of the Client UI.
*
* @param args[0] The host to connect to.
* @param args[0] The loginID to connect to.
* @param args[1] The host to connect to.
* @param args[2] The port to connect to.
*/
public static void main(String[] args)
{
String loginID = "";
String host = "";
int port = 0; //The port number

try
{
host = args[0];
int port = 5555; //The port number
try{
loginID = args[0];
}
catch(ArrayIndexOutOfBoundsException e)
{
catch(ArrayIndexOutOfBoundsException ex){
System.out.println("You need to enter a login ID. Terminating client..");
System.exit(1);
}
try{
host = args[1];
}
catch(ArrayIndexOutOfBoundsException ex){
host = "localhost";
}
ClientConsole chat= new ClientConsole(host, DEFAULT_PORT);
try{
port = Integer.parseInt(args[2]);
}
catch(ArrayIndexOutOfBoundsException ex)
{
port = DEFAULT_PORT;
}
ClientConsole chat= new ClientConsole(loginID, host, port);
chat.accept(); //Wait for console data
}
}
Expand Down
128 changes: 117 additions & 11 deletions code/simplechat1/EchoServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import java.io.*;
import ocsf.server.*;
import common.*;

/**
* This class overrides some of the methods in the abstract
Expand All @@ -25,7 +26,7 @@ public class EchoServer extends AbstractServer
final public static int DEFAULT_PORT = 5555;

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

ChatIF serverUI;
/**
* Constructs an instance of the echo server.
*
Expand All @@ -36,6 +37,11 @@ public EchoServer(int port)
super(port);
}

public EchoServer(int port, ChatIF serverUI) throws IOException {
super(port);
this.serverUI = serverUI;
}


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

Expand All @@ -45,21 +51,108 @@ 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){
if(msg.toString().startsWith("#login")){
if(client.getInfo("loginID") != null){
try{
System.out.println("You are already logged in.");
}
catch(Exception e){}
}
client.setInfo("loginID",msg.toString().substring(8));
try{
client.sendToClient(client.getInfo("loginID") + " has logged on.");
}
catch(IOException g){}
}
else{
try{
if(client.getInfo("loginID") == null){
System.out.println("Could not login.. use #login has first argument!");
client.close();
}
}
catch(IOException e){
e.printStackTrace();
}
}
System.out.println("Message received: " + msg + " from " + client.getInfo("loginID") + ", Users IP: " + client);
if(msg.toString().startsWith("#login")){
if(client.getInfo("loginID") != null){
try{
System.out.println(client.getInfo("loginID") + " has logged on.");
}
catch(Exception gg){}
}
}
this.sendToAllClients(client.getInfo("loginID") + " > " + msg);
}

public void handleMessageFromServerUI(String message){
try{
if(message.charAt(0) == '#'){
if(message.equals("#quit")){
stopListening();
close();
System.out.println("Server is quitting..");
System.exit(1);
}
if(message.equals("#stop")){
stopListening();
this.sendToAllClients("WARNING - Server has stopped listening for connections.");
}
if(message.equals("#close")){
close();
}
if(message.startsWith("#setport")){
if(!isListening()){
try{
String p = message.substring(9);
setPort(Integer.parseInt(p));
System.out.println("The server port has been changed to: " + getPort());
}
catch(Exception ex){
System.out.println("Could not set the server port!");
}
}
else{
System.out.println("Server must be stopped.");
}
}
if(message.equals("#start")){
if(!isListening()){
try{
listen();
}
catch(Exception el){
System.out.println("Could not listen for new connections.");
}
}
else{
System.out.println("Server must be stopped.");
}
}
if(message.equals("#getport")){
System.out.println("Port: " + getPort());
}
}
else{
serverUI.display(message);
this.sendToAllClients("SERVER MSG> " + message);
}
}
catch(IOException e){
System.out.println("Could not send message from server. Terminating server.");
System.exit(1);
}
}

/**
* This method overrides the one in the superclass. Called
* when the server starts listening for connections.
*/
protected void serverStarted()
{
System.out.println
("Server listening for connections on port " + getPort());
System.out.println("Server listening for connections on port " + getPort());
}

/**
Expand All @@ -68,9 +161,22 @@ protected void serverStarted()
*/
protected void serverStopped()
{
System.out.println
("Server has stopped listening for connections.");
System.out.println("Server has stopped listening for connections.");
}

protected void clientConnected(ConnectionToClient client) {
System.out.println("A new client is attempting to connect to the server.");
}
synchronized protected void clientDisconnected(ConnectionToClient client) {
System.out.println(client.getInfo("loginID") + " has disconnected from the server.");
this.sendToAllClients(client.getInfo("loginID") + " has disconnected from the server.");
}
synchronized protected void clientException(ConnectionToClient client, Throwable exception) {
System.out.println(client.getInfo("loginID") + " has disconnected from the server.");
this.sendToAllClients(client.getInfo("loginID") + " has disconnected from the server.");
}



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

Expand Down
128 changes: 128 additions & 0 deletions code/simplechat1/ServerConsole.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
// 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 server 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 ServerConsole implements ChatIF
{
//Class variables *************************************************

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

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

/**
* The instance of the EchoServer class.
*/
EchoServer server;


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

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


//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();
server.handleMessageFromServerUI(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 host to connect to.
*/
public static void main(String[] args)
{
int port = 5555; //The port number

try
{
port = Integer.parseInt(args[0]);
}
catch(ArrayIndexOutOfBoundsException e)
{
port = DEFAULT_PORT;
}
ServerConsole chat = new ServerConsole(port);
chat.accept(); //Wait for console data
}
}
//End of ConsoleChat class
Loading