Skip to content

Commit

Permalink
add result
Browse files Browse the repository at this point in the history
  • Loading branch information
yangxvlin committed Jun 17, 2020
1 parent bcc5189 commit 7a1390a
Show file tree
Hide file tree
Showing 34 changed files with 2,993 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
## Introduction
Shared whiteboards allow multiple users to draw simultaneously on a canvas. There are multiple examples found on the Internet that support a range of features such as freehand drawing with the mouse, drawing lines and shapes such as circles and squares that can be moved and resized, and inserting text.

## result
- 25/25
- ![](./docs/result.png)

## How to run

The first user creates a whiteboard and becomes the whiteboard’s manager
Expand Down
Binary file modified docs/COMP90015 Projet2 2020S1 Report Xulin Yang 904904.pdf
Binary file not shown.
Binary file added docs/result.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified submit/COMP90015 Projet2 2020S1 Report Xulin Yang 904904.pdf
Binary file not shown.
Binary file modified submit/Xulin Yang 904904.zip
Binary file not shown.
139 changes: 139 additions & 0 deletions submit/src/Communication/ClientConnection.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
package Communication;

import WhiteBoard.WhiteBoardApplication;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

import java.io.IOException;

import static Communication.CommunicationConstant.*;
import static WhiteBoard.Util.popupNoServerConnectionErrorDialog;

/**
* Xulin Yang, 904904
*
* @create 2020-05-11 15:24
* description: client's connection socket to server
**/

public class ClientConnection {
/**
* server socket
*/
private CommunicationSocket socket;

public ClientConnection(CommunicationSocket socket) {
this.socket = socket;
}

/**
* @param app application
* @param username user's name
*/
public void connect(WhiteBoardApplication app, String username) {
JSONObject obj = new JSONObject();
JSONParser parser = new JSONParser();

if (app.isManager()) {
// request to create a white board
obj.put(USER_REQUEST, CREATE_WHITEBOARD);
obj.put(USERNAME, username);
} else {
// request to join a white board
obj.put(USER_REQUEST, REQUEST_JOIN_WHITEBOARD);
obj.put(USERNAME, username);
}

String response = "";
try {
socket.send(obj.toJSONString());
// grant access to whiteboard or rejected
response = socket.receive();
obj = (JSONObject) parser.parse(response);

String responseType = (String) obj.get(RESPONSE);

switch (responseType) {
case ALREADY_HAS_MANAGER:
app.error(responseType);
break;
case SUCCESSFUL_CREATE_WHITEBOARD:
app.start((String) obj.get(UID));
break;
case REJECTED_BY_MANAGER:
app.error(responseType);
break;
case SUCCESSFUL_JOIN_WHITEBOARD:
app.start((String) obj.get(UID));
break;
case NO_MANAGER_YET:
app.error(responseType);
break;
default:
System.out.println("Unknown response: " + response);
}
} catch (IOException e) {
popupNoServerConnectionErrorDialog();
} catch (ParseException e) {
System.out.println("Invalid response: " + response);
}
}

/**
* @param isManager true if is manager
* @param uid user's unique id
*/
public void disconnect(boolean isManager, String uid) {

JSONObject obj = new JSONObject();

if (isManager) {
// manager close
obj.put(USER_REQUEST, CLOSE_WHITE_BOARD);
obj.put(UID, uid);
} else {
// user leave
obj.put(USER_REQUEST, LEAVE_WHITE_BOARD);
obj.put(UID, uid);
}

try {
socket.send(obj.toJSONString());
socket.close();
} catch (IOException e) {
popupNoServerConnectionErrorDialog();
}
}

/**
* @param uid user to be kicked out
*/
public void kickOut(String uid) {
JSONObject obj = new JSONObject();

obj.put(USER_REQUEST, KICK_OUT_USER);
obj.put(UID, uid);

try {
socket.send(obj.toJSONString());
} catch (IOException e) {
popupNoServerConnectionErrorDialog();
}
}

/**
* @param type operation type
*/
public void notifyUserWithManagerOperation(String type) {
JSONObject obj = new JSONObject();

obj.put(USER_REQUEST, type);

try {
socket.send(obj.toJSONString());
} catch (IOException e) {
popupNoServerConnectionErrorDialog();
}
}
}
128 changes: 128 additions & 0 deletions submit/src/Communication/ClientRequestsThread.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package Communication;

import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import remote.UserManager;

import java.io.IOException;

import static Communication.CommunicationConstant.*;

/**
* Xulin Yang, 904904
*
* @create 2020-05-11 1:21
* description: the thread run for server to process request from client
**/

public class ClientRequestsThread extends Thread {

/**
* socket
*/
private CommunicationSocket client;

/**
* manage users using whiteboard
*/
private UserManager userManager;

public ClientRequestsThread(CommunicationSocket client, UserManager userManager) {
this.client = client;

this.userManager = userManager;
}

@Override
public void run() {
super.run();

JSONParser parser = new JSONParser();
JSONObject jsonObject = new JSONObject();

String request;
while (true) {
try {
request = client.receive();
try {
jsonObject = (JSONObject) parser.parse(request);
} catch (ParseException e) {
System.out.println("Invalid request: " + request);
interrupt();
}

String requestType = (String) jsonObject.get(USER_REQUEST);

// send response
switch (requestType) {
case CREATE_WHITEBOARD:
// already has manager
if (userManager.hasManager()) {
client.responseAlreadyHasManager();
} else {
String uid = userManager.addUser((String) jsonObject.get(USERNAME));
userManager.setManagerUID(uid);
userManager.addUserSocket(uid, client);
client.responseSuccessfulCreateWhiteboard(uid);
}
break;
case REQUEST_JOIN_WHITEBOARD:
CommunicationSocket managerSocket = userManager.getManagerCommunicationSocket();
// no manager reject
if (!userManager.hasManager()) {
client.responseNoManagerYet();
break;
}
String uid = userManager.addCandidateUser((String) jsonObject.get(USERNAME));
userManager.addUserSocket(uid, client);
managerSocket.askManagerAccept(uid);
break;
case ASK_JOIN_WHITEBOARD_RESULT:
String candidateUID = (String) jsonObject.get(UID);
boolean result = Boolean.parseBoolean((String) jsonObject.get(RESULT));
CommunicationSocket candidateSocket = userManager.getCommunicationSocket(candidateUID);
assert candidateSocket != null;
if (result) {
candidateSocket.sendJoinRequestResult(RESPONSE, SUCCESSFUL_JOIN_WHITEBOARD, result, candidateUID);
userManager.acceptCandidateUser(candidateUID);
} else {
candidateSocket.sendJoinRequestResult(RESPONSE, REJECTED_BY_MANAGER, result, candidateUID);
userManager.rejectCandidateUser(candidateUID);
}
break;
case LEAVE_WHITE_BOARD:
String uidToBeRemoved = (String) jsonObject.get(UID);
userManager.removeUser(uidToBeRemoved);
break;
case CLOSE_WHITE_BOARD:
userManager.broadcastManagerOperation(MANAGER_CLOSE);
userManager.clear();
break;
case KICK_OUT_USER:
String kickOutUID = (String) jsonObject.get(UID);
CommunicationSocket kickOutSocket = userManager.getCommunicationSocket(kickOutUID);
if (kickOutSocket != null) {
System.out.println(" |send kick out request to " + UID);
kickOutSocket.sendKickOutRequest();
} else {
System.out.println(" |kick out non-existing uer: " + UID);
}
userManager.removeUser(kickOutUID);
break;
case MANAGER_NEW:
userManager.broadcastManagerOperation(MANAGER_NEW);
break;
case MANAGER_OPEN:
userManager.broadcastManagerOperation(MANAGER_OPEN);
break;
default:
System.out.println("Unknown request type: " + request);
}
} catch (IOException e) {
System.out.println("Error in server socket");
break;
}
}
}
}
110 changes: 110 additions & 0 deletions submit/src/Communication/CommunicationConstant.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package Communication;

/**
* Xulin Yang, 904904
*
* @create 2020-05-11 19:02
* description: constants for communication protocol
**/

public class CommunicationConstant {
/**
* user's request
*/
public static final String USER_REQUEST = "request";

/**
* create whiteboard
*/
public static final String CREATE_WHITEBOARD = "Create Whiteboard";

/**
* join whiteboard request
*/
public static final String REQUEST_JOIN_WHITEBOARD = "Join Whiteboard";

/**
* ask join whiteboard
*/
public static final String ASK_JOIN_WHITEBOARD = "Ask Join Whiteboard";

/**
* ask join whiteboard result
*/
public static final String ASK_JOIN_WHITEBOARD_RESULT = "Ask Join Whiteboard result";

/**
* manager close the whiteboard
*/
public static final String CLOSE_WHITE_BOARD = "Manager close Whiteboard";

/**
* user leave the whiteboard
*/
public static final String LEAVE_WHITE_BOARD = "User leave Whiteboard";

/**
* manager kickout an user
*/
public static final String KICK_OUT_USER = "Kick out user";

/**
* manager close the whiteboard
*/
public static final String MANAGER_CLOSE = "Manager closed the whiteboard";

/**
* manager initialize a whiteboard
*/
public static final String MANAGER_NEW = "Manager new a whiteboard";

/**
* manager open a saved whiteboard
*/
public static final String MANAGER_OPEN = "Manager open a whiteboard";

/**
* result message
*/
public static final String RESULT = "result";

/**
* key for username
*/
public static final String USERNAME = "username";

/**
* key for response
*/
public static final String RESPONSE = "response";

/**
* already has manager exception
*/
public static final String ALREADY_HAS_MANAGER = "already has manager";

/**
* no manager create whiteboard exception
*/
public static final String NO_MANAGER_YET = "No whiteboard created by manager";

/**
* create whiteboard success
*/
public static final String SUCCESSFUL_CREATE_WHITEBOARD = "Successful create Whiteboard";

/**
* manager rejection exception
*/
public static final String REJECTED_BY_MANAGER = "rejected by the manager to join";

/**
* join whiteboard success
*/
public static final String SUCCESSFUL_JOIN_WHITEBOARD = "Successful join Whiteboard";

/**
* key for uid
*/
public static final String UID = "uid";
}
Loading

0 comments on commit 7a1390a

Please sign in to comment.