Search in sources :

Example 1 with ServerServiceDefinition

use of io.grpc.ServerServiceDefinition in project grpc-java by grpc.

the class MutableHandlerRegistry method lookupMethod.

/**
   * Note: This does not actually honor the authority provided.  It will, eventually in the future.
   */
@Override
@Nullable
public ServerMethodDefinition<?, ?> lookupMethod(String methodName, @Nullable String authority) {
    String serviceName = MethodDescriptor.extractFullServiceName(methodName);
    if (serviceName == null) {
        return null;
    }
    ServerServiceDefinition service = services.get(serviceName);
    if (service == null) {
        return null;
    }
    return service.getMethod(methodName);
}
Also used : ServerServiceDefinition(io.grpc.ServerServiceDefinition) Nullable(javax.annotation.Nullable)

Example 2 with ServerServiceDefinition

use of io.grpc.ServerServiceDefinition in project grpc-java by grpc.

the class ClientCallsTest method inprocessTransportInboundFlowControl.

@Test
public void inprocessTransportInboundFlowControl() throws Exception {
    final Semaphore semaphore = new Semaphore(0);
    ServerServiceDefinition service = ServerServiceDefinition.builder(new ServiceDescriptor("some", STREAMING_METHOD)).addMethod(STREAMING_METHOD, ServerCalls.asyncBidiStreamingCall(new ServerCalls.BidiStreamingMethod<Integer, Integer>() {

        int iteration;

        @Override
        public StreamObserver<Integer> invoke(StreamObserver<Integer> responseObserver) {
            final ServerCallStreamObserver<Integer> serverCallObserver = (ServerCallStreamObserver<Integer>) responseObserver;
            serverCallObserver.setOnReadyHandler(new Runnable() {

                @Override
                public void run() {
                    while (serverCallObserver.isReady()) {
                        serverCallObserver.onNext(iteration);
                    }
                    iteration++;
                    semaphore.release();
                }
            });
            return new ServerCalls.NoopStreamObserver<Integer>() {

                @Override
                public void onCompleted() {
                    serverCallObserver.onCompleted();
                }
            };
        }
    })).build();
    long tag = System.nanoTime();
    server = InProcessServerBuilder.forName("go-with-the-flow" + tag).directExecutor().addService(service).build().start();
    channel = InProcessChannelBuilder.forName("go-with-the-flow" + tag).directExecutor().build();
    final ClientCall<Integer, Integer> clientCall = channel.newCall(STREAMING_METHOD, CallOptions.DEFAULT);
    final CountDownLatch latch = new CountDownLatch(1);
    final List<Object> receivedMessages = new ArrayList<Object>(6);
    ClientResponseObserver<Integer, Integer> responseObserver = new ClientResponseObserver<Integer, Integer>() {

        @Override
        public void beforeStart(final ClientCallStreamObserver<Integer> requestStream) {
            requestStream.disableAutoInboundFlowControl();
        }

        @Override
        public void onNext(Integer value) {
            receivedMessages.add(value);
        }

        @Override
        public void onError(Throwable t) {
            receivedMessages.add(t);
            latch.countDown();
        }

        @Override
        public void onCompleted() {
            latch.countDown();
        }
    };
    CallStreamObserver<Integer> integerStreamObserver = (CallStreamObserver<Integer>) ClientCalls.asyncBidiStreamingCall(clientCall, responseObserver);
    semaphore.acquire();
    integerStreamObserver.request(2);
    semaphore.acquire();
    integerStreamObserver.request(3);
    integerStreamObserver.onCompleted();
    assertTrue(latch.await(5, TimeUnit.SECONDS));
    // Verify that number of messages produced in each onReady handler call matches the number
    // requested by the client. Note that ClientCalls.asyncBidiStreamingCall will request(1)
    assertEquals(Arrays.asList(0, 1, 1, 2, 2, 2), receivedMessages);
}
Also used : ArrayList(java.util.ArrayList) Semaphore(java.util.concurrent.Semaphore) ServiceDescriptor(io.grpc.ServiceDescriptor) ServerServiceDefinition(io.grpc.ServerServiceDefinition) NoopStreamObserver(io.grpc.stub.ServerCalls.NoopStreamObserver) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 3 with ServerServiceDefinition

use of io.grpc.ServerServiceDefinition in project grpc-java by grpc.

the class ServerCallsTest method inprocessTransportManualFlow.

@Test
public void inprocessTransportManualFlow() throws Exception {
    final Semaphore semaphore = new Semaphore(1);
    ServerServiceDefinition service = ServerServiceDefinition.builder(new ServiceDescriptor("some", STREAMING_METHOD)).addMethod(STREAMING_METHOD, ServerCalls.asyncBidiStreamingCall(new ServerCalls.BidiStreamingMethod<Integer, Integer>() {

        int iteration;

        @Override
        public StreamObserver<Integer> invoke(StreamObserver<Integer> responseObserver) {
            final ServerCallStreamObserver<Integer> serverCallObserver = (ServerCallStreamObserver<Integer>) responseObserver;
            serverCallObserver.setOnReadyHandler(new Runnable() {

                @Override
                public void run() {
                    while (serverCallObserver.isReady()) {
                        serverCallObserver.onNext(iteration);
                    }
                    iteration++;
                    semaphore.release();
                }
            });
            return new ServerCalls.NoopStreamObserver<Integer>() {

                @Override
                public void onCompleted() {
                    serverCallObserver.onCompleted();
                }
            };
        }
    })).build();
    long tag = System.nanoTime();
    InProcessServerBuilder.forName("go-with-the-flow" + tag).addService(service).build().start();
    ManagedChannel channel = InProcessChannelBuilder.forName("go-with-the-flow" + tag).build();
    final ClientCall<Integer, Integer> clientCall = channel.newCall(STREAMING_METHOD, CallOptions.DEFAULT);
    final CountDownLatch latch = new CountDownLatch(1);
    final int[] receivedMessages = new int[6];
    clientCall.start(new ClientCall.Listener<Integer>() {

        int index;

        @Override
        public void onMessage(Integer message) {
            receivedMessages[index++] = message;
        }

        @Override
        public void onClose(Status status, Metadata trailers) {
            latch.countDown();
        }
    }, new Metadata());
    semaphore.acquire();
    clientCall.request(1);
    semaphore.acquire();
    clientCall.request(2);
    semaphore.acquire();
    clientCall.request(3);
    clientCall.halfClose();
    latch.await(5, TimeUnit.SECONDS);
    // Very that number of messages produced in each onReady handler call matches the number
    // requested by the client.
    assertArrayEquals(new int[] { 0, 1, 1, 2, 2, 2 }, receivedMessages);
}
Also used : Status(io.grpc.Status) Metadata(io.grpc.Metadata) Semaphore(java.util.concurrent.Semaphore) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ServiceDescriptor(io.grpc.ServiceDescriptor) ClientCall(io.grpc.ClientCall) ServerServiceDefinition(io.grpc.ServerServiceDefinition) ManagedChannel(io.grpc.ManagedChannel) Test(org.junit.Test)

Example 4 with ServerServiceDefinition

use of io.grpc.ServerServiceDefinition 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 5 with ServerServiceDefinition

use of io.grpc.ServerServiceDefinition in project core-java by SpineEventEngine.

the class GrpcContainerShould method add_and_remove_parameters_from_builder.

@SuppressWarnings("MagicNumber")
@Test
public void add_and_remove_parameters_from_builder() {
    final GrpcContainer.Builder builder = GrpcContainer.newBuilder().setPort(8080).setPort(60);
    assertEquals(60, builder.getPort());
    int count = 3;
    final List<ServerServiceDefinition> definitions = new ArrayList<>(count);
    for (int i = 0; i < count; i++) {
        final BindableService mockService = mock(BindableService.class);
        final ServerServiceDefinition mockDefinition = ServerServiceDefinition.builder(format("service-%s", i)).build();
        when(mockService.bindService()).thenReturn(mockDefinition);
        definitions.add(mockDefinition);
        builder.addService(mockService);
    }
    count--;
    // Perform removal and check that the return value is builder itself.
    assertEquals(builder, builder.removeService(definitions.get(count)));
    final Set<ServerServiceDefinition> serviceSet = builder.getServices();
    assertSize(count, serviceSet);
    final GrpcContainer container = builder.build();
    assertNotNull(container);
}
Also used : ServerServiceDefinition(io.grpc.ServerServiceDefinition) ArrayList(java.util.ArrayList) BindableService(io.grpc.BindableService) Test(org.junit.Test)

Aggregations

ServerServiceDefinition (io.grpc.ServerServiceDefinition)8 Test (org.junit.Test)5 ServiceDescriptor (io.grpc.ServiceDescriptor)4 ArrayList (java.util.ArrayList)3 Semaphore (java.util.concurrent.Semaphore)3 NoopStreamObserver (io.grpc.stub.ServerCalls.NoopStreamObserver)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 FileDescriptor (com.google.protobuf.Descriptors.FileDescriptor)1 BindableService (io.grpc.BindableService)1 ClientCall (io.grpc.ClientCall)1 ManagedChannel (io.grpc.ManagedChannel)1 Metadata (io.grpc.Metadata)1 Status (io.grpc.Status)1 ProtoFileDescriptorSupplier (io.grpc.protobuf.ProtoFileDescriptorSupplier)1 HashSet (java.util.HashSet)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Nullable (javax.annotation.Nullable)1