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
48 changes: 37 additions & 11 deletions code/simplechat1/ClientConsole.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class ClientConsole implements ChatIF
* The default port to connect on.
*/
final public static int DEFAULT_PORT = 5555;
private static String loginID;

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

Expand All @@ -41,13 +42,13 @@ 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, loginID, this);
}
catch(IOException exception)
catch(IOException exception)
{
System.out.println("Error: Can't setup connection!"
+ " Terminating client.");
Expand Down Expand Up @@ -91,7 +92,7 @@ public void accept()
*/
public void display(String message)
{
System.out.println("> " + message);
System.out.println(message);
}


Expand All @@ -102,21 +103,46 @@ public void display(String message)
*
* @param args[0] The host to connect to.
*/
public static void main(String[] args)
public static void main(String[] args)
{
String host = "";
int port = 0; //The port number

try
loginID = "";

try
{
loginID = args[0];
}
catch(ArrayIndexOutOfBoundsException e) {}

try
{
host = args[0];
host = args[2];
}
catch(ArrayIndexOutOfBoundsException e)
catch(Exception e)
{
host = "localhost";
}
ClientConsole chat= new ClientConsole(host, DEFAULT_PORT);
chat.accept(); //Wait for console data

try
{
port = Integer.parseInt(args[1]);
}
catch(Exception e)
{
port = DEFAULT_PORT;
}


if(args.length > 0) {
try {
ClientConsole chat= new ClientConsole(host, port, loginID);
chat.accept(); //Wait for console data
} catch(Exception e) {}
} else {
System.out.println("Invalid Command!\nMust Enter Login ID!");
System.exit(0);
}
}
}
//End of ConsoleChat class
56 changes: 26 additions & 30 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 @@ -23,6 +24,8 @@ public class EchoServer extends AbstractServer
* The default port to listen on.
*/
final public static int DEFAULT_PORT = 5555;
private static String loginID;
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)
{
super(port);
this.serverUI = serverUI;
}


Expand All @@ -45,12 +49,20 @@ public EchoServer(int port)
* @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 temp[] = String.valueOf(msg).split(" ");

if(temp[0].equals("#login") && temp.length > 1) {
client.setInfo(loginID, temp[1]);
this.sendToAllClients(client.getInfo(loginID) + " has logged on");
System.out.println("Message received: " + msg + " from " + client);
} else {
System.out.println("Message received: " + msg + " from " + client);
this.sendToAllClients(client.getInfo(loginID) + "> " + msg);
}
}

/**
* This method overrides the one in the superclass. Called
Expand Down Expand Up @@ -81,29 +93,13 @@ 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!");
}
synchronized protected void clientConnected(ConnectionToClient client) {
System.out.println("A client has connected");
}

synchronized protected void clientDisconnected(ConnectionToClient client) {
System.out.println("A client has disconnected");
this.sendToAllClients(client.getInfo(loginID) + " has logged off");
}
}
//End of EchoServer class
186 changes: 186 additions & 0 deletions code/simplechat1/ServerConsole.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
// 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 client.*;
import ocsf.server.*;
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;
private EchoServer sv;
private boolean serverClosed = false;
//Constructors ****************************************************

/**
* Constructs an instance of the ClientConsole UI.
*
* @param host The host to connect to.
* @param port The port to connect on.
*/
public ServerConsole(int port)
{
sv = new EchoServer(port, this);
}

public EchoServer get() {
return sv;
}

public void handleMessageFromServer(String message) {
String temp[] = message.split(" ");

if(message.equals("#quit")) {
try {
displayMessage("client has quit");
System.out.println("Closing Connection");
sv.close();
System.exit(0);
} catch(Exception e) {
System.out.println("ERROR!");
}
} else if(message.equals("#stop")) {
try {
displayMessage("Server has stopped listening for new clients");
System.out.println("Closing Connection");
sv.stopListening();
serverClosed = true;
} catch(Exception e) {
System.out.println("ERROR!");
}
} else if(message.equals("#close")) {
try {
sv.close();
serverClosed = true;
} catch(Exception e) {
System.out.println("ERROR!");
}
} else if(temp[0].equals("#setport") && serverClosed) {
try {
System.out.println("Setting port to: " + Integer.parseInt(temp[1]));
sv.setPort(Integer.parseInt(temp[1]));
sv.listen();
serverClosed = false;
} catch(Exception e) {
System.out.println("ERROR!");
}
} else if(message.equals("#start")) {
try {
displayMessage("client is logging in");
System.out.println("Opening connection to server");
sv.listen();
serverClosed = false;
} catch(Exception e) {
System.out.println("ERROR!");
}
} else if(message.equals("#getport")) {
try {
System.out.println("Port is currently: " + sv.getPort());
} catch(Exception e) {
System.out.println("ERROR!");
}
} else if(message.charAt(0) == '#') {
try {
System.out.println("ERROR! Command not valid");
} catch(Exception e) {
System.out.println("ERROR!");
}
} else
displayMessage(message);
}


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

/**
* This method waits for input from the console. Once it is
* received, it sends it to the client's message handler.
*/
public void displayMessage(String msg) {
sv.sendToAllClients("SERVER MSG> " + msg);
}

public void accept()
{
try
{
BufferedReader fromConsole =
new BufferedReader(new InputStreamReader(System.in));
String message;

while (true)
{
message = fromConsole.readLine();
handleMessageFromServer(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 sv = new ServerConsole(port);

try
{
sv.get().listen(); //Start listening for connections
sv.accept();
}
catch (Exception ex)
{
System.out.println("ERROR - Could not listen for clients!");
}
}
}
//End of ConsoleChat class
Loading