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
115 changes: 74 additions & 41 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,30 @@
/**
* 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;

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

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


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

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



client= new ChatClient(loginId,host, port, this);
}
catch(IOException exception)
{
System.out.println("Cannot open connection. Awaiting command.");





}
}



//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,33 +95,60 @@ public void accept()
*
* @param message The string to be displayed.
*/
public void display(String message)
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)
public static void main(String[] args)
{
String loginId="";
String host = "";
int port = 0; //The port number

try
{
host = args[0];
}
catch(ArrayIndexOutOfBoundsException e)
{
host = "localhost";
try{
loginId=args[0];

}catch(ArrayIndexOutOfBoundsException exeception){
System.out.println("ERROR - No login ID specified. Connection aborted.");
System.exit(0);
}
ClientConsole chat= new ClientConsole(host, DEFAULT_PORT);

try
{


host=args[1];
/////YOU'RE MODIFYING HERE!
if(args.length >2 ){

port=Integer.valueOf(args[2]);
}
else{
port=DEFAULT_PORT;


}


}

catch(ArrayIndexOutOfBoundsException e)
{
host = "localhost";
port=DEFAULT_PORT;
/////YOU'RE MODIFYING HERE!
//port=DEFAULT_PORT;
}
ClientConsole chat= new ClientConsole(loginId,host, port);
chat.accept(); //Wait for console data
}
}
Expand Down
99 changes: 59 additions & 40 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.ChatIF;

/**
* 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,30 +16,38 @@
* @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;

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

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



public EchoServer(int port,ChatIF svUserInterface)
{
super(port);
serverUI = svUserInterface;
}


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

/**
* This method handles any messages received from the client.
*
Expand All @@ -48,10 +57,25 @@ public EchoServer(int port)
public void handleMessageFromClient
(Object msg, ConnectionToClient client)
{
System.out.println("Message received: " + msg + " from " + client);
this.sendToAllClients(msg);
String message=String.valueOf(msg);
if(message.startsWith("#login")){
if(client.getInfo( "#loginId" )!= null){
System.out.println("You can't set your login Id twice!");
}else{
String iD= message.substring(10,message.length()-14);
client.setInfo("#loginId",(Object) iD);
System.out.println(msg);
}
this.sendToAllClients(client.getInfo("#loginId")+" has logged in");
}


else{
System.out.println("Message received:< " + msg + " >from< " + client.getInfo("#loginId")+">");
this.sendToAllClients( "Message from "+ client.getInfo("#loginId")+":"+msg);
}
}

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



protected void clientConnected(ConnectionToClient client) {

System.out.println("A new client is attempting to connect to the server.");

//this.sendToAllClients(client.getInfo("#loginId")+"has logged in");
}
synchronized protected void clientDisconnected(
ConnectionToClient client) {
System.out.println("Client has disconnected!");
}
synchronized protected void clientException(
ConnectionToClient client, Throwable exception) {
System.out.println(client.getInfo("#loginId")+" has disconnected");
this.sendToAllClients(client.getInfo("#loginId")+" has disconnected");

}

//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