Search in sources :

Example 1 with ConnectionHandler

use of io.reactivex.netty.channel.ConnectionHandler in project ribbon by Netflix.

the class HelloUdpServerExternalResource method start.

public void start() {
    int port;
    try {
        port = choosePort();
    } catch (SocketException e) {
        throw new RuntimeException("Error choosing point", e);
    }
    server = RxNetty.createUdpServer(port, new ConnectionHandler<DatagramPacket, DatagramPacket>() {

        @Override
        public Observable<Void> handle(final ObservableConnection<DatagramPacket, DatagramPacket> newConnection) {
            return newConnection.getInput().flatMap(new Func1<DatagramPacket, Observable<Void>>() {

                @Override
                public Observable<Void> call(final DatagramPacket received) {
                    return Observable.interval(timeout, TimeUnit.MILLISECONDS).take(1).flatMap(new Func1<Long, Observable<Void>>() {

                        @Override
                        public Observable<Void> call(Long aLong) {
                            InetSocketAddress sender = received.sender();
                            LOG.info("Received datagram. Sender: " + sender);
                            ByteBuf data = newConnection.getChannel().alloc().buffer(WELCOME_MSG_BYTES.length);
                            data.writeBytes(WELCOME_MSG_BYTES);
                            return newConnection.writeAndFlush(new DatagramPacket(data, sender));
                        }
                    });
                }
            });
        }
    });
    server.start();
    LOG.info("UDP hello server started at port: " + port);
}
Also used : SocketException(java.net.SocketException) ObservableConnection(io.reactivex.netty.channel.ObservableConnection) InetSocketAddress(java.net.InetSocketAddress) ByteBuf(io.netty.buffer.ByteBuf) Observable(rx.Observable) ConnectionHandler(io.reactivex.netty.channel.ConnectionHandler) DatagramPacket(io.netty.channel.socket.DatagramPacket) Func1(rx.functions.Func1)

Aggregations

ByteBuf (io.netty.buffer.ByteBuf)1 DatagramPacket (io.netty.channel.socket.DatagramPacket)1 ConnectionHandler (io.reactivex.netty.channel.ConnectionHandler)1 ObservableConnection (io.reactivex.netty.channel.ObservableConnection)1 InetSocketAddress (java.net.InetSocketAddress)1 SocketException (java.net.SocketException)1 Observable (rx.Observable)1 Func1 (rx.functions.Func1)1