类com.google.protobuf.Descriptors.MethodDescriptor源码实例Demo

下面列出了怎么用com.google.protobuf.Descriptors.MethodDescriptor的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: grpc-nebula-java   文件: ProtoReflectionService.java
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);
  }
}
 
源代码2 项目: travelguide   文件: ServiceTest.java
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();
}
 
源代码3 项目: protobuf-socket-rpc   文件: RpcForwarder.java
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);
  }
}
 
源代码4 项目: protobuf-socket-rpc   文件: RpcChannelImpl.java
@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);
  }
}
 
源代码5 项目: protobuf-socket-rpc   文件: RpcChannelImpl.java
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);
  }
}
 
源代码6 项目: tajo   文件: BlockingRpcClient.java
@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());
  }
}
 
源代码7 项目: incubator-tajo   文件: BlockingRpcClient.java
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);
    }
  }
}
 
源代码8 项目: grpc-swagger   文件: GrpcReflectionUtils.java
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;
    }
}
 
源代码9 项目: grpc-swagger   文件: GrpcProxyService.java
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;
}
 
源代码10 项目: grpc-swagger   文件: GrpcClientService.java
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();
}
 
源代码11 项目: grpc-swagger   文件: ServiceResolver.java
/**
 * 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;
}
 
源代码12 项目: milkman   文件: ServiceResolver.java
/**
 * 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());
}
 
源代码13 项目: milkman   文件: ServiceResolver.java
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;
}
 
源代码14 项目: milkman   文件: DynamicGrpcClient.java
private io.grpc.MethodDescriptor<DynamicMessage, DynamicMessage> createGrpcMethodDescriptor() {
  return io.grpc.MethodDescriptor.<DynamicMessage, DynamicMessage>create(
      getMethodType(),
      getFullMethodName(),
      new DynamicMessageMarshaller(protoMethodDescriptor.getInputType()),
      new DynamicMessageMarshaller(protoMethodDescriptor.getOutputType()));
}
 
源代码15 项目: grpc-swagger   文件: GrpcReflectionUtils.java
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;
    }
}
 
源代码16 项目: grpc-swagger   文件: GrpcProxyService.java
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;
}
 
源代码17 项目: grpc-swagger   文件: GrpcClientService.java
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();
}
 
源代码18 项目: grpc-swagger   文件: ServiceResolver.java
/**
 * 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;
}
 
源代码19 项目: fuchsia   文件: ProtoGenerator.java
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");
}
 
源代码20 项目: phoenix-tephra   文件: TransactionAwareHTable.java
@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);
}
 
源代码21 项目: phoenix-tephra   文件: TransactionAwareHTable.java
@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);
}
 
源代码22 项目: travelguide   文件: ServiceTest.java
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();
}
 
源代码23 项目: travelguide   文件: DescriptorsTest.java
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());
  }
}
 
源代码24 项目: protobuf-socket-rpc   文件: RpcForwarder.java
/**
 * 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;
}
 
源代码25 项目: armeria   文件: GrpcDocServicePlugin.java
@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);
}
 
源代码26 项目: armeria   文件: GrpcDocServicePlugin.java
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();
    }
}
 
源代码27 项目: swellrt   文件: WebSocketClientRpcChannel.java
@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);
}
 
源代码28 项目: swellrt   文件: ServerRpcProvider.java
/**
 * 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));
    }
  }
}
 
源代码29 项目: tajo   文件: RemoteCallException.java
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();
  }
}
 
源代码30 项目: tajo   文件: AsyncRpcClient.java
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);
}
 
 类所在包
 类方法
 同包方法