Changed code as requested by @CyB3RC0nN0R

This commit is contained in:
delvh 2019-12-29 17:52:57 +01:00
parent 4174296e60
commit caef287137

View File

@ -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
}
}