下面列出了io.grpc.internal.NoopServerCall#io.grpc.ServerMethodDefinition 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 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);
}
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());
}
/** 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);
}
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);
}
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();
}
/**
* 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);
}
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());
}
/** 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);
}
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);
}
@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);
}
@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());
}
/**
* 重新设置服务和方法
*
* @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);
}
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));
}
/**
* 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);
}
@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());
}
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;
}
@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)));
}
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());
}
/**
* 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()));
}
@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());
}
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));
}
/**
* 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);
}
@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());
}
@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);
}