use of com.google.protobuf.Message in project jvm-serializers by eishay.
the class ProtoServerHandler method handle.
void handle(final OutputStream os, final InputStream is) throws IOException {
RpcCallback<Message> done = new RpcCallback<Message>() {
DataOutputStream dos = new DataOutputStream(os);
public void run(Message content) {
try {
byte[] array = _serializer.serialize((MediaContent) content);
dos.writeInt(array.length);
dos.write(array);
dos.flush();
} catch (Exception e) {
e.printStackTrace();
}
}
};
DataInputStream dis = new DataInputStream(is);
int index = dis.readInt();
MethodDescriptor method = getDescriptor().getMethods().get(index);
byte[] array = new byte[dis.readInt()];
dis.readFully(array);
Message request = getRequestPrototype(method).newBuilderForType().mergeFrom(array).build();
callMethod(method, null, request, done);
}
use of com.google.protobuf.Message in project hive by apache.
the class TestLlapDaemonProtocolClientProxy method testMultipleNodes.
@Test(timeout = 5000)
public void testMultipleNodes() {
RequestManagerForTest requestManager = new RequestManagerForTest(1);
LlapNodeId nodeId1 = LlapNodeId.getInstance("host1", 1025);
LlapNodeId nodeId2 = LlapNodeId.getInstance("host2", 1025);
Message mockMessage = mock(Message.class);
LlapProtocolClientProxy.ExecuteRequestCallback mockExecuteRequestCallback = mock(LlapProtocolClientProxy.ExecuteRequestCallback.class);
// Request two messages
requestManager.queueRequest(new CallableRequestForTest(nodeId1, mockMessage, mockExecuteRequestCallback));
requestManager.queueRequest(new CallableRequestForTest(nodeId2, mockMessage, mockExecuteRequestCallback));
// Should go through in a single process call
requestManager.process();
assertEquals(2, requestManager.numSubmissionsCounters);
assertNotNull(requestManager.numInvocationsPerNode.get(nodeId1));
assertNotNull(requestManager.numInvocationsPerNode.get(nodeId2));
Assert.assertEquals(1, requestManager.numInvocationsPerNode.get(nodeId1).getValue().intValue());
Assert.assertEquals(1, requestManager.numInvocationsPerNode.get(nodeId2).getValue().intValue());
assertEquals(0, requestManager.currentLoopSkippedRequests.size());
assertEquals(0, requestManager.currentLoopSkippedRequests.size());
assertEquals(0, requestManager.currentLoopDisabledNodes.size());
}
use of com.google.protobuf.Message in project hive by apache.
the class TestLlapDaemonProtocolClientProxy method testSingleInvocationPerNode.
@Test(timeout = 5000)
public void testSingleInvocationPerNode() {
RequestManagerForTest requestManager = new RequestManagerForTest(1);
LlapNodeId nodeId1 = LlapNodeId.getInstance("host1", 1025);
Message mockMessage = mock(Message.class);
LlapProtocolClientProxy.ExecuteRequestCallback mockExecuteRequestCallback = mock(LlapProtocolClientProxy.ExecuteRequestCallback.class);
// First request for host.
requestManager.queueRequest(new CallableRequestForTest(nodeId1, mockMessage, mockExecuteRequestCallback));
requestManager.process();
assertEquals(1, requestManager.numSubmissionsCounters);
assertNotNull(requestManager.numInvocationsPerNode.get(nodeId1));
Assert.assertEquals(1, requestManager.numInvocationsPerNode.get(nodeId1).getValue().intValue());
assertEquals(0, requestManager.currentLoopSkippedRequests.size());
// Second request for host. Single invocation since the last has not completed.
requestManager.queueRequest(new CallableRequestForTest(nodeId1, mockMessage, mockExecuteRequestCallback));
requestManager.process();
assertEquals(1, requestManager.numSubmissionsCounters);
assertNotNull(requestManager.numInvocationsPerNode.get(nodeId1));
Assert.assertEquals(1, requestManager.numInvocationsPerNode.get(nodeId1).getValue().intValue());
assertEquals(1, requestManager.currentLoopSkippedRequests.size());
assertEquals(1, requestManager.currentLoopDisabledNodes.size());
assertTrue(requestManager.currentLoopDisabledNodes.contains(nodeId1));
// Complete first request. Second pending request should go through.
requestManager.requestFinished(nodeId1);
requestManager.process();
assertEquals(2, requestManager.numSubmissionsCounters);
assertNotNull(requestManager.numInvocationsPerNode.get(nodeId1));
Assert.assertEquals(2, requestManager.numInvocationsPerNode.get(nodeId1).getValue().intValue());
assertEquals(0, requestManager.currentLoopSkippedRequests.size());
assertEquals(0, requestManager.currentLoopDisabledNodes.size());
assertFalse(requestManager.currentLoopDisabledNodes.contains(nodeId1));
}
use of com.google.protobuf.Message in project hbase by apache.
the class HbaseObjectWritableFor96Migration method tryInstantiateProtobuf.
/**
* Try to instantiate a protocol buffer of the given message class
* from the given input stream.
*
* @param protoClass the class of the generated protocol buffer
* @param dataIn the input stream to read from
* @return the instantiated Message instance
* @throws IOException if an IO problem occurs
*/
static Message tryInstantiateProtobuf(Class<?> protoClass, DataInput dataIn) throws IOException {
try {
if (dataIn instanceof InputStream) {
// We can use the built-in parseDelimitedFrom and not have to re-copy
// the data
Method parseMethod = getStaticProtobufMethod(protoClass, "parseDelimitedFrom", InputStream.class);
return (Message) parseMethod.invoke(null, (InputStream) dataIn);
} else {
// Have to read it into a buffer first, since protobuf doesn't deal
// with the DataInput interface directly.
// Read the size delimiter that writeDelimitedTo writes
int size = ProtoUtil.readRawVarint32(dataIn);
if (size < 0) {
throw new IOException("Invalid size: " + size);
}
byte[] data = new byte[size];
dataIn.readFully(data);
Method parseMethod = getStaticProtobufMethod(protoClass, "parseFrom", byte[].class);
return (Message) parseMethod.invoke(null, data);
}
} catch (InvocationTargetException e) {
if (e.getCause() instanceof IOException) {
throw (IOException) e.getCause();
} else {
throw new IOException(e.getCause());
}
} catch (IllegalAccessException iae) {
throw new AssertionError("Could not access parse method in " + protoClass);
}
}
use of com.google.protobuf.Message in project hbase by apache.
the class HbaseObjectWritableFor96Migration method writeObject.
/**
* Write a {@link Writable}, {@link String}, primitive type, or an array of
* the preceding.
* @param out
* @param instance
* @param declaredClass
* @param conf
* @throws IOException
*/
@SuppressWarnings("unchecked")
static void writeObject(DataOutput out, Object instance, Class declaredClass, Configuration conf) throws IOException {
Object instanceObj = instance;
Class declClass = declaredClass;
if (instanceObj == null) {
// null
instanceObj = new NullInstance(declClass, conf);
declClass = Writable.class;
}
writeClassCode(out, declClass);
if (declClass.isArray()) {
// byte-at-a-time we were previously doing.
if (declClass.equals(byte[].class)) {
Bytes.writeByteArray(out, (byte[]) instanceObj);
} else {
//if it is a Generic array, write the element's type
if (getClassCode(declaredClass) == GENERIC_ARRAY_CODE) {
Class<?> componentType = declaredClass.getComponentType();
writeClass(out, componentType);
}
int length = Array.getLength(instanceObj);
out.writeInt(length);
for (int i = 0; i < length; i++) {
Object item = Array.get(instanceObj, i);
writeObject(out, item, item.getClass(), conf);
}
}
} else if (List.class.isAssignableFrom(declClass)) {
List list = (List) instanceObj;
int length = list.size();
out.writeInt(length);
for (int i = 0; i < length; i++) {
Object elem = list.get(i);
writeObject(out, elem, elem == null ? Writable.class : elem.getClass(), conf);
}
} else if (declClass == String.class) {
// String
Text.writeString(out, (String) instanceObj);
} else if (declClass.isPrimitive()) {
// primitive type
if (declClass == Boolean.TYPE) {
// boolean
out.writeBoolean(((Boolean) instanceObj).booleanValue());
} else if (declClass == Character.TYPE) {
// char
out.writeChar(((Character) instanceObj).charValue());
} else if (declClass == Byte.TYPE) {
// byte
out.writeByte(((Byte) instanceObj).byteValue());
} else if (declClass == Short.TYPE) {
// short
out.writeShort(((Short) instanceObj).shortValue());
} else if (declClass == Integer.TYPE) {
// int
out.writeInt(((Integer) instanceObj).intValue());
} else if (declClass == Long.TYPE) {
// long
out.writeLong(((Long) instanceObj).longValue());
} else if (declClass == Float.TYPE) {
// float
out.writeFloat(((Float) instanceObj).floatValue());
} else if (declClass == Double.TYPE) {
// double
out.writeDouble(((Double) instanceObj).doubleValue());
} else if (declClass == Void.TYPE) {
// void
} else {
throw new IllegalArgumentException("Not a primitive: " + declClass);
}
} else if (declClass.isEnum()) {
// enum
Text.writeString(out, ((Enum) instanceObj).name());
} else if (Message.class.isAssignableFrom(declaredClass)) {
Text.writeString(out, instanceObj.getClass().getName());
((Message) instance).writeDelimitedTo(DataOutputOutputStream.constructOutputStream(out));
} else if (Writable.class.isAssignableFrom(declClass)) {
// Writable
Class<?> c = instanceObj.getClass();
Integer code = CLASS_TO_CODE.get(c);
if (code == null) {
out.writeByte(NOT_ENCODED);
Text.writeString(out, c.getName());
} else {
writeClassCode(out, c);
}
((Writable) instanceObj).write(out);
} else if (Serializable.class.isAssignableFrom(declClass)) {
Class<?> c = instanceObj.getClass();
Integer code = CLASS_TO_CODE.get(c);
if (code == null) {
out.writeByte(NOT_ENCODED);
Text.writeString(out, c.getName());
} else {
writeClassCode(out, c);
}
ByteArrayOutputStream bos = null;
ObjectOutputStream oos = null;
try {
bos = new ByteArrayOutputStream();
oos = new ObjectOutputStream(bos);
oos.writeObject(instanceObj);
byte[] value = bos.toByteArray();
out.writeInt(value.length);
out.write(value);
} finally {
if (bos != null)
bos.close();
if (oos != null)
oos.close();
}
} else if (Scan.class.isAssignableFrom(declClass)) {
Scan scan = (Scan) instanceObj;
byte[] scanBytes = ProtobufUtil.toScan(scan).toByteArray();
out.writeInt(scanBytes.length);
out.write(scanBytes);
} else {
throw new IOException("Can't write: " + instanceObj + " as " + declClass);
}
}
Aggregations