-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
34 changed files
with
2,993 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
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
BIN
+13.4 KB
(100%)
submit/COMP90015 Projet2 2020S1 Report Xulin Yang 904904.pdf
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} |
Oops, something went wrong.