2020-01-06 13:58:28 +01:00
|
|
|
package envoy.server.net;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
2020-07-06 11:40:13 +02:00
|
|
|
import java.util.Set;
|
2020-06-11 12:06:06 +02:00
|
|
|
import java.util.logging.Logger;
|
2020-07-06 11:40:13 +02:00
|
|
|
import java.util.stream.Stream;
|
2020-01-06 13:58:28 +01:00
|
|
|
|
2020-09-25 14:29:23 +02:00
|
|
|
import com.jenkov.nioserver.*;
|
2020-01-06 13:58:28 +01:00
|
|
|
|
2020-09-25 14:29:23 +02:00
|
|
|
import envoy.util.*;
|
2020-01-06 13:58:28 +01:00
|
|
|
|
2020-10-19 18:17:51 +02:00
|
|
|
import envoy.server.data.Contact;
|
|
|
|
|
2020-01-06 13:58:28 +01:00
|
|
|
/**
|
2020-09-25 14:29:23 +02:00
|
|
|
* This class defines methods to send an object to a client.
|
2020-01-06 17:40:19 +01:00
|
|
|
*
|
2020-01-06 13:58:28 +01:00
|
|
|
* @author Kai S. K. Engelbart
|
|
|
|
* @since Envoy Server Standalone v0.1-alpha
|
|
|
|
*/
|
2020-08-22 13:37:07 +02:00
|
|
|
public final class ObjectWriteProxy {
|
2020-01-06 13:58:28 +01:00
|
|
|
|
|
|
|
private final WriteProxy writeProxy;
|
|
|
|
|
2020-07-06 11:40:13 +02:00
|
|
|
private static final ConnectionManager connectionManager = ConnectionManager.getInstance();
|
2020-10-19 18:17:51 +02:00
|
|
|
private static final Logger logger =
|
|
|
|
EnvoyLog.getLogger(ObjectWriteProxy.class);
|
2020-06-11 12:06:06 +02:00
|
|
|
|
2020-01-11 18:28:48 +01:00
|
|
|
/**
|
2020-02-01 23:37:44 +01:00
|
|
|
* Creates an instance of {@link ObjectWriteProxy}.
|
2020-01-11 18:28:48 +01:00
|
|
|
*
|
|
|
|
* @param writeProxy the {@link WriteProxy} to write objects to another client
|
|
|
|
* @since Envoy Server Standalone v0.1-alpha
|
|
|
|
*/
|
2020-10-19 18:17:51 +02:00
|
|
|
public ObjectWriteProxy(WriteProxy writeProxy) {
|
|
|
|
this.writeProxy = writeProxy;
|
|
|
|
}
|
2020-01-06 13:58:28 +01:00
|
|
|
|
2020-01-11 18:28:48 +01:00
|
|
|
/**
|
2020-03-26 20:14:09 +01:00
|
|
|
* @param recipientSocketID the socket id of the recipient
|
2020-01-11 18:28:48 +01:00
|
|
|
* @param obj the object to return to the client
|
2020-10-19 18:17:51 +02:00
|
|
|
* @throws RuntimeException if the serialization of the object failed (this is highly unlikely)
|
2020-01-11 18:28:48 +01:00
|
|
|
* @since Envoy Server Standalone v0.1-alpha
|
|
|
|
*/
|
2020-07-06 11:40:13 +02:00
|
|
|
public void write(long recipientSocketID, Object obj) {
|
2020-01-06 13:58:28 +01:00
|
|
|
// Create message targeted at the client
|
2020-06-09 18:18:13 +02:00
|
|
|
final Message response = writeProxy.getMessage();
|
2020-03-26 20:14:09 +01:00
|
|
|
response.socketId = recipientSocketID;
|
2020-06-11 12:06:06 +02:00
|
|
|
logger.fine("Sending " + obj);
|
2020-06-09 18:18:13 +02:00
|
|
|
|
2020-07-06 11:40:13 +02:00
|
|
|
try {
|
2020-01-06 17:40:19 +01:00
|
|
|
|
2020-07-06 11:40:13 +02:00
|
|
|
// Serialize object to byte array
|
|
|
|
final byte[] objBytes = SerializationUtils.writeToByteArray(obj);
|
2020-01-06 17:40:19 +01:00
|
|
|
|
2020-07-06 11:40:13 +02:00
|
|
|
// Acquire object length in bytes
|
|
|
|
final byte[] objLen = SerializationUtils.intToBytes(objBytes.length);
|
|
|
|
|
|
|
|
response.writeToMessage(objLen);
|
|
|
|
response.writeToMessage(objBytes);
|
|
|
|
writeProxy.enqueue(response);
|
|
|
|
} catch (IOException e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-09-27 12:06:01 +02:00
|
|
|
* Sends an object to all contacts in a set that are online.
|
2020-09-25 14:29:23 +02:00
|
|
|
*
|
2020-07-06 11:40:13 +02:00
|
|
|
* @param contacts the contacts to send the object to
|
|
|
|
* @param message the object to send
|
|
|
|
* @since Envoy Server Standalone v0.1-beta
|
|
|
|
*/
|
2020-10-19 18:17:51 +02:00
|
|
|
public void writeToOnlineContacts(Set<? extends Contact> contacts, Object message) {
|
|
|
|
writeToOnlineContacts(contacts.stream(), message);
|
|
|
|
}
|
2020-07-06 11:40:13 +02:00
|
|
|
|
|
|
|
/**
|
2020-09-27 12:06:01 +02:00
|
|
|
* Sends an object to all contacts in a set that are online.
|
2020-09-25 14:29:23 +02:00
|
|
|
*
|
2020-07-06 11:40:13 +02:00
|
|
|
* @param contacts the contacts to send the object to
|
|
|
|
* @param message the object to send
|
|
|
|
* @since Envoy Server Standalone v0.1-beta
|
|
|
|
*/
|
|
|
|
public void writeToOnlineContacts(Stream<? extends Contact> contacts, Object message) {
|
2020-10-19 18:17:51 +02:00
|
|
|
contacts.map(Contact::getID).filter(connectionManager::isOnline)
|
|
|
|
.map(connectionManager::getSocketID).forEach(id -> write(id, message));
|
2020-01-06 13:58:28 +01:00
|
|
|
}
|
|
|
|
}
|