Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ocsf phase2 #28

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Binary file added .DS_Store
Binary file not shown.
539 changes: 539 additions & 0 deletions PHASE1TestCases

Large diffs are not rendered by default.

827 changes: 827 additions & 0 deletions PHASE2TestCases

Large diffs are not rendered by default.

Binary file added V2Code/.DS_Store
Binary file not shown.
Binary file added V2Code/com/.DS_Store
Binary file not shown.
Binary file added V2Code/com/lloseng/.DS_Store
Binary file not shown.
Binary file added V2Code/com/lloseng/ocsf/.DS_Store
Binary file not shown.
Binary file added code/.DS_Store
Binary file not shown.
Binary file added code/ocsf/.DS_Store
Binary file not shown.
Binary file added code/postal/.DS_Store
Binary file not shown.
Binary file added code/simplechat1/.DS_Store
Binary file not shown.
12 changes: 8 additions & 4 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 host, int port,String loginID)
{
try
{
client= new ChatClient(host, port, this);
client= new ChatClient(host, port, this,loginID);
}
catch(IOException exception)
{
Expand Down Expand Up @@ -105,17 +105,21 @@ public void display(String message)
public static void main(String[] args)
{
String host = "";
String loginID="";
int port = 0; //The port number

try
{
host = args[0];
loginID = args[0];
host = args[1];
port = Integer.parseInt(args[2]);
}
catch(ArrayIndexOutOfBoundsException e)
{
host = "localhost";
port = DEFAULT_PORT;
}
ClientConsole chat= new ClientConsole(host, DEFAULT_PORT);
ClientConsole chat= new ClientConsole(host,port,loginID);
chat.accept(); //Wait for console data
}
}
Expand Down
195 changes: 185 additions & 10 deletions code/simplechat1/EchoServer.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
// Test push
// 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.*;
import common.*;

/**
* This class overrides some of the methods in the abstract
Expand All @@ -23,6 +25,8 @@ public class EchoServer extends AbstractServer
* The default port to listen on.
*/
final public static int DEFAULT_PORT = 5555;
private boolean serverIsOpen = false ;
private ChatIF serverUI;

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

Expand All @@ -35,31 +39,164 @@ public EchoServer(int port)
{
super(port);
}
public EchoServer(int port,ChatIF serverUI) throws IOException{
super(port);
this.serverUI = serverUI;
listen();
}


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

/**
* This method handles any messages received from the client.
*
* @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){
String mesg = (String)msg;

if (mesg.contains("#login")){
String loginID = mesg.substring(7);
client.setInfo("#login",loginID);
System.out.println("> Message received: "+mesg+" from "+client);
System.out.println("> "+loginID+" has logged on.");
}

else{
String loginSTR = (String) client.getInfo("#login"); //cast getInfo data to String
System.out.println("> Message received: "+mesg+" from "+loginSTR);
this.sendToAllClients(loginSTR+": "+mesg);
}
}
/*
*
*/
public void handleMessageFromServerUI(String message){
try{
if (message.equals("#quit")) {
sendToAllClients("The server is quitting...");
quit();
}

else if (message.equals("#stop")) {
sendToAllClients("The server has stopped listening for new connections...");
stopListening();
}

else if (message.equals("#close")) {
sendToAllClients("Server is shutting down...\nServer will now disconnect...");
close();

}

else if (message.startsWith("#setport")) {
if (!serverIsOpen) {
int port = Integer.parseInt(message.split("\\s+")[1]);
setPort(port);
}
else{
serverUI.display("Cant set port");
}






/*
if (!serverIsOpen) {
serverUI.display("Invalid command\nCannot set port if server is open\nPlease close server with command #close");
return;
}
try{
int port = Integer.parseInt(message.split("\\s+")[1]);
setPort(port);
serverUI.display("Port has been set to :"+port);
}
catch(Exception e){
serverUI.display("Invalid port");
return;
}
*/
}

else if (message.equals("#getport")) {
serverUI.display("Port: "+getPort());

}
else if (message.equals("#start")) {
listen();
}
else if (message.startsWith("#")) {
serverUI.display("Invalid command. Please try again.\nPossible commands are:\n#quit\t: to quit the server\n#stop\t: to stop listening for new clients\n#close\t: to stop listening for new clients and disconnect all existing clients\n#start\t: to start the server, only valid if stopped\n#setport\t: to set the port if stopped\n#getport\t: to get the current port");
}
else{
sendToAllClients("SERVER MSG >"+message);
}
}
catch(IOException e)
{
serverUI.display
("Could not send message to all clients. Terminating server.");
quit();
}
}

/**
* 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) {
System.out.println("A new user 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) {
//System.out.println("A user has disconnected from the server.");

String loginSTR = (String) client.getInfo("#login");
sendToAllClients("> "+loginSTR+" 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);
}






/**
* 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());
serverIsOpen = true;
}

/**
Expand All @@ -70,8 +207,45 @@ protected void serverStopped()
{
System.out.println
("Server has stopped listening for connections.");
serverIsOpen = false;
}


/**
* This method overrides the one in the superclass. Called
* when the server stops listening for connections.
*/
public void quit(){
try{close();}
catch(IOException e){}
System.exit(0);
}

/**
* 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();
handleMessageFromServerUI(message);
}
}
catch (Exception ex)
{
System.out.println
("Unexpected error while reading from console!");
}
}


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

/**
Expand Down Expand Up @@ -99,7 +273,8 @@ public static void main(String[] args)
try
{
sv.listen(); //Start listening for connections
}
sv.accept();
}
catch (Exception ex)
{
System.out.println("ERROR - Could not listen for clients!");
Expand Down
Loading