From b7af8aa7bd6d48a6d1ca1073091a11571cbc2a17 Mon Sep 17 00:00:00 2001 From: delvh Date: Sun, 29 Dec 2019 17:52:57 +0100 Subject: [PATCH] Changed code as requested by @CyB3RC0nN0R --- .../envoy/server/ObjectMessageProcessor.java | 59 ++++++++++++------- 1 file changed, 38 insertions(+), 21 deletions(-) diff --git a/src/main/java/envoy/server/ObjectMessageProcessor.java b/src/main/java/envoy/server/ObjectMessageProcessor.java index 8b999c2..7c9592b 100644 --- a/src/main/java/envoy/server/ObjectMessageProcessor.java +++ b/src/main/java/envoy/server/ObjectMessageProcessor.java @@ -24,8 +24,6 @@ import envoy.event.Event; */ public class ObjectMessageProcessor implements IMessageProcessor { - private long currentUserID = 0;// temporary - @Override public void process(Message message, WriteProxy writeProxy) { try (ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(message.sharedArray, message.offset + 4, message.length - 4))) { @@ -51,36 +49,55 @@ public class ObjectMessageProcessor implements IMessageProcessor { * @since Envoy Server Standalone v0.1-alpha */ private void handleObject(Message request, WriteProxy writeProxy, Object obj) throws IllegalArgumentException { - boolean responseToSameSocket = false; + + long currentUserID = 0; // TODO temporary. Only for testing purposes + boolean responseToSameSocket = false, immediateResponse = true; Object usage; + + // determining the type of the incoming object if (obj instanceof envoy.data.Message) {// if object is Message envoy.data.Message cast = (envoy.data.Message) obj; - usage = cast.getClass(); + usage = cast.getClass(); + immediateResponse = isRecipientAvailable(-1); // TODO replace with wanted clientID } else if (obj instanceof Event) {// if object is Event - usage = (Event) obj; + usage = (Event) obj; + immediateResponse = isRecipientAvailable(-1); // TODO replace with wanted clientID } else if (obj instanceof LoginCredentials) {// if object is LoginCredential responseToSameSocket = true; LoginCredentials cast = (LoginCredentials) obj; usage = new User(currentUserID++, cast.getName()); } else throw new IllegalArgumentException(); - Message response = writeProxy.getMessage(); - if (responseToSameSocket) { - response.socketId = request.socketId; - } else { - response.socketId = -0;// TODO temporary.Needs to be replaced - return; + // handling of incoming object + if (immediateResponse) { + Message response = writeProxy.getMessage(); + if (responseToSameSocket) { + response.socketId = request.socketId; + } else { + response.socketId = -0;// TODO temporary.Needs to be replaced + return; + } + // Serialize object to byte array + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + try (ObjectOutputStream oout = new ObjectOutputStream(baos)) { + oout.writeObject(usage); + } catch (IOException e) { + e.printStackTrace(); + } + byte[] objBytes = baos.toByteArray(); + response.writeToMessage(objBytes); + writeProxy.enqueue(response); } - // Serialize object to byte array - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - try (ObjectOutputStream oout = new ObjectOutputStream(baos)) { - oout.writeObject(usage); - } catch (IOException e) { - e.printStackTrace(); - } - byte[] objBytes = baos.toByteArray(); - response.writeToMessage(objBytes); - writeProxy.enqueue(response); + } + /** + * This method determines if the recipient is online + * + * @param otherClientID the ID of the recipient + * @return true, if the recipient is online + * @since Envoy Server Standalone v0.1-alpha + */ + private boolean isRecipientAvailable(long otherClientID) { + return false;// TODO needs to be adapted to return true if the wanted client is online } } \ No newline at end of file