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
41 changes: 33 additions & 8 deletions code/simplechat1/ClientConsole.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
* @author Dr Robert Laganière
* @version July 2000
*/
public class ClientConsole implements ChatIF
{
public class ClientConsole implements ChatIF {
//Class variables *************************************************

/**
* The default port to connect on.
*/

final public static int DEFAULT_PORT = 5555;

//Instance variables **********************************************
Expand All @@ -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 loginID, String host, int port)
{
try
{
client= new ChatClient(host, port, this);
client= new ChatClient(loginID, host, port, this);
}
catch(IOException exception)
{
Expand Down Expand Up @@ -104,18 +104,43 @@ public void display(String message)
*/
public static void main(String[] args)
{
String host = "";
String loginID = "";
String host = "";
int port = 0; //The port number


// User-specified login ID
try
{
host = args[0];
loginID = args[0];
}
// No user-specified login ID given
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Error. No login ID given. Exiting.");
System.exit(1); // Not an intentional attempt to exit, so error code 1
}

// User-specified host name
try
{
host = args[1];
}
// Default host name
catch(ArrayIndexOutOfBoundsException e)
{
host = "localhost";
}
ClientConsole chat= new ClientConsole(host, DEFAULT_PORT);

// User-specified port number
try {
port = Integer.parseInt(args[2]);
}
// Default port number
catch(ArrayIndexOutOfBoundsException e) {
port = DEFAULT_PORT;
}

ClientConsole chat= new ClientConsole(loginID, host, port);
chat.accept(); //Wait for console data
}
}
Expand Down
155 changes: 125 additions & 30 deletions code/simplechat1/EchoServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
* @author Paul Holden
* @version July 2000
*/
public class EchoServer extends AbstractServer
{
public class EchoServer extends AbstractServer{
//Class variables *************************************************

/**
Expand All @@ -31,45 +30,146 @@ public class EchoServer extends AbstractServer
*
* @param port The port number to connect on.
*/
public EchoServer(int port)
{
public EchoServer(int port) {
super(port);
}


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

// When an exception of a client occurs
public void clientException(ConnectionToClient client) {
System.out.println("An exception occured.");
clientDisconnected(client);
}

// When a client connects
public void clientConnected(ConnectionToClient client) {
System.out.println("A client is now connected.");
}

// When a client disconnects
public void clientDisconnected(ConnectionToClient client) {
System.out.println("A client is now disconnected.");
}

/**
* 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 stringMsg = (String) msg;
String[] splitMsg = stringMsg.split(" ");
if (splitMsg[0].equals("#login")) {
if (splitMsg.length > 1) {
// loginID is not yet defined
if (client.getInfo("loginID") == null) {
client.setInfo("loginID", splitMsg[1]);
sendToAllClients(splitMsg[1] + " logged in.");
}
// loginID is already defined
else {
try {
client.sendToClient("Error. " + splitMsg[1] + " is already logged in.");
client.close();
}
catch (IOException e) {
System.out.println("Unexpected error while attempting to log in.");
}
}
} else {
try {
client.sendToClient("Error. Must define login ID.");
}
catch (IOException e) {
System.out.println("Unexpected error while attempting to log in.");
}
}
}
// Server echoes each message to all clients
else {
System.out.println("Message received: " + msg + " from " + client);
this.sendToAllClients(client.getInfo("loginID") + " : " + msg);
}
}

/**
* This method handles any messages received from the server.
*
* @param msg The message received from the server.
*/
public void handleMessageFromServer(String message) {
// Anything starting with "#" is considered to be a command
if (message.startsWith("#")) {
String[] splitMsg = message.split(" ");
switch (splitMsg[0]) {
// Server terminates gracefully
case "#quit":
System.out.println("Commanded to quit.");
System.exit(0); // Exit is intentional, so error code of 0
break;
// Server stops listening for new clients
case "#stop":
System.out.println("Commanded to stop.");
stopListening();
break;
// Server stops listening for new clients and disconnects all existing clients
case "#close":
System.out.println("Commanded to close.");
stopListening();
try {
close();
}
catch (IOException e) {}
break;
// Sets server port number for next connection, but only if server is closed
case "#setport":
if (!isListening()) {
System.out.println("Commanded to set port.");
setPort(Integer.parseInt(splitMsg[1]));
} else {
System.out.println("Error. Cannot set port because the server is not closed.");
}
break;
// Server starts listening for new clients, but only if server is stopped
case "#start":
if (!isListening()) {
System.out.println("Commanded to start.");
try {
listen();
}
catch (IOException e) {}
} else {
System.out.println("Error. Cannot start because the server is not stopped.");
}
break;
// Display current port number
case "#getport":
System.out.println("Port number: " + getPort());
break;
// Command not recognized
default:
System.out.println("Command not recognized");
break;
}
}
}

/**
* 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());
protected void serverStarted() {
System.out.println("Server listening for connections on port " + getPort());
}

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

//Class methods ***************************************************
Expand All @@ -81,27 +181,22 @@ 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

try
{
try {
port = Integer.parseInt(args[0]); //Get port from command line
}
catch(Throwable t)
{
catch(Throwable t) {
port = DEFAULT_PORT; //Set port to 5555
}

EchoServer sv = new EchoServer(port);
ServerConsole sc = new ServerConsole(port);

try
{
sv.listen(); //Start listening for connections
try {
sc.accept();
}
catch (Exception ex)
{
catch (Exception ex) {
System.out.println("ERROR - Could not listen for clients!");
}
}
Expand Down
50 changes: 50 additions & 0 deletions code/simplechat1/ServerConsole.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// ServerConsole class allows user input directly into the server's console, then
// echoes the input to all clients

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

public class ServerConsole implements ChatIF{
final public static int DEFAULT_PORT = 5555;
EchoServer server;
public ServerConsole(int port) {
server = new EchoServer(port);
try {
server.listen();
}
catch(IOException exception) {
System.out.println("Error: Can't setup connection!"
+ " Terminating client.");
System.exit(1);
}
}
public void accept() {
try {
BufferedReader fromConsole = new BufferedReader(new InputStreamReader(System.in));
String message;
while (true) {
message = fromConsole.readLine();
server.handleMessageFromServer(message);
display(message);
}
}
catch (Exception ex) {
System.out.println("Unexpected error while reading from console!");
}
}
public void display(String message) {
System.out.println("SERVER MSG > " + message);
}
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);
sc.accept();
}
}
// End of ServerConsole class
Loading