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);
}
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());
}
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();
}
}
Aggregations