Search in sources :

Example 36 with Future

use of io.vertx.core.Future in project vertx-examples by vert-x3.

the class Server method start.

@Override
public void start() throws Exception {
    // The rcp service
    ProducerServiceGrpc.ProducerServiceVertxImplBase service = new ProducerServiceGrpc.ProducerServiceVertxImplBase() {

        @Override
        public void streamingInputCall(GrpcReadStream<Messages.StreamingInputCallRequest> request, Future<Messages.StreamingInputCallResponse> future) {
            request.handler(payload -> {
                System.out.println(payload.getPayload().getType().getNumber());
            }).endHandler(v -> {
                System.out.println("Request has ended.");
                future.complete(Messages.StreamingInputCallResponse.newBuilder().build());
            });
        }
    };
    // Create the server
    VertxServer rpcServer = VertxServerBuilder.forPort(vertx, 8080).addService(service).build();
    // start the server
    rpcServer.start(ar -> {
        if (ar.failed()) {
            ar.cause().printStackTrace();
        }
    });
}
Also used : Messages(io.vertx.example.grpc.Messages) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AbstractVerticle(io.vertx.core.AbstractVerticle) VertxServerBuilder(io.vertx.grpc.VertxServerBuilder) Future(io.vertx.core.Future) Runner(io.vertx.example.util.Runner) GrpcReadStream(io.vertx.grpc.GrpcReadStream) VertxServer(io.vertx.grpc.VertxServer) ProducerServiceGrpc(io.vertx.example.grpc.ProducerServiceGrpc) ProducerServiceGrpc(io.vertx.example.grpc.ProducerServiceGrpc) GrpcReadStream(io.vertx.grpc.GrpcReadStream) Messages(io.vertx.example.grpc.Messages) VertxServer(io.vertx.grpc.VertxServer) Future(io.vertx.core.Future)

Example 37 with Future

use of io.vertx.core.Future in project vertx-examples by vert-x3.

the class Server method start.

@Override
public void start() throws Exception {
    URL featureFile = Util.getDefaultFeaturesFile();
    features = Util.parseFeatures(featureFile);
    VertxServer server = VertxServerBuilder.forAddress(vertx, "localhost", 8080).addService(new RouteGuideGrpc.RouteGuideVertxImplBase() {

        @Override
        public void getFeature(Point request, Future<Feature> response) {
            response.complete(checkFeature(request));
        }

        @Override
        public void listFeatures(Rectangle request, GrpcWriteStream<Feature> response) {
            int left = Math.min(request.getLo().getLongitude(), request.getHi().getLongitude());
            int right = Math.max(request.getLo().getLongitude(), request.getHi().getLongitude());
            int top = Math.max(request.getLo().getLatitude(), request.getHi().getLatitude());
            int bottom = Math.min(request.getLo().getLatitude(), request.getHi().getLatitude());
            for (Feature feature : features) {
                if (!Util.exists(feature)) {
                    continue;
                }
                int lat = feature.getLocation().getLatitude();
                int lon = feature.getLocation().getLongitude();
                if (lon >= left && lon <= right && lat >= bottom && lat <= top) {
                    response.write(feature);
                }
            }
            response.end();
        }

        @Override
        public void recordRoute(GrpcReadStream<Point> request, Future<RouteSummary> response) {
            request.exceptionHandler(err -> {
                System.out.println("recordRoute cancelled");
            });
            RouteRecorder recorder = new RouteRecorder();
            request.handler(recorder::append);
            request.endHandler(v -> {
                response.complete(recorder.build());
            });
        }

        @Override
        public void routeChat(GrpcBidiExchange<RouteNote, RouteNote> exchange) {
            exchange.handler(note -> {
                List<RouteNote> notes = getOrCreateNotes(note.getLocation());
                // Respond with all previous notes at this location.
                for (RouteNote prevNote : notes.toArray(new RouteNote[0])) {
                    exchange.write(prevNote);
                }
                // Now add the new note to the list
                notes.add(note);
            });
            exchange.exceptionHandler(err -> {
                System.out.println("routeChat cancelled");
            });
            exchange.endHandler(v -> exchange.end());
        }
    }).build();
    server.start(ar -> {
        if (ar.succeeded()) {
            System.out.println("gRPC service started");
        } else {
            System.out.println("Could not start server " + ar.cause().getMessage());
        }
    });
}
Also used : GrpcBidiExchange(io.vertx.grpc.GrpcBidiExchange) VertxServer(io.vertx.grpc.VertxServer) Rectangle(io.grpc.examples.routeguide.Rectangle) Point(io.grpc.examples.routeguide.Point) Feature(io.grpc.examples.routeguide.Feature) URL(java.net.URL) Point(io.grpc.examples.routeguide.Point) GrpcReadStream(io.vertx.grpc.GrpcReadStream) RouteNote(io.grpc.examples.routeguide.RouteNote) Future(io.vertx.core.Future) GrpcWriteStream(io.vertx.grpc.GrpcWriteStream)

Example 38 with Future

use of io.vertx.core.Future in project vertx-examples by vert-x3.

the class Server method start.

@Override
public void start() throws Exception {
    // The rcp service
    EmptyPingPongServiceGrpc.EmptyPingPongServiceVertxImplBase service = new EmptyPingPongServiceGrpc.EmptyPingPongServiceVertxImplBase() {

        @Override
        public void emptyCall(EmptyProtos.Empty request, Future<EmptyProtos.Empty> future) {
            future.complete(EmptyProtos.Empty.newBuilder().build());
        }
    };
    // Create the server
    VertxServer rpcServer = VertxServerBuilder.forPort(vertx, 8080).addService(service).build();
    // start the server
    rpcServer.start(ar -> {
        if (ar.failed()) {
            ar.cause().printStackTrace();
        }
    });
}
Also used : EmptyPingPongServiceGrpc(io.vertx.example.grpc.EmptyPingPongServiceGrpc) VertxServer(io.vertx.grpc.VertxServer) Future(io.vertx.core.Future) EmptyProtos(io.vertx.example.grpc.EmptyProtos)

Example 39 with Future

use of io.vertx.core.Future in project vertx-examples by vert-x3.

the class Server method start.

@Override
public void start() throws Exception {
    // The rcp service
    PingPongServiceGrpc.PingPongServiceVertxImplBase service = new PingPongServiceGrpc.PingPongServiceVertxImplBase() {

        @Override
        public void unaryCall(Messages.SimpleRequest request, Future<Messages.SimpleResponse> future) {
            future.complete(Messages.SimpleResponse.newBuilder().setUsername("Paulo").build());
        }
    };
    // Create the server
    VertxServer rpcServer = VertxServerBuilder.forPort(vertx, 8080).addService(service).build();
    // start the server
    rpcServer.start(ar -> {
        if (ar.failed()) {
            ar.cause().printStackTrace();
        }
    });
}
Also used : Messages(io.vertx.example.grpc.Messages) VertxServer(io.vertx.grpc.VertxServer) Future(io.vertx.core.Future) PingPongServiceGrpc(io.vertx.example.grpc.PingPongServiceGrpc) EmptyPingPongServiceGrpc(io.vertx.example.grpc.EmptyPingPongServiceGrpc)

Example 40 with Future

use of io.vertx.core.Future in project vertx-examples by vert-x3.

the class Server method start.

@Override
public void start(Future<Void> startFuture) throws Exception {
    HttpServer server = vertx.createHttpServer(new HttpServerOptions().setUseAlpn(true).setKeyCertOptions(new JksOptions().setPath("io/vertx/example/java9/server-keystore.jks").setPassword("wibble")).setSsl(true));
    server.requestHandler(req -> {
        req.response().end("Hello " + req.version());
    }).listen(8080, ar -> startFuture.handle(ar.mapEmpty()));
}
Also used : AbstractVerticle(io.vertx.core.AbstractVerticle) HttpServer(io.vertx.core.http.HttpServer) JksOptions(io.vertx.core.net.JksOptions) Vertx(io.vertx.core.Vertx) HttpServerOptions(io.vertx.core.http.HttpServerOptions) Future(io.vertx.core.Future) JksOptions(io.vertx.core.net.JksOptions) HttpServer(io.vertx.core.http.HttpServer) HttpServerOptions(io.vertx.core.http.HttpServerOptions)

Aggregations

Future (io.vertx.core.Future)375 HttpURLConnection (java.net.HttpURLConnection)195 Handler (io.vertx.core.Handler)174 List (java.util.List)166 Objects (java.util.Objects)164 JsonObject (io.vertx.core.json.JsonObject)163 Promise (io.vertx.core.Promise)159 Vertx (io.vertx.core.Vertx)157 Buffer (io.vertx.core.buffer.Buffer)149 Optional (java.util.Optional)147 Logger (org.slf4j.Logger)136 LoggerFactory (org.slf4j.LoggerFactory)136 CompositeFuture (io.vertx.core.CompositeFuture)127 ClientErrorException (org.eclipse.hono.client.ClientErrorException)127 Map (java.util.Map)122 Span (io.opentracing.Span)117 AsyncResult (io.vertx.core.AsyncResult)112 TracingHelper (org.eclipse.hono.tracing.TracingHelper)98 Constants (org.eclipse.hono.util.Constants)97 ArrayList (java.util.ArrayList)94