Search in sources :

Example 1 with SslContextBuilder

use of io.grpc.netty.shaded.io.netty.handler.ssl.SslContextBuilder in project thingsboard by thingsboard.

the class EdgeGrpcClient method connect.

@Override
public void connect(String edgeKey, String edgeSecret, Consumer<UplinkResponseMsg> onUplinkResponse, Consumer<EdgeConfiguration> onEdgeUpdate, Consumer<DownlinkMsg> onDownlink, Consumer<Exception> onError) {
    NettyChannelBuilder builder = NettyChannelBuilder.forAddress(rpcHost, rpcPort).keepAliveTime(keepAliveTimeSec, TimeUnit.SECONDS);
    if (sslEnabled) {
        try {
            SslContextBuilder sslContextBuilder = GrpcSslContexts.forClient();
            if (StringUtils.isNotEmpty(certResource)) {
                sslContextBuilder.trustManager(ResourceUtils.getInputStream(this, certResource));
            }
            builder.sslContext(sslContextBuilder.build());
        } catch (SSLException e) {
            log.error("Failed to initialize channel!", e);
            throw new RuntimeException(e);
        }
    } else {
        builder.usePlaintext();
    }
    channel = builder.build();
    EdgeRpcServiceGrpc.EdgeRpcServiceStub stub = EdgeRpcServiceGrpc.newStub(channel);
    log.info("[{}] Sending a connect request to the TB!", edgeKey);
    this.inputStream = stub.withCompression("gzip").handleMsgs(initOutputStream(edgeKey, onUplinkResponse, onEdgeUpdate, onDownlink, onError));
    this.inputStream.onNext(RequestMsg.newBuilder().setMsgType(RequestMsgType.CONNECT_RPC_MESSAGE).setConnectRequestMsg(ConnectRequestMsg.newBuilder().setEdgeRoutingKey(edgeKey).setEdgeSecret(edgeSecret).setEdgeVersion(EdgeVersion.V_3_3_3).build()).build());
}
Also used : SslContextBuilder(io.grpc.netty.shaded.io.netty.handler.ssl.SslContextBuilder) NettyChannelBuilder(io.grpc.netty.shaded.io.grpc.netty.NettyChannelBuilder) EdgeRpcServiceGrpc(org.thingsboard.server.gen.edge.v1.EdgeRpcServiceGrpc) SSLException(javax.net.ssl.SSLException)

Aggregations

NettyChannelBuilder (io.grpc.netty.shaded.io.grpc.netty.NettyChannelBuilder)1 SslContextBuilder (io.grpc.netty.shaded.io.netty.handler.ssl.SslContextBuilder)1 SSLException (javax.net.ssl.SSLException)1 EdgeRpcServiceGrpc (org.thingsboard.server.gen.edge.v1.EdgeRpcServiceGrpc)1