Search in sources :

Example 1 with ObservableConnection

use of io.reactivex.netty.channel.ObservableConnection 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)

Example 2 with ObservableConnection

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

the class ShutDownTest method testLifeCycleShutdown.

@Test
public void testLifeCycleShutdown() throws Exception {
    final AtomicBoolean shutDownCalled = new AtomicBoolean(false);
    final HttpClient<ByteBuf, ByteBuf> client = new HttpClient<ByteBuf, ByteBuf>() {

        @Override
        public Observable<HttpClientResponse<ByteBuf>> submit(HttpClientRequest<ByteBuf> request) {
            return null;
        }

        @Override
        public Observable<HttpClientResponse<ByteBuf>> submit(HttpClientRequest<ByteBuf> request, ClientConfig config) {
            return null;
        }

        @Override
        public Observable<ObservableConnection<HttpClientResponse<ByteBuf>, HttpClientRequest<ByteBuf>>> connect() {
            return null;
        }

        @Override
        public void shutdown() {
            shutDownCalled.set(true);
        }

        @Override
        public String name() {
            return "SampleMovieService";
        }

        @Override
        public Subscription subscribe(MetricEventsListener<? extends ClientMetricsEvent<?>> listener) {
            return null;
        }
    };
    RibbonTransportFactory transportFactory = new RibbonTransportFactory(ClientConfigFactory.DEFAULT) {

        @Override
        public HttpClient<ByteBuf, ByteBuf> newHttpClient(IClientConfig config) {
            return client;
        }
    };
    HttpResourceGroup.Builder groupBuilder = HttpResourceGroup.Builder.newBuilder("SampleMovieService", ClientConfigFactory.DEFAULT, transportFactory);
    HttpResourceGroup group = groupBuilder.build();
    SampleMovieService service = RibbonDynamicProxy.newInstance(SampleMovieService.class, group);
    ProxyLifeCycle proxyLifeCycle = (ProxyLifeCycle) service;
    proxyLifeCycle.shutdown();
    assertTrue(proxyLifeCycle.isShutDown());
    assertTrue(shutDownCalled.get());
}
Also used : ObservableConnection(io.reactivex.netty.channel.ObservableConnection) RibbonTransportFactory(com.netflix.ribbon.RibbonTransportFactory) MetricEventsListener(io.reactivex.netty.metrics.MetricEventsListener) HttpResourceGroup(com.netflix.ribbon.http.HttpResourceGroup) ByteBuf(io.netty.buffer.ByteBuf) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HttpClientRequest(io.reactivex.netty.protocol.http.client.HttpClientRequest) SampleMovieService(com.netflix.ribbon.proxy.sample.MovieServiceInterfaces.SampleMovieService) ClientMetricsEvent(io.reactivex.netty.client.ClientMetricsEvent) HttpClient(io.reactivex.netty.protocol.http.client.HttpClient) HttpClientResponse(io.reactivex.netty.protocol.http.client.HttpClientResponse) IClientConfig(com.netflix.client.config.IClientConfig) IClientConfig(com.netflix.client.config.IClientConfig) Test(org.junit.Test)

Example 3 with ObservableConnection

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

the class UdpClientTest method testUdpClientWithoutTimeout.

@Test
public void testUdpClientWithoutTimeout() throws Exception {
    int port = choosePort();
    UdpServer<DatagramPacket, DatagramPacket> server = new HelloUdpServer(port, 0).createServer();
    server.start();
    BaseLoadBalancer lb = new BaseLoadBalancer();
    lb.setServersList(Lists.newArrayList(new Server("localhost", port)));
    RxClient<DatagramPacket, DatagramPacket> client = RibbonTransport.newUdpClient(lb, DefaultClientConfigImpl.getClientConfigWithDefaultValues());
    try {
        String response = client.connect().flatMap(new Func1<ObservableConnection<DatagramPacket, DatagramPacket>, Observable<DatagramPacket>>() {

            @Override
            public Observable<DatagramPacket> call(ObservableConnection<DatagramPacket, DatagramPacket> connection) {
                connection.writeStringAndFlush("Is there anybody out there?");
                return connection.getInput();
            }
        }).take(1).map(new Func1<DatagramPacket, String>() {

            @Override
            public String call(DatagramPacket datagramPacket) {
                return datagramPacket.content().toString(Charset.defaultCharset());
            }
        }).toBlocking().first();
        assertEquals(HelloUdpServer.WELCOME_MSG, response);
    } finally {
        server.shutdown();
    }
}
Also used : Server(com.netflix.loadbalancer.Server) UdpServer(io.reactivex.netty.protocol.udp.server.UdpServer) ObservableConnection(io.reactivex.netty.channel.ObservableConnection) BaseLoadBalancer(com.netflix.loadbalancer.BaseLoadBalancer) Observable(rx.Observable) DatagramPacket(io.netty.channel.socket.DatagramPacket) Test(org.junit.Test)

Aggregations

ObservableConnection (io.reactivex.netty.channel.ObservableConnection)3 ByteBuf (io.netty.buffer.ByteBuf)2 DatagramPacket (io.netty.channel.socket.DatagramPacket)2 Test (org.junit.Test)2 Observable (rx.Observable)2 IClientConfig (com.netflix.client.config.IClientConfig)1 BaseLoadBalancer (com.netflix.loadbalancer.BaseLoadBalancer)1 Server (com.netflix.loadbalancer.Server)1 RibbonTransportFactory (com.netflix.ribbon.RibbonTransportFactory)1 HttpResourceGroup (com.netflix.ribbon.http.HttpResourceGroup)1 SampleMovieService (com.netflix.ribbon.proxy.sample.MovieServiceInterfaces.SampleMovieService)1 ConnectionHandler (io.reactivex.netty.channel.ConnectionHandler)1 ClientMetricsEvent (io.reactivex.netty.client.ClientMetricsEvent)1 MetricEventsListener (io.reactivex.netty.metrics.MetricEventsListener)1 HttpClient (io.reactivex.netty.protocol.http.client.HttpClient)1 HttpClientRequest (io.reactivex.netty.protocol.http.client.HttpClientRequest)1 HttpClientResponse (io.reactivex.netty.protocol.http.client.HttpClientResponse)1 UdpServer (io.reactivex.netty.protocol.udp.server.UdpServer)1 InetSocketAddress (java.net.InetSocketAddress)1 SocketException (java.net.SocketException)1