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 code/ocsf/client/AbstractClient.class
Binary file not shown.
5 changes: 4 additions & 1 deletion code/ocsf/client/AbstractClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -246,14 +246,16 @@ final public void run() {
handleMessageFromServer(msg);
}
} catch (Exception exception) {

if (!readyToStop) {
try {
closeAll();
} catch (Exception ex) {
}

connectionException(exception);
}

} finally {
clientReader = null;
}
Expand All @@ -268,6 +270,7 @@ final public void run() {
* attempting to reconnect.
*/
protected void connectionClosed() {

}

/**
Expand Down
Binary file added code/ocsf/server/AbstractServer$1.class
Binary file not shown.
Binary file added code/ocsf/server/AbstractServer.class
Binary file not shown.
Binary file added code/ocsf/server/ConnectionToClient.class
Binary file not shown.
Binary file added code/simplechat1/ClientConsole.class
Binary file not shown.
36 changes: 27 additions & 9 deletions code/simplechat1/ClientConsole.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,13 @@ public class ClientConsole implements ChatIF
*
* @param host The host to connect to.
* @param port The port to connect on.
* @param loginID The loginID user gave
*/
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 @@ -106,17 +107,34 @@ public static void main(String[] args)
{
String host = "";
int port = 0; //The port number
String loginID = "";

try
{
host = args[0];
//Checks if the first argument is a loginID
try{
loginID = args[0];
}catch(ArrayIndexOutOfBoundsException e){
System.out.println("No login ID detected, program will not exit.");
System.exit(0);
}
catch(ArrayIndexOutOfBoundsException e)
{

//Saves the inputted host or uses default if non is given
try{
host = args[1];
}
catch(ArrayIndexOutOfBoundsException e){
host = "localhost";
}
ClientConsole chat= new ClientConsole(host, DEFAULT_PORT);
chat.accept(); //Wait for console data

//Saves the inputted port or uses default if non is given
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
Binary file added code/simplechat1/EchoServer.class
Binary file not shown.
78 changes: 74 additions & 4 deletions code/simplechat1/EchoServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

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

/**
* This class overrides some of the methods in the abstract
Expand All @@ -23,6 +25,7 @@ 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 +34,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 @@ -48,8 +52,13 @@ public EchoServer(int port)
public void handleMessageFromClient
(Object msg, ConnectionToClient client)
{
if(msg.toString().contains("#")){
if(msg.toString().contains("login")){
client.setInfo("LoginID", msg.toString().replace("#login",""));
}
}
System.out.println("Message received: " + msg + " from " + client);
this.sendToAllClients(msg);
this.sendToAllClients("(" + client.getInfo("LoginID") + "): " + msg);
}

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


//This method overrides the superclass to display when clients are connected
protected void clientConnected(ConnectionToClient client){
serverUI.display("#login" + client + " has connected to this server.");
}

//This method overrides the superclass to display when clients are disconnected
synchronized protected void clientDisconnected(ConnectionToClient client){
serverUI.display("#login" + client + " has disconnected from the server.");
}


//This method handles all user input and excutes the desire effects
public void handleMessageFromServerUI(String message)
{
if(message.contains("#")){
if(message.contains("quit")){
try{
close();
}catch(IOException e){}
System.exit(0);
}

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

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

if(message.contains("setport")){
if(isListening()){
serverUI.display("This command can not be issued while the server is still on.");
}else{
setPort(Integer.parseInt(message.replace("#setport","")));
}
}

if(message.contains("start")){
if(isListening()){
serverUI.display("This command can not be issued while the server is still on.");
}else{
try{
listen();
}
catch(IOException e) {}
}
}

if(message.contains("getport")){
serverUI.display(String.valueOf(getPort()));
}
}else{
message = "SERVER MSG> "+ message;
this.sendToAllClients(message);
}
}

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

Expand All @@ -94,11 +164,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.server.listen(); //Start listening for connections
}
catch (Exception ex)
{
Expand Down
Binary file added code/simplechat1/ServerConsole.class
Binary file not shown.
126 changes: 126 additions & 0 deletions code/simplechat1/ServerConsole.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
import java.io.*;
import client.*;
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)
{
try
{
server = new EchoServer(port, this);
}
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 server's message handler.
*/
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!");
}
}

/**
* 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("> " + 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 = 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 chat= new ServerConsole(DEFAULT_PORT);
chat.accept(); //Wait for console data

try
{
chat.server.listen(); //Start listening for connections
}
catch (Exception ex)
{
System.out.println("ERROR - Could not listen for clients!");
}
}
}
//End of ConsoleChat class
Binary file added code/simplechat1/client/ChatClient.class
Binary file not shown.
Loading