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
11 changes: 11 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry excluding="threads/|postal/|simplechat1/|pointcp/" kind="src" path="code"/>
<classpathentry kind="src" path="code/pointcp"/>
<classpathentry kind="src" path="code/postal"/>
<classpathentry kind="src" path="code/simplechat1"/>
<classpathentry kind="src" path="code/threads"/>
<classpathentry kind="src" path="V2Code"/>
<classpathentry kind="output" path="bin"/>
</classpath>
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
/bin/
17 changes: 17 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Lloseng</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
Binary file added TestCases.txt.docx
Binary file not shown.
8 changes: 7 additions & 1 deletion code/ocsf/client/AbstractClient.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package ocsf.client;
// 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

package ocsf.client;


import java.io.*;
import java.net.*;
Expand Down Expand Up @@ -268,6 +269,8 @@ final public void run() {
* attempting to reconnect.
*/
protected void connectionClosed() {


}

/**
Expand All @@ -279,7 +282,10 @@ protected void connectionClosed() {
* the exception raised.
*/
protected void connectionException(Exception exception) {

}



/**
* Hook method called after a connection has been established. The default
Expand Down
3 changes: 2 additions & 1 deletion code/ocsf/client/AdaptableClient.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package ocsf.client;
// This file contains material supporting section 6.13 of the textbook:
// "Object Oriented Software Engineering" and is issued under the open-source
// license found at www.lloseng.com

package ocsf.client;


import java.io.*;
import java.net.*;
Expand Down
3 changes: 2 additions & 1 deletion code/ocsf/client/ObservableClient.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package ocsf.client;
// This file contains material supporting section 6.13 of the textbook:
// "Object Oriented Software Engineering" and is issued under the open-source
// license found at www.lloseng.com

package ocsf.client;


import java.util.*;
import java.io.*;
Expand Down
10 changes: 7 additions & 3 deletions code/ocsf/server/AbstractServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ final synchronized public void close() throws IOException
}

/**
* Sends a message to every client connected to the server.
* s a message to every client connected to the server.
* This is merely a utility; a subclass may want to do some checks
* before actually sending messages to all clients. This method
* can be overriden, but if so it should still perform the general
Expand Down Expand Up @@ -404,7 +404,9 @@ protected void listeningException(Throwable exception) {}
* connections. The default implementation does nothing.
* The method may be overridden by subclasses.
*/
protected void serverStarted() {}
protected void serverStarted() {
System.out.println("Welcome!");
}

/**
* Hook method called when the server stops accepting
Expand All @@ -419,7 +421,9 @@ protected void serverStopped() {}
* overriden by subclasses. When the server is closed while still
* listening, serverStopped() will also be called.
*/
protected void serverClosed() {}
protected void serverClosed() {
System.out.println("GoodBye!");
}

/**
* Handles a command sent from one client to the server.
Expand Down
109 changes: 94 additions & 15 deletions code/simplechat1/ClientConsole.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,24 @@ 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 loginID, String host, int port)
{
try
{
client= new ChatClient(host, port, this);
client= new ChatClient(loginID, host, port, this);
}
catch(IOException exception)
{
System.out.println("Error: Can't setup connection!"
+ " Terminating client.");
System.exit(1);

}
}




//Instance methods ************************************************
Expand All @@ -64,17 +69,72 @@ public ClientConsole(String host, int port)
*/
public void accept()
{

try
{
BufferedReader fromConsole =
new BufferedReader(new InputStreamReader(System.in));
String message;

boolean log_in_out=true;
while (true)
{
message = fromConsole.readLine();
client.handleMessageFromClientUI(message);
}
char first = message.charAt(0);
if (first == '#') {
if (message.equals("#quit")) {
System.out.println("The client has quit");
System.exit(1);
}
else if (message.equals("#logoff")) {
log_in_out = false;
client.handleMessageFromClientUI(client.getLogin()+" left the server");
System.out.println("The connection has closed");
}
else if (message.split(" ")[0].equals(("#sethost"))) {
if (log_in_out==false) {
client.setHost(message.split(" ")[1]);
System.out.println("Host set to: " + client.getHost());
}
else {
System.out.println("You have to be logged out to set the host");
}
}
else if (message.split(" ")[0].equals(("#setport"))) {
if (log_in_out==false) {
int holder = Integer.parseInt(message.split(" ")[1]);
client.setPort(holder);
System.out.println("Port set to: " + client.getPort());
}
else {
System.out.println("You have to be logged out to set the port");
}
}
else if (message.equals("#login")) {
if (log_in_out==true) {
System.out.println("You are logged in");
}
else {
client.openConnection();
log_in_out=true;
System.out.println("Welcome Back!");
}
}
else if (message.equals("#gethost")) {
System.out.println(client.getHost());
}
else if (message.equals("#getport")) {
System.out.println(client.getPort());
}
else {
System.out.println("Not a proper command");
}
}
else {
client.handleMessageFromClientUI(message);
}

}

}
catch (Exception ex)
{
Expand All @@ -101,22 +161,41 @@ public void display(String message)
* This method is responsible for the creation of the Client UI.
*
* @param args[0] The host to connect to.
* @throws IOException
*/
public static void main(String[] args)
{
String host = "";
int port = 0; //The port number

try
{
host = args[0];
String log_in = "";
if (args.length>0) {
log_in=args[0];
}
catch(ArrayIndexOutOfBoundsException e)
{
host = "localhost";
else {
System.out.println("You must have a login ID");
}
try {
host=args[1];
}
catch (ArrayIndexOutOfBoundsException e) {
host="localhost";
}

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


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

//System.out.println(args[1]);


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

//End of ConsoleChat class
51 changes: 42 additions & 9 deletions code/simplechat1/EchoServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

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

/**
* This class overrides some of the methods in the abstract
Expand All @@ -23,6 +24,7 @@ public class EchoServer extends AbstractServer
* The default port to listen on.
*/
final public static int DEFAULT_PORT = 5555;
ChatIF main_server_UI;

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

Expand All @@ -31,9 +33,11 @@ public class EchoServer extends AbstractServer
*
* @param port The port number to connect on.
*/
public EchoServer(int port)
{
super(port);

public EchoServer(int port) {
super(port);


}


Expand All @@ -45,9 +49,19 @@ 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)
public void handleMessageFromClient(Object msg, ConnectionToClient client)
{
String holder=msg.toString();
if (holder.contains("#login")) {
System.out.println(msg);
this.sendToAllClients("Welcome to the server " + holder.split(" ")[1]+".");
client.setInfo("Login ID",holder.split(" ")[1]);
}
else {
System.out.println("This message has been received: " + msg + "from: " + client);
this.sendToAllClients(client.getInfo("Login ID")+": "+msg);
}

System.out.println("Message received: " + msg + " from " + client);
this.sendToAllClients(msg);
}
Expand All @@ -58,18 +72,37 @@ public EchoServer(int port)
*/
protected void serverStarted()
{

System.out.println
("Server listening for connections on port " + getPort());
try {
listen();
}

catch(IOException exception) {
System.out.println("There is an 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();

this.sendToAllClients("Server stopped listeneing for connections" + getPort());

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

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

//Class methods ***************************************************
Expand Down
Loading