use of triple.Response in project sofa-rpc by sofastack.
the class TripleServer method buildSofaServiceDef.
private ServerServiceDefinition buildSofaServiceDef(GenericServiceImpl genericService, ProviderConfig providerConfig) {
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();
}
use of triple.Response in project sofa-rpc by sofastack.
the class TripleClientInvoker method invoke.
@Override
public SofaResponse invoke(SofaRequest sofaRequest, int timeout) throws Exception {
if (!useGeneric) {
SofaResponse sofaResponse = new SofaResponse();
Object stub = sofaStub.invoke(null, channel, buildCustomCallOptions(sofaRequest, timeout), timeout);
final Method method = sofaRequest.getMethod();
Object appResponse = method.invoke(stub, sofaRequest.getMethodArgs()[0]);
sofaResponse.setAppResponse(appResponse);
return sofaResponse;
} else {
String serviceName = sofaRequest.getInterfaceName();
String methodName = sofaRequest.getMethodName();
MethodDescriptor.Marshaller<?> requestMarshaller = null;
MethodDescriptor.Marshaller<?> responseMarshaller = null;
requestMarshaller = io.grpc.protobuf.ProtoUtils.marshaller(Request.getDefaultInstance());
responseMarshaller = io.grpc.protobuf.ProtoUtils.marshaller(Response.getDefaultInstance());
String fullMethodName = generateFullMethodName(serviceName, methodName);
MethodDescriptor methodDescriptor = io.grpc.MethodDescriptor.newBuilder().setType(io.grpc.MethodDescriptor.MethodType.UNARY).setFullMethodName(fullMethodName).setSampledToLocalTracing(true).setRequestMarshaller((MethodDescriptor.Marshaller<Object>) requestMarshaller).setResponseMarshaller((MethodDescriptor.Marshaller<Object>) responseMarshaller).build();
Request request = getRequest(sofaRequest, serialization, serializer);
Response response = (Response) ClientCalls.blockingUnaryCall(channel, methodDescriptor, buildCustomCallOptions(sofaRequest, timeout), request);
SofaResponse sofaResponse = new SofaResponse();
byte[] responseDate = response.getData().toByteArray();
Class returnType = sofaRequest.getMethod().getReturnType();
if (returnType != void.class) {
if (responseDate != null && responseDate.length > 0) {
Serializer responseSerializer = SerializerFactory.getSerializer(response.getSerializeType());
Object appResponse = responseSerializer.decode(new ByteArrayWrapperByteBuf(responseDate), returnType, null);
sofaResponse.setAppResponse(appResponse);
}
}
return sofaResponse;
}
}
use of triple.Response in project sofa-rpc by sofastack.
the class GenericServiceImplTest method getReturnValue.
private Object getReturnValue(Method method) {
Object appResponse = null;
Response response = responseObserver.getValue();
byte[] responseDate = response.getData().toByteArray();
Class returnType = method.getReturnType();
if (returnType != void.class) {
if (responseDate != null && responseDate.length > 0) {
Serializer responseSerializer = SerializerFactory.getSerializer(response.getSerializeType());
appResponse = responseSerializer.decode(new ByteArrayWrapperByteBuf(responseDate), returnType, null);
}
}
return appResponse;
}
use of triple.Response in project sofa-rpc by sofastack.
the class GenericServiceImpl method generic.
@Override
public void generic(Request request, StreamObserver<Response> responseObserver) {
SofaRequest sofaRequest = TracingContextKey.getKeySofaRequest().get(Context.current());
String methodName = sofaRequest.getMethodName();
String interfaceName = sofaRequest.getInterfaceName();
ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
try {
Class proxyClass = ClassTypeUtils.getClass(interfaceName);
ClassLoader interfaceClassLoader = proxyClass.getClassLoader();
Thread.currentThread().setContextClassLoader(interfaceClassLoader);
Class[] argTypes = getArgTypes(request);
Serializer serializer = SerializerFactory.getSerializer(request.getSerializeType());
Method declaredMethod = proxyClass.getDeclaredMethod(methodName, argTypes);
Object[] invokeArgs = getInvokeArgs(request, argTypes, serializer);
// fill sofaRequest
sofaRequest.setMethod(declaredMethod);
sofaRequest.setMethodArgs(invokeArgs);
sofaRequest.setMethodArgSigs(ClassTypeUtils.getTypeStrs(argTypes, true));
SofaResponse response = invoker.invoke(sofaRequest);
Object ret = getAppResponse(declaredMethod, response);
Response.Builder builder = Response.newBuilder();
builder.setSerializeType(request.getSerializeType());
builder.setType(declaredMethod.getReturnType().getName());
builder.setData(ByteString.copyFrom(serializer.encode(ret, null).array()));
Response build = builder.build();
responseObserver.onNext(build);
responseObserver.onCompleted();
} catch (Exception e) {
LOGGER.error("Invoke " + methodName + " error:", e);
throw new SofaRpcRuntimeException(e);
} finally {
Thread.currentThread().setContextClassLoader(oldClassLoader);
}
}
Aggregations