2019-12-28 14:48:39 +01:00
|
|
|
package envoy.server;
|
|
|
|
|
|
|
|
import java.io.ByteArrayInputStream;
|
2019-12-29 14:16:39 +01:00
|
|
|
import java.io.ByteArrayOutputStream;
|
2019-12-28 14:48:39 +01:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.ObjectInputStream;
|
2019-12-29 14:16:39 +01:00
|
|
|
import java.io.ObjectOutputStream;
|
2019-12-28 14:48:39 +01:00
|
|
|
|
|
|
|
import com.jenkov.nioserver.IMessageProcessor;
|
|
|
|
import com.jenkov.nioserver.Message;
|
|
|
|
import com.jenkov.nioserver.WriteProxy;
|
|
|
|
|
2019-12-29 14:16:39 +01:00
|
|
|
import envoy.data.LoginCredentials;
|
|
|
|
import envoy.data.User;
|
|
|
|
import envoy.event.Event;
|
|
|
|
|
2019-12-28 14:48:39 +01:00
|
|
|
/**
|
|
|
|
* 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
|
2019-12-28 14:48:39 +01:00
|
|
|
*/
|
|
|
|
public class ObjectMessageProcessor implements IMessageProcessor {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void process(Message message, WriteProxy writeProxy) {
|
2019-12-29 09:48:15 +01:00
|
|
|
try (ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(message.sharedArray, message.offset + 4, message.length - 4))) {
|
2019-12-28 14:48:39 +01:00
|
|
|
Object obj = in.readObject();
|
2019-12-29 14:16:39 +01:00
|
|
|
|
2019-12-28 14:48:39 +01:00
|
|
|
System.out.println("Read object: " + obj.toString());
|
2019-12-29 14:16:39 +01:00
|
|
|
handleObject(message, writeProxy, obj);
|
2019-12-28 14:48:39 +01:00
|
|
|
} catch (IOException | ClassNotFoundException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
2019-12-29 14:16:39 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 {
|
2019-12-29 17:52:57 +01:00
|
|
|
|
|
|
|
long currentUserID = 0; // TODO temporary. Only for testing purposes
|
|
|
|
boolean responseToSameSocket = false, immediateResponse = true;
|
2019-12-29 14:16:39 +01:00
|
|
|
Object usage;
|
2019-12-29 17:52:57 +01:00
|
|
|
|
|
|
|
// determining the type of the incoming object
|
2019-12-29 14:16:39 +01:00
|
|
|
if (obj instanceof envoy.data.Message) {// if object is Message
|
|
|
|
envoy.data.Message cast = (envoy.data.Message) obj;
|
2019-12-29 17:52:57 +01:00
|
|
|
usage = cast.getClass();
|
|
|
|
immediateResponse = isRecipientAvailable(-1); // TODO replace with wanted clientID
|
2019-12-29 14:16:39 +01:00
|
|
|
} else if (obj instanceof Event) {// if object is Event
|
2019-12-29 17:52:57 +01:00
|
|
|
usage = (Event<?>) obj;
|
|
|
|
immediateResponse = isRecipientAvailable(-1); // TODO replace with wanted clientID
|
2019-12-29 14:16:39 +01:00
|
|
|
} 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();
|
|
|
|
|
2019-12-29 17:52:57 +01:00
|
|
|
// 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);
|
2019-12-29 14:16:39 +01:00
|
|
|
}
|
2019-12-29 17:52:57 +01:00
|
|
|
}
|
2019-12-29 14:16:39 +01:00
|
|
|
|
2019-12-29 17:52:57 +01:00
|
|
|
/**
|
|
|
|
* 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
|
2019-12-29 14:16:39 +01:00
|
|
|
}
|
2019-12-28 14:48:39 +01:00
|
|
|
}
|