Skip to content
Open

A1 #49

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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ Some relevant websites related to this repository:
* Student solutions to exercises (first edition): http://highered.mheducation.com/sites/0077097610/student_view0/student_solutions.html

Professors may obtain solutions to exercises including code by contacting the authors. They must show a web presence confirming their email address and that they are indeed an academic at that same domain.

Ahmed Abdulwahab 300124243 aabdu170@uottawa.ca
43 changes: 37 additions & 6 deletions code/simplechat1/ClientConsole.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,19 @@ 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 id, String host, int port)
{
try
{
client= new ChatClient(host, port, this);
try {
client.sendToServer("#login "+id);
}catch(IOException e){
System.out.println("Could login to server. Terminating.");
quit();
}
}
catch(IOException exception)
{
catch(IOException exception) {
System.out.println("Error: Can't setup connection!"
+ " Terminating client.");
System.exit(1);
Expand Down Expand Up @@ -104,19 +109,45 @@ public void display(String message)
*/
public static void main(String[] args)
{
String id = "";
String host = "";
int port = 0; //The port number
try{
id = args[0];
}catch(ArrayIndexOutOfBoundsException e){
System.out.println("user id required.");
System.exit(0);
}

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

try
{
host = args[0];
port = Integer.parseInt(args[1]);
}
catch(ArrayIndexOutOfBoundsException e)
{
host = "localhost";
port = 5555;
}
ClientConsole chat= new ClientConsole(host, DEFAULT_PORT);

ClientConsole chat = new ClientConsole(id, host, port);
chat.accept(); //Wait for console data
}

public void quit(){
/*try{
closeConnection();
}
catch(IOException e) {

}*/
System.exit(0);
}


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

import java.io.*;

import client.ChatClient;
import ocsf.server.*;

/**
Expand Down Expand Up @@ -45,11 +47,17 @@ 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 message = msg.toString();
if(message.charAt(0) =='#'){
if(message.substring(1,6).equals("login")){
System.out.println("xD");
client.setInfo("id",message.substring(6));
}

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

/**
Expand All @@ -72,8 +80,45 @@ protected void serverStopped()
("Server has stopped listening for connections.");
}

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

protected void clientConnected(ConnectionToClient client) {
System.out.println("A Client has connected to the server !");
}
synchronized protected void clientDisconnected(ConnectionToClient client) {
System.out.println("A Client has disconnected from the server .");
}

//Class methods ***************************************************
public void handleMessageFromServerUI(String message){
if(message.charAt(0) == '#'){
if(message.substring(1,5).equals("quit")){
System.exit(0);
}else if(message.substring(1,5).equals("stop")){
stopListening();
}else if(message.substring(1,6).equals("close")){
stopListening();
}else if(message.substring(1,6).equals("start")){
if(!isListening()){
try{
listen();
} catch(Exception exception){
System.out.println("friggin error xD");
}
}else{
System.out.println("Server already listening");
}
}else if(message.substring(1,8).equals("setport")){
if(isListening()){
System.out.println("Error: yous already connected foo");
}else{
setPort(Integer.parseInt(message.substring(9)));
}
}else if(message.substring(1,8).equals("getport")){
System.out.println(getPort());
}
}else{
this.sendToAllClients("SERVER MSG > "+message);
}
}
/**
* This method is responsible for the creation of
* the server instance (there is no UI in this phase).
Expand Down
129 changes: 129 additions & 0 deletions code/simplechat1/ServerConsole.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
// 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

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

/**
* 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
*
* @author François Bélanger
* @author Dr Timothy C. Lethbridge
* @author Dr Robert Laganière
* @version July 2000
*/
public class ServerConsole 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 ConsoEchoServerleChat.
*/
EchoServer server;


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

/**
* Constructs an instance of the ClientConsole UI.
*
* @param host The host to connect to.
* @param port The port to connect on.
*/
public ServerConsole(int port)
{
try
{
server= new EchoServer(port);
server.listen();
}
catch(Exception 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
* 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();
server.handleMessageFromServerUI(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);
}


//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)
{
int port = 0; //The port number
try
{
port = Integer.parseInt(args[0]);
}
catch(ArrayIndexOutOfBoundsException e)
{
port = 5555;
}

ServerConsole sv = new ServerConsole(port);
try{
sv.accept();
}catch (Exception ex){
}
}

public void quit(){
System.exit(0);
}
}
//End of ConsoleChat class