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

Yuanyuan Ji-Assignment1 #27

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
75 changes: 70 additions & 5 deletions code/simplechat1/ClientConsole.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ 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);
client = new ChatClient(id, host, port, this);
}
catch(IOException exception)
{
Expand Down Expand Up @@ -73,7 +73,51 @@ public void accept()
while (true)
{
message = fromConsole.readLine();
client.handleMessageFromClientUI(message);
char first = message.charAt(0);
char command = '#';
if (first == command) {
String msg = message.substring(1);
String[] cmd = msg.split(" ");
if (cmd[0].equals("quit")) {
System.out.println("The client is quit.");
client.quit();
}else if (cmd[0].equals("logoff")) {
System.out.println("The client is logoff.");
client.closeConnection();
}else if (cmd[0].equals("sethost")) {
if (!client.isConnected()) {
client.setHost(cmd[1]);
System.out.println("The host set to: " + cmd[1]);
} else {
System.out.println("The client is still connected, so host cannot be set!");
}
}else if (cmd[0].equals("setport")) {
if (!client.isConnected()) {
client.setPort(Integer.parseInt(cmd[1]));
System.out.println("The port set to: " + cmd[1]);
} else {
System.out.println("The client is still connected, so port cannot be set!");
}
}else if (cmd[0].equals("login")) {
if (!client.isConnected()) {
try {
System.out.println(cmd[1] + " is login.");
client.openConnection();
client.handleMessageFromClientUI("#login " + client.getId());
} catch (Exception e) {
System.out.println("No id! Connection cannot be established.");
}
} else {
System.out.println("It is already connected, so the client cannot login!");
}
}else if (cmd[0].equals("gethost")) {
System.out.println("The host is: " + client.getHost());
}else if (cmd[0].equals("getport")) {
System.out.println("The port is: " + client.getPort());
}
}else {
client.handleMessageFromClientUI(message);
}
}
}
catch (Exception ex)
Expand Down Expand Up @@ -106,16 +150,37 @@ 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)
{
System.out.println("ERROR - No login ID specified. Connection aborted.");
System.exit(0);
}

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

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

ClientConsole chat = new ClientConsole(id, host, port);
chat.accept(); //Wait for console data
}
}
Expand Down
50 changes: 47 additions & 3 deletions code/simplechat1/EchoServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public class EchoServer extends AbstractServer
* The default port to listen on.
*/
final public static int DEFAULT_PORT = 5555;

private boolean cls = false;

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

Expand All @@ -47,9 +49,26 @@ public EchoServer(int port)
*/
public void handleMessageFromClient
(Object msg, ConnectionToClient client)
{
System.out.println("Message received: " + msg + " from " + client);
this.sendToAllClients(msg);
{
if (msg.toString().startsWith("#login ")) {
if (client.getInfo("id") == null) {
String message = String.valueOf(msg);
String[] cmd = message.split(" ");
if (cmd[0].equals("#login")) {
client.setInfo("id", cmd[1]);
this.sendToAllClients(client.getInfo("id") + " is logged in.");
}
} else if (client.getInfo("id") != null) {
try
{
client.sendToClient("You need to login before you can chat!");
client.close();
}
catch (IOException e) {}
}
}
System.out.println("Message received: " + msg + " from " + client.getInfo("id"));
this.sendToAllClients(client.getInfo("id") + "> " + msg);
}

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

protected void clientConnected(ConnectionToClient client) {
System.out.println(client.getInfo("id") + " is connected. ");
}

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

synchronized protected void clientException(
ConnectionToClient client, Throwable exception) {
System.out.println(client.getInfo("id") + " is disconnected. ");
}

protected void serverClosed() {
cls = true;
}

protected boolean serverIsClosed() {
return cls;
}

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

/**
Expand All @@ -95,6 +135,8 @@ public static void main(String[] args)
}

EchoServer sv = new EchoServer(port);

ServerConsole sc = new ServerConsole(sv);

try
{
Expand All @@ -104,6 +146,8 @@ public static void main(String[] args)
{
System.out.println("ERROR - Could not listen for clients!");
}

sc.accept();
}
}
//End of EchoServer class
79 changes: 79 additions & 0 deletions code/simplechat1/ServerConsole.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import java.io.*;
import common.*;

public class ServerConsole implements ChatIF {

EchoServer server;

public ServerConsole(EchoServer server) {
this.server = server;
}

public void accept() {
try {
BufferedReader fromConsole = new BufferedReader(new InputStreamReader(System.in));
String message;
while (true) {
message = fromConsole.readLine();
char first = message.charAt(0);
char command = '#';
if (first == command) {
String msg = message.substring(1);
String[] cmd = msg.split(" ");
if (cmd[0].equals("quit")) {
System.out.println("The server is quit.");
try {
server.close();
}
catch(IOException e) {}
System.exit(0);
} else if (cmd[0].equals("stop")) {
server.sendToAllClients("WARNING - Server has stopped listening for connections.");
server.stopListening();
} else if (cmd[0].equals("close")) {
server.sendToAllClients("WARNING - The server has stopped listening for connections" +
"\n" + "SERVER SHUTTING DOWN! DISCONNECTING!");
server.close();
} else if (cmd[0].equals("setport")) {
if (server.serverIsClosed()) {
server.setPort(Integer.parseInt(cmd[1]));
System.out.println("The port set to: " + cmd[1]);
} else {
System.out.println("The server is not closed, so port cannot be set!");
}
} else if (cmd[0].equals("start")) {
if (!server.isListening()) {
server.listen();
System.out.println("The server is started.");
} else {
System.out.println("The server is not stopped, so it cannot start!");
}
}else if (cmd[0].equals("getport")) {
System.out.println("The port is: " + server.getPort());
}
}else {
server.sendToAllClients( "SERVER MSG>" + message);
}
}
}
catch (Exception ex) {
System.out.println("Unexpected error while reading from console!");
}
}

public void display(String message) {
System.out.println("> " + message);
}

public static void main(String[] args) {
EchoServer server = new EchoServer(0);
try
{
server.setPort(Integer.parseInt(args[0]));
}
catch(ArrayIndexOutOfBoundsException e)
{
server.setPort(5555);
}
}
}
18 changes: 17 additions & 1 deletion code/simplechat1/client/ChatClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class ChatClient extends AbstractClient
*/
ChatIF clientUI;

String id;

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

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

public ChatClient(String host, int port, ChatIF clientUI)
public ChatClient( String id, String host, int port, ChatIF clientUI)
throws IOException
{
super(host, port); //Call the superclass constructor
this.clientUI = clientUI;
this.id = id;
openConnection();
sendToServer("#login " + id);
}


Expand Down Expand Up @@ -90,5 +93,18 @@ public void quit()
catch(IOException e) {}
System.exit(0);
}


public void ConnectionClosed() {
System.out.println("The connection is closed.");
}

public void ConnectionException(){
System.out.println("Abnormal termination of connection.");
}

public String getId() {
return id;
}
}
//End of ChatClient class