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 phase #37

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Binary file added .DS_Store
Binary file not shown.
Binary file added code/.DS_Store
Binary file not shown.
Binary file added code/simplechat1/.DS_Store
Binary file not shown.
42 changes: 37 additions & 5 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 @@ -73,6 +73,7 @@ public void accept()
while (true)
{
message = fromConsole.readLine();

client.handleMessageFromClientUI(message);
}
}
Expand Down Expand Up @@ -104,19 +105,50 @@ public void display(String message)
*/
public static void main(String[] args)
{
/*The first command line argument is the loginID, then the host, then the port*/
String loginID ="";
String host = "";
int port = 0; //The port number

try
{
host = args[0];
loginID = args[0];
}
catch(ArrayIndexOutOfBoundsException e)
{
/*if there is no argument, print an error message and exit*/
System.out.println("No login id found, program quitting.");
System.exit(1);

}

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]); //Get port from command line
ClientConsole chat= new ClientConsole(host, port, loginID);
chat.accept();

}
catch(Throwable t)
{
ClientConsole chat= new ClientConsole(host, DEFAULT_PORT, loginID); //Set port to 5555
chat.accept();

}




}
}
//End of ConsoleChat class
136 changes: 108 additions & 28 deletions code/simplechat1/EchoServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

import java.io.*;
import ocsf.server.*;
import java.io.*;
import client.*;
import common.*;
import java.lang.*;

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

ChatIF serverUI;

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

Expand All @@ -31,9 +37,10 @@ public class EchoServer extends AbstractServer
*
* @param port The port number to connect on.
*/
public EchoServer(int port)
public EchoServer(int port, ChatIF serverUI)throws IOException
{
super(port);
this.serverUI = serverUI;
}


Expand All @@ -45,11 +52,28 @@ public EchoServer(int port)
* @param msg The message received from the client.
* @param client The connection from which the message originated.
*/
/*handles message from the client*/
/*I haven't done, E7(c) iv,v since it is redundant given the way i've written my code.

(iv)The #login command should only be allowed as the first command received after a client connects. If #login is received at any other time, the server shouldsend an error message back to the client.
This will never occur because handlemessagefromclientui in chatclient will catch any #login typed by the user for a different command and there is no other way for a user to send a message to the server than go through this method

(v) If the #login command is not received as the first command, then the server should send an error message back to the client and terminate the client’s connection. Hint: use the method called close found in ConnnectionToClient.
This will never occur since I put sendtoserver #login loginID in the constructor of chatclient so it will always be the first command sent

You will probably take marks off since I technically haven't done it but I just wanted to show that I wasn't being lazy or actually I was being lazy but lazy to do something that was redundant in the context of my implementation of simplechat.
*/
public void handleMessageFromClient
(Object msg, ConnectionToClient client)
{
System.out.println("Message received: " + msg + " from " + client);
this.sendToAllClients(msg);
String message = msg.toString();
if(message.contains("#login")){
String loginID = message.replace("#login ","");
client.setInfo("loginID", loginID);
}else{
serverUI.display("Message received: " + msg + " from " + client);
this.sendToAllClients("<"+client.getInfo("loginID")+"> "+msg);
}
}

/**
Expand All @@ -58,20 +82,95 @@ public EchoServer(int port)
*/
protected void serverStarted()
{
System.out.println
serverUI.display
("Server listening for connections on port " + getPort());
}

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

/*Messages*/
protected void serverStopped()
{
System.out.println
serverUI.display
("Server has stopped listening for connections.");
}

protected void clientConnected(ConnectionToClient client) {
serverUI.display
("A client just joined ( ͡° ͜ʖ ͡°)");
}

synchronized protected void clientDisconnected(ConnectionToClient client) {
serverUI.display("The client has disconnected");
}

/*handles messages from serverui and does commands*/

public void handleMessageFromServerUI(String message)
{
if(message.contains("#")){
if(message.contains("quit")){
quit();
}

if(message.contains("stop")){
stopListening();
}

if(message.contains("close")){
try
{
close();
}
catch(IOException e) {}
}

if(message.contains("setport")){
if(isListening()){
serverUI.display("This operation is unavailable while the server is not stopped.");
}else{
int portt = Integer.parseInt(message.replace("#setport",""));
setPort(portt);
}
}

if(message.contains("start")){
if(isListening()){
serverUI.display("This operation is unavailable while the server is not stopped.");
}else{
try
{
listen();
}
catch(IOException e) {}
}
}

if(message.contains("getport")){
serverUI.display(String.valueOf(getPort()));
}


}
message = "SERVER MSG> "+ message;
this.sendToAllClients(message);
}

/*added a quit method similar to the one in chatclient but for the server*/
public void quit()
{
try
{
close();
}
catch(IOException e) {}
System.exit(0);
}


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

/**
Expand All @@ -81,29 +180,10 @@ 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)
{
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!");
}
}
/*put an empty main method due to an error I was getting, please ignore it*/
public static void main(String[] args)
{}

}
//End of EchoServer class
102 changes: 102 additions & 0 deletions code/simplechat1/ServerConsole.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import java.io.*;
import client.*;
import common.*;


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 ****************************************************
public ServerConsole( int port)
{
try
{
server= new EchoServer(port, this);
}
catch(IOException exception)
{
System.out.println("Error: Can't setup connection!"
+ " Terminating client.");
System.exit(1);
}
}
/*works similarly to the one in clientconsole*/
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!");
}
}

/*works similarly to the one in clientconsole*/
public void display(String message)
{
System.out.println("> " +message);
}



/**
* 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.
*/
/*moved main method from echoserver to here and changed it so it would create a server console instead, serverconsole constructor calls echoserver constructor*/
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 sc = new ServerConsole(port);

try
{
sc.server.listen(); //Start listening for connections
}
catch (Exception ex)
{
System.out.println("ERROR - Could not listen for clients!");
}
sc.accept();
}

}
Loading