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
4 changes: 4 additions & 0 deletions OcsfPhase2.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
$0=OcsfPhase2
Name = Nick Bailuc
Student Number = 300014335
Email Address = nbail040@uottawa.ca
224 changes: 129 additions & 95 deletions code/simplechat1/ClientConsole.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,116 +7,150 @@
import common.*;

/**
* This class constructs the UI for a chat client. It implements the
* 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 Timothy C. Lethbridge
* @author Dr Robert Laganière
* @version July 2000
*/
public class ClientConsole 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 ConsoleChat.
*/
ChatClient client;
//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 ConsoleChat.
*/
ChatClient client;

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

/**
* Constructs an instance of the ClientConsole UI.
*
* @param host The host to connect to.
* @param port The port to connect on.
*/
public ClientConsole(String host, int port)
{
try
{
client= new ChatClient(host, port, this);
}
catch(IOException exception)
{
System.out.println("Error: Can't setup connection!"
+ " Terminating client.");
System.exit(1);
}
}
/**
* Constructs an instance of the ClientConsole UI.
*
* @param host The host to connect to.
* @param port The port to connect on.
*/
public ClientConsole(String host, int port)
{
try
{
client= new ChatClient(host, port, this);
}
catch(IOException 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;
//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();
client.handleMessageFromClientUI(message);
}
}
catch (Exception ex)
{
System.out.println
("Unexpected error while reading from console!");
}
}
while (true)
{
message = fromConsole.readLine();
client.handleMessageFromClientUI(message);
if (message.charAt(0) == '#')
{/*TODO lang.String seems to not recognize full sys-messages.
* *TODO Attempt to stdin with different character encoding.
* *TODO Try escaping # or specifying ASCII number 0x23 */
if (message == "#quit")
{
System.out.println("Client terminated");
connectionClosed();
System.exit(0); // Exit code 0 represents user exit
}
if (message == "#logoff") connectionClosed();
if (message == "#sethost")//TODO Parse string with tokenizer
{
String newHost = "";//TODO Add string tokenizer
// setHost(newHost);
}
if (message == "#setport") {}//setPort(newPort);
// if (message == "#gethost") getHost();
// if (message == "#getport") getPort();
}
}
}
catch (Exception ex)
{
System.out.println
("Unexpected error while reading from console!");
}


/* catch(Exception exception)
{
//connectionException(Exception exception);
System.out.println("Server has shut down");
}*/
}

protected void connectionClosed()
{
System.out.println("Server has shut down");
// System.exit(2); // Exit code 2 represents server shutdown
//connectionException(exception);
}

/**
* 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);
}
/**
* 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)
{
String host = "";
int port = 0; //The port number
//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)
{
String host = "";
int port = Integer.parseInt(args[1]); //stdin > port

try
{
host = args[0];
}
catch(ArrayIndexOutOfBoundsException e)
{
host = "localhost";
}
ClientConsole chat= new ClientConsole(host, DEFAULT_PORT);
chat.accept(); //Wait for console data
}
try
{
host = args[0];
}
catch(ArrayIndexOutOfBoundsException e)
{
host = "localhost";
}
ClientConsole chat= new ClientConsole(host, port);
chat.accept(); //Wait for console data
}
}
//End of ConsoleChat class
Loading