use of org.ballerinalang.net.grpc.stubs.DefaultStreamObserver in project ballerina by ballerina-lang.
the class StreamingExecute method execute.
@Override
public void execute(Context context) {
BStruct serviceStub = (BStruct) context.getRefArgument(SERVICE_STUB_REF_INDEX);
if (serviceStub == null) {
notifyErrorReply(context, "Error while getting connector. gRPC Client connector " + "is not initialized properly");
return;
}
Object connectionStub = serviceStub.getNativeData(SERVICE_STUB);
if (connectionStub == null) {
notifyErrorReply(context, "Error while getting connection stub. gRPC Client connector " + "is not initialized properly");
return;
}
String methodName = context.getStringArgument(0);
if (methodName == null) {
notifyErrorReply(context, "Error while processing the request. RPC endpoint doesn't " + "set properly");
return;
}
com.google.protobuf.Descriptors.MethodDescriptor methodDescriptor = MessageRegistry.getInstance().getMethodDescriptor(methodName);
if (methodDescriptor == null) {
notifyErrorReply(context, "No registered method descriptor for '" + methodName + "'");
return;
}
if (connectionStub instanceof GrpcNonBlockingStub) {
GrpcNonBlockingStub grpcNonBlockingStub = (GrpcNonBlockingStub) connectionStub;
BTypeDescValue serviceType = (BTypeDescValue) context.getRefArgument(1);
Service callbackService = BLangConnectorSPIUtil.getServiceFromType(context.getProgramFile(), getTypeField(serviceType));
try {
MethodDescriptor.MethodType methodType = getMethodType(methodDescriptor);
DefaultStreamObserver responseObserver = new DefaultStreamObserver(callbackService);
StreamObserver<Message> requestSender;
if (methodType.equals(MethodDescriptor.MethodType.CLIENT_STREAMING)) {
requestSender = grpcNonBlockingStub.executeClientStreaming(responseObserver, methodName);
} else if (methodType.equals(MethodDescriptor.MethodType.BIDI_STREAMING)) {
requestSender = grpcNonBlockingStub.executeBidiStreaming(responseObserver, methodName);
} else {
notifyErrorReply(context, "Error while executing the client call. Method type " + methodType.name() + " not supported");
return;
}
BStruct connStruct = createStruct(context, CLIENT_CONNECTION);
connStruct.addNativeData(REQUEST_SENDER, requestSender);
connStruct.addNativeData(REQUEST_MESSAGE_DEFINITION, methodDescriptor.getInputType());
BStruct clientEndpoint = (BStruct) serviceStub.getNativeData(CLIENT_END_POINT);
clientEndpoint.addNativeData(CLIENT_CONNECTION, connStruct);
context.setReturnValues(clientEndpoint);
} catch (RuntimeException | GrpcClientException e) {
notifyErrorReply(context, "gRPC Client Connector Error :" + e.getMessage());
}
}
}
use of org.ballerinalang.net.grpc.stubs.DefaultStreamObserver in project ballerina by ballerina-lang.
the class NonBlockingExecute method execute.
@Override
public void execute(Context context) {
BStruct serviceStub = (BStruct) context.getRefArgument(SERVICE_STUB_REF_INDEX);
if (serviceStub == null) {
notifyErrorReply(context, "Error while getting connector. gRPC Client connector is " + "not initialized properly");
return;
}
Object connectionStub = serviceStub.getNativeData(SERVICE_STUB);
if (connectionStub == null) {
notifyErrorReply(context, "Error while getting connection stub. gRPC Client connector " + "is not initialized properly");
return;
}
String methodName = context.getStringArgument(0);
if (methodName == null) {
notifyErrorReply(context, "Error while processing the request. RPC endpoint doesn't " + "set properly");
return;
}
com.google.protobuf.Descriptors.MethodDescriptor methodDescriptor = MessageRegistry.getInstance().getMethodDescriptor(methodName);
if (methodDescriptor == null) {
notifyErrorReply(context, "No registered method descriptor for '" + methodName + "'");
return;
}
if (connectionStub instanceof GrpcNonBlockingStub) {
BValue payloadBValue = context.getRefArgument(1);
Message requestMsg = MessageUtils.generateProtoMessage(payloadBValue, methodDescriptor.getInputType());
GrpcNonBlockingStub grpcNonBlockingStub = (GrpcNonBlockingStub) connectionStub;
BTypeDescValue serviceType = (BTypeDescValue) context.getRefArgument(2);
Service callbackService = BLangConnectorSPIUtil.getServiceFromType(context.getProgramFile(), getTypeField(serviceType));
try {
MethodDescriptor.MethodType methodType = getMethodType(methodDescriptor);
if (methodType.equals(MethodDescriptor.MethodType.UNARY)) {
grpcNonBlockingStub.executeUnary(requestMsg, new DefaultStreamObserver(callbackService), methodName);
} else if (methodType.equals(MethodDescriptor.MethodType.SERVER_STREAMING)) {
grpcNonBlockingStub.executeServerStreaming(requestMsg, new DefaultStreamObserver(callbackService), methodName);
} else {
notifyErrorReply(context, "Error while executing the client call. Method type " + methodType.name() + " not supported");
return;
}
context.setReturnValues();
return;
} catch (RuntimeException | GrpcClientException e) {
notifyErrorReply(context, "gRPC Client Connector Error :" + e.getMessage());
return;
}
}
notifyErrorReply(context, "Error while processing the request message. Connection Sub " + "type not supported");
}
Aggregations