Merge pull request #8 from informatik-ag-ngl/f/object_handling
Added method to handle incoming objects
This commit is contained in:
commit
41e91ef4cb
29
src/main/java/envoy/server/LoginCredentialProcessor.java
Normal file
29
src/main/java/envoy/server/LoginCredentialProcessor.java
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
package envoy.server;
|
||||||
|
|
||||||
|
import envoy.data.LoginCredentials;
|
||||||
|
import envoy.data.User;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This {@link ObjectProcessor} handles {@link LoginCredentials}.<br>
|
||||||
|
* <br>
|
||||||
|
* 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 Envoy Server Standalone v0.1-alpha
|
||||||
|
*/
|
||||||
|
public class LoginCredentialProcessor implements ObjectProcessor<LoginCredentials, User> {
|
||||||
|
|
||||||
|
// TODO: Acquire user IDs from database
|
||||||
|
private static long currentUserId = 1;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Class<LoginCredentials> getInputClass() { return LoginCredentials.class; }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public User process(LoginCredentials input) {
|
||||||
|
System.out.println("Received login credentials " + input);
|
||||||
|
return new User(currentUserId++, input.getName());
|
||||||
|
}
|
||||||
|
}
|
22
src/main/java/envoy/server/MessageProcessor.java
Normal file
22
src/main/java/envoy/server/MessageProcessor.java
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
package envoy.server;
|
||||||
|
|
||||||
|
import envoy.data.Message;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This {@link ObjectProcessor} handles incoming {@link Message}s.<br>
|
||||||
|
* <br>
|
||||||
|
* 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 Envoy Server Standalone v0.1-alpha
|
||||||
|
*/
|
||||||
|
public class MessageProcessor implements ObjectProcessor<Message, Void> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Class<Message> getInputClass() { return Message.class; }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Void process(Message input) { return null; }
|
||||||
|
}
|
@ -1,14 +1,19 @@
|
|||||||
package envoy.server;
|
package envoy.server;
|
||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
|
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.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;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Handles incoming objects.<br>
|
||||||
|
* <br>
|
||||||
* 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>
|
||||||
* Created: <strong>28.12.2019</strong><br>
|
* Created: <strong>28.12.2019</strong><br>
|
||||||
@ -18,12 +23,43 @@ import com.jenkov.nioserver.WriteProxy;
|
|||||||
*/
|
*/
|
||||||
public class ObjectMessageProcessor implements IMessageProcessor {
|
public class ObjectMessageProcessor implements IMessageProcessor {
|
||||||
|
|
||||||
|
private final Set<ObjectProcessor<?, ?>> processors;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The constructor to set the {@link ObjectProcessor}s.
|
||||||
|
*
|
||||||
|
* @param processors the {@link ObjectProcessor} to set
|
||||||
|
* @since Envoy Server Standalone v0.1-alpha
|
||||||
|
*/
|
||||||
|
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();
|
||||||
// TODO: Process pipeline
|
|
||||||
System.out.println("Read object: " + obj.toString());
|
System.out.println("Read object: " + obj.toString());
|
||||||
|
|
||||||
|
// 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();
|
||||||
}
|
}
|
||||||
|
@ -11,6 +11,8 @@ import com.jenkov.nioserver.MessageBuffer;
|
|||||||
import com.jenkov.nioserver.Socket;
|
import com.jenkov.nioserver.Socket;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* This {@link IMessageReader} decodes serialized Java objects.<br>
|
||||||
|
* <br>
|
||||||
* Project: <strong>envoy-server-standalone</strong><br>
|
* Project: <strong>envoy-server-standalone</strong><br>
|
||||||
* File: <strong>ObjectMessageReader.java</strong><br>
|
* File: <strong>ObjectMessageReader.java</strong><br>
|
||||||
* Created: <strong>28.12.2019</strong><br>
|
* Created: <strong>28.12.2019</strong><br>
|
||||||
@ -24,6 +26,14 @@ public class ObjectMessageReader implements IMessageReader {
|
|||||||
private Message nextMessage;
|
private Message nextMessage;
|
||||||
private MessageBuffer messageBuffer;
|
private MessageBuffer messageBuffer;
|
||||||
|
|
||||||
|
private int fromByteArray(byte[] bytes, int offset) {
|
||||||
|
return ((bytes[offset] & 0xFF) << 24) | ((bytes[offset + 1] & 0xFF) << 16) | ((bytes[offset + 2] & 0xFF) << 8)
|
||||||
|
| ((bytes[offset + 3] & 0xFF) << 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Message> getMessages() { return completeMessages; }
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void init(MessageBuffer messageBuffer) {
|
public void init(MessageBuffer messageBuffer) {
|
||||||
this.messageBuffer = messageBuffer;
|
this.messageBuffer = messageBuffer;
|
||||||
@ -55,12 +65,4 @@ public class ObjectMessageReader implements IMessageReader {
|
|||||||
|
|
||||||
buffer.clear();
|
buffer.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
private int fromByteArray(byte[] bytes, int offset) {
|
|
||||||
return ((bytes[offset] & 0xFF) << 24) | ((bytes[offset + 1] & 0xFF) << 16) | ((bytes[offset + 2] & 0xFF) << 8)
|
|
||||||
| ((bytes[offset + 3] & 0xFF) << 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public List<Message> getMessages() { return completeMessages; }
|
|
||||||
}
|
}
|
30
src/main/java/envoy/server/ObjectProcessor.java
Normal file
30
src/main/java/envoy/server/ObjectProcessor.java
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
package envoy.server;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This interface defines methods for processing objects of a specific
|
||||||
|
* type incoming from a client.<br>
|
||||||
|
* <br>
|
||||||
|
* 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
|
||||||
|
* @param <T> type of the request object
|
||||||
|
* @param <U> type of the response object
|
||||||
|
* @since Envoy Server Standalone v0.1-alpha
|
||||||
|
*/
|
||||||
|
public interface ObjectProcessor<T, U> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the Class of the request object
|
||||||
|
* @since Envoy Server Standalone v0.1-alpha
|
||||||
|
*/
|
||||||
|
Class<T> getInputClass();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param input the request object
|
||||||
|
* @return the response object
|
||||||
|
* @since Envoy Server Standalone v0.1-alpha
|
||||||
|
*/
|
||||||
|
U process(T input);
|
||||||
|
}
|
@ -1,10 +1,14 @@
|
|||||||
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;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Starts the server.<br>
|
||||||
|
* <br>
|
||||||
* Project: <strong>envoy-server-standalone</strong><br>
|
* Project: <strong>envoy-server-standalone</strong><br>
|
||||||
* File: <strong>Startup.java</strong><br>
|
* File: <strong>Startup.java</strong><br>
|
||||||
* Created: <strong>24.12.2019</strong><br>
|
* Created: <strong>24.12.2019</strong><br>
|
||||||
@ -14,8 +18,18 @@ import com.jenkov.nioserver.Server;
|
|||||||
*/
|
*/
|
||||||
public class Startup {
|
public class Startup {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Starts the server.
|
||||||
|
*
|
||||||
|
* @param args the run configuration. Currently unused.
|
||||||
|
* @throws IOException if the server crashes
|
||||||
|
* @since Envoy Server Standalone v0.1-alpha
|
||||||
|
*/
|
||||||
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();
|
||||||
}
|
}
|
||||||
}
|
}
|
Reference in New Issue
Block a user