Updated Javadoc
This commit is contained in:
parent
070118f612
commit
e265ef0b8f
@ -4,24 +4,26 @@ 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
|
||||
* @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());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<LoginCredentials> getInputClass() { return LoginCredentials.class; }
|
||||
}
|
||||
|
@ -3,18 +3,20 @@ 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
|
||||
* @since Envoy Server Standalone v0.1-alpha
|
||||
*/
|
||||
public class MessageProcessor implements ObjectProcessor<Message, Void> {
|
||||
|
||||
@Override
|
||||
public Void process(Message input) { return null; }
|
||||
public Class<Message> getInputClass() { return Message.class; }
|
||||
|
||||
@Override
|
||||
public Class<Message> getInputClass() { return Message.class; }
|
||||
public Void process(Message input) { return null; }
|
||||
}
|
||||
|
@ -12,6 +12,8 @@ import com.jenkov.nioserver.Message;
|
||||
import com.jenkov.nioserver.WriteProxy;
|
||||
|
||||
/**
|
||||
* Handles incoming objects.<br>
|
||||
* <br>
|
||||
* Project: <strong>envoy-server-standalone</strong><br>
|
||||
* File: <strong>ObjectMessageProcessor.java</strong><br>
|
||||
* Created: <strong>28.12.2019</strong><br>
|
||||
@ -23,6 +25,12 @@ 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")
|
||||
@ -33,27 +41,25 @@ public class ObjectMessageProcessor implements IMessageProcessor {
|
||||
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;
|
||||
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);
|
||||
// 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) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
@ -11,6 +11,8 @@ import com.jenkov.nioserver.MessageBuffer;
|
||||
import com.jenkov.nioserver.Socket;
|
||||
|
||||
/**
|
||||
* This {@link IMessageReader} decodes serialized Java objects.<br>
|
||||
* <br>
|
||||
* Project: <strong>envoy-server-standalone</strong><br>
|
||||
* File: <strong>ObjectMessageReader.java</strong><br>
|
||||
* Created: <strong>28.12.2019</strong><br>
|
||||
@ -24,6 +26,14 @@ public class ObjectMessageReader implements IMessageReader {
|
||||
private Message nextMessage;
|
||||
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
|
||||
public void init(MessageBuffer messageBuffer) {
|
||||
this.messageBuffer = messageBuffer;
|
||||
@ -55,12 +65,4 @@ public class ObjectMessageReader implements IMessageReader {
|
||||
|
||||
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; }
|
||||
}
|
@ -1,17 +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> {
|
||||
|
||||
U process(T input);
|
||||
|
||||
/**
|
||||
* @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);
|
||||
}
|
@ -7,6 +7,8 @@ import java.util.Set;
|
||||
import com.jenkov.nioserver.Server;
|
||||
|
||||
/**
|
||||
* Starts the server.<br>
|
||||
* <br>
|
||||
* Project: <strong>envoy-server-standalone</strong><br>
|
||||
* File: <strong>Startup.java</strong><br>
|
||||
* Created: <strong>24.12.2019</strong><br>
|
||||
@ -16,6 +18,13 @@ import com.jenkov.nioserver.Server;
|
||||
*/
|
||||
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 {
|
||||
Set<ObjectProcessor<?, ?>> processors = new HashSet<>();
|
||||
processors.add(new LoginCredentialProcessor());
|
||||
|
Reference in New Issue
Block a user