Search in sources :

Example 1 with EndpointConfiguration

use of org.ballerinalang.net.grpc.config.EndpointConfiguration in project ballerina by ballerina-lang.

the class InitEndpoint method execute.

@Override
public void execute(Context context) {
    try {
        Struct serviceEndpoint = BLangConnectorSPIUtil.getConnectorEndpointStruct(context);
        Struct serviceEndpointConfig = serviceEndpoint.getStructField(EndpointConstants.ENDPOINT_CONFIG);
        EndpointConfiguration configuration = EndpointUtils.getEndpointConfiguration(serviceEndpointConfig);
        io.grpc.ServerBuilder serverBuilder;
        if (configuration.getSslConfig() != null) {
            SslContext sslCtx = new SSLHandlerFactory(configuration.getSslConfig()).createHttp2TLSContextForServer();
            serverBuilder = GrpcServicesBuilder.initService(configuration, sslCtx);
        } else {
            serverBuilder = GrpcServicesBuilder.initService(configuration, null);
        }
        serviceEndpoint.addNativeData(SERVICE_BUILDER, serverBuilder);
        context.setReturnValues();
    } catch (Throwable throwable) {
        BStruct err = getConnectorError(context, throwable);
        context.setError(err);
    }
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) EndpointConfiguration(org.ballerinalang.net.grpc.config.EndpointConfiguration) SSLHandlerFactory(org.ballerinalang.net.grpc.ssl.SSLHandlerFactory) BStruct(org.ballerinalang.model.values.BStruct) Struct(org.ballerinalang.connector.api.Struct) SslContext(io.netty.handler.ssl.SslContext)

Example 2 with EndpointConfiguration

use of org.ballerinalang.net.grpc.config.EndpointConfiguration in project ballerina by ballerina-lang.

the class InitEndpoint method execute.

@Override
public void execute(Context context) {
    try {
        Struct clientEndpoint = BLangConnectorSPIUtil.getConnectorEndpointStruct(context);
        // Creating client endpoint with channel as native data.
        Struct endpointConfig = clientEndpoint.getStructField(EndpointConstants.ENDPOINT_CONFIG);
        EndpointConfiguration configuration = EndpointUtils.getEndpointConfiguration(endpointConfig);
        ManagedChannel channel;
        if (configuration.getSslConfig() == null) {
            channel = ManagedChannelBuilder.forAddress(configuration.getHost(), configuration.getPort()).usePlaintext(true).build();
        } else {
            SslContext sslContext = new SSLHandlerFactory(configuration.getSslConfig()).createHttp2TLSContextForClient();
            channel = NettyChannelBuilder.forAddress(generateSocketAddress(configuration.getHost(), configuration.getPort())).flowControlWindow(65 * 1024).maxInboundMessageSize(MAX_MESSAGE_SIZE).sslContext(sslContext).build();
        }
        clientEndpoint.addNativeData(CHANNEL_KEY, channel);
    } catch (Throwable throwable) {
        BStruct errorStruct = MessageUtils.getConnectorError(context, throwable);
        context.setError(errorStruct);
    }
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) ManagedChannel(io.grpc.ManagedChannel) EndpointConfiguration(org.ballerinalang.net.grpc.config.EndpointConfiguration) SSLHandlerFactory(org.ballerinalang.net.grpc.ssl.SSLHandlerFactory) BStruct(org.ballerinalang.model.values.BStruct) Struct(org.ballerinalang.connector.api.Struct) SslContext(io.netty.handler.ssl.SslContext)

Example 3 with EndpointConfiguration

use of org.ballerinalang.net.grpc.config.EndpointConfiguration in project ballerina by ballerina-lang.

the class EndpointUtils method getEndpointConfiguration.

/**
 * Generate server endpoint object from service endpoint configuration struct.
 *
 * @param endpointConfig service endpoint configuration struct.
 * @return server endpoint object
 */
public static EndpointConfiguration getEndpointConfiguration(Struct endpointConfig) {
    String host = endpointConfig.getStringField(EndpointConstants.ENDPOINT_CONFIG_HOST);
    long port = endpointConfig.getIntField(EndpointConstants.ENDPOINT_CONFIG_PORT);
    Struct sslConfig = endpointConfig.getStructField(EndpointConstants.ENDPOINT_CONFIG_SSL);
    EndpointConfiguration endpointConfiguration = new EndpointConfiguration();
    if (host == null || host.isEmpty()) {
        endpointConfiguration.setHost(EndpointConstants.HTTP_DEFAULT_HOST);
    } else {
        endpointConfiguration.setHost(host);
    }
    endpointConfiguration.setPort(Math.toIntExact(port));
    if (sslConfig != null && (!EMPTY_STRING.equals(sslConfig.getStringField(TRUST_FILE)) || !EMPTY_STRING.equals(sslConfig.getStringField(KEY_FILE)))) {
        endpointConfiguration.setScheme(EndpointConstants.PROTOCOL_HTTPS);
        endpointConfiguration.setSslConfig(getSslConfig(sslConfig));
        return endpointConfiguration;
    }
    return endpointConfiguration;
}
Also used : EndpointConfiguration(org.ballerinalang.net.grpc.config.EndpointConfiguration) Struct(org.ballerinalang.connector.api.Struct)

Aggregations

Struct (org.ballerinalang.connector.api.Struct)3 EndpointConfiguration (org.ballerinalang.net.grpc.config.EndpointConfiguration)3 SslContext (io.netty.handler.ssl.SslContext)2 BStruct (org.ballerinalang.model.values.BStruct)2 SSLHandlerFactory (org.ballerinalang.net.grpc.ssl.SSLHandlerFactory)2 ManagedChannel (io.grpc.ManagedChannel)1