use of org.ballerinalang.net.grpc.listener.ClientStreamingListener in project ballerina by ballerina-lang.
the class GrpcServicesBuilder method getServiceDefinition.
private static ServerServiceDefinition getServiceDefinition(Service service, Descriptors.ServiceDescriptor serviceDescriptor) throws GrpcServerException {
// Generate full service name for the service definition. <package>.<service>
final String serviceName;
if (ServiceProtoConstants.CLASSPATH_SYMBOL.equals(service.getPackage())) {
serviceName = service.getName();
} else {
serviceName = service.getPackage() + ServiceProtoConstants.CLASSPATH_SYMBOL + service.getName();
}
// Server Definition Builder for the service.
Builder serviceDefBuilder = ServerServiceDefinition.builder(serviceName);
for (Descriptors.MethodDescriptor methodDescriptor : serviceDescriptor.getMethods()) {
final String methodName = serviceName + FILE_SEPARATOR + methodDescriptor.getName();
Descriptors.Descriptor requestDescriptor = serviceDescriptor.findMethodByName(methodDescriptor.getName()).getInputType();
Descriptors.Descriptor responseDescriptor = serviceDescriptor.findMethodByName(methodDescriptor.getName()).getOutputType();
MessageRegistry.getInstance().addMessageDescriptor(requestDescriptor.getName(), requestDescriptor);
MessageRegistry.getInstance().addMessageDescriptor(responseDescriptor.getName(), responseDescriptor);
MethodDescriptor.Marshaller<Message> reqMarshaller = ProtoUtils.marshaller(Message.newBuilder(requestDescriptor.getName()).build());
MethodDescriptor.Marshaller<Message> resMarshaller = ProtoUtils.marshaller(Message.newBuilder(responseDescriptor.getName()).build());
MethodDescriptor.MethodType methodType;
ServerCallHandler<Message, Message> serverCallHandler;
Map<String, Resource> resourceMap = new HashMap<>();
Resource mappedResource = null;
for (Resource resource : service.getResources()) {
if (methodDescriptor.getName().equals(resource.getName())) {
mappedResource = resource;
}
resourceMap.put(resource.getName(), resource);
}
if (methodDescriptor.toProto().getServerStreaming() && methodDescriptor.toProto().getClientStreaming()) {
methodType = MethodDescriptor.MethodType.BIDI_STREAMING;
serverCallHandler = ServerCalls.asyncBidiStreamingCall(new BidirectionalStreamingListener(methodDescriptor, resourceMap));
} else if (methodDescriptor.toProto().getClientStreaming()) {
methodType = MethodDescriptor.MethodType.CLIENT_STREAMING;
serverCallHandler = ServerCalls.asyncClientStreamingCall(new ClientStreamingListener(methodDescriptor, resourceMap));
} else if (methodDescriptor.toProto().getServerStreaming()) {
methodType = MethodDescriptor.MethodType.SERVER_STREAMING;
serverCallHandler = ServerCalls.asyncServerStreamingCall(new ServerStreamingListener(methodDescriptor, mappedResource));
} else {
methodType = MethodDescriptor.MethodType.UNARY;
serverCallHandler = ServerCalls.asyncUnaryCall(new UnaryMethodListener(methodDescriptor, mappedResource));
}
MethodDescriptor.Builder<Message, Message> methodBuilder = MethodDescriptor.newBuilder();
MethodDescriptor<Message, Message> grpcMethodDescriptor = methodBuilder.setType(methodType).setFullMethodName(methodName).setRequestMarshaller(reqMarshaller).setResponseMarshaller(resMarshaller).setSchemaDescriptor(methodDescriptor).build();
serviceDefBuilder.addMethod(grpcMethodDescriptor, serverCallHandler);
}
return serviceDefBuilder.build();
}
Aggregations