use of com.cinchapi.concourse.server.plugin.RemoteMessage in project concourse by cinchapi.
the class PluginSerializerTest method testSerializeRemoteMessage.
@Test
public void testSerializeRemoteMessage() {
RemoteMessage expected = randomRemoteMessage();
ByteBuffer buffer = serializer.serialize(expected);
RemoteMessage actual = serializer.deserialize(buffer);
Assert.assertEquals(expected, actual);
}
use of com.cinchapi.concourse.server.plugin.RemoteMessage in project concourse by cinchapi.
the class PluginSerializerTest method randomRemoteMessage.
/**
* Generate a random {@link RemoteMessage} for testing.
*
* @return a RemoteMessage
*/
private RemoteMessage randomRemoteMessage() {
int seed = Random.getInt();
RemoteMessage message;
if (seed % 3 == 0) {
message = Reflection.newInstance("com.cinchapi.concourse.server.plugin.RemoteAttributeExchange", Random.getString(), Random.getString());
} else if (seed % 2 == 0) {
AccessToken creds = new AccessToken(ByteBuffer.wrap(Random.getString().getBytes(StandardCharsets.UTF_8)));
TransactionToken transaction = new TransactionToken(creds, Time.now());
String method = Random.getSimpleString();
String environment = Random.getSimpleString();
int argsCount = Math.abs(Random.getInt()) % 8;
ComplexTObject[] args = new ComplexTObject[argsCount];
for (int i = 0; i < argsCount; ++i) {
args[i] = ComplexTObject.fromJavaObject(Random.getObject());
}
message = Reflection.newInstance("com.cinchapi.concourse.server.plugin.RemoteMethodRequest", method, creds, transaction, environment, args);
} else {
AccessToken creds = new AccessToken(ByteBuffer.wrap(Random.getString().getBytes(StandardCharsets.UTF_8)));
ComplexTObject response = ComplexTObject.fromJavaObject(Random.getObject());
message = Reflection.newInstance("com.cinchapi.concourse.server.plugin.RemoteMethodResponse", creds, response);
}
return message;
}
use of com.cinchapi.concourse.server.plugin.RemoteMessage in project concourse by cinchapi.
the class PluginSerializer method serialize.
/**
* Return a {@link ByteBuffer} that contains the serialized form of the
* input {@code object}.
*
* @param object the object to serialize
* @return the serialized form within a ByteBuffer
*/
public ByteBuffer serialize(Object object) {
ByteBuffer buffer = null;
if (object instanceof PluginSerializable) {
HeapBuffer buffer0 = HeapBuffer.allocate();
buffer0.writeByte(Scheme.PLUGIN_SERIALIZABLE.ordinal());
buffer0.writeUTF8(object.getClass().getName());
((PluginSerializable) object).serialize(buffer0);
byte[] bytes = new byte[(int) buffer0.position()];
buffer0.flip();
buffer0.read(bytes);
buffer = ByteBuffer.wrap(bytes);
return buffer;
} else if (object instanceof RemoteMessage) {
HeapBuffer buffer0 = (HeapBuffer) ((RemoteMessage) object).serialize();
buffer = ByteBuffer.allocate((int) buffer0.remaining() + 1);
buffer.put((byte) Scheme.REMOTE_MESSAGE.ordinal());
buffer.put(buffer0.array(), 0, (int) buffer0.remaining());
buffer.flip();
return buffer;
} else if (object instanceof ComplexTObject) {
byte[] bytes = ((ComplexTObject) object).toByteBuffer().array();
buffer = ByteBuffer.allocate(bytes.length + 1);
buffer.put((byte) Scheme.COMPLEX_TOBJECT.ordinal());
buffer.put(bytes);
buffer.flip();
return buffer;
} else if (object instanceof TObject) {
byte[] bytes = ((TObject) object).getData();
buffer = ByteBuffer.allocate(bytes.length + 2);
buffer.put((byte) Scheme.TOBJECT.ordinal());
buffer.put((byte) ((TObject) object).getType().ordinal());
buffer.put(bytes);
buffer.flip();
return buffer;
} else if (object instanceof String || object.getClass().isPrimitive() || object instanceof Number || object instanceof Boolean || object instanceof Map || object instanceof List || object instanceof Set) {
return serialize(ComplexTObject.fromJavaObject(object));
} else if (object instanceof Serializable) {
byte[] bytes = ByteBuffers.getByteArray(Serializables.getBytes((Serializable) object));
byte[] classBytes = object.getClass().getName().getBytes(StandardCharsets.UTF_8);
buffer = ByteBuffer.allocate(1 + 2 + classBytes.length + bytes.length);
buffer.put((byte) Scheme.JAVA_SERIALIZABLE.ordinal());
buffer.putShort((short) classBytes.length);
buffer.put(classBytes);
buffer.put(bytes);
buffer.flip();
return buffer;
} else {
throw new IllegalStateException("Cannot plugin serialize an object of type " + object.getClass());
}
}
Aggregations