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
|
|
|
|
|
|
|
import com.jenkov.nioserver.Message;
|
|
|
|
import com.jenkov.nioserver.WriteProxy;
|
|
|
|
|
2020-07-06 11:40:13 +02:00
|
|
|
import envoy.server.data.Contact;
|
2020-06-11 12:06:06 +02:00
|
|
|
import envoy.util.EnvoyLog;
|
2020-01-06 13:58:28 +01:00
|
|
|
import envoy.util.SerializationUtils;
|
|
|
|
|
|
|
|
/**
|
2020-01-11 18:28:48 +01:00
|
|
|
* This class defines methods to send an object to a client.<br>
|
|
|
|
* <br>
|
2020-01-06 13:58:28 +01:00
|
|
|
* Project: <strong>envoy-server-standalone</strong><br>
|
|
|
|
* File: <strong>ObjectWriteProxy.java</strong><br>
|
|
|
|
* Created: <strong>04.01.2020</strong><br>
|
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
|
|
|
|
*/
|
|
|
|
public class ObjectWriteProxy {
|
|
|
|
|
|
|
|
private final WriteProxy writeProxy;
|
|
|
|
|
2020-07-06 11:40:13 +02:00
|
|
|
private static final ConnectionManager connectionManager = ConnectionManager.getInstance();
|
|
|
|
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-01-06 13:58:28 +01:00
|
|
|
public ObjectWriteProxy(WriteProxy writeProxy) { this.writeProxy = writeProxy; }
|
|
|
|
|
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-07-06 11:40:13 +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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends an object to all contact in a set that are online.
|
|
|
|
*
|
|
|
|
* @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(Set<? extends Contact> contacts, Object message) { writeToOnlineContacts(contacts.stream(), message); }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends an object to all contact in a set that are online.
|
|
|
|
*
|
|
|
|
* @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) {
|
|
|
|
contacts.map(Contact::getID).filter(connectionManager::isOnline).map(connectionManager::getSocketID).forEach(id -> write(id, message));
|
2020-01-06 13:58:28 +01:00
|
|
|
}
|
|
|
|
}
|