Search in sources :

Example 1 with ProtoFileDescriptorSupplier

use of io.grpc.protobuf.ProtoFileDescriptorSupplier in project grpc-java by grpc.

the class ProtoReflectionService method updateIndexIfNecessary.

/**
   * Checks for updates to the server's mutable services and updates the index if any changes are
   * detected. A change is any addition or removal in the set of file descriptors attached to the
   * mutable services or a change in the service names.
   *
   * @return The (potentially updated) index.
   */
private ServerReflectionIndex updateIndexIfNecessary() {
    synchronized (lock) {
        if (serverReflectionIndex == null) {
            serverReflectionIndex = new ServerReflectionIndex(server.getImmutableServices(), server.getMutableServices());
            return serverReflectionIndex;
        }
        Set<FileDescriptor> serverFileDescriptors = new HashSet<FileDescriptor>();
        Set<String> serverServiceNames = new HashSet<String>();
        List<ServerServiceDefinition> serverMutableServices = server.getMutableServices();
        for (ServerServiceDefinition mutableService : serverMutableServices) {
            io.grpc.ServiceDescriptor serviceDescriptor = mutableService.getServiceDescriptor();
            if (serviceDescriptor.getSchemaDescriptor() instanceof ProtoFileDescriptorSupplier) {
                String serviceName = serviceDescriptor.getName();
                FileDescriptor fileDescriptor = ((ProtoFileDescriptorSupplier) serviceDescriptor.getSchemaDescriptor()).getFileDescriptor();
                serverFileDescriptors.add(fileDescriptor);
                serverServiceNames.add(serviceName);
            }
        }
        // Replace the index if the underlying mutable services have changed. Check both the file
        // descriptors and the service names, because one file descriptor can define multiple
        // services.
        FileDescriptorIndex mutableServicesIndex = serverReflectionIndex.getMutableServicesIndex();
        if (!mutableServicesIndex.getServiceFileDescriptors().equals(serverFileDescriptors) || !mutableServicesIndex.getServiceNames().equals(serverServiceNames)) {
            serverReflectionIndex = new ServerReflectionIndex(server.getImmutableServices(), serverMutableServices);
        }
        return serverReflectionIndex;
    }
}
Also used : ServerServiceDefinition(io.grpc.ServerServiceDefinition) ProtoFileDescriptorSupplier(io.grpc.protobuf.ProtoFileDescriptorSupplier) FileDescriptor(com.google.protobuf.Descriptors.FileDescriptor) HashSet(java.util.HashSet)

Example 2 with ProtoFileDescriptorSupplier

use of io.grpc.protobuf.ProtoFileDescriptorSupplier in project grpc-java by grpc.

the class ProtoReflectionService method getRefreshedIndex.

/**
 * Retrieves the index for services of the server that dispatches the current call. Computes
 * one if not exist. The index is updated if any changes to the server's mutable services are
 * detected. A change is any addition or removal in the set of file descriptors attached to the
 * mutable services or a change in the service names.
 */
private ServerReflectionIndex getRefreshedIndex() {
    synchronized (lock) {
        Server server = InternalServer.SERVER_CONTEXT_KEY.get();
        ServerReflectionIndex index = serverReflectionIndexes.get(server);
        if (index == null) {
            index = new ServerReflectionIndex(server.getImmutableServices(), server.getMutableServices());
            serverReflectionIndexes.put(server, index);
            return index;
        }
        Set<FileDescriptor> serverFileDescriptors = new HashSet<>();
        Set<String> serverServiceNames = new HashSet<>();
        List<ServerServiceDefinition> serverMutableServices = server.getMutableServices();
        for (ServerServiceDefinition mutableService : serverMutableServices) {
            io.grpc.ServiceDescriptor serviceDescriptor = mutableService.getServiceDescriptor();
            if (serviceDescriptor.getSchemaDescriptor() instanceof ProtoFileDescriptorSupplier) {
                String serviceName = serviceDescriptor.getName();
                FileDescriptor fileDescriptor = ((ProtoFileDescriptorSupplier) serviceDescriptor.getSchemaDescriptor()).getFileDescriptor();
                serverFileDescriptors.add(fileDescriptor);
                serverServiceNames.add(serviceName);
            }
        }
        // Replace the index if the underlying mutable services have changed. Check both the file
        // descriptors and the service names, because one file descriptor can define multiple
        // services.
        FileDescriptorIndex mutableServicesIndex = index.getMutableServicesIndex();
        if (!mutableServicesIndex.getServiceFileDescriptors().equals(serverFileDescriptors) || !mutableServicesIndex.getServiceNames().equals(serverServiceNames)) {
            index = new ServerReflectionIndex(server.getImmutableServices(), serverMutableServices);
            serverReflectionIndexes.put(server, index);
        }
        return index;
    }
}
Also used : Server(io.grpc.Server) InternalServer(io.grpc.InternalServer) ProtoFileDescriptorSupplier(io.grpc.protobuf.ProtoFileDescriptorSupplier) FileDescriptor(com.google.protobuf.Descriptors.FileDescriptor) ServerServiceDefinition(io.grpc.ServerServiceDefinition) HashSet(java.util.HashSet)

Aggregations

FileDescriptor (com.google.protobuf.Descriptors.FileDescriptor)2 ServerServiceDefinition (io.grpc.ServerServiceDefinition)2 ProtoFileDescriptorSupplier (io.grpc.protobuf.ProtoFileDescriptorSupplier)2 HashSet (java.util.HashSet)2 InternalServer (io.grpc.InternalServer)1 Server (io.grpc.Server)1