Added ObjectProcessor interface with some implementations

This commit is contained in:
Kai S. K. Engelbart 2019-12-30 15:53:40 +02:00
parent b7af8aa7bd
commit 2c9223236f
5 changed files with 99 additions and 72 deletions

View File

@ -0,0 +1,27 @@
package envoy.server;
import envoy.data.LoginCredentials;
import envoy.data.User;
/**
* Project: <strong>envoy-server-standalone</strong><br>
* File: <strong>LoginCredentialProcessor.java</strong><br>
* Created: <strong>30.12.2019</strong><br>
*
* @author Kai S. K. Engelbart
* @since
*/
public class LoginCredentialProcessor implements ObjectProcessor<LoginCredentials, User> {
// TODO: Acquire user IDs from database
private static long currentUserId = 1;
@Override
public User process(LoginCredentials input) {
System.out.println("Received login credentials " + input);
return new User(currentUserId++, input.getName());
}
@Override
public Class<LoginCredentials> getInputClass() { return LoginCredentials.class; }
}

View File

@ -0,0 +1,20 @@
package envoy.server;
import envoy.data.Message;
/**
* Project: <strong>envoy-server-standalone</strong><br>
* File: <strong>MessageProcessor.java</strong><br>
* Created: <strong>30.12.2019</strong><br>
*
* @author Kai S. K. Engelbart
* @since
*/
public class MessageProcessor implements ObjectProcessor<Message, Void> {
@Override
public Void process(Message input) { return null; }
@Override
public Class<Message> getInputClass() { return Message.class; }
}

View File

@ -5,15 +5,12 @@ import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.ObjectInputStream; import java.io.ObjectInputStream;
import java.io.ObjectOutputStream; import java.io.ObjectOutputStream;
import java.util.Set;
import com.jenkov.nioserver.IMessageProcessor; import com.jenkov.nioserver.IMessageProcessor;
import com.jenkov.nioserver.Message; import com.jenkov.nioserver.Message;
import com.jenkov.nioserver.WriteProxy; import com.jenkov.nioserver.WriteProxy;
import envoy.data.LoginCredentials;
import envoy.data.User;
import envoy.event.Event;
/** /**
* Project: <strong>envoy-server-standalone</strong><br> * Project: <strong>envoy-server-standalone</strong><br>
* File: <strong>ObjectMessageProcessor.java</strong><br> * File: <strong>ObjectMessageProcessor.java</strong><br>
@ -24,80 +21,41 @@ import envoy.event.Event;
*/ */
public class ObjectMessageProcessor implements IMessageProcessor { public class ObjectMessageProcessor implements IMessageProcessor {
private final Set<ObjectProcessor<?, ?>> processors;
public ObjectMessageProcessor(Set<ObjectProcessor<?, ?>> processors) { this.processors = processors; }
@SuppressWarnings("unchecked")
@Override @Override
public void process(Message message, WriteProxy writeProxy) { public void process(Message message, WriteProxy writeProxy) {
try (ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(message.sharedArray, message.offset + 4, message.length - 4))) { try (ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(message.sharedArray, message.offset + 4, message.length - 4))) {
Object obj = in.readObject(); Object obj = in.readObject();
System.out.println("Read object: " + obj.toString()); System.out.println("Read object: " + obj.toString());
handleObject(message, writeProxy, obj);
// Process object
processors.stream()
.filter(p -> p.getInputClass().isInstance(obj))
.forEach((@SuppressWarnings("rawtypes") ObjectProcessor p) -> {
Object responseObj = p.process(p.getInputClass().cast(obj));
if (responseObj != null) {
// Create message targeted at the client
Message response = writeProxy.getMessage();
response.socketId = message.socketId;
// Serialize object to byte array
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try (ObjectOutputStream oout = new ObjectOutputStream(baos)) {
oout.writeObject(responseObj);
} catch (IOException e) {
e.printStackTrace();
}
byte[] objBytes = baos.toByteArray();
response.writeToMessage(objBytes);
writeProxy.enqueue(response);
}
});
} catch (IOException | ClassNotFoundException e) { } catch (IOException | ClassNotFoundException e) {
e.printStackTrace(); 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
}
} }

View File

@ -0,0 +1,17 @@
package envoy.server;
/**
* Project: <strong>envoy-server-standalone</strong><br>
* File: <strong>ObjectProcessor.java</strong><br>
* Created: <strong>30.12.2019</strong><br>
*
* @author Kai S. K. Engelbart
* @since Envoy Server Standalone v0.1-alpha
*/
public interface ObjectProcessor<T, U> {
U process(T input);
Class<T> getInputClass();
}

View File

@ -1,6 +1,8 @@
package envoy.server; package envoy.server;
import java.io.IOException; import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
import com.jenkov.nioserver.Server; import com.jenkov.nioserver.Server;
@ -15,7 +17,10 @@ import com.jenkov.nioserver.Server;
public class Startup { public class Startup {
public static void main(String[] args) throws IOException { public static void main(String[] args) throws IOException {
Server server = new Server(8080, () -> new ObjectMessageReader(), new ObjectMessageProcessor()); Set<ObjectProcessor<?, ?>> processors = new HashSet<>();
processors.add(new LoginCredentialProcessor());
processors.add(new MessageProcessor());
Server server = new Server(8080, () -> new ObjectMessageReader(), new ObjectMessageProcessor(processors));
server.start(); server.start();
} }
} }