Search in sources :

Example 1 with Socket

use of io.grpc.channelz.v1.Socket in project grpc-java by grpc.

the class ChannelzProtoUtil method toSocket.

static Socket toSocket(InternalInstrumented<SocketStats> obj) {
    SocketStats socketStats = getFuture(obj.getStats());
    Socket.Builder builder = Socket.newBuilder().setRef(toSocketRef(obj)).setLocal(toAddress(socketStats.local));
    if (socketStats.security != null) {
        builder.setSecurity(toSecurity(socketStats.security));
    }
    // listen sockets do not have remote nor data
    if (socketStats.remote != null) {
        builder.setRemote(toAddress(socketStats.remote));
    }
    builder.setData(extractSocketData(socketStats));
    return builder.build();
}
Also used : SocketStats(io.grpc.InternalChannelz.SocketStats) Socket(io.grpc.channelz.v1.Socket)

Example 2 with Socket

use of io.grpc.channelz.v1.Socket in project grpc-java by grpc.

the class ChannelzProtoUtilTest method socketSecurityTls.

@Test
public void socketSecurityTls() throws Exception {
    Certificate local = mock(Certificate.class);
    Certificate remote = mock(Certificate.class);
    when(local.getEncoded()).thenReturn("localcert".getBytes(Charsets.UTF_8));
    when(remote.getEncoded()).thenReturn("remotecert".getBytes(Charsets.UTF_8));
    socket.security = new InternalChannelz.Security(new InternalChannelz.Tls("TLS_NULL_WITH_NULL_NULL", local, remote));
    assertEquals(Security.newBuilder().setTls(Tls.newBuilder().setStandardName("TLS_NULL_WITH_NULL_NULL").setLocalCertificate(ByteString.copyFrom("localcert", Charsets.UTF_8)).setRemoteCertificate(ByteString.copyFrom("remotecert", Charsets.UTF_8))).build(), ChannelzProtoUtil.toSocket(socket).getSecurity());
    socket.security = new InternalChannelz.Security(new InternalChannelz.Tls("TLS_NULL_WITH_NULL_NULL", /*localCert=*/
    null, remote));
    assertEquals(Security.newBuilder().setTls(Tls.newBuilder().setStandardName("TLS_NULL_WITH_NULL_NULL").setRemoteCertificate(ByteString.copyFrom("remotecert", Charsets.UTF_8))).build(), ChannelzProtoUtil.toSocket(socket).getSecurity());
    socket.security = new InternalChannelz.Security(new InternalChannelz.Tls("TLS_NULL_WITH_NULL_NULL", local, /*remoteCert=*/
    null));
    assertEquals(Security.newBuilder().setTls(Tls.newBuilder().setStandardName("TLS_NULL_WITH_NULL_NULL").setLocalCertificate(ByteString.copyFrom("localcert", Charsets.UTF_8))).build(), ChannelzProtoUtil.toSocket(socket).getSecurity());
}
Also used : InternalChannelz(io.grpc.InternalChannelz) Tls(io.grpc.channelz.v1.Security.Tls) Certificate(java.security.cert.Certificate) Test(org.junit.Test)

Example 3 with Socket

use of io.grpc.channelz.v1.Socket in project grpc-java by grpc.

the class ChannelzProtoUtilTest method toServer.

@Test
public void toServer() throws Exception {
    // no listen sockets
    assertEquals(serverProto, ChannelzProtoUtil.toServer(server));
    // 1 listen socket
    server.serverStats = toBuilder(server.serverStats).addListenSockets(ImmutableList.<InternalInstrumented<SocketStats>>of(listenSocket)).build();
    assertEquals(serverProto.toBuilder().addListenSocket(listenSocketRef).build(), ChannelzProtoUtil.toServer(server));
    // multiple listen sockets
    TestListenSocket otherListenSocket = new TestListenSocket();
    SocketRef otherListenSocketRef = ChannelzProtoUtil.toSocketRef(otherListenSocket);
    server.serverStats = toBuilder(server.serverStats).addListenSockets(ImmutableList.<InternalInstrumented<SocketStats>>of(otherListenSocket)).build();
    assertEquals(serverProto.toBuilder().addListenSocket(listenSocketRef).addListenSocket(otherListenSocketRef).build(), ChannelzProtoUtil.toServer(server));
}
Also used : TestListenSocket(io.grpc.protobuf.services.ChannelzTestHelper.TestListenSocket) SocketRef(io.grpc.channelz.v1.SocketRef) SocketStats(io.grpc.InternalChannelz.SocketStats) Test(org.junit.Test)

Example 4 with Socket

use of io.grpc.channelz.v1.Socket in project grpc-java by grpc.

the class ChannelzService method getSocket.

/**
 * Returns a socket.
 */
@Override
public void getSocket(GetSocketRequest request, StreamObserver<GetSocketResponse> responseObserver) {
    InternalInstrumented<SocketStats> s = channelz.getSocket(request.getSocketId());
    if (s == null) {
        responseObserver.onError(Status.NOT_FOUND.withDescription("Can't find socket " + request.getSocketId()).asRuntimeException());
        return;
    }
    GetSocketResponse resp;
    try {
        resp = GetSocketResponse.newBuilder().setSocket(ChannelzProtoUtil.toSocket(s)).build();
    } catch (StatusRuntimeException e) {
        responseObserver.onError(e);
        return;
    }
    responseObserver.onNext(resp);
    responseObserver.onCompleted();
}
Also used : StatusRuntimeException(io.grpc.StatusRuntimeException) SocketStats(io.grpc.InternalChannelz.SocketStats) GetSocketResponse(io.grpc.channelz.v1.GetSocketResponse)

Example 5 with Socket

use of io.grpc.channelz.v1.Socket in project grpc-java by grpc.

the class ChannelzProtoUtilTest method socketSecurityOther.

@Test
public void socketSecurityOther() throws Exception {
    // what is packed here is not important, just pick some proto message
    Message contents = GetChannelRequest.newBuilder().setChannelId(1).build();
    Any packed = Any.pack(contents);
    socket.security = new InternalChannelz.Security(new InternalChannelz.OtherSecurity("other_security", packed));
    assertEquals(Security.newBuilder().setOther(OtherSecurity.newBuilder().setName("other_security").setValue(packed)).build(), ChannelzProtoUtil.toSocket(socket).getSecurity());
}
Also used : Message(com.google.protobuf.Message) InternalChannelz(io.grpc.InternalChannelz) OtherSecurity(io.grpc.channelz.v1.Security.OtherSecurity) Any(com.google.protobuf.Any) Test(org.junit.Test)

Aggregations

SocketStats (io.grpc.InternalChannelz.SocketStats)3 Test (org.junit.Test)3 InternalChannelz (io.grpc.InternalChannelz)2 Any (com.google.protobuf.Any)1 Message (com.google.protobuf.Message)1 StatusRuntimeException (io.grpc.StatusRuntimeException)1 GetSocketResponse (io.grpc.channelz.v1.GetSocketResponse)1 OtherSecurity (io.grpc.channelz.v1.Security.OtherSecurity)1 Tls (io.grpc.channelz.v1.Security.Tls)1 Socket (io.grpc.channelz.v1.Socket)1 SocketRef (io.grpc.channelz.v1.SocketRef)1 TestListenSocket (io.grpc.protobuf.services.ChannelzTestHelper.TestListenSocket)1 Certificate (java.security.cert.Certificate)1