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
80 changes: 51 additions & 29 deletions code/simplechat1/ClientConsole.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// 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
// license found at www.lloseng.com

import java.io.*;
import client.*;
Expand All @@ -9,30 +9,31 @@
/**
* 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
* Warning: Some of the code here is cloned in ServerConsole
*
* @author François Bélanger
* @author Dr Timothy C. Lethbridge
* @author Dr Timothy C. Lethbridge
* @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;

private static String loginId;

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

/**
* The instance of the client that created this ConsoleChat.
*/
ChatClient client;


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

/**
Expand All @@ -41,42 +42,42 @@ 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
try
{
client= new ChatClient(host, port, this);
}
catch(IOException exception)
client= new ChatClient(host,port,loginId,this);
}
catch(IOException exception)
{
System.out.println("Error: Can't setup connection!"
+ " Terminating client.");
System.exit(1);
}
}


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

/**
* This method waits for input from the console. Once it is
* This method waits for input from the console. Once it is
* received, it sends it to the client's message handler.
*/
public void accept()
public void accept()
{
try
{
BufferedReader fromConsole =
BufferedReader fromConsole =
new BufferedReader(new InputStreamReader(System.in));
String message;

while (true)
while (true)
{
message = fromConsole.readLine();
client.handleMessageFromClientUI(message);
}
}
catch (Exception ex)
}
catch (Exception ex)
{
System.out.println
("Unexpected error while reading from console!");
Expand All @@ -89,34 +90,55 @@ public void accept()
*
* @param message The string to be displayed.
*/
public void display(String message)
public void display(String message)
{
System.out.println("> " + 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)
public static void main(String[] args)
{
String host = "";
int port = 0; //The port number
loginId = "";
try{
loginId = (args[0]);
}
catch(ArrayIndexOutOfBoundsException e)
{}

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

try
{
host = args[0];
host = args[2];
}
catch(ArrayIndexOutOfBoundsException e)
{
host = "localhost";
}
ClientConsole chat= new ClientConsole(host, DEFAULT_PORT);
chat.accept(); //Wait for console data
if(args.length > 0){
try{
ClientConsole chat= new ClientConsole(host,port,loginId);
chat.accept(); //Wait for console data
// System.out.println(loginId+"has logged in");
}catch (Exception e) {}
}else{
System.out.println("ERROR - No login ID specified. Connection aborted.");
}
}
}
//End of ConsoleChat class
84 changes: 43 additions & 41 deletions code/simplechat1/EchoServer.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
// 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
// license found at www.lloseng.com

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

/**
* This class overrides some of the methods in the abstract
* This class overrides some of the methods in the abstract
* superclass in order to give more functionality to the server.
*
* @author Dr Timothy C. Lethbridge
Expand All @@ -15,43 +16,60 @@
* @author Paul Holden
* @version July 2000
*/
public class EchoServer extends AbstractServer
public class EchoServer extends AbstractServer
{
//Class variables *************************************************

/**
* The default port to listen on.
*/
final public static int DEFAULT_PORT = 5555;

ChatIF serverUI;
private static String loginId;

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

/**
* Constructs an instance of the echo server.
*
* @param port The port number to connect on.
*/
public EchoServer(int port)
public EchoServer(int port,ChatIF serverUI)
{
super(port);
this.serverUI = serverUI;
}


//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 setInfo(String loginId){
// this.loginId = loginId;
// }
// public String getInfo(){
// return loginId;
// }

public void handleMessageFromClient
(Object msg, ConnectionToClient client)
{
(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 in");
System.out.println("Message received: " + msg + " from " + client);
}else{
System.out.println("Message received: " + msg + " from " + client);
this.sendToAllClients(msg);
this.sendToAllClients(client.getInfo(loginId) + ">" + msg);
}

}

/**
* This method overrides the one in the superclass. Called
* when the server starts listening for connections.
Expand All @@ -61,7 +79,7 @@ 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.
Expand All @@ -71,39 +89,23 @@ protected void serverStopped()
System.out.println
("Server has stopped listening for connections.");
}


protected void clientConnected(ConnectionToClient client) {
System.out.println("The Server has accepted a client ");
}
protected void clientDisconnected(ConnectionToClient client) {
System.out.println("The client has disconnected");
this.sendToAllClients(client.getInfo(loginId) + "has logged off");
}
//Class methods ***************************************************

/**
* This method is responsible for the creation of
* 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
* @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!");
}
}
}
//End of EchoServer class
Loading