From 4174296e607a5378eeec18c11c2d151e35aaeb81 Mon Sep 17 00:00:00 2001 From: delvh Date: Sun, 29 Dec 2019 14:16:39 +0100 Subject: [PATCH] Added method to handle incoming objects (not finished as the methods to further process these "Events" have not been implemented yet) --- .../envoy/server/ObjectMessageProcessor.java | 57 ++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-) diff --git a/src/main/java/envoy/server/ObjectMessageProcessor.java b/src/main/java/envoy/server/ObjectMessageProcessor.java index 77a086b..8b999c2 100644 --- a/src/main/java/envoy/server/ObjectMessageProcessor.java +++ b/src/main/java/envoy/server/ObjectMessageProcessor.java @@ -1,13 +1,19 @@ package envoy.server; import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; import com.jenkov.nioserver.IMessageProcessor; import com.jenkov.nioserver.Message; import com.jenkov.nioserver.WriteProxy; +import envoy.data.LoginCredentials; +import envoy.data.User; +import envoy.event.Event; + /** * Project: envoy-server-standalone
* File: ObjectMessageProcessor.java
@@ -18,14 +24,63 @@ import com.jenkov.nioserver.WriteProxy; */ 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))) { Object obj = in.readObject(); - // TODO: Process pipeline + System.out.println("Read object: " + obj.toString()); + handleObject(message, writeProxy, obj); } catch (IOException | ClassNotFoundException e) { e.printStackTrace(); } } + + /** + * This method splits incoming objects into the different objects that are + * relevant to the server and guides them to their predestined spot. + * + * @param request the {@link Message} in which the objects are saved + * @param writeProxy the writeProxy to define the resulting socket for + * @param obj the object that has been read out of the {@link Message} + * @throws IllegalArgumentException if the object given is neither an + * {@link envoy.data.Message}, an {@link Event} + * nor a {@link LoginCredentials} + * @since Envoy Server Standalone v0.1-alpha + */ + private void handleObject(Message request, WriteProxy writeProxy, Object obj) throws IllegalArgumentException { + boolean responseToSameSocket = false; + Object usage; + if (obj instanceof envoy.data.Message) {// if object is Message + envoy.data.Message cast = (envoy.data.Message) obj; + usage = cast.getClass(); + } else if (obj instanceof Event) {// if object is Event + usage = (Event) obj; + } 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; + } + // 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); + + } } \ No newline at end of file