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 Phase 2 PDF.pdf
Binary file not shown.
36 changes: 30 additions & 6 deletions code/simplechat1/ClientConsole.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ 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!"
System.out.println("Can't setup connection!"
+ " Terminating client.");
System.exit(1);
}
Expand Down Expand Up @@ -104,18 +104,42 @@ public void display(String message)
*/
public static void main(String[] args)
{
String host = "";
String loginid;
String host;
int port = 0; //The port number

try
{
host = args[0];
loginid = args[0];
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("ERROR: No login ID provided.");
System.exit(1);
return;

}

//set host
try
{
host = args[1];
}
catch(ArrayIndexOutOfBoundsException e)
{
host = "localhost";
}
ClientConsole chat= new ClientConsole(host, DEFAULT_PORT);
// set port
try
{
port = Integer.parseInt(args[2]);
}
catch(ArrayIndexOutOfBoundsException e)
{
port = DEFAULT_PORT;
}

ClientConsole chat= new ClientConsole(loginid, host, port);
chat.accept(); //Wait for console data
}
}
Expand Down
119 changes: 114 additions & 5 deletions code/simplechat1/EchoServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* @author Paul Holden
* @version July 2000
*/
public class EchoServer extends AbstractServer
public class EchoServer extends AbstractServer
{
//Class variables *************************************************

Expand Down Expand Up @@ -48,8 +48,36 @@ public EchoServer(int port)
public void handleMessageFromClient
(Object msg, ConnectionToClient client)
{
System.out.println("Message received: " + msg + " from " + client);
this.sendToAllClients(msg);
String message = msg.toString();
if (message.startsWith("#")) {
String[] splitMessage = message.substring(1).split(" ");
if (splitMessage.length > 1 && splitMessage[0]==("login")) {
if (client.getInfo("loginid") == null) {
client.setInfo("loginid", splitMessage[1]);
System.out.println(client.getInfo("loginid")+" has logged on.");
this.sendToAllClients(client.getInfo("loginid") + " has logged on.");
} else {
try {
client.sendToClient("ERROR: Your username has been set already.");
} catch (IOException e) {
System.out.print("An error has occurred.");
}
}

}
} else {
if (client.getInfo("loginid") == null) {
try {
client.sendToClient("ERROR: You must set a username.");
client.close();
} catch (IOException e) {
System.out.print("An error has occurred.");
}
} else {
System.out.println("Message received: " + msg + " from " + client.getInfo("loginid"));
this.sendToAllClients(client.getInfo("loginid") + " > " + message);
}
}
}

/**
Expand All @@ -71,6 +99,87 @@ protected void serverStopped()
System.out.println
("Server has stopped listening for connections.");
}

public void clientConnected(ConnectionToClient client){
if(client.getInfo("loginid")!=null){
System.out.println
(client.getInfo("loginid") + " has connected to the chat.");
}
else{
System.out.println("A client has connected to the chat.");
}
}

@Override
public void clientDisconnected(ConnectionToClient client){
System.out.println
(client.getInfo("loginid") + " has disconnected from the chat.");
this.sendToAllClients(client.getInfo("loginid") + " has disconnected from the chat.");
}

public void handleMessageFromServer(String message)
{
if(message.startsWith("#")){
String[] splitMessage = message.split(" ");
switch (splitMessage[0]){
case "#quit":
try {
System.out.println("Quitting...");
this.close();
System.out.println("Server successfully quit.");
}
catch(IOException exception){
System.out.println("An error occurred when trying to close the server.");
}
break;
case "#stop":
this.sendToAllClients("WARNING: the server has stopped listening for connections.");
this.stopListening();
break;
case "#close":
this.stopListening();
try {
this.close();
System.out.println("The server is now closed.");
}
catch(IOException exception){
System.out.println("An error occurred when trying to close the server.");
}
break;
case "#setport":
if (!this.isListening()) {
super.setPort(Integer.parseInt(splitMessage[1]));
System.out.println("New port: " + Integer.parseInt(splitMessage[1]));
} else {
System.out.println("An error occurred when changing the port.");
}
break;
case "#start":
if (!this.isListening()) {
try{this.listen();
System.out.println("The server has begun listening for connections.");
}
catch(IOException exception){
System.out.println("An unexpected error occurred.");
}
}
else{
System.out.println("The server has already started.");
}
break;
case "#getport":
System.out.println("Current port: " + this.getPort());
break;
default:
System.out.println("Invalid command.");
break;
}
}
else{
sendToAllClients("SERVER MSG>" + message);
}

}

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

Expand All @@ -94,11 +203,11 @@ public static void main(String[] args)
port = DEFAULT_PORT; //Set port to 5555
}

EchoServer sv = new EchoServer(port);
ServerConsole sv = new ServerConsole(port);

try
{
sv.listen(); //Start listening for connections
sv.accept(); //Start listening for connections
}
catch (Exception ex)
{
Expand Down
120 changes: 120 additions & 0 deletions code/simplechat1/ServerConsole.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@

import java.io.*;
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 **********************************************

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


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

/**
* Constructs an instance of the ClientConsole UI.
*
* @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 server.");
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.handleMessageFromServer(message);
this.display(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;

// set port
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 ServerConsole class
Loading