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
112 changes: 79 additions & 33 deletions code/simplechat1/ClientConsole.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/*
* This is the edited version of the ClientConsole class for the Assignment1.
* All edits made specifically for the question number will be added as a comment
*/

// 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
Expand Down Expand Up @@ -40,12 +45,14 @@ public class ClientConsole implements ChatIF
*
* @param host The host to connect to.
* @param port The port to connect on.
* new edit #1 E6 a)
* @param loginID The client login ID
*/
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 All @@ -62,35 +69,66 @@ public ClientConsole(String host, int port)
* 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));
public void accept(){
boolean loginStatus = true;
try{
BufferedReader fromConsole = new BufferedReader(new InputStreamReader(System.in));
String message;

while (true)
{
while (true) {
message = fromConsole.readLine();
client.handleMessageFromClientUI(message);
//new edit #2 E6 a)
if(message.contains("#")){
if (message.equals("#quit")){
System.exit(0);
}else if(message.equals("#gethost")){
System.out.println(client.getHost());
}else if (message.equals("#getport")){
System.out.println(client.getPort());
}else if (message.equals("#logoff")){
loginStatus = false;
client.handleMessageFromClientUI(client.getLogin()+" has logged off");
System.out.println("the connection has closed.");
}else if (message.equals("#login")){
if(loginStatus==true){
System.out.println("You are logged in");
}
loginStatus = true;
}else if (message.contains("#sethost")){
if (loginStatus==false){
client.setHost(message.split(" ")[1]);
}else{
System.out.println("Cannot set host while logged in.");
}
}else if (message.contains("#setport")){
if (loginStatus==false) {
String tmp =message.split(" ")[1];
int tmp1 = Integer.parseInt(tmp);
client.setPort(tmp1);
}else{
System.out.println("Cannot set port while logged in.");
}
}else{
System.out.println("This function does not exist.");
}
}else{
if(loginStatus==true){
client.handleMessageFromClientUI(message);
}else{
System.out.println("You are logged out, please log in.");
}
}
}
}
catch (Exception ex)
{
System.out.println
("Unexpected error while reading from console!");
}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)
{
public void display(String message){
System.out.println("> " + message);
}

Expand All @@ -101,22 +139,30 @@ public void display(String message)
* This method is responsible for the creation of the Client UI.
*
* @param args[0] The host to connect to.
* new edit#3 E6a)
*/
public static void main(String[] args)
{
public static void main(String[] args){
String host = "";
String loginID="";
int port = 0; //The port number

try
{
host = args[0];
}
catch(ArrayIndexOutOfBoundsException e)
{
try{
host = args[1];
}catch(ArrayIndexOutOfBoundsException e){
host = "localhost";
}
ClientConsole chat= new ClientConsole(host, DEFAULT_PORT);
chat.accept(); //Wait for console data
try{
port = Integer.parseInt(args[2]);
}catch(ArrayIndexOutOfBoundsException e){
//new edit #2 E5b)
port = DEFAULT_PORT;
}
//new edit #3 E5b)
if (args.length !=0){
loginID = args[0];
ClientConsole chat=new ClientConsole(loginID,host,port);
chat.accept(); //Wait for console data
}else{
System.out.println("You didn't login, please login.");
}
}
}
//End of ConsoleChat class
}
89 changes: 76 additions & 13 deletions code/simplechat1/EchoServer.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
/*
* This is the edited version of the EchoServer class for the Assignment1.
* All edits made specifically for the question number will be added as a comment
*/

// 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 ocsf.server.*;
//new edit #1 E6 b)
import ocsf.server.*;
import common.ChatIF;

/**
* This class overrides some of the methods in the abstract
Expand All @@ -23,17 +31,25 @@ public class EchoServer extends AbstractServer
* The default port to listen on.
*/
final public static int DEFAULT_PORT = 5555;


/**
*new edit #2 E6b)
*Instance variables
*/
ChatIF serverUI;

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

/**
* Constructs an instance of the echo server.
*
* @param port The port number to connect on.
* new edit #3 E6 b) line 48, line 51
*/
public EchoServer(int port)
public EchoServer(int port, ChatIF serverUI)
{
super(port);
this.serverUI = serverUI;
}


Expand All @@ -44,33 +60,80 @@ public EchoServer(int port)
*
* @param msg The message received from the client.
* @param client The connection from which the message originated.
* new edit #4 E6 b)
*/
public void handleMessageFromClient
(Object msg, ConnectionToClient client)
public void handleMessageFromClient (Object msg, ConnectionToClient client)
{
System.out.println("Message received: " + msg + " from " + client);
this.sendToAllClients(msg);
String tmp = msg.toString();
if (tmp.contains("#login")){
System.out.println(msg);
this.sendToAllClients("Server MSG>: "+tmp.split(" ")[1]+".");
client.setInfo("LoginID", tmp.split(" ")[1]);
}else{
System.out.println("Message received: " + msg + " from " + client);
this.sendToAllClients(client.getInfo("Login ID ")+""+msg);
}
}

/**
* This method overrides the one in the superclass. Called
* when the server starts listening for connections.
* new edit #5 E6 b)
*/
protected void serverStarted()
{
System.out.println
("Server listening for connections on port " + getPort());
System.out.println ("Server listening for connections on port " + getPort());
try{
listen();
}catch(IOException exception){
System.out.println("Error");
}
}


/**
* new edit #1 E5c)
* This method shows if the client has made a connection with the server
* @param client the client that is trying to make the connetion
* new edit #7 E6 b) line 100
*/
protected void clientConnected(ConnectionToClient client){
System.out.println("A client is connected: "+client.getInfo("Login ID"));
}

/**
* new edit #2 E5c)
* This method shows if the client has disconnected with the server
* @param client the client that has disconnected with the server
* new edit #8 E6 b) line 109
*/
synchronized protected void clientDisconnected(ConnectionToClient client){
System.out.println("A client is disconnected: "+client.getInfo("Login ID"));
}

/**
* new edit #3 E5c)
* This method is called whenever an exception is thrown
* @param client the client that had the exception
* new edit #9 E6 b) line 119
*/
synchronized protected void clientException(ConnectionToClient client, Throwable exception){
System.out.println("Client error: "+client.getInfo("Login ID"));
}



/**
* This method overrides the one in the superclass. Called
* when the server stops listening for connections.
* new edit #6 E6 b)
*/
protected void serverStopped()
{
System.out.println
("Server has stopped listening for connections.");
stopListening();
System.out.println("Server has stopped listening for connections.");
}
}

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

Expand All @@ -81,7 +144,7 @@ protected void serverStopped()
* @param args[0] The port number to listen on. Defaults to 5555
* if no argument is entered.
*/
public static void main(String[] args)
/* public static void main(String[] args)
{
int port = 0; //Port to listen on

Expand All @@ -105,5 +168,5 @@ public static void main(String[] args)
System.out.println("ERROR - Could not listen for clients!");
}
}
}
//End of EchoServer class
}*/
//End of EchoServer class
Binary file added code/simplechat1/SEG2105 A1 Testcases.pdf
Binary file not shown.
83 changes: 83 additions & 0 deletions code/simplechat1/ServerConsole.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* This is the ServerConsole calss for the Assignment1.
* Helps Echoserver access multiple methods and variables specific to the class
*/

import java.io.*;
import client.*;
import common.*;

public class ServerConsole implements ChatIF {
private EchoServer server;
final public static int DEFAULT_PORT = 5555;
public ServerConsole(int port) {
server = new EchoServer(port,this);
}
public void display(String message) {
server.sendToAllClients("ServerMSG> " + message);
}
public void handleMessageFromConsole(String message){
display(message);
}
public void accept(){
try{
BufferedReader fromConsole = new BufferedReader(new InputStreamReader(System.in));
String message;
boolean status = true;
while (true) {
message = fromConsole.readLine();
if(message.contains("#")){
if(message.equals("#quit")){
System.exit(0);
}else if(message.equals("#stop")){
server.serverStopped();
status = false;
}else if(message.equals("#start")){
server.serverStarted();
status= true;
}else if(message.equals("#close")) {
server.close();
}else if(message.contains("#setport")){
if(status==false){
String temp =message.split(" ")[1];
int temp1 = Integer.parseInt(temp);
server.setPort(temp1);
}else{
System.out.println("Cannot set port while connections are made");
}
}else if (message.equals("#getport")) {
System.out.println(server.getPort());
}else{
System.out.println("This function does not exist.");
}
}else{
handleMessageFromConsole(message);
}
}
}
catch (Exception ex){
System.out.println("Unexpected error while reading from console!");
}
}

public EchoServer get(){
return server;
}

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
}
ServerConsole server = new ServerConsole(port);
try{
server.get().listen(); //Start listening for connections
server.accept();
}
catch (Exception ex){
System.out.println("ERROR - Could not listen for clients!");
}
}
}
Loading