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
101 changes: 92 additions & 9 deletions code/simplechat1/ClientConsole.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,16 @@ 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)
{
client= new ChatClient(loginid,host, port, this);
try
{
client= new ChatClient(host, port, this);
}
client.openConnection();
}
catch(IOException exception)
{
System.out.println("Error: Can't setup connection!"
+ " Terminating client.");
System.exit(1);
System.out.println("Cannot open connection. Awaiting command.");
}
}

Expand All @@ -73,7 +72,62 @@ public void accept()
while (true)
{
message = fromConsole.readLine();
client.handleMessageFromClientUI(message);

//hml #command
char messagecheck = message.charAt(0);
if (messagecheck == '#'){
String[] cmessage = message.split(" ");
String command = cmessage[0];
System.out.println(command);
if (command.equals("#quit")){
System.out.println("The client has quit");
client.quit();
}
else if (command.equals("#logoff")){
client.closeConnection();
}
else if (command.equals("#sethost")){
if (client.isConnected()){
System.out.println("The client is still connected.");
}
else{
String newhost = cmessage[1];
client.setHost(newhost);
System.out.println("Host set to " + newhost);
}
}
else if (command.equals("#setport")){
if (client.isConnected()){
System.out.println("The client is still connected.");
}
else{
int newport = Integer.parseInt(cmessage[1]);
client.setPort(newport);
System.out.println("Port set to " + newport);
}
}
else if (command.equals("#login")){
if (client.isConnected()){
System.out.println("The client is still connected.");
}
else{
client.openConnection();
client.handleMessageFromClientUI("#login " + client.getloginid());
}
}
else if (command.equals("#gethost")){
System.out.println(client.getHost());
}
else if (command.equals("#getport")){
System.out.println(client.getPort());
}
else{
System.out.println("Wrong command.");
}
}
else{
client.handleMessageFromClientUI(message);
}
}
}
catch (Exception ex)
Expand Down Expand Up @@ -106,16 +160,45 @@ public static void main(String[] args)
{
String host = "";
int port = 0; //The port number
String id = "";

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

try
{
host = args[1];
}
catch(ArrayIndexOutOfBoundsException e)
{
host = "localhost";
}
ClientConsole chat= new ClientConsole(host, DEFAULT_PORT);

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

ClientConsole chat= new ClientConsole(id, host, port);

if(chat.client.isConnected()){
if (chat.client.getloginid().equals("")){
System.out.println("ERROR - No login ID specified. Connection aborted.");
chat.client.quit();
}
chat.client.handleMessageFromClientUI("#login "+chat.client.getloginid());
}

chat.accept(); //Wait for console data
}
}
Expand Down
52 changes: 46 additions & 6 deletions code/simplechat1/EchoServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

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

/**
* This class overrides some of the methods in the abstract
* superclass in order to give more functionality to the server.
Expand All @@ -24,6 +23,8 @@ public class EchoServer extends AbstractServer
*/
final public static int DEFAULT_PORT = 5555;

private boolean sc = false;

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

/**
Expand All @@ -47,9 +48,29 @@ public EchoServer(int port)
*/
public void handleMessageFromClient
(Object msg, ConnectionToClient client)
{
System.out.println("Message received: " + msg + " from " + client);
this.sendToAllClients(msg);
{
try
{
String idcommand = (String)(msg);
String[] aidcommand = idcommand.split(" ");
if(aidcommand[0].equals("#login"))
{
if (client.getInfo("loginid")==null){
String idname = aidcommand[1];
client.setInfo("loginid",idname);
this.sendToAllClients(client.getInfo("loginid") + " has logged on.");
}
else{
client.sendToClient("Error, wrong command.");
client.close();
}
}
}
catch(Exception ex){
}
System.out.println("Message received: " + msg + " from " + client.getInfo("loginid"));
Object echmsg = (String)(client.getInfo("loginid")) + " " + (String)(msg);
this.sendToAllClients(echmsg);
}

/**
Expand All @@ -71,7 +92,22 @@ protected void serverStopped()
System.out.println
("Server has stopped listening for connections.");
}

//hml
protected void clientConnected(ConnectionToClient client) {
System.out.println("A new client is attempting to connect to the server.");
System.out.println(client.getInfo("loginid") + " has logged on.");
}

synchronized protected void clientDisconnected(ConnectionToClient client) {
System.out.println(client.getInfo("loginid") + " has disconnected.");
}

protected void serverClosed(){
sc = true;
}
protected boolean serverisClosed(){
return sc;
}
//Class methods ***************************************************

/**
Expand All @@ -95,7 +131,10 @@ public static void main(String[] args)
}

EchoServer sv = new EchoServer(port);

ServerConsole serverco = new ServerConsole(sv);



try
{
sv.listen(); //Start listening for connections
Expand All @@ -104,6 +143,7 @@ public static void main(String[] args)
{
System.out.println("ERROR - Could not listen for clients!");
}
serverco.accept();
}
}
//End of EchoServer class
70 changes: 70 additions & 0 deletions code/simplechat1/ServerConsole.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import common.*;
import java.io.*;
public class ServerConsole implements ChatIF{
EchoServer server;
public ServerConsole(EchoServer server){
this.server = server;
}
public void display(String message)
{
System.out.println("> " + message);
}
public void accept(){
try
{
BufferedReader fromConsole =
new BufferedReader(new InputStreamReader(System.in));
String message;

while (true){
message = fromConsole.readLine();
char messagecheck = message.charAt(0);
if (messagecheck == '#'){
String[] cmessage = message.split(" ");
String command = cmessage[0];
System.out.println(command);
if (command.equals("#quit")){
System.out.println("The server has quit");
try
{
server.close();
}
catch(IOException e) {}
System.exit(0);
}
else if (command.equals("#stop")){
server.stopListening();
server.sendToAllClients("WARNING - Server has stopped listening for connections.");
}
else if (command.equals("#close")){
server.sendToAllClients("WARNING - The server has stopped listening for connections"+
"\n"+"SERVER SHUTTING DOWN! DISCONNECTING!");
server.close();
}
else if (command.equals("#setport")){
if (!server.serverisClosed()){
System.out.println("The server is still connected.");
}
else{
int newport = Integer.parseInt(cmessage[1]);
server.setPort(newport);
}
}
else if (command.equals("#start")){
if(server.isListening()){
System.out.println("The server is still listening")
}
else{
server.listen();
}
}
}
server.sendToAllClients("SERVER MSG> " + message);
}
}
catch(Exception ex){
System.out.println
("Unexpected error while reading from console!");
}
}
}
27 changes: 20 additions & 7 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;

private String loginid;

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

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

public ChatClient(String host, int port, ChatIF clientUI)
throws IOException
public ChatClient(String loginid, String host, int port, ChatIF clientUI)

{
super(host, port); //Call the superclass constructor
this.clientUI = clientUI;
openConnection();
this.loginid = loginid;
}


Expand Down Expand Up @@ -72,12 +72,25 @@ public void handleMessageFromClientUI(String message)
}
catch(IOException e)
{
clientUI.display
("Could not send message to server. Terminating client.");
clientUI.display("Could not send message to server. Terminating client.");
quit();
}
}

//hml
protected void connectionClosed() {
System.out.println("The connetion is closed");
}

protected void connectionException(Exception exception) {
System.out.println("Abnormal termination of connection.");
}
public void setloginid(String id){
this.loginid = id;
}
public String getloginid(){
return loginid;
}

/**
* This method terminates the client.
*/
Expand Down