use of org.ballerinalang.net.grpc.Message in project ballerina by ballerina-lang.
the class BlockingExecute 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 service stub " + "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");
}
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 GrpcBlockingStub) {
BValue payloadBValue = context.getRefArgument(1);
Message requestMsg = MessageUtils.generateProtoMessage(payloadBValue, methodDescriptor.getInputType());
GrpcBlockingStub grpcBlockingStub = (GrpcBlockingStub) connectionStub;
try {
MethodDescriptor.MethodType methodType = getMethodType(methodDescriptor);
// TODO : check whether we can support blocking server streaming.
if (methodType.equals(MethodDescriptor.MethodType.UNARY)) {
Message responseMsg = grpcBlockingStub.executeUnary(requestMsg, methodName);
Descriptors.Descriptor outputDescriptor = methodDescriptor.getOutputType();
BValue responseBValue = MessageUtils.generateRequestStruct(responseMsg, outputDescriptor.getName(), getBalType(outputDescriptor.getName(), context), context);
context.setReturnValues(responseBValue);
return;
} else {
notifyErrorReply(context, "Error while executing the client call. Method type " + methodType.name() + " not supported");
}
} catch (RuntimeException | GrpcClientException e) {
notifyErrorReply(context, "gRPC Client Connector Error :" + e.getMessage());
}
}
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 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.Message in project ballerina by ballerina-lang.
the class DefaultStreamObserver method generateRequestStruct.
private BValue generateRequestStruct(Message request, String fieldName, BType structType, Resource resource) {
BValue bValue = null;
int stringIndex = 0;
int intIndex = 0;
int floatIndex = 0;
int boolIndex = 0;
int refIndex = 0;
if (structType instanceof BStructType) {
BStruct requestStruct = BLangConnectorSPIUtil.createBStruct(MessageUtils.getProgramFile(resource), structType.getPackagePath(), structType.getName());
for (BStructType.StructField structField : ((BStructType) structType).getStructFields()) {
String structFieldName = structField.getFieldName();
if (structField.getFieldType() instanceof BRefType) {
BType bType = structField.getFieldType();
if (MessageRegistry.getInstance().getMessageDescriptorMap().containsKey(bType.getName())) {
Message message = (Message) request.getFields().get(structFieldName);
requestStruct.setRefField(refIndex++, (BRefType) generateRequestStruct(message, structFieldName, structField.getFieldType(), resource));
}
} else {
if (request.getFields().containsKey(structFieldName)) {
String fieldType = structField.getFieldType().getName();
switch(fieldType) {
case STRING:
{
requestStruct.setStringField(stringIndex++, (String) request.getFields().get(structFieldName));
break;
}
case INT:
{
requestStruct.setIntField(intIndex++, (Long) request.getFields().get(structFieldName));
break;
}
case FLOAT:
{
Float value = (Float) request.getFields().get(structFieldName);
if (value != null) {
requestStruct.setFloatField(floatIndex++, Double.parseDouble(value.toString()));
}
break;
}
case DOUBLE:
{
Double value = (Double) request.getFields().get(structFieldName);
if (value != null) {
requestStruct.setFloatField(floatIndex++, Double.parseDouble(value.toString()));
}
break;
}
case BOOLEAN:
{
requestStruct.setBooleanField(boolIndex++, (Integer) request.getFields().get(structFieldName));
break;
}
default:
{
throw new UnsupportedFieldTypeException("Error while generating request struct. Field" + " type is not supported : " + fieldType);
}
}
}
}
}
bValue = requestStruct;
} else {
Map<String, Object> fields = request.getFields();
if (fields.size() == 1 && fields.containsKey("value")) {
fieldName = "value";
}
if (fields.containsKey(fieldName)) {
String fieldType = structType.getName();
switch(fieldType) {
case STRING:
{
bValue = new BString((String) fields.get(fieldName));
break;
}
case INT:
{
bValue = new BInteger((Long) fields.get(fieldName));
break;
}
case FLOAT:
{
Float value = (Float) fields.get(fieldName);
if (value != null) {
bValue = new BFloat(Double.parseDouble(value.toString()));
}
break;
}
case DOUBLE:
{
Double value = (Double) fields.get(fieldName);
if (value != null) {
bValue = new BFloat(Double.parseDouble(value.toString()));
}
break;
}
case BOOLEAN:
{
bValue = new BBoolean((Boolean) fields.get(fieldName));
break;
}
default:
{
throw new UnsupportedFieldTypeException("Error while generating request struct. Field " + "type is not supported : " + fieldType);
}
}
}
}
return bValue;
}
use of org.ballerinalang.net.grpc.Message in project ballerina by ballerina-lang.
the class MethodListener method generateRequestStruct.
private BValue generateRequestStruct(Message request, ProgramFile programFile, String fieldName, BType structType) {
BValue bValue = null;
int stringIndex = 0;
int intIndex = 0;
int floatIndex = 0;
int boolIndex = 0;
int refIndex = 0;
if (structType instanceof BStructType) {
BStruct requestStruct = BLangConnectorSPIUtil.createBStruct(programFile, structType.getPackagePath(), structType.getName());
for (BStructType.StructField structField : ((BStructType) structType).getStructFields()) {
String structFieldName = structField.getFieldName();
if (structField.getFieldType() instanceof BRefType) {
BType bType = structField.getFieldType();
if (MessageRegistry.getInstance().getMessageDescriptorMap().containsKey(bType.getName())) {
Message message = (Message) request.getFields().get(structFieldName);
requestStruct.setRefField(refIndex++, (BRefType) generateRequestStruct(message, programFile, structFieldName, structField.getFieldType()));
}
} else {
if (request.getFields().containsKey(structFieldName)) {
String fieldType = structField.getFieldType().getName();
switch(fieldType) {
case STRING:
{
requestStruct.setStringField(stringIndex++, (String) request.getFields().get(structFieldName));
break;
}
case INT:
{
requestStruct.setIntField(intIndex++, (Long) request.getFields().get(structFieldName));
break;
}
case FLOAT:
{
Float value = (Float) request.getFields().get(structFieldName);
if (value != null) {
requestStruct.setFloatField(floatIndex++, Double.parseDouble(value.toString()));
}
break;
}
case DOUBLE:
{
Double value = (Double) request.getFields().get(structFieldName);
if (value != null) {
requestStruct.setFloatField(floatIndex++, Double.parseDouble(value.toString()));
}
break;
}
case BOOLEAN:
{
requestStruct.setBooleanField(boolIndex++, (Integer) request.getFields().get(structFieldName));
break;
}
default:
{
throw new UnsupportedFieldTypeException("Error while generating request struct. Field" + " type is not supported : " + fieldType);
}
}
}
}
}
bValue = requestStruct;
} else {
Map<String, Object> fields = request.getFields();
if (fields.size() == 1 && fields.containsKey("value")) {
fieldName = "value";
}
if (fields.containsKey(fieldName)) {
String fieldType = structType.getName();
switch(fieldType) {
case STRING:
{
bValue = new BString((String) fields.get(fieldName));
break;
}
case INT:
{
bValue = new BInteger((Long) fields.get(fieldName));
break;
}
case FLOAT:
{
Float value = (Float) fields.get(fieldName);
if (value != null) {
bValue = new BFloat(Double.parseDouble(value.toString()));
}
break;
}
case DOUBLE:
{
Double value = (Double) fields.get(fieldName);
if (value != null) {
bValue = new BFloat(Double.parseDouble(value.toString()));
}
break;
}
case BOOLEAN:
{
bValue = new BBoolean((Boolean) fields.get(fieldName));
break;
}
default:
{
throw new UnsupportedFieldTypeException("Error while generating request struct. Field " + "type is not supported : " + fieldType);
}
}
}
}
return bValue;
}
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 clientEndpoint = (BStruct) context.getRefArgument(CLIENT_RESPONDER_REF_INDEX);
BValue responseValue = context.getRefArgument(RESPONSE_MESSAGE_REF_INDEX);
StreamObserver<Message> responseObserver = MessageUtils.getResponseObserver(clientEndpoint);
Descriptors.Descriptor outputType = (Descriptors.Descriptor) clientEndpoint.getNativeData(MessageConstants.RESPONSE_MESSAGE_DEFINITION);
if (responseObserver == null) {
context.setError(MessageUtils.getConnectorError(context, new StatusRuntimeException(Status.fromCode(Status.INTERNAL.getCode()).withDescription("Error while initializing connector. " + "response sender doesnot exist"))));
} else {
try {
// If there is no response message like conn -> send(), system doesn't send the message.
if (!MessageUtils.isEmptyResponse(outputType)) {
Message responseMessage = MessageUtils.generateProtoMessage(responseValue, outputType);
responseObserver.onNext(responseMessage);
}
} catch (Throwable e) {
LOG.error("Error while sending client response.", e);
context.setError(MessageUtils.getConnectorError(context, e));
}
}
}
Aggregations