2020-01-06 13:58:28 +01:00
|
|
|
package envoy.server.net;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
|
|
import com.jenkov.nioserver.Message;
|
|
|
|
import com.jenkov.nioserver.WriteProxy;
|
|
|
|
|
|
|
|
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-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
|
|
|
|
* @throws IOException if the serialization of the object failed
|
|
|
|
* @since Envoy Server Standalone v0.1-alpha
|
|
|
|
*/
|
2020-03-26 20:14:09 +01:00
|
|
|
public void write(long recipientSocketID, Object obj) throws IOException {
|
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-01-06 13:58:28 +01:00
|
|
|
|
2020-06-09 18:18:13 +02:00
|
|
|
System.out.println("Sending object " + obj);
|
|
|
|
|
2020-01-06 13:58:28 +01:00
|
|
|
// Serialize object to byte array
|
2020-06-09 18:18:13 +02:00
|
|
|
final byte[] objBytes = SerializationUtils.writeToByteArray(obj);
|
2020-01-06 17:40:19 +01:00
|
|
|
|
|
|
|
// Acquire object length in bytes
|
2020-06-09 18:18:13 +02:00
|
|
|
final byte[] objLen = SerializationUtils.intToBytes(objBytes.length);
|
2020-01-06 17:40:19 +01:00
|
|
|
|
|
|
|
response.writeToMessage(objLen);
|
2020-01-06 13:58:28 +01:00
|
|
|
response.writeToMessage(objBytes);
|
|
|
|
writeProxy.enqueue(response);
|
|
|
|
}
|
|
|
|
}
|