|
| 1 | +package com.baeldung.rsocket; |
| 2 | + |
| 3 | +import com.baeldung.rsocket.support.WindDataPublisher; |
| 4 | +import static com.baeldung.rsocket.support.Constants.*; |
| 5 | +import com.baeldung.rsocket.support.GameController; |
| 6 | +import io.rsocket.AbstractRSocket; |
| 7 | +import io.rsocket.Payload; |
| 8 | +import io.rsocket.RSocketFactory; |
| 9 | +import io.rsocket.transport.netty.server.TcpServerTransport; |
| 10 | +import org.reactivestreams.Publisher; |
| 11 | +import org.slf4j.Logger; |
| 12 | +import org.slf4j.LoggerFactory; |
| 13 | +import reactor.core.Disposable; |
| 14 | +import reactor.core.publisher.Flux; |
| 15 | +import reactor.core.publisher.Mono; |
| 16 | + |
| 17 | +public class Server { |
| 18 | + |
| 19 | + private static final Logger LOG = LoggerFactory.getLogger(Server.class); |
| 20 | + |
| 21 | + private final Disposable server; |
| 22 | + private final WindDataPublisher windDataPublisher = new WindDataPublisher(); |
| 23 | + private final GameController gameController; |
| 24 | + |
| 25 | + public Server() { |
| 26 | + this.server = RSocketFactory.receive() |
| 27 | + .acceptor((setupPayload, reactiveSocket) -> Mono.just(new RSocketImpl())) |
| 28 | + .transport(TcpServerTransport.create("localhost", TCP_PORT)) |
| 29 | + .start() |
| 30 | + .doOnNext(x -> LOG.info("Server started")) |
| 31 | + .subscribe(); |
| 32 | + |
| 33 | + this.gameController = new GameController("Server Player"); |
| 34 | + } |
| 35 | + |
| 36 | + public void dispose() { |
| 37 | + windDataPublisher.complete(); |
| 38 | + this.server.dispose(); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * RSocket implementation |
| 43 | + */ |
| 44 | + private class RSocketImpl extends AbstractRSocket { |
| 45 | + |
| 46 | + /** |
| 47 | + * Handle Request/Response messages |
| 48 | + * |
| 49 | + * @param payload Message payload |
| 50 | + * @return payload response |
| 51 | + */ |
| 52 | + @Override |
| 53 | + public Mono<Payload> requestResponse(Payload payload) { |
| 54 | + try { |
| 55 | + return Mono.just(payload); // reflect the payload back to the sender |
| 56 | + } catch (Exception x) { |
| 57 | + return Mono.error(x); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Handle Fire-and-Forget messages |
| 63 | + * |
| 64 | + * @param payload Message payload |
| 65 | + * @return nothing |
| 66 | + */ |
| 67 | + @Override |
| 68 | + public Mono<Void> fireAndForget(Payload payload) { |
| 69 | + try { |
| 70 | + windDataPublisher.publish(payload); // forward the payload |
| 71 | + return Mono.empty(); |
| 72 | + } catch (Exception x) { |
| 73 | + return Mono.error(x); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Handle Request/Stream messages. Each request returns a new stream. |
| 79 | + * |
| 80 | + * @param payload Payload that can be used to determine which stream to return |
| 81 | + * @return Flux stream containing simulated wind speed data |
| 82 | + */ |
| 83 | + @Override |
| 84 | + public Flux<Payload> requestStream(Payload payload) { |
| 85 | + String streamName = payload.getDataUtf8(); |
| 86 | + if (WIND_DATA_STREAM_NAME.equals(streamName)) { |
| 87 | + return Flux.from(windDataPublisher); |
| 88 | + } |
| 89 | + return Flux.error(new IllegalArgumentException(streamName)); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Handle request for bidirectional channel |
| 94 | + * |
| 95 | + * @param payloads Stream of payloads delivered from the client |
| 96 | + * @return |
| 97 | + */ |
| 98 | + @Override |
| 99 | + public Flux<Payload> requestChannel(Publisher<Payload> payloads) { |
| 100 | + Flux.from(payloads) |
| 101 | + .subscribe(gameController::processPayload); |
| 102 | + Flux<Payload> channel = Flux.from(gameController); |
| 103 | + return channel; |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | +} |
0 commit comments