Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed ClientConsole and ChatClient #52

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
73 changes: 62 additions & 11 deletions code/simplechat1/ClientConsole.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class ClientConsole implements ChatIF
* The default port to connect on.
*/
final public static int DEFAULT_PORT = 5555;
String loginid;

//Instance variables **********************************************

Expand All @@ -41,11 +42,13 @@ 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 loginid)
{
loginid = loginid;
try
{
client= new ChatClient(host, port, this);
client = new ChatClient(host, port, this);
client.handleMessageFromClientUI("#login " + loginid);
}
catch(IOException exception)
{
Expand Down Expand Up @@ -73,15 +76,62 @@ public void accept()
while (true)
{
message = fromConsole.readLine();
client.handleMessageFromClientUI(message);
}
if (message.toString().charAt(0) == '#'){
commands(message.split(" "));

}
else {
client.handleMessageFromClientUI(message);
}
}
}
catch (Exception ex)
{
System.out.println
("Unexpected error while reading from console!");
}
}

protected void commands(String[] splitMessage) {
if (splitMessage[0] == "#quit") {
try{
client.closeConnection();
client.quit();
}
catch(Exception e) {
}
}
else if (splitMessage[0] == "#logoff"){
try {
client.closeConnection();
}
catch(Exception e) {
}
}
else if (splitMessage[0] == "#sethost"){
if(!client.isConnected()){
client.setHost(splitMessage[1]);
}
}
else if (splitMessage[0] == "#setport"){
if(!client.isConnected()) {
client.setPort(Integer.parseInt(splitMessage[1]));
}
}
else if (splitMessage[0] == "#login"){
try{
client.openConnection();
}
catch(IOException e) {
}
}
else if (splitMessage[0] == "#gethost"){
System.out.println("Host: "+client.getHost());
}
else if (splitMessage[0] == "#getport"){
System.out.println("Port: "+client.getPort());
}
}

/**
* This method overrides the method in the ChatIF interface. It
Expand All @@ -105,17 +155,18 @@ public void display(String message)
public static void main(String[] args)
{
String host = "";
String loginID = "";
int port = 0; //The port number

try
{
host = args[0];
if(args.length==0){
System.out.println("ERROR - No login ID specified. Connection Aborted.");
System.exit(1);
}
catch(ArrayIndexOutOfBoundsException e)
{
host = "localhost";
else if(args.length == 1){
loginID = args[0];
host = "localhost";
}
ClientConsole chat= new ClientConsole(host, DEFAULT_PORT);
ClientConsole chat= new ClientConsole(host, DEFAULT_PORT, loginID);
chat.accept(); //Wait for console data
}
}
Expand Down
10 changes: 10 additions & 0 deletions code/simplechat1/client/ChatClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@ public void handleMessageFromServer(Object msg)
{
clientUI.display(msg.toString());
}

protected void connectionException(Exception exception) {
clientUI.display("WARNING - Server has stopped listening for connections");
clientUI.display("SERVER SHUTTING DOWN! DISCONNECTING!");
clientUI.display("Abnormal termination of connection.");
}

protected void connectionClosed() {
System.out.println("Disconnected from server.");
}

/**
* This method handles all data coming from the UI
Expand Down