类io.grpc.ServerMethodDefinition源码实例Demo

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

源代码1 项目: grpc-nebula-java   文件: BinaryLogProvider.java
/**
 * Wraps a {@link ServerMethodDefinition} such that it performs binary logging if needed.
 */
@Override
public final <ReqT, RespT> ServerMethodDefinition<?, ?> wrapMethodDefinition(
    ServerMethodDefinition<ReqT, RespT> oMethodDef) {
  ServerInterceptor binlogInterceptor =
      getServerInterceptor(oMethodDef.getMethodDescriptor().getFullMethodName());
  if (binlogInterceptor == null) {
    return oMethodDef;
  }
  MethodDescriptor<byte[], byte[]> binMethod =
      BinaryLogProvider.toByteBufferMethod(oMethodDef.getMethodDescriptor());
  ServerMethodDefinition<byte[], byte[]> binDef =
      InternalServerInterceptors.wrapMethod(oMethodDef, binMethod);
  ServerCallHandler<byte[], byte[]> binlogHandler =
      InternalServerInterceptors.interceptCallHandlerCreate(
          binlogInterceptor, binDef.getServerCallHandler());
  return ServerMethodDefinition.create(binMethod, binlogHandler);
}
 
源代码2 项目: grpc-nebula-java   文件: BinaryLogProviderTest.java
private static <ReqT, RespT> ServerCall.Listener<ReqT> startServerCallHelper(
    final ServerMethodDefinition<ReqT, RespT> methodDef,
    final List<Object> serializedResp) {
  ServerCall<ReqT, RespT> serverCall = new NoopServerCall<ReqT, RespT>() {
    @Override
    public void sendMessage(RespT message) {
      serializedResp.add(message);
    }

    @Override
    public MethodDescriptor<ReqT, RespT> getMethodDescriptor() {
      return methodDef.getMethodDescriptor();
    }
  };
  return methodDef.getServerCallHandler().startCall(serverCall, new Metadata());
}
 
源代码3 项目: grpc-nebula-java   文件: ServerImpl.java
/** Never returns {@code null}. */
private <ReqT, RespT> ServerStreamListener startCall(ServerStream stream, String fullMethodName,
    ServerMethodDefinition<ReqT, RespT> methodDef, Metadata headers,
    Context.CancellableContext context, StatsTraceContext statsTraceCtx) {
  // TODO(ejona86): should we update fullMethodName to have the canonical path of the method?
  statsTraceCtx.serverCallStarted(
      new ServerCallInfoImpl<ReqT, RespT>(
          methodDef.getMethodDescriptor(), // notify with original method descriptor
          stream.getAttributes(),
          stream.getAuthority()));
  ServerCallHandler<ReqT, RespT> handler = methodDef.getServerCallHandler();
  for (ServerInterceptor interceptor : interceptors) {
    handler = InternalServerInterceptors.interceptCallHandler(interceptor, handler);
  }
  ServerMethodDefinition<ReqT, RespT> interceptedDef = methodDef.withServerCallHandler(handler);
  ServerMethodDefinition<?, ?> wMethodDef = binlog == null
      ? interceptedDef : binlog.wrapMethodDefinition(interceptedDef);
  return startWrappedCall(fullMethodName, wMethodDef, stream, headers, context);
}
 
源代码4 项目: grpc-nebula-java   文件: ServerImpl.java
private <WReqT, WRespT> ServerStreamListener startWrappedCall(
    String fullMethodName,
    ServerMethodDefinition<WReqT, WRespT> methodDef,
    ServerStream stream,
    Metadata headers,
    Context.CancellableContext context) {
  ServerCallImpl<WReqT, WRespT> call = new ServerCallImpl<WReqT, WRespT>(
      stream,
      methodDef.getMethodDescriptor(),
      headers,
      context,
      decompressorRegistry,
      compressorRegistry,
      serverCallTracer);

  ServerCall.Listener<WReqT> listener =
      methodDef.getServerCallHandler().startCall(call, headers);
  if (listener == null) {
    throw new NullPointerException(
        "startCall() returned a null listener for method " + fullMethodName);
  }
  return call.newServerStreamListener(listener);
}
 
@Test
public void replaceAndLookup() {
  assertNull(registry.addService(basicServiceDefinition));
  assertNotNull(registry.lookupMethod("basic/flow"));
  MethodDescriptor<String, Integer> anotherMethod = MethodDescriptor.<String, Integer>newBuilder()
      .setType(MethodType.UNKNOWN)
      .setFullMethodName("basic/another")
      .setRequestMarshaller(requestMarshaller)
      .setResponseMarshaller(responseMarshaller)
      .build();
  ServerServiceDefinition replaceServiceDefinition = ServerServiceDefinition.builder(
      new ServiceDescriptor("basic", anotherMethod))
      .addMethod(anotherMethod, flowHandler).build();
  ServerMethodDefinition<?, ?> anotherMethodDefinition =
      replaceServiceDefinition.getMethod("basic/another");
  assertSame(basicServiceDefinition, registry.addService(replaceServiceDefinition));

  assertNull(registry.lookupMethod("basic/flow"));
  ServerMethodDefinition<?, ?> method = registry.lookupMethod("basic/another");
  assertSame(anotherMethodDefinition, method);
}
 
源代码6 项目: sofa-rpc   文件: TripleServer.java
private ServerServiceDefinition buildSofaServiceDef(GenericServiceImpl genericService,
                                                    ProviderConfig providerConfig, Invoker instance) {
    ServerServiceDefinition templateDefinition = genericService.bindService();
    ServerCallHandler<Request, Response> templateHandler = (ServerCallHandler<Request, Response>) templateDefinition
        .getMethods().iterator().next().getServerCallHandler();
    List<MethodDescriptor<Request, Response>> methodDescriptor = getMethodDescriptor(providerConfig);
    List<ServerMethodDefinition<Request, Response>> methodDefs = getMethodDefinitions(templateHandler,
        methodDescriptor);
    ServerServiceDefinition.Builder builder = ServerServiceDefinition.builder(getServiceDescriptor(
        templateDefinition, providerConfig, methodDescriptor));
    for (ServerMethodDefinition<Request, Response> methodDef : methodDefs) {
        builder.addMethod(methodDef);

    }
    return builder.build();

}
 
源代码7 项目: grpc-java   文件: BinaryLogProvider.java
/**
 * Wraps a {@link ServerMethodDefinition} such that it performs binary logging if needed.
 */
@Override
public final <ReqT, RespT> ServerMethodDefinition<?, ?> wrapMethodDefinition(
    ServerMethodDefinition<ReqT, RespT> oMethodDef) {
  ServerInterceptor binlogInterceptor =
      getServerInterceptor(oMethodDef.getMethodDescriptor().getFullMethodName());
  if (binlogInterceptor == null) {
    return oMethodDef;
  }
  MethodDescriptor<byte[], byte[]> binMethod =
      BinaryLogProvider.toByteBufferMethod(oMethodDef.getMethodDescriptor());
  ServerMethodDefinition<byte[], byte[]> binDef =
      InternalServerInterceptors.wrapMethod(oMethodDef, binMethod);
  ServerCallHandler<byte[], byte[]> binlogHandler =
      InternalServerInterceptors.interceptCallHandlerCreate(
          binlogInterceptor, binDef.getServerCallHandler());
  return ServerMethodDefinition.create(binMethod, binlogHandler);
}
 
源代码8 项目: grpc-java   文件: BinaryLogProviderTest.java
private static <ReqT, RespT> ServerCall.Listener<ReqT> startServerCallHelper(
    final ServerMethodDefinition<ReqT, RespT> methodDef,
    final List<Object> serializedResp) {
  ServerCall<ReqT, RespT> serverCall = new NoopServerCall<ReqT, RespT>() {
    @Override
    public void sendMessage(RespT message) {
      serializedResp.add(message);
    }

    @Override
    public MethodDescriptor<ReqT, RespT> getMethodDescriptor() {
      return methodDef.getMethodDescriptor();
    }
  };
  return methodDef.getServerCallHandler().startCall(serverCall, new Metadata());
}
 
源代码9 项目: grpc-java   文件: ServerImpl.java
/** Never returns {@code null}. */
private <ReqT, RespT> ServerStreamListener startCall(ServerStream stream, String fullMethodName,
    ServerMethodDefinition<ReqT, RespT> methodDef, Metadata headers,
    Context.CancellableContext context, StatsTraceContext statsTraceCtx, Tag tag) {
  // TODO(ejona86): should we update fullMethodName to have the canonical path of the method?
  statsTraceCtx.serverCallStarted(
      new ServerCallInfoImpl<>(
          methodDef.getMethodDescriptor(), // notify with original method descriptor
          stream.getAttributes(),
          stream.getAuthority()));
  ServerCallHandler<ReqT, RespT> handler = methodDef.getServerCallHandler();
  for (ServerInterceptor interceptor : interceptors) {
    handler = InternalServerInterceptors.interceptCallHandler(interceptor, handler);
  }
  ServerMethodDefinition<ReqT, RespT> interceptedDef = methodDef.withServerCallHandler(handler);
  ServerMethodDefinition<?, ?> wMethodDef = binlog == null
      ? interceptedDef : binlog.wrapMethodDefinition(interceptedDef);
  return startWrappedCall(fullMethodName, wMethodDef, stream, headers, context, tag);
}
 
源代码10 项目: grpc-java   文件: ServerImpl.java
private <WReqT, WRespT> ServerStreamListener startWrappedCall(
    String fullMethodName,
    ServerMethodDefinition<WReqT, WRespT> methodDef,
    ServerStream stream,
    Metadata headers,
    Context.CancellableContext context,
    Tag tag) {

  ServerCallImpl<WReqT, WRespT> call = new ServerCallImpl<>(
      stream,
      methodDef.getMethodDescriptor(),
      headers,
      context,
      decompressorRegistry,
      compressorRegistry,
      serverCallTracer,
      tag);

  ServerCall.Listener<WReqT> listener =
      methodDef.getServerCallHandler().startCall(call, headers);
  if (listener == null) {
    throw new NullPointerException(
        "startCall() returned a null listener for method " + fullMethodName);
  }
  return call.newServerStreamListener(listener);
}
 
源代码11 项目: grpc-java   文件: MutableHandlerRegistryTest.java
@Test
public void replaceAndLookup() {
  assertNull(registry.addService(basicServiceDefinition));
  assertNotNull(registry.lookupMethod("basic/flow"));
  MethodDescriptor<String, Integer> anotherMethod = MethodDescriptor.<String, Integer>newBuilder()
      .setType(MethodType.UNKNOWN)
      .setFullMethodName("basic/another")
      .setRequestMarshaller(requestMarshaller)
      .setResponseMarshaller(responseMarshaller)
      .build();
  ServerServiceDefinition replaceServiceDefinition = ServerServiceDefinition.builder(
      new ServiceDescriptor("basic", anotherMethod))
      .addMethod(anotherMethod, flowHandler).build();
  ServerMethodDefinition<?, ?> anotherMethodDefinition =
      replaceServiceDefinition.getMethod("basic/another");
  assertSame(basicServiceDefinition, registry.addService(replaceServiceDefinition));

  assertNull(registry.lookupMethod("basic/flow"));
  ServerMethodDefinition<?, ?> method = registry.lookupMethod("basic/another");
  assertSame(anotherMethodDefinition, method);
}
 
源代码12 项目: grpc-nebula-java   文件: BinaryLogProviderTest.java
@Test
public void wrapMethodDefinition_methodDescriptor() throws Exception {
  ServerMethodDefinition<String, Integer> methodDef =
      ServerMethodDefinition.create(
          method,
          new ServerCallHandler<String, Integer>() {
            @Override
            public Listener<String> startCall(
                ServerCall<String, Integer> call, Metadata headers) {
              throw new UnsupportedOperationException();
            }
          });
  ServerMethodDefinition<?, ?> wMethodDef = binlogProvider.wrapMethodDefinition(methodDef);
  validateWrappedMethod(wMethodDef.getMethodDescriptor());
}
 
源代码13 项目: grpc-nebula-java   文件: InternalHandlerRegistry.java
/**
 * 重新设置服务和方法
 *
 * @author sxp
 * @since 2019/7/16
 */
@Override
public void resetServicesAndMethods(List<ServerServiceDefinition> newServices) {
  Map<String, ServerMethodDefinition<?, ?>> map = new HashMap<>();
  for (ServerServiceDefinition service : newServices) {
    for (ServerMethodDefinition<?, ?> method : service.getMethods()) {
      map.put(method.getMethodDescriptor().getFullMethodName(), method);
    }
  }

  this.services = Collections.unmodifiableList(newServices);
  this.methods = Collections.unmodifiableMap(map);
}
 
源代码14 项目: grpc-nebula-java   文件: InternalHandlerRegistry.java
InternalHandlerRegistry build() {
  Map<String, ServerMethodDefinition<?, ?>> map =
      new HashMap<String, ServerMethodDefinition<?, ?>>();
  for (ServerServiceDefinition service : services.values()) {
    for (ServerMethodDefinition<?, ?> method : service.getMethods()) {
      map.put(method.getMethodDescriptor().getFullMethodName(), method);
    }
  }
  return new InternalHandlerRegistry(
      Collections.unmodifiableList(new ArrayList<>(services.values())),
      Collections.unmodifiableMap(map));
}
 
源代码15 项目: grpc-nebula-java   文件: MutableHandlerRegistry.java
/**
 * Note: This does not actually honor the authority provided.  It will, eventually in the future.
 */
@Override
@Nullable
public ServerMethodDefinition<?, ?> lookupMethod(String methodName, @Nullable String authority) {
  String serviceName = MethodDescriptor.extractFullServiceName(methodName);
  if (serviceName == null) {
    return null;
  }
  ServerServiceDefinition service = services.get(serviceName);
  if (service == null) {
    return null;
  }
  return service.getMethod(methodName);
}
 
源代码16 项目: grpc-nebula-java   文件: ServerImplTest.java
@Test
public void binaryLogInstalled() throws Exception {
  final SettableFuture<Boolean> intercepted = SettableFuture.create();
  final ServerInterceptor interceptor = new ServerInterceptor() {
    @Override
    public <ReqT, RespT> Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call,
        Metadata headers,
        ServerCallHandler<ReqT, RespT> next) {
      intercepted.set(true);
      return next.startCall(call, headers);
    }
  };

  builder.binlog = new BinaryLog() {
    @Override
    public void close() throws IOException {
      // noop
    }

    @Override
    public <ReqT, RespT> ServerMethodDefinition<?, ?> wrapMethodDefinition(
        ServerMethodDefinition<ReqT, RespT> oMethodDef) {
      return ServerMethodDefinition.create(
          oMethodDef.getMethodDescriptor(),
          InternalServerInterceptors.interceptCallHandlerCreate(
              interceptor,
              oMethodDef.getServerCallHandler()));
    }

    @Override
    public Channel wrapChannel(Channel channel) {
      return channel;
    }
  };
  createAndStartServer();
  basicExchangeHelper(METHOD, "Lots of pizza, please", 314, 50);
  assertTrue(intercepted.get());
}
 
@Test
public void simpleLookup() {
  assertNull(registry.addService(basicServiceDefinition));
  ServerMethodDefinition<?, ?> method = registry.lookupMethod("basic/flow");
  assertSame(flowMethodDefinition, method);

  assertNull(registry.lookupMethod("/basic/flow"));
  assertNull(registry.lookupMethod("basic/basic"));
  assertNull(registry.lookupMethod("flow/flow"));
  assertNull(registry.lookupMethod("completely/random"));
}
 
@Test
public void simpleLookupWithBindable() {
  BindableService bindableService =
      new BindableService() {
        @Override
        public ServerServiceDefinition bindService() {
          return basicServiceDefinition;
        }
      };

  assertNull(registry.addService(bindableService));

  ServerMethodDefinition<?, ?> method = registry.lookupMethod("basic/flow");
  assertSame(flowMethodDefinition, method);
}
 
@SuppressWarnings("unchecked")
private static ServerMethodDefinition<Void, Void> getSoleMethod(
    ServerServiceDefinition serviceDef) {
  if (serviceDef.getMethods().size() != 1) {
    throw new AssertionError("Not exactly one method present");
  }
  return (ServerMethodDefinition<Void, Void>) getOnlyElement(serviceDef.getMethods());
}
 
源代码20 项目: sofa-rpc   文件: TripleServer.java
private List<ServerMethodDefinition<Request, Response>> getMethodDefinitions(ServerCallHandler<Request, Response> templateHandler,List<MethodDescriptor<Request, Response>> methodDescriptors) {
    List<ServerMethodDefinition<Request, Response>> result = new ArrayList<>();

    for (MethodDescriptor<Request, Response> methodDescriptor : methodDescriptors) {
        ServerMethodDefinition<Request, Response> serverMethodDefinition = ServerMethodDefinition.create(methodDescriptor, templateHandler);
        result.add(serverMethodDefinition);
    }
    return result;
}
 
源代码21 项目: grpc-proxy   文件: ProxyHandlerRegistry.java
@Override
public ServerMethodDefinition<?, ?> lookupMethod(String methodName, @Nullable String authority) {
  return ServerMethodDefinition.create(
      MethodDescriptor.<byte[], byte[]>newBuilder()
          .setRequestMarshaller(new ByteArrayMarshaller())
          .setResponseMarshaller(new ByteArrayMarshaller())
          .setType(MethodType.UNARY)
          .setFullMethodName(methodName)
          .build(),
      ServerCalls.asyncUnaryCall(new ProxyUnaryMethod(backend, methodName)));
}
 
源代码22 项目: armeria   文件: HandlerRegistry.java
HandlerRegistry build() {
    final ImmutableMap.Builder<String, ServerMethodDefinition<?, ?>> mapBuilder =
            ImmutableMap.builder();
    for (ServerServiceDefinition service : services.values()) {
        for (ServerMethodDefinition<?, ?> method : service.getMethods()) {
            mapBuilder.put(method.getMethodDescriptor().getFullMethodName(), method);
        }
    }
    return new HandlerRegistry(ImmutableList.copyOf(services.values()), mapBuilder.build());
}
 
源代码23 项目: armeria   文件: UnframedGrpcService.java
/**
 * Creates a new instance that decorates the specified {@link HttpService}.
 */
UnframedGrpcService(GrpcService delegate) {
    super(delegate);
    checkArgument(delegate.isFramed(), "Decorated service must be a framed GrpcService.");
    delegateGrpcService = delegate;
    methodsByName = delegate.services()
                            .stream()
                            .flatMap(service -> service.getMethods().stream())
                            .map(ServerMethodDefinition::getMethodDescriptor)
                            .collect(toImmutableMap(MethodDescriptor::getFullMethodName,
                                                    Function.identity()));
}
 
源代码24 项目: grpc-java   文件: BinaryLogProviderTest.java
@Test
public void wrapMethodDefinition_methodDescriptor() throws Exception {
  ServerMethodDefinition<String, Integer> methodDef =
      ServerMethodDefinition.create(
          method,
          new ServerCallHandler<String, Integer>() {
            @Override
            public ServerCall.Listener<String> startCall(
                ServerCall<String, Integer> call, Metadata headers) {
              throw new UnsupportedOperationException();
            }
          });
  ServerMethodDefinition<?, ?> wMethodDef = binlogProvider.wrapMethodDefinition(methodDef);
  validateWrappedMethod(wMethodDef.getMethodDescriptor());
}
 
源代码25 项目: grpc-java   文件: InternalHandlerRegistry.java
InternalHandlerRegistry build() {
  Map<String, ServerMethodDefinition<?, ?>> map =
      new HashMap<>();
  for (ServerServiceDefinition service : services.values()) {
    for (ServerMethodDefinition<?, ?> method : service.getMethods()) {
      map.put(method.getMethodDescriptor().getFullMethodName(), method);
    }
  }
  return new InternalHandlerRegistry(
      Collections.unmodifiableList(new ArrayList<>(services.values())),
      Collections.unmodifiableMap(map));
}
 
源代码26 项目: grpc-java   文件: MutableHandlerRegistry.java
/**
 * Note: This does not actually honor the authority provided.  It will, eventually in the future.
 */
@Override
@Nullable
public ServerMethodDefinition<?, ?> lookupMethod(String methodName, @Nullable String authority) {
  String serviceName = MethodDescriptor.extractFullServiceName(methodName);
  if (serviceName == null) {
    return null;
  }
  ServerServiceDefinition service = services.get(serviceName);
  if (service == null) {
    return null;
  }
  return service.getMethod(methodName);
}
 
源代码27 项目: grpc-java   文件: ManagedChannelImplTest.java
@Test
public void binaryLogInstalled() throws Exception {
  final SettableFuture<Boolean> intercepted = SettableFuture.create();
  channelBuilder.binlog = new BinaryLog() {
    @Override
    public void close() throws IOException {
      // noop
    }

    @Override
    public <ReqT, RespT> ServerMethodDefinition<?, ?> wrapMethodDefinition(
        ServerMethodDefinition<ReqT, RespT> oMethodDef) {
      return oMethodDef;
    }

    @Override
    public Channel wrapChannel(Channel channel) {
      return ClientInterceptors.intercept(channel,
          new ClientInterceptor() {
            @Override
            public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
                MethodDescriptor<ReqT, RespT> method,
                CallOptions callOptions,
                Channel next) {
              intercepted.set(true);
              return next.newCall(method, callOptions);
            }
          });
    }
  };

  createChannel();
  ClientCall<String, Integer> call = channel.newCall(method, CallOptions.DEFAULT);
  call.start(mockCallListener, new Metadata());
  assertTrue(intercepted.get());
}
 
源代码28 项目: grpc-java   文件: ServerImplTest.java
@Test
public void binaryLogInstalled() throws Exception {
  final SettableFuture<Boolean> intercepted = SettableFuture.create();
  final ServerInterceptor interceptor = new ServerInterceptor() {
    @Override
    public <ReqT, RespT> Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call,
        Metadata headers,
        ServerCallHandler<ReqT, RespT> next) {
      intercepted.set(true);
      return next.startCall(call, headers);
    }
  };

  builder.binlog = new BinaryLog() {
    @Override
    public void close() throws IOException {
      // noop
    }

    @Override
    public <ReqT, RespT> ServerMethodDefinition<?, ?> wrapMethodDefinition(
        ServerMethodDefinition<ReqT, RespT> oMethodDef) {
      return ServerMethodDefinition.create(
          oMethodDef.getMethodDescriptor(),
          InternalServerInterceptors.interceptCallHandlerCreate(
              interceptor,
              oMethodDef.getServerCallHandler()));
    }

    @Override
    public Channel wrapChannel(Channel channel) {
      return channel;
    }
  };
  createAndStartServer();
  basicExchangeHelper(METHOD, "Lots of pizza, please", 314, 50);
  assertTrue(intercepted.get());
}
 
源代码29 项目: grpc-java   文件: MutableHandlerRegistryTest.java
@Test
public void simpleLookup() {
  assertNull(registry.addService(basicServiceDefinition));
  ServerMethodDefinition<?, ?> method = registry.lookupMethod("basic/flow");
  assertSame(flowMethodDefinition, method);

  assertNull(registry.lookupMethod("/basic/flow"));
  assertNull(registry.lookupMethod("basic/basic"));
  assertNull(registry.lookupMethod("flow/flow"));
  assertNull(registry.lookupMethod("completely/random"));
}
 
源代码30 项目: grpc-java   文件: MutableHandlerRegistryTest.java
@Test
public void simpleLookupWithBindable() {
  BindableService bindableService =
      new BindableService() {
        @Override
        public ServerServiceDefinition bindService() {
          return basicServiceDefinition;
        }
      };

  assertNull(registry.addService(bindableService));

  ServerMethodDefinition<?, ?> method = registry.lookupMethod("basic/flow");
  assertSame(flowMethodDefinition, method);
}
 
 类所在包
 同包方法