This repository has been archived on 2021-12-05. You can view files and clone it, but cannot push or open issues or pull requests.
envoy/src/main/java/envoy/server/ObjectMessageProcessor.java

103 lines
3.7 KiB
Java
Raw Normal View History

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: <strong>envoy-server-standalone</strong><br>
* File: <strong>ObjectMessageProcessor.java</strong><br>
* Created: <strong>28.12.2019</strong><br>
*
* @author Kai S. K. Engelbart
2019-12-29 10:09:26 +01:00
* @since Envoy Server Standalone v0.1-alpha
*/
public class ObjectMessageProcessor implements IMessageProcessor {
@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();
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 {
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();
immediateResponse = isRecipientAvailable(-1); // TODO replace with wanted clientID
} else if (obj instanceof Event) {// if object is Event
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();
// 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);
}
}
/**
* 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
}
}