Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
ad1bf6f
'Test'
stevenli5 Jun 8, 2020
49ea28e
Testing
stevenli5 Jun 8, 2020
c7ac1bc
Added
stevenli5 Jun 8, 2020
5a9db0c
Added connectionClosed and connectionException
stevenli5 Jun 8, 2020
f7b5456
Adjusted message for connectionException
stevenli5 Jun 9, 2020
e8f3660
E5b) Adding support for obtaining port number from command line
stevenli5 Jun 9, 2020
87c2684
Better support for E5b)
stevenli5 Jun 9, 2020
4a14bb7
E5c) Server side now recognizes when a client connects or disconnects
stevenli5 Jun 9, 2020
79588e1
Implemented clientUI.display
stevenli5 Jun 18, 2020
66823b9
E6a) Implemented new commands
stevenli5 Jun 18, 2020
5857869
E6a) Fixed an issue with throwing IOException
stevenli5 Jun 18, 2020
e683029
E6b) Created ServerConsole.java
stevenli5 Jun 18, 2020
e6bd5be
E6b) Small edit to differentiate from ClientConsole.java
stevenli5 Jun 18, 2020
bb70a95
E6b) Implemented ServerConsole
stevenli5 Jun 19, 2020
57f67b9
E6b) Removed unnecessary comments
stevenli5 Jun 19, 2020
600e267
E6c) Implemented handleMessageFromServerUI
stevenli5 Jun 19, 2020
4a618c5
E7a) Added new loginID argument
stevenli5 Jun 19, 2020
363164b
E7a) Added new loginID argument
stevenli5 Jun 19, 2020
4fe7495
E7b) Added login message
stevenli5 Jun 19, 2020
5f69fa8
E7a) Added support in main for loginID
stevenli5 Jun 19, 2020
e719cf3
Removed unnecessary import
stevenli5 Jun 19, 2020
20842f5
E7c) Implemented support for loginID for server side
stevenli5 Jun 19, 2020
9aed8fc
Edited print line to match Testcase 2002
stevenli5 Jun 19, 2020
98068a2
Edited print line to match Testcase 2004
stevenli5 Jun 19, 2020
d394078
Edited print line to match Testcase 2005
stevenli5 Jun 19, 2020
cac219a
Fixed unnecessary print outs when logging off
stevenli5 Jun 19, 2020
31a53eb
Add files via upload
stevenli5 Jun 19, 2020
0bd27f0
Delete SEG2105_A1-TESTCASES_300120889.pdf
stevenli5 Jun 19, 2020
35fd6bc
Added test cases
stevenli5 Jun 19, 2020
672fbfc
Delete SEG2105_A1-TESTCASES_300120889.pdf
stevenli5 Jan 26, 2021
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
32 changes: 22 additions & 10 deletions code/simplechat1/ClientConsole.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ 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)
{
Expand Down Expand Up @@ -104,18 +104,30 @@ public void display(String message)
*/
public static void main(String[] args)
{
String loginID = "";
String host = "";
int port = 0; //The port number

try
{
host = args[0];
}
catch(ArrayIndexOutOfBoundsException e)
{
host = "localhost";
try {
loginID = args[0];
host = args[1];
port = Integer.parseInt(args[2]);
} catch(ArrayIndexOutOfBoundsException e1) {
port = DEFAULT_PORT;
try {
loginID = args[0];
host = args[1];
} catch (ArrayIndexOutOfBoundsException e2) {
host = "localhost";
try {
loginID = args[0];
} catch (ArrayIndexOutOfBoundsException e3) {
System.out.println("ERROR - No login ID specified. Connection aborted.");
System.exit(1);
}
}
}
ClientConsole chat= new ClientConsole(host, DEFAULT_PORT);
ClientConsole chat= new ClientConsole(loginID, host, port); // MUST implement loginID to main
chat.accept(); //Wait for console data
}
}
Expand Down
121 changes: 119 additions & 2 deletions code/simplechat1/EchoServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,89 @@ public EchoServer(int port)
public void handleMessageFromClient
(Object msg, ConnectionToClient client)
{
System.out.println("Message received: " + msg + " from " + client);
this.sendToAllClients(msg);
if (msg.toString().startsWith("#login")) {
String[] message = msg.toString().split(" ");
if (message.length != 0) {
if (client.getInfo("loginID") == null) {
client.setInfo("loginID", message[1]);
this.sendToAllClients(client.getInfo("loginID") + " has logged on.");
} else {
try {
client.sendToClient("Username cannot be changed.");
} catch (IOException e) {
//
}
}
}
} else {
if (client.getInfo("loginID") == null) {
try {
client.sendToClient("Error: No username provided.");
client.close();
} catch (IOException e) {
//
}
} else {
System.out.println("Message received: " + msg + " from " + client.getInfo("loginID"));
this.sendToAllClients(client.getInfo("loginID") + "> " + msg);
}
}
}

public void handleMessageFromServerUI(String message)
{
if (message.startsWith("#")) {
String[] cmd = message.split(" ");
switch (cmd[0]) {
case "#quit":
try {
close();
System.out.println("Connection terminated.");
} catch (IOException e) {
System.out.println("Unable to terminate connection.");
}
break;
case "#stop":
this.sendToAllClients("Server has stopped listening for new clients.");
this.stopListening();
break;
case "#close":
this.sendToAllClients("Server has stopped listening for new clients.");
this.stopListening();
try {
close();
System.out.println("Connection terminated.");
} catch (IOException e) {
System.out.println("Unable to terminate connection.");
}
break;
case "#setport":
if (!isListening()) {
this.setPort(Integer.parseInt(cmd[1]));
System.out.println("New port has been set.");
} else {
System.out.println("Server must be closed.");
}
break;
case "#start":
if (!isListening()) {
try {
listen();
} catch (IOException e) {
System.out.println("Unable to listen for new clients.");
}
} else {
System.out.println("Server must be stopped.");
}
break;
case "#getport":
System.out.println("Current port: " + this.getPort());
break;
}
} else {
this.sendToAllClients("SERVER MSG> " + message);
}
}

/**
* This method overrides the one in the superclass. Called
Expand All @@ -72,6 +152,43 @@ protected void serverStopped()
("Server has stopped listening for connections.");
}

/**
* Hook method called each time a new client connection is
* accepted. The default implementation does nothing.
* @param client the connection connected to the client.
*/
protected void clientConnected(ConnectionToClient client) {
if (client.getInfo("loginID") != null) {
System.out.println(client.getInfo("loginID") + " has connected to the server.");
} else {
System.out.println("A client has connected 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.
*/
synchronized protected void clientDisconnected(ConnectionToClient client) {
this.sendToAllClients(client.getInfo("loginID") + " has disconnected from the server.");
}

/**
* 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.
*/
synchronized protected void clientException(ConnectionToClient client, Throwable exception) {
clientDisconnected(client);
}

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

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

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

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

EchoServer server;

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

/**
* Constructs an instance of the ClientConsole UI.
*
* @param host The host to connect to.
* @param port The port to connect on.
*/
public ServerConsole(int port)
{
server = new EchoServer(port);
try
{
server.listen();
}
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();
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("SERVER MSG> " + 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 = 0;

try {
port = Integer.parseInt(args[0]);
} catch(ArrayIndexOutOfBoundsException e) {
port = DEFAULT_PORT;
}
ServerConsole chat = new ServerConsole(port);
chat.accept();
}

}
//End of ServerConsole class
Loading