Skip to content
82 changes: 61 additions & 21 deletions code/simplechat1/ClientConsole.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,9 @@ 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 host, int port, String id)
{
try
{
client= new ChatClient(host, port, this);
}
catch(IOException exception)
{
System.out.println("Error: Can't setup connection!"
+ " Terminating client.");
System.exit(1);
}
client= new ChatClient(host, port, id, this);
}


Expand All @@ -72,26 +63,50 @@ public void accept()

while (true)
{
message = fromConsole.readLine();
client.handleMessageFromClientUI(message);
message = fromConsole.readLine();
// For commands passed
if (message.equals("#sethost")) {
// User didn't enter full command for changing host
System.out.println("No host specified. Using default host...");
client.handleMessageFromClientUI("#sethost localhost");
}
// No commands passed. Just plain messages
else
client.handleMessageFromClientUI(message);
}
}
catch (Exception ex)
{
System.out.println
("Unexpected error while reading from console!");
System.out.println("Could not read 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.
* @param message The string fto be displayed.
*/
public void display(String message)
{
System.out.println("> " + message);
if (message.contains("#server")) {
// Remove #server and display server's maessage
int index = message.indexOf('#');
String mes = message.substring(0, index);
System.out.println("SERVER MSG> " + mes);
}
else {
// Split the message and print: loginID> msg (messages)
if (message.contains(" has loged on")) {
String[] mes = message.split(" ", 2);
System.out.println(mes[0] + " " + mes[1]);
}
else {
String[] mes = message.split(" ", 2);
System.out.println(mes[0] + "> " + mes[1]);
}
}

}


Expand All @@ -100,22 +115,47 @@ 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] Login-id of the user to connect to a server.
*/
public static void main(String[] args)
{
String clientID = "";
String host = "";
int port = 0; //The port number

try
{
host = args[0];
// Given that only clientID argument is mandatory, check if user entered additional arguments
if (args.length == 3) {
clientID = args[0];
host = args[1];
port = Integer.parseInt(args[2]);

}
else if (args.length == 2) {
// Determine the type of argument. Assume that second argument is port argument
clientID = args[0];
port = Integer.parseInt(args[1]);
host = "localhost";
}
else
clientID = args[0];
host = "localhost";
port = DEFAULT_PORT;
}
catch(ArrayIndexOutOfBoundsException e)
{
host = "localhost";
System.out.println("ERROR - No login ID specified. Connection aborted.");
System.exit(1);
}
ClientConsole chat= new ClientConsole(host, DEFAULT_PORT);
// The assumption was wrong.
catch (NumberFormatException e)
{
// Use the default port
port = DEFAULT_PORT;
host = args[1];
}
ClientConsole chat= new ClientConsole(host, port, clientID);
chat.accept(); //Wait for console data
}
}
Expand Down
164 changes: 117 additions & 47 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 @@ -17,13 +18,6 @@
*/
public class EchoServer extends AbstractServer
{
//Class variables *************************************************

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

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

/**
Expand All @@ -36,6 +30,14 @@ public EchoServer(int port)
super(port);
}

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

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


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

Expand All @@ -48,10 +50,108 @@ public EchoServer(int port)
public void handleMessageFromClient
(Object msg, ConnectionToClient client)
{
System.out.println("Message received: " + msg + " from " + client);
this.sendToAllClients(msg);
String mes = (String) msg;
// Don't display the command to the server interface and send it to other clients
if (mes.contains("#login"))
{
// Log the user into the system
int ind = mes.indexOf(" "); // Start of identification string
int len = mes.length(); // End of identiification string
String ID = mes.substring(ind+1, len);
client.setInfo("Login ID", ID);

// Indicate who connected to the administrator
System.out.println("A new client is attempting to connect to the server.");
System.out.println("Message received #login " + client.getInfo("Login ID") + " from null.");
System.out.println(client.getInfo("Login ID") + " has logged on.");
try {
// Add > so that client's console does not mistake the login as a message
client.sendToClient(client.getInfo("Login ID") + " has logged on.");
}
catch (IOException e) {
System.out.println("Could not notify the client!");
}


}
else {
System.out.println("Message received: " + msg + " from " + client.getInfo("Login ID"));
this.sendToAllClients(client.getInfo("Login ID") + " " + msg);
}
}


/**
* This method reads user input's from the console
*
* @param in Input from the console
*/
public void handleInputFromServerConsole(String input)
{
// Check if message is a command
if (input.charAt(0) == '#') {
// Split the message into two
String[] mes = input.split(" ", 2);
// Select appropriate function
switch(mes[0]) {
case "#close":
try {
close();
}
catch(IOException e)
{
System.out.println(": " + e);
}
break;
case "#stop":
stopListening();
break;
case "#quit":
try {
close();
System.exit(0);
}
catch(IOException e)
{
System.out.println(": " + e);
}
break;
case "#setport":
try {
setPort(Integer.parseInt(mes[1]));
}
catch (Exception e) {
System.out.println("No port specified. Using default port...");
setPort(5555);
}
break;
case "#start":
try {
listen();
}
catch(IOException e)
{
System.out.println(": " + e);
}
break;
case "#getport":
System.out.println(getPort());
break;
default:
System.out.println("Command not valid!");
break;
}
}
else
{

// Echo to the server console
System.out.println("SERVER MESSAGE> " + input);
// Send to the clients
input += ("#server"); // Add identifier
this.sendToAllClients(input);
}
}

/**
* This method overrides the one in the superclass. Called
* when the server starts listening for connections.
Expand All @@ -61,49 +161,19 @@ protected void serverStarted()
System.out.println
("Server listening for connections on port " + getPort());
}

/**
* This method overrides the one in the superclass. Called
* when the server stops listening for connections.
*/

protected void serverStopped()
{
System.out.println
("Server has stopped listening for connections.");
sendToAllClients("WARNING - Server has stopped listening for connections.");
}

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

/**
* This method is responsible for the creation of
* the server instance (there is no UI in this phase).
*
* @param args[0] The port number to listen on. Defaults to 5555
* if no argument is entered.
*/
public static void main(String[] args)
{
int port = 0; //Port to listen on

try
{
port = Integer.parseInt(args[0]); //Get port from command line
}
catch(Throwable t)
{
port = DEFAULT_PORT; //Set port to 5555
}

EchoServer sv = new EchoServer(port);

try
{
sv.listen(); //Start listening for connections
}
catch (Exception ex)
{
System.out.println("ERROR - Could not listen for clients!");
}
protected void serverClosed()
{
System.out.println("Server is closed");
sendToAllClients("WARNING - The server has stopped listening for connections. SERVER SHUTTING DOWN! DISCONNECTING!");
}

}
//End of EchoServer class
Loading