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 .DS_Store
Binary file not shown.
Binary file added code/.DS_Store
Binary file not shown.
93 changes: 74 additions & 19 deletions code/simplechat1/ClientConsole.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,12 @@ 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 userId)
{
try
{
client= new ChatClient(host, port, this);
}
catch(IOException exception)
{
System.out.println("Error: Can't setup connection!"
+ " Terminating client.");
System.exit(1);
}

client= new ChatClient(host, port,this, userId);


}


Expand All @@ -66,14 +60,57 @@ public void accept()
{
try
{
BufferedReader fromConsole =
new BufferedReader(new InputStreamReader(System.in));
BufferedReader fromConsole = new BufferedReader(new InputStreamReader(System.in));
String message;

while (true)
{
message = fromConsole.readLine();
client.handleMessageFromClientUI(message);
String[] cmd = message.split(" ");
if(message.charAt(0)==('#')){
if(message.equals("#quit")){
System.out.println("client quit");
client.quit();
}
else if((cmd[0].equals("#setport"))||(cmd[0].equals("#sethost"))){
if (client.isConnected()){
System.out.println("The client is already connected.");
}
else{
if(cmd[0].equals("#sethost")){
client.setHost(cmd[1]);
System.out.println("Host set to :"+cmd[1]);
}
else{
System.out.println("Port set to :"+cmd[1]);
client.setPort(Integer.parseInt(cmd[1]));
}
}
}
else if(message.equals("#logoff")){
client.closeConnection();
}
else if(message.equals("#login")){
if (client.isConnected()){
System.out.println("The client is already connected.");
}
else{
client.openConnection();
client.handleMessageFromClientUI( client.getuserId()+" has logged on");
}
}
else if(message.equals("#getport")){
System.out.println(client.getPort());
}
else if(message.equals("#gethost")){
System.out.println(client.getHost());
}
else{
System.out.println("invaild command");
}
}
else{
client.handleMessageFromClientUI(client.getuserId()+"> "+message);
}
}
}
catch (Exception ex)
Expand All @@ -91,7 +128,7 @@ public void accept()
*/
public void display(String message)
{
System.out.println("> " + message);
System.out.println("> " + message);
}


Expand All @@ -101,21 +138,39 @@ public void display(String message)
* This method is responsible for the creation of the Client UI.
*
* @param args[0] The host to connect to.
* arg[1] port
*/
public static void main(String[] args)
{
String host = "";
int port = 0; //The port number

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

}
ClientConsole chat= new ClientConsole(host, DEFAULT_PORT);
try
{
port = Integer.parseInt(args[2]);
}
catch(ArrayIndexOutOfBoundsException e)
{
port = DEFAULT_PORT;
}
try
{
userId = args[0];
}
catch(ArrayIndexOutOfBoundsException e)
{
userId = "";
}
ClientConsole chat= new ClientConsole(host, port,userId);
chat.accept(); //Wait for console data
}
}
Expand Down
33 changes: 31 additions & 2 deletions code/simplechat1/EchoServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,23 @@ public EchoServer(int port)
public void handleMessageFromClient
(Object msg, ConnectionToClient client)
{
System.out.println("Message received: " + msg + " from " + client);
try{
String a =msg.toString();
String cmd[]=a.split(" ");
if (cmd[0].equals("#login")) {
if(client.getInfo("userId")==null){
client.setInfo("userId",cmd[1]);
this.sendToAllClients(client.getInfo("userId") + " has logged on.");
}
else{
client.sendToClient("A user has already logged on");
}
}
}
catch(Exception e){

}
System.out.println("Message received: " + msg + " from " + client.getInfo("userId")+" "+client);
this.sendToAllClients(msg);
}

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

protected void clientConnected(ConnectionToClient client){
System.out.println(client.getInfo("userId") + " has logged on.");
}
protected void clientDisconnected(ConnectionToClient client){
System.out.println("client is disconnected"+client.toString());
}
protected void clientException(ConnectionToClient client,Throwable exception){
System.out.println("client is now disconnected");
}

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

Expand All @@ -84,7 +110,6 @@ protected void serverStopped()
public static void main(String[] args)
{
int port = 0; //Port to listen on

try
{
port = Integer.parseInt(args[0]); //Get port from command line
Expand All @@ -95,6 +120,7 @@ public static void main(String[] args)
}

EchoServer sv = new EchoServer(port);


try
{
Expand All @@ -104,6 +130,9 @@ public static void main(String[] args)
{
System.out.println("ERROR - Could not listen for clients!");
}
ServerConsole sc = new ServerConsole(sv);
sc.accept();

}
}
//End of EchoServer class
105 changes: 105 additions & 0 deletions code/simplechat1/ServerConsole.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@

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

public class ServerConsole implements ChatIF
{
//Class variables *************************************************

private EchoServer server;


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

/**
* Constructs an instance of the ClientConsole UI.
*
* @param port The port to connect on.
*/
public ServerConsole(EchoServer server){

this.server = server;
}



//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 accept()
{
try
{
BufferedReader fromConsole =
new BufferedReader(new InputStreamReader(System.in));
String message;

while (true)
{
message = fromConsole.readLine();
if(message.charAt(0)==('#')){
if(message.equals("#quit")){
System.out.println("server quit");
server.close();
}
else if(message.equals("#setport")){
if (server.isListening()){
System.out.println("The serveris already connected.");
}
else{
String[] cmd = message.split(" ");
try
{
server.setPort(Integer.parseInt(cmd[1]));
}
catch(NumberFormatException e)
{
System.out.println("invaild input");
}
}
}
else if(message.equals("#stop")){
server.sendToAllClients("server stop listening");
server.stopListening();
}
else if(message.equals("#close")){
server.sendToAllClients("server stop listening and stop connecting");
server.close();
}
else if(message.equals("#start")){
if (server.isListening()){
System.out.println("The serveris already connected.");
}
else{
server.listen();
}
}
else if(message.equals("#getport")){
System.out.println(server.getPort());
}
}
server.sendToAllClients("Server MSG> " + 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);
}
}
//End of ConsoleChat class
46 changes: 42 additions & 4 deletions code/simplechat1/client/ChatClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ public class ChatClient extends AbstractClient
* The interface type variable. It allows the implementation of
* the display method in the client.
*/
ChatIF clientUI;
ChatIF clientUI;
private String userId;


//Constructors ****************************************************
Expand All @@ -38,12 +39,29 @@ public class ChatClient extends AbstractClient
* @param clientUI The interface type variable.
*/

public ChatClient(String host, int port, ChatIF clientUI)
throws IOException
public ChatClient(String host, int port, ChatIF clientUI, String userId)
{
super(host, port); //Call the superclass constructor
this.clientUI = clientUI;
openConnection();
this.userId = userId;

try
{
openConnection();

if(userId==""){
System.out.println("ERROR - No login ID specified. Connection aborted.");
quit();
}
else{
System.out.println(userId+" has logged on.");
}
}
catch(IOException exception)
{
System.out.println("Cannot open connection. Awaiting command.");
}

}


Expand Down Expand Up @@ -72,6 +90,7 @@ public void handleMessageFromClientUI(String message)
}
catch(IOException e)
{

clientUI.display
("Could not send message to server. Terminating client.");
quit();
Expand All @@ -90,5 +109,24 @@ public void quit()
catch(IOException e) {}
System.exit(0);
}


//print message when server disconnect
public void connectionException(Exception exception){
System.out.println("The server closed, quit connection");
quit();
}
//when connection closed print message
public void connectionClosed() {
System.out.println("Connection Closed");
}
public String getuserId(){
return userId;
}
public void setuserId(String userId){
this.userId = userId;
}


}
//End of ChatClient class
Loading