Updated Javadoc
This commit is contained in:
parent
070118f612
commit
e265ef0b8f
@ -4,24 +4,26 @@ import envoy.data.LoginCredentials;
|
|||||||
import envoy.data.User;
|
import envoy.data.User;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* This {@link ObjectProcessor} handles {@link LoginCredentials}.<br>
|
||||||
|
* <br>
|
||||||
* Project: <strong>envoy-server-standalone</strong><br>
|
* Project: <strong>envoy-server-standalone</strong><br>
|
||||||
* File: <strong>LoginCredentialProcessor.java</strong><br>
|
* File: <strong>LoginCredentialProcessor.java</strong><br>
|
||||||
* Created: <strong>30.12.2019</strong><br>
|
* Created: <strong>30.12.2019</strong><br>
|
||||||
*
|
*
|
||||||
* @author Kai S. K. Engelbart
|
* @author Kai S. K. Engelbart
|
||||||
* @since
|
* @since Envoy Server Standalone v0.1-alpha
|
||||||
*/
|
*/
|
||||||
public class LoginCredentialProcessor implements ObjectProcessor<LoginCredentials, User> {
|
public class LoginCredentialProcessor implements ObjectProcessor<LoginCredentials, User> {
|
||||||
|
|
||||||
// TODO: Acquire user IDs from database
|
// TODO: Acquire user IDs from database
|
||||||
private static long currentUserId = 1;
|
private static long currentUserId = 1;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Class<LoginCredentials> getInputClass() { return LoginCredentials.class; }
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public User process(LoginCredentials input) {
|
public User process(LoginCredentials input) {
|
||||||
System.out.println("Received login credentials " + input);
|
System.out.println("Received login credentials " + input);
|
||||||
return new User(currentUserId++, input.getName());
|
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;
|
import envoy.data.Message;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* This {@link ObjectProcessor} handles incoming {@link Message}s.<br>
|
||||||
|
* <br>
|
||||||
* Project: <strong>envoy-server-standalone</strong><br>
|
* Project: <strong>envoy-server-standalone</strong><br>
|
||||||
* File: <strong>MessageProcessor.java</strong><br>
|
* File: <strong>MessageProcessor.java</strong><br>
|
||||||
* Created: <strong>30.12.2019</strong><br>
|
* Created: <strong>30.12.2019</strong><br>
|
||||||
*
|
*
|
||||||
* @author Kai S. K. Engelbart
|
* @author Kai S. K. Engelbart
|
||||||
* @since
|
* @since Envoy Server Standalone v0.1-alpha
|
||||||
*/
|
*/
|
||||||
public class MessageProcessor implements ObjectProcessor<Message, Void> {
|
public class MessageProcessor implements ObjectProcessor<Message, Void> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Void process(Message input) { return null; }
|
public Class<Message> getInputClass() { return Message.class; }
|
||||||
|
|
||||||
@Override
|
@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;
|
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>
|
||||||
@ -23,6 +25,12 @@ public class ObjectMessageProcessor implements IMessageProcessor {
|
|||||||
|
|
||||||
private final Set<ObjectProcessor<?, ?>> processors;
|
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; }
|
public ObjectMessageProcessor(Set<ObjectProcessor<?, ?>> processors) { this.processors = processors; }
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
@ -33,27 +41,25 @@ public class ObjectMessageProcessor implements IMessageProcessor {
|
|||||||
System.out.println("Read object: " + obj.toString());
|
System.out.println("Read object: " + obj.toString());
|
||||||
|
|
||||||
// Process object
|
// Process object
|
||||||
processors.stream()
|
processors.stream().filter(p -> p.getInputClass().isInstance(obj)).forEach((@SuppressWarnings("rawtypes") ObjectProcessor p) -> {
|
||||||
.filter(p -> p.getInputClass().isInstance(obj))
|
Object responseObj = p.process(p.getInputClass().cast(obj));
|
||||||
.forEach((@SuppressWarnings("rawtypes") ObjectProcessor p) -> {
|
if (responseObj != null) {
|
||||||
Object responseObj = p.process(p.getInputClass().cast(obj));
|
// Create message targeted at the client
|
||||||
if (responseObj != null) {
|
Message response = writeProxy.getMessage();
|
||||||
// Create message targeted at the client
|
response.socketId = message.socketId;
|
||||||
Message response = writeProxy.getMessage();
|
|
||||||
response.socketId = message.socketId;
|
|
||||||
|
|
||||||
// Serialize object to byte array
|
// Serialize object to byte array
|
||||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||||
try (ObjectOutputStream oout = new ObjectOutputStream(baos)) {
|
try (ObjectOutputStream oout = new ObjectOutputStream(baos)) {
|
||||||
oout.writeObject(responseObj);
|
oout.writeObject(responseObj);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
|
||||||
byte[] objBytes = baos.toByteArray();
|
|
||||||
response.writeToMessage(objBytes);
|
|
||||||
writeProxy.enqueue(response);
|
|
||||||
}
|
}
|
||||||
});
|
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; }
|
|
||||||
}
|
}
|
@ -1,17 +1,30 @@
|
|||||||
package envoy.server;
|
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>
|
* Project: <strong>envoy-server-standalone</strong><br>
|
||||||
* File: <strong>ObjectProcessor.java</strong><br>
|
* File: <strong>ObjectProcessor.java</strong><br>
|
||||||
* Created: <strong>30.12.2019</strong><br>
|
* Created: <strong>30.12.2019</strong><br>
|
||||||
*
|
*
|
||||||
* @author Kai S. K. Engelbart
|
* @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
|
* @since Envoy Server Standalone v0.1-alpha
|
||||||
*/
|
*/
|
||||||
public interface ObjectProcessor<T, U> {
|
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();
|
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;
|
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>
|
||||||
@ -16,6 +18,13 @@ 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 {
|
||||||
Set<ObjectProcessor<?, ?>> processors = new HashSet<>();
|
Set<ObjectProcessor<?, ?>> processors = new HashSet<>();
|
||||||
processors.add(new LoginCredentialProcessor());
|
processors.add(new LoginCredentialProcessor());
|
||||||
|
Reference in New Issue
Block a user