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 ShivamSyal_300065225_ssyal030@uottawa.ca.pdf
Binary file not shown.
74 changes: 65 additions & 9 deletions code/simplechat1/ClientConsole.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 login, String host, int port)
{
try
{
client= new ChatClient(host, port, this);
client= new ChatClient(login, host, port, this);
}
catch(IOException exception)
{
Expand All @@ -64,16 +64,59 @@ public ClientConsole(String host, int port)
*/
public void accept()
{
boolean logStatus = true;
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);
if(message.contains("#")){
if (message.equals("#quit")) {
System.exit(0);
}else if(message.equals("#gethost")){
System.out.println(client.getHost());
}else if (message.equals("#getport")){
System.out.println(client.getPort());
}else if (message.equals("#logoff")){
logStatus = false;
client.handleMessageFromClientUI(client.getLogin()+" left the server.");
System.out.println("Connection closed.");
}else if (message.equals("#login")){
if(logStatus==true){
System.out.println("You are already logged in.");
}
logStatus = true;

}else if (message.contains("#sethost")){
if (logStatus==false) {
client.setHost(message.split(" ")[1]);
}else{
System.out.println("Error, you can only set host when you are logged out.");
}
}else if (message.contains("#setport")){
if (logStatus==false) {
String temp =message.split(" ")[1];
int temp1 = Integer.parseInt(temp);
client.setPort(temp1);
}else{
System.out.println("Error, you can only set port when you are logged out.");
}
}else{
System.out.println("Error, #'s are functions, this function does not exist.");
}
}else{
if(logStatus==true){
client.handleMessageFromClientUI(message);
}else{
System.out.println("Currently, you are logged out, please log back in.");
}
}


}
}
catch (Exception ex)
Expand All @@ -92,6 +135,7 @@ public void accept()
public void display(String message)
{
System.out.println("> " + message);

}


Expand All @@ -106,17 +150,29 @@ public static void main(String[] args)
{
String host = "";
int port = 0; //The port number

String login = "";
try
{
host = args[0];
host = args[1];
}
catch(ArrayIndexOutOfBoundsException e)
{
host = "localhost";
}
ClientConsole chat= new ClientConsole(host, DEFAULT_PORT);
chat.accept(); //Wait for console data
try{
port = Integer.parseInt(args[2]);
}catch(ArrayIndexOutOfBoundsException e){
port = DEFAULT_PORT;
}
if (args.length !=0){
login = args[0];
ClientConsole chat= new ClientConsole(login, host, port);
chat.accept(); //Wait for console data

}else{
System.out.println("You didn't login, please login.");

}
}
}
//End of ConsoleChat class
90 changes: 43 additions & 47 deletions code/simplechat1/EchoServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// license found at www.lloseng.com

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

/**
Expand Down Expand Up @@ -31,11 +32,15 @@ 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;

}

ChatIF serverUI;


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

Expand All @@ -45,33 +50,48 @@ 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 = msg.toString();
if(temp.contains("#login")){
System.out.println(msg);
this.sendToAllClients("ServerMSG> Welcome to the server " + temp.split(" ")[1]+".");
client.setInfo("Login ID",temp.split(" ")[1]);
}else{
System.out.println("Message received: " + msg + " from " + client);
this.sendToAllClients(client.getInfo("Login ID")+": "+msg);
}
}


/**
* 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());
try{
listen();
}catch(IOException exception){
System.out.println("ERROR.");
}
}

/**
* 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(){
stopListening();
System.out.println("Server has stopped listening for connections.");
this.sendToAllClients("Server stopped listening for connections.");

}

protected void clientConnected(ConnectionToClient client) {
System.out.println("A new client has connected to the server.");
}

synchronized protected void clientException(ConnectionToClient client, Throwable exception) {
System.out.println(client.getInfo("Login ID")+" has left the server.");
}

synchronized protected void clientDisconnected(ConnectionToClient client) {
System.out.println(client.getInfo("Login ID")+" has left the server.");
}



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

/**
Expand All @@ -81,29 +101,5 @@ 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!");
}
}
}
//End of EchoServer class
88 changes: 88 additions & 0 deletions code/simplechat1/ServerConsole.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import java.io.*;
import client.*;
import common.*;

public class ServerConsole implements ChatIF {

private EchoServer server;
final public static int DEFAULT_PORT = 5555;

public ServerConsole(int port) {
server = new EchoServer(port,this);
}

public void display(String message) {
server.sendToAllClients("ServerMSG> " + message);
}

public void handleMessageFromConsole(String message){
display(message);
}

public void accept(){
try{
BufferedReader fromConsole = new BufferedReader(new InputStreamReader(System.in));
String message;
boolean status = true;
while (true) {
message = fromConsole.readLine();
if(message.contains("#")){
if(message.equals("#quit")){
System.exit(0);
}else if(message.equals("#stop")){
server.serverStopped();
status = false;
}else if(message.equals("#start")){
server.serverStarted();
status= true;
}else if(message.equals("#close")) {
server.close();
}else if(message.contains("#setport")){
if(status==false){
String temp =message.split(" ")[1];
int temp1 = Integer.parseInt(temp);
server.setPort(temp1);
}else{
System.out.println("Error, you can only set port when connections are stopped.");
}
}else if (message.equals("#getport")) {
System.out.println(server.getPort());
}else{
System.out.println("Error, #'s are functions, this function does not exist.");
}
}else{
handleMessageFromConsole(message);
}

}
}
catch (Exception ex) {
System.out.println
("Unexpected error while reading from console!");
}
}
public EchoServer get(){
return server;
}

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 server = new ServerConsole(port);

try{
server.get().listen(); //Start listening for connections
server.accept();
}
catch (Exception ex){
System.out.println("ERROR - Could not listen for clients!");
}
}
}
25 changes: 23 additions & 2 deletions code/simplechat1/client/ChatClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class ChatClient extends AbstractClient
* the display method in the client.
*/
ChatIF clientUI;

String login;

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

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

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

Expand Down Expand Up @@ -77,6 +78,7 @@ public void handleMessageFromClientUI(String message)
quit();
}
}


/**
* This method terminates the client.
Expand All @@ -90,5 +92,24 @@ public void quit()
catch(IOException e) {}
System.exit(0);
}

protected void connectionException(Exception exception) {
quit();
}
protected void connectionClosed() {
clientUI.display("Server has shutdown ... quitting");

}
public String getLogin(){
return login;
}
public void sendLogin(){

handleMessageFromClientUI("#login "+getLogin());

}
protected void connectionEstablished() {
sendLogin();
}
}
//End of ChatClient class