下面列出了怎么用com.google.protobuf.Descriptors.MethodDescriptor的API类实例代码及写法,或者点击链接到github查看源代码。
private void processService(ServiceDescriptor service, FileDescriptor fd) {
String serviceName = service.getFullName();
checkState(
!fileDescriptorsBySymbol.containsKey(serviceName),
"Service already defined: %s",
serviceName);
fileDescriptorsBySymbol.put(serviceName, fd);
for (MethodDescriptor method : service.getMethods()) {
String methodName = method.getFullName();
checkState(
!fileDescriptorsBySymbol.containsKey(methodName),
"Method already defined: %s",
methodName);
fileDescriptorsBySymbol.put(methodName, fd);
}
}
public void testNewReflectiveBlockingService() throws ServiceException {
ServiceWithNoOuter.BlockingInterface impl =
control.createMock(ServiceWithNoOuter.BlockingInterface.class);
RpcController controller = control.createMock(RpcController.class);
BlockingService service =
ServiceWithNoOuter.newReflectiveBlockingService(impl);
MethodDescriptor fooMethod =
ServiceWithNoOuter.getDescriptor().findMethodByName("Foo");
MessageWithNoOuter request = MessageWithNoOuter.getDefaultInstance();
TestAllTypes expectedResponse = TestAllTypes.getDefaultInstance();
EasyMock.expect(impl.foo(EasyMock.same(controller), EasyMock.same(request)))
.andReturn(expectedResponse);
control.replay();
Message response =
service.callBlockingMethod(fooMethod, controller, request);
assertEquals(expectedResponse, response);
control.verify();
}
private void forwardToService(SocketRpcProtos.Request rpcRequest,
RpcCallback<Message> callback, Service service,
RpcController socketController) throws RpcException {
// Get matching method
MethodDescriptor method = getMethod(rpcRequest,
service.getDescriptorForType());
// Create request for method
Message request = getRequestProto(rpcRequest,
service.getRequestPrototype(method));
// Call method
try {
service.callMethod(method, socketController, request, callback);
} catch (RuntimeException e) {
throw new RpcException(ErrorReason.RPC_ERROR,
"Error running method " + method.getFullName(), e);
}
}
@Override
public Message callBlockingMethod(MethodDescriptor method,
RpcController controller, Message request, Message responsePrototype)
throws ServiceException {
// Must pass in a SocketRpcController
SocketRpcController socketController = (SocketRpcController) controller;
final Connection connection = createConnection(socketController);
try {
sendRpcRequest(method, socketController, request, connection);
Response rpcResponse = receiveRpcResponse(socketController, connection);
return handleRpcResponse(responsePrototype, rpcResponse,
socketController);
} finally {
close(connection);
}
}
private void sendRpcRequest(MethodDescriptor method,
SocketRpcController socketController, Message request,
Connection connection) throws ServiceException {
// Check request
if (!request.isInitialized()) {
handleError(socketController, ErrorReason.INVALID_REQUEST_PROTO,
"Request is uninitialized", null);
}
// Create RPC request protobuf
SocketRpcProtos.Request rpcRequest = SocketRpcProtos.Request.newBuilder()
.setRequestProto(request.toByteString())
.setServiceName(method.getService().getFullName())
.setMethodName(method.getName())
.build();
// Send request
try {
connection.sendProtoMessage(rpcRequest);
} catch (IOException e) {
handleError(socketController, ErrorReason.IO_ERROR, String.format(
"Error writing over connection %s", connection), e);
}
}
@Override
public Message callBlockingMethod(final MethodDescriptor method,
final RpcController controller,
final Message param,
final Message responsePrototype)
throws TajoServiceException {
int nextSeqId = sequence.getAndIncrement();
RpcProtos.RpcRequest rpcRequest = buildRequest(nextSeqId, method, param);
ProtoCallFuture callFuture = new ProtoCallFuture(controller, responsePrototype);
invoke(rpcRequest, callFuture, 0);
try {
return callFuture.get();
} catch (Throwable t) {
if (t instanceof ExecutionException) {
Throwable cause = t.getCause();
if (cause != null && cause instanceof TajoServiceException) {
throw (TajoServiceException) cause;
}
}
throw new TajoServiceException(t.getMessage());
}
}
public Message callBlockingMethod(final MethodDescriptor method,
final RpcController controller,
final Message param,
final Message responsePrototype)
throws ServiceException {
int nextSeqId = sequence.getAndIncrement();
Message rpcRequest = buildRequest(nextSeqId, method, param);
ProtoCallFuture callFuture =
new ProtoCallFuture(controller, responsePrototype);
requests.put(nextSeqId, callFuture);
getChannel().write(rpcRequest);
try {
return callFuture.get();
} catch (Throwable t) {
if(t instanceof ExecutionException) {
ExecutionException ee = (ExecutionException)t;
throw new ServiceException(ee.getCause());
} else {
throw new RemoteException(t);
}
}
}
public static MethodType fetchMethodType(MethodDescriptor methodDescriptor) {
boolean clientStreaming = methodDescriptor.toProto().getClientStreaming();
boolean serverStreaming = methodDescriptor.toProto().getServerStreaming();
if (clientStreaming && serverStreaming) {
return MethodType.BIDI_STREAMING;
} else if (!clientStreaming && !serverStreaming) {
return MethodType.UNARY;
} else if (!clientStreaming) {
return MethodType.SERVER_STREAMING;
} else {
return MethodType.SERVER_STREAMING;
}
}
public CallResults invokeMethod(GrpcMethodDefinition definition, Channel channel, CallOptions callOptions,
List<String> requestJsonTexts) {
FileDescriptorSet fileDescriptorSet = GrpcReflectionUtils.resolveService(channel, definition.getFullServiceName());
if (fileDescriptorSet == null) {
return null;
}
ServiceResolver serviceResolver = ServiceResolver.fromFileDescriptorSet(fileDescriptorSet);
MethodDescriptor methodDescriptor = serviceResolver.resolveServiceMethod(definition);
TypeRegistry registry = TypeRegistry.newBuilder().add(serviceResolver.listMessageTypes()).build();
List<DynamicMessage> requestMessages = GrpcReflectionUtils.parseToMessages(registry, methodDescriptor.getInputType(),
requestJsonTexts);
CallResults results = new CallResults();
StreamObserver<DynamicMessage> streamObserver = MessageWriter.newInstance(registry, results);
CallParams callParams = CallParams.builder()
.methodDescriptor(methodDescriptor)
.channel(channel)
.callOptions(callOptions)
.requests(requestMessages)
.responseObserver(streamObserver)
.build();
try {
grpcClientService.call(callParams).get();
} catch (InterruptedException | ExecutionException e) {
throw new RuntimeException("Caught exception while waiting for rpc", e);
}
return results;
}
private io.grpc.MethodDescriptor<DynamicMessage, DynamicMessage> createGrpcMethodDescriptor(MethodDescriptor descriptor) {
return io.grpc.MethodDescriptor.<DynamicMessage, DynamicMessage>newBuilder()
.setType(fetchMethodType(descriptor))
.setFullMethodName(fetchFullMethodName(descriptor))
.setRequestMarshaller(new DynamicMessageMarshaller(descriptor.getInputType()))
.setResponseMarshaller(new DynamicMessageMarshaller(descriptor.getOutputType()))
.build();
}
/**
* Returns the descriptor of a protobuf method with the supplied grpc method name. If the method
* cannot be found, this throws {@link IllegalArgumentException}.
*/
public MethodDescriptor resolveServiceMethod(GrpcMethodDefinition definition) {
ServiceDescriptor service = findService(definition.getPackageName(), definition.getServiceName());
MethodDescriptor method = service.findMethodByName(definition.getMethodName());
if (method == null) {
throw new IllegalArgumentException(
"Unable to find method " + definition.getMethodName()
+ " in service " + definition.getServiceName());
}
return method;
}
/**
* Returns the descriptor of a protobuf method with the supplied grpc method name. If the method
* cannot be found, this throws {@link IllegalArgumentException}.
*/
public MethodDescriptor resolveServiceMethod(ProtoMethodName method) {
return resolveServiceMethod(
method.getServiceName(),
method.getMethodName(),
method.getPackageName());
}
private MethodDescriptor resolveServiceMethod(
String serviceName, String methodName, String packageName) {
ServiceDescriptor service = findService(serviceName, packageName);
MethodDescriptor method = service.findMethodByName(methodName);
if (method == null) {
throw new IllegalArgumentException(
"Unable to find method " + methodName + " in service " + serviceName);
}
return method;
}
private io.grpc.MethodDescriptor<DynamicMessage, DynamicMessage> createGrpcMethodDescriptor() {
return io.grpc.MethodDescriptor.<DynamicMessage, DynamicMessage>create(
getMethodType(),
getFullMethodName(),
new DynamicMessageMarshaller(protoMethodDescriptor.getInputType()),
new DynamicMessageMarshaller(protoMethodDescriptor.getOutputType()));
}
public static MethodType fetchMethodType(MethodDescriptor methodDescriptor) {
boolean clientStreaming = methodDescriptor.toProto().getClientStreaming();
boolean serverStreaming = methodDescriptor.toProto().getServerStreaming();
if (clientStreaming && serverStreaming) {
return MethodType.BIDI_STREAMING;
} else if (!clientStreaming && !serverStreaming) {
return MethodType.UNARY;
} else if (!clientStreaming) {
return MethodType.SERVER_STREAMING;
} else {
return MethodType.SERVER_STREAMING;
}
}
public CallResults invokeMethod(GrpcMethodDefinition definition, Channel channel, CallOptions callOptions,
List<String> requestJsonTexts) {
FileDescriptorSet fileDescriptorSet = GrpcReflectionUtils.resolveService(channel, definition.getFullServiceName());
if (fileDescriptorSet == null) {
return null;
}
ServiceResolver serviceResolver = ServiceResolver.fromFileDescriptorSet(fileDescriptorSet);
MethodDescriptor methodDescriptor = serviceResolver.resolveServiceMethod(definition);
TypeRegistry registry = TypeRegistry.newBuilder().add(serviceResolver.listMessageTypes()).build();
List<DynamicMessage> requestMessages = GrpcReflectionUtils.parseToMessages(registry, methodDescriptor.getInputType(),
requestJsonTexts);
CallResults results = new CallResults();
StreamObserver<DynamicMessage> streamObserver = MessageWriter.newInstance(registry, results);
CallParams callParams = CallParams.builder()
.methodDescriptor(methodDescriptor)
.channel(channel)
.callOptions(callOptions)
.requests(requestMessages)
.responseObserver(streamObserver)
.build();
try {
grpcClientService.call(callParams).get();
} catch (InterruptedException | ExecutionException e) {
throw new RuntimeException("Caught exception while waiting for rpc", e);
}
return results;
}
private io.grpc.MethodDescriptor<DynamicMessage, DynamicMessage> createGrpcMethodDescriptor(MethodDescriptor descriptor) {
return io.grpc.MethodDescriptor.<DynamicMessage, DynamicMessage>newBuilder()
.setType(fetchMethodType(descriptor))
.setFullMethodName(fetchFullMethodName(descriptor))
.setRequestMarshaller(new DynamicMessageMarshaller(descriptor.getInputType()))
.setResponseMarshaller(new DynamicMessageMarshaller(descriptor.getOutputType()))
.build();
}
/**
* Returns the descriptor of a protobuf method with the supplied grpc method name. If the method
* cannot be found, this throws {@link IllegalArgumentException}.
*/
public MethodDescriptor resolveServiceMethod(GrpcMethodDefinition definition) {
ServiceDescriptor service = findService(definition.getPackageName(), definition.getServiceName());
MethodDescriptor method = service.findMethodByName(definition.getMethodName());
if (method == null) {
throw new IllegalArgumentException(
"Unable to find method " + definition.getMethodName()
+ " in service " + definition.getServiceName());
}
return method;
}
private void generateProtoFromDescriptor(MethodDescriptor descriptor,
Appendable out) throws IOException {
out.append(" rpc ");
out.append(descriptor.getName());
out.append(" (" + descriptor.getInputType().getFullName() + ')');
out.append(" returns");
out.append(" (" + descriptor.getOutputType().getFullName() + ')');
out.append(";\n");
}
@Override
public <R extends Message> Map<byte[], R> batchCoprocessorService(
MethodDescriptor methodDescriptor, Message request, byte[] startKey, byte[] endKey,
R responsePrototype) throws ServiceException, Throwable {
return hTable.batchCoprocessorService(methodDescriptor, request, startKey, endKey,
responsePrototype);
}
@Override
public <R extends Message> void batchCoprocessorService(MethodDescriptor methodDescriptor,
Message request, byte[] startKey, byte[] endKey, R responsePrototype, Callback<R> callback)
throws ServiceException, Throwable {
hTable.batchCoprocessorService(methodDescriptor, request, startKey, endKey, responsePrototype,
callback);
}
public void testNewReflectiveService() {
ServiceWithNoOuter.Interface impl =
control.createMock(ServiceWithNoOuter.Interface.class);
RpcController controller = control.createMock(RpcController.class);
Service service = ServiceWithNoOuter.newReflectiveService(impl);
MethodDescriptor fooMethod =
ServiceWithNoOuter.getDescriptor().findMethodByName("Foo");
MessageWithNoOuter request = MessageWithNoOuter.getDefaultInstance();
RpcCallback<Message> callback = new RpcCallback<Message>() {
public void run(Message parameter) {
// No reason this should be run.
fail();
}
};
RpcCallback<TestAllTypes> specializedCallback =
RpcUtil.specializeCallback(callback);
impl.foo(EasyMock.same(controller), EasyMock.same(request),
EasyMock.same(specializedCallback));
EasyMock.expectLastCall();
control.replay();
service.callMethod(fooMethod, controller, request, callback);
control.verify();
}
public void testServiceDescriptor() throws Exception {
ServiceDescriptor service = TestService.getDescriptor();
assertEquals("TestService", service.getName());
assertEquals("protobuf_unittest.TestService", service.getFullName());
assertEquals(UnittestProto.getDescriptor(), service.getFile());
assertEquals(2, service.getMethods().size());
MethodDescriptor fooMethod = service.getMethods().get(0);
assertEquals("Foo", fooMethod.getName());
assertEquals(UnittestProto.FooRequest.getDescriptor(),
fooMethod.getInputType());
assertEquals(UnittestProto.FooResponse.getDescriptor(),
fooMethod.getOutputType());
assertEquals(fooMethod, service.findMethodByName("Foo"));
MethodDescriptor barMethod = service.getMethods().get(1);
assertEquals("Bar", barMethod.getName());
assertEquals(UnittestProto.BarRequest.getDescriptor(),
barMethod.getInputType());
assertEquals(UnittestProto.BarResponse.getDescriptor(),
barMethod.getOutputType());
assertEquals(barMethod, service.findMethodByName("Bar"));
assertNull(service.findMethodByName("NoSuchMethod"));
for (int i = 0; i < service.getMethods().size(); i++) {
assertEquals(i, service.getMethods().get(i).getIndex());
}
}
/**
* Get matching method.
*/
private MethodDescriptor getMethod(SocketRpcProtos.Request rpcRequest,
ServiceDescriptor descriptor) throws RpcException {
MethodDescriptor method = descriptor.findMethodByName(
rpcRequest.getMethodName());
if (method == null) {
throw new RpcException(
ErrorReason.METHOD_NOT_FOUND,
String.format("Could not find method %s in service %s",
rpcRequest.getMethodName(), descriptor.getFullName()),
null);
}
return method;
}
@VisibleForTesting
static MethodInfo newMethodInfo(MethodDescriptor method, ServiceEntry service) {
final Set<EndpointInfo> methodEndpoints =
service.endpointInfos.stream()
.map(e -> {
final EndpointInfoBuilder builder = EndpointInfo.builder(
e.hostnamePattern(), e.pathMapping() + method.getName());
if (e.fragment() != null) {
builder.fragment(e.fragment());
}
if (e.defaultMimeType() != null) {
builder.defaultMimeType(e.defaultMimeType());
}
return builder.availableMimeTypes(e.availableMimeTypes()).build();
})
.collect(toImmutableSet());
return new MethodInfo(
method.getName(),
namedMessageSignature(method.getOutputType()),
// gRPC methods always take a single request parameter of message type.
ImmutableList.of(FieldInfo.builder("request", namedMessageSignature(method.getInputType()))
.requirement(FieldRequirement.REQUIRED).build()),
/* exceptionTypeSignatures */ ImmutableList.of(),
methodEndpoints,
/* exampleHttpHeaders */ ImmutableList.of(),
defaultExamples(method),
/* examplePaths */ ImmutableList.of(),
/* exampleQueries */ ImmutableList.of(),
HttpMethod.POST,
/* docString */ null);
}
private static List<String> defaultExamples(MethodDescriptor method) {
try {
final DynamicMessage defaultInput = DynamicMessage.getDefaultInstance(method.getInputType());
final String serialized = defaultExamplePrinter.print(defaultInput).trim();
if ("{\n}".equals(serialized) || "{}".equals(serialized)) {
// Ignore an empty object.
return ImmutableList.of();
}
return ImmutableList.of(serialized);
} catch (InvalidProtocolBufferException e) {
return ImmutableList.of();
}
}
@Override
public void callMethod(MethodDescriptor method, RpcController genericRpcController,
Message request, Message responsePrototype, RpcCallback<Message> callback) {
// Cast the given generic controller to a ClientRpcController.
final ClientRpcController controller;
if (genericRpcController instanceof ClientRpcController) {
controller = (ClientRpcController) genericRpcController;
} else {
throw new IllegalArgumentException("Expected ClientRpcController, got: "
+ genericRpcController.getClass());
}
// Generate a new sequence number, and configure the controller - notably,
// this throws an IllegalStateException if it is *already* configured.
final int sequenceNo = lastSequenceNumber.incrementAndGet();
final ClientRpcController.RpcState rpcStatus =
new ClientRpcController.RpcState(this, method.getOptions()
.getExtension(Rpc.isStreamingRpc), callback, new Runnable() {
@Override
public void run() {
clientChannel.sendMessage(sequenceNo, Rpc.CancelRpc.getDefaultInstance());
}
});
controller.configure(rpcStatus);
synchronized (activeMethodMap) {
activeMethodMap.put(sequenceNo, controller);
}
LOG.fine("Calling a new RPC (seq " + sequenceNo + "), method " + method.getFullName() + " for "
+ clientChannel);
// Kick off the RPC by sending the request to the server end-point.
clientChannel.sendMessage(sequenceNo, request, responsePrototype);
}
/**
* Register all methods provided by the given service type.
*/
public void registerService(Service service) {
synchronized (registeredServices) {
for (MethodDescriptor methodDescriptor : service.getDescriptorForType().getMethods()) {
registeredServices.put(methodDescriptor.getInputType(),
new RegisteredServiceMethod(service, methodDescriptor));
}
}
}
public RemoteCallException(int seqId, MethodDescriptor methodDesc,
Throwable t) {
super("Remote call error occurs when " + methodDesc.getFullName() + "is called:", t);
this.seqId = seqId;
if (t != null) {
originExceptionClass = t.getClass().getCanonicalName();
}
}
public void callMethod(final MethodDescriptor method,
final RpcController controller,
final Message param,
final Message responseType,
final RpcCallback<Message> done) {
int nextSeqId = sequence.getAndIncrement();
RpcProtos.RpcRequest rpcRequest = buildRequest(nextSeqId, method, param);
invoke(rpcRequest, new ResponseCallback(controller, responseType, done), 0);
}