use of org.ballerinalang.net.grpc.Message in project ballerina by ballerina-lang.
the class ClientStreamingListener method invoke.
@Override
public StreamObserver<Message> invoke(StreamObserver<Message> responseObserver) {
Resource onOpen = resourceMap.get(MessageConstants.ON_OPEN_RESOURCE);
List<ParamDetail> paramDetails = onOpen.getParamDetails();
BValue[] signatureParams = new BValue[paramDetails.size()];
signatureParams[0] = getConnectionParameter(onOpen, responseObserver);
CallableUnitCallback callback = new GrpcCallableUnitCallBack(responseObserver, Boolean.FALSE);
Executor.submit(onOpen, callback, null, null, signatureParams);
return new StreamObserver<Message>() {
@Override
public void onNext(Message value) {
Resource onMessage = resourceMap.get(MessageConstants.ON_MESSAGE_RESOURCE);
List<ParamDetail> paramDetails = onMessage.getParamDetails();
BValue[] signatureParams = new BValue[paramDetails.size()];
signatureParams[0] = getConnectionParameter(onMessage, responseObserver);
BValue requestParam = getRequestParameter(onMessage, value);
if (requestParam != null) {
signatureParams[1] = requestParam;
}
CallableUnitCallback callback = new GrpcCallableUnitCallBack(responseObserver, Boolean.FALSE);
Executor.submit(onMessage, callback, null, null, signatureParams);
}
@Override
public void onError(Throwable t) {
Resource onError = resourceMap.get(MessageConstants.ON_ERROR_RESOURCE);
if (onError == null) {
String message = "Error in listener service definition. onError resource does not exists";
LOG.error(message);
throw new RuntimeException(message);
}
List<ParamDetail> paramDetails = onError.getParamDetails();
BValue[] signatureParams = new BValue[paramDetails.size()];
signatureParams[0] = getConnectionParameter(onError, responseObserver);
if (paramDetails.size() != 2) {
String message = "Error in onError resource definition. It must have two input params, but have " + paramDetails.size();
LOG.error(message);
throw new RuntimeException(message);
}
BType errorType = paramDetails.get(1).getVarType();
BStruct errorStruct = MessageUtils.getConnectorError((BStructType) errorType, t);
signatureParams[1] = errorStruct;
CallableUnitCallback callback = new GrpcCallableUnitCallBack(responseObserver, Boolean.FALSE);
Executor.submit(onError, callback, null, null, signatureParams);
}
@Override
public void onCompleted() {
Resource onCompleted = resourceMap.get(MessageConstants.ON_COMPLETE_RESOURCE);
if (onCompleted == null) {
String message = "Error in listener service definition. onError resource does not exists";
LOG.error(message);
throw new RuntimeException(message);
}
List<ParamDetail> paramDetails = onCompleted.getParamDetails();
BValue[] signatureParams = new BValue[paramDetails.size()];
signatureParams[0] = getConnectionParameter(onCompleted, responseObserver);
CallableUnitCallback callback = new GrpcCallableUnitCallBack(responseObserver, isEmptyResponse());
Executor.submit(onCompleted, callback, null, null, signatureParams);
}
};
}
use of org.ballerinalang.net.grpc.Message 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");
}
use of org.ballerinalang.net.grpc.Message in project ballerina by ballerina-lang.
the class Send method execute.
@Override
public void execute(Context context) {
BStruct connectionStruct = (BStruct) context.getRefArgument(0);
BValue responseValue = context.getRefArgument(1);
StreamObserver requestSender = (StreamObserver) connectionStruct.getNativeData(REQUEST_SENDER);
if (requestSender == null) {
context.setError(MessageUtils.getConnectorError(context, new StatusRuntimeException(Status.fromCode(Status.INTERNAL.getCode()).withDescription("Error while initializing connector. " + "response sender doesnot exist"))));
} else {
Descriptors.Descriptor inputType = (Descriptors.Descriptor) connectionStruct.getNativeData(MessageConstants.REQUEST_MESSAGE_DEFINITION);
try {
Message requestMessage = MessageUtils.generateProtoMessage(responseValue, inputType);
requestSender.onNext(requestMessage);
} catch (Throwable e) {
LOG.error("Error while sending client response.", e);
context.setError(MessageUtils.getConnectorError(context, e));
}
}
}
use of org.ballerinalang.net.grpc.Message in project ballerina by ballerina-lang.
the class BidirectionalStreamingListener method invoke.
@Override
public StreamObserver<Message> invoke(StreamObserver<Message> responseObserver) {
Resource onOpen = resourceMap.get(MessageConstants.ON_OPEN_RESOURCE);
List<ParamDetail> paramDetails = onOpen.getParamDetails();
BValue[] signatureParams = new BValue[paramDetails.size()];
signatureParams[0] = getConnectionParameter(onOpen, responseObserver);
CallableUnitCallback callback = new GrpcCallableUnitCallBack(responseObserver, Boolean.FALSE);
Executor.submit(onOpen, callback, null, null, signatureParams);
return new StreamObserver<Message>() {
@Override
public void onNext(Message value) {
Resource onMessage = resourceMap.get(MessageConstants.ON_MESSAGE_RESOURCE);
List<ParamDetail> paramDetails = onMessage.getParamDetails();
BValue[] signatureParams = new BValue[paramDetails.size()];
signatureParams[0] = getConnectionParameter(onMessage, responseObserver);
BValue requestParam = getRequestParameter(onMessage, value);
if (requestParam != null) {
signatureParams[1] = requestParam;
}
CallableUnitCallback callback = new GrpcCallableUnitCallBack(responseObserver, isEmptyResponse());
Executor.submit(onMessage, callback, null, null, signatureParams);
}
@Override
public void onError(Throwable t) {
Resource onError = resourceMap.get(MessageConstants.ON_ERROR_RESOURCE);
if (onError == null) {
String message = "Error in listener service definition. onError resource does not exists";
LOG.error(message);
throw new RuntimeException(message);
}
List<ParamDetail> paramDetails = onError.getParamDetails();
BValue[] signatureParams = new BValue[paramDetails.size()];
signatureParams[0] = getConnectionParameter(onError, responseObserver);
if (paramDetails.size() != 2) {
String message = "Error in onError resource definition. It must have two input params, but have " + paramDetails.size();
LOG.error(message);
throw new RuntimeException(message);
}
BType errorType = paramDetails.get(1).getVarType();
BStruct errorStruct = MessageUtils.getConnectorError((BStructType) errorType, t);
signatureParams[1] = errorStruct;
CallableUnitCallback callback = new GrpcCallableUnitCallBack(responseObserver, Boolean.FALSE);
Executor.submit(onError, callback, null, null, signatureParams);
}
@Override
public void onCompleted() {
Resource onCompleted = resourceMap.get(MessageConstants.ON_COMPLETE_RESOURCE);
if (onCompleted == null) {
String message = "Error in listener service definition. onError resource does not exists";
LOG.error(message);
throw new RuntimeException(message);
}
List<ParamDetail> paramDetails = onCompleted.getParamDetails();
BValue[] signatureParams = new BValue[paramDetails.size()];
signatureParams[0] = getConnectionParameter(onCompleted, responseObserver);
CallableUnitCallback callback = new GrpcCallableUnitCallBack(responseObserver, Boolean.FALSE);
Executor.submit(onCompleted, callback, null, null, signatureParams);
}
};
}
Aggregations