Skip to content

Commit 023bb54

Browse files
NiteshKantstevegury
authored andcommitted
Using RxNetty for tcp transport (#101)
* Using RxNetty for tcp transport Current TCP transport implementation lacks a few critical features around insights and network flow control. Since RxNetty already has these features, it makes sense to use it. * Merge branch 'master' of https://github.com/ReactiveSocket/reactivesocket-java into rxnetty # Conflicts: # reactivesocket-examples/src/main/java/io/reactivesocket/examples/EchoClient.java # reactivesocket-transport-tcp/src/main/java/io/reactivesocket/transport/tcp/client/ClientTcpDuplexConnection.java # reactivesocket-transport-tcp/src/main/java/io/reactivesocket/transport/tcp/client/TcpReactiveSocketConnector.java * Review comments Also updated to rxnetty-0.5.2-rc.3
1 parent 59a4ebe commit 023bb54

File tree

26 files changed

+750
-909
lines changed

26 files changed

+750
-909
lines changed

reactivesocket-examples/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ dependencies {
66
compile project(':reactivesocket-transport-tcp')
77

88
compile project(':reactivesocket-test')
9+
runtime group: 'org.slf4j', name: 'slf4j-log4j12', version: '1.7.21'
910
}

reactivesocket-examples/src/main/java/io/reactivesocket/examples/EchoClient.java

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,46 +15,53 @@
1515
*/
1616
package io.reactivesocket.examples;
1717

18-
import io.netty.channel.nio.NioEventLoopGroup;
18+
import io.reactivesocket.ConnectionSetupHandler;
1919
import io.reactivesocket.ConnectionSetupPayload;
2020
import io.reactivesocket.Payload;
2121
import io.reactivesocket.ReactiveSocket;
22+
import io.reactivesocket.RequestHandler;
2223
import io.reactivesocket.client.ClientBuilder;
24+
import io.reactivesocket.test.TestUtil;
2325
import io.reactivesocket.transport.tcp.client.TcpReactiveSocketConnector;
26+
import io.reactivesocket.transport.tcp.server.TcpReactiveSocketServer;
2427
import io.reactivesocket.util.Unsafe;
25-
import io.reactivesocket.test.TestUtil;
26-
import org.reactivestreams.Publisher;
28+
import rx.Observable;
29+
import rx.RxReactiveStreams;
2730

28-
import java.net.InetSocketAddress;
2931
import java.net.SocketAddress;
3032
import java.util.Collections;
31-
import java.util.List;
32-
import java.util.concurrent.TimeUnit;
3333

3434
public final class EchoClient {
3535

36-
private static Publisher<List<SocketAddress>> source(SocketAddress sa) {
37-
return sub -> sub.onNext(Collections.singletonList(sa));
38-
}
39-
4036
public static void main(String... args) throws Exception {
41-
InetSocketAddress address = InetSocketAddress.createUnresolved("localhost", 8888);
37+
38+
ConnectionSetupHandler setupHandler = (setupPayload, reactiveSocket) -> {
39+
return new RequestHandler.Builder()
40+
.withRequestResponse(
41+
payload -> RxReactiveStreams.toPublisher(Observable.just(payload)))
42+
.build();
43+
};
44+
45+
SocketAddress serverAddress = TcpReactiveSocketServer.create()
46+
.start(setupHandler)
47+
.getServerAddress();
48+
4249
ConnectionSetupPayload setupPayload =
4350
ConnectionSetupPayload.create("UTF-8", "UTF-8", ConnectionSetupPayload.NO_FLAGS);
4451

45-
TcpReactiveSocketConnector tcp =
46-
new TcpReactiveSocketConnector(new NioEventLoopGroup(8), setupPayload, System.err::println);
52+
TcpReactiveSocketConnector tcp = TcpReactiveSocketConnector.create(setupPayload, Throwable::printStackTrace);
4753

4854
ReactiveSocket client = ClientBuilder.instance()
49-
.withSource(source(address))
55+
.withSource(RxReactiveStreams.toPublisher(Observable.just(Collections.singletonList(serverAddress))))
5056
.withConnector(tcp)
5157
.build();
5258

5359
Unsafe.awaitAvailability(client);
5460

5561
Payload request = TestUtil.utf8EncodedPayload("Hello", "META");
56-
Payload response = Unsafe.blockingSingleWait(client.requestResponse(request), 1, TimeUnit.SECONDS);
57-
58-
System.out.println(response);
62+
RxReactiveStreams.toObservable(client.requestResponse(request))
63+
.map(TestUtil::dataAsString)
64+
.toBlocking()
65+
.forEach(System.out::println);
5966
}
6067
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#
2+
# Copyright 2015 Netflix, Inc.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
log4j.rootLogger=INFO, stdout
17+
18+
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
19+
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
20+
log4j.appender.stdout.layout.ConversionPattern=%c %d{dd MMM yyyy HH:mm:ss,SSS} %5p [%t] (%F:%L) - %m%n

reactivesocket-test/src/main/java/io/reactivesocket/test/TestUtil.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,13 @@ public static Payload utf8EncodedPayload(final String data, final String metadat
6363
return new PayloadImpl(data, metadata);
6464
}
6565

66+
public static String dataAsString(Payload payload) {
67+
ByteBuffer data = payload.getData();
68+
byte[] dst = new byte[data.remaining()];
69+
data.get(dst);
70+
return new String(dst);
71+
}
72+
6673
public static String byteToString(ByteBuffer byteBuffer)
6774
{
6875
byteBuffer = byteBuffer.duplicate();

reactivesocket-transport-tcp/build.gradle

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
dependencies {
22
compile project(':reactivesocket-core')
3-
compile 'io.netty:netty-handler:4.1.0.CR7'
4-
compile 'io.netty:netty-codec-http:4.1.0.CR7'
3+
compile 'io.reactivex:rxnetty-tcp:0.5.2-rc.3'
54

65
testCompile project(':reactivesocket-test')
76
}

reactivesocket-transport-tcp/src/examples/java/io/reactivesocket/transport/tcp/EchoServer.java

Lines changed: 0 additions & 38 deletions
This file was deleted.

reactivesocket-transport-tcp/src/examples/java/io/reactivesocket/transport/tcp/EchoServerHandler.java

Lines changed: 0 additions & 68 deletions
This file was deleted.

reactivesocket-transport-tcp/src/examples/java/io/reactivesocket/transport/tcp/HttpServerHandler.java

Lines changed: 0 additions & 22 deletions
This file was deleted.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2016 Netflix, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
package io.reactivesocket.transport.tcp;
19+
20+
import io.reactivesocket.Frame;
21+
import io.reactivesocket.rx.Observer;
22+
import rx.Subscriber;
23+
24+
public class ObserverSubscriber extends Subscriber<Frame> {
25+
26+
private final Observer<Frame> o;
27+
28+
public ObserverSubscriber(Observer<Frame> o) {
29+
this.o = o;
30+
}
31+
32+
@Override
33+
public void onCompleted() {
34+
o.onComplete();
35+
}
36+
37+
@Override
38+
public void onError(Throwable e) {
39+
o.onError(e);
40+
}
41+
42+
@Override
43+
public void onNext(Frame frame) {
44+
o.onNext(frame);
45+
}
46+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright 2016 Netflix, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
package io.reactivesocket.transport.tcp;
19+
20+
import io.netty.buffer.ByteBuf;
21+
import io.netty.buffer.Unpooled;
22+
import io.netty.channel.ChannelDuplexHandler;
23+
import io.netty.channel.ChannelHandlerContext;
24+
import io.netty.channel.ChannelPromise;
25+
import io.netty.util.ReferenceCountUtil;
26+
import io.reactivesocket.Frame;
27+
28+
import java.nio.ByteBuffer;
29+
30+
/**
31+
* A Codec that aids reading and writing of ReactiveSocket {@link Frame}s.
32+
*/
33+
public class ReactiveSocketFrameCodec extends ChannelDuplexHandler {
34+
35+
private final MutableDirectByteBuf buffer = new MutableDirectByteBuf(Unpooled.buffer(0));
36+
private final Frame frame = Frame.allocate(buffer);
37+
38+
@Override
39+
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
40+
if (msg instanceof ByteBuf) {
41+
try {
42+
buffer.wrap((ByteBuf) msg);
43+
frame.wrap(buffer, 0);
44+
ctx.fireChannelRead(frame);
45+
} finally {
46+
ReferenceCountUtil.release(msg);
47+
}
48+
} else {
49+
super.channelRead(ctx, msg);
50+
}
51+
}
52+
53+
@Override
54+
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
55+
if (msg instanceof Frame) {
56+
ByteBuffer src = ((Frame)msg).getByteBuffer();
57+
ByteBuf toWrite = ctx.alloc().buffer(src.remaining()).writeBytes(src);
58+
ctx.write(toWrite, promise);
59+
} else {
60+
super.write(ctx, msg, promise);
61+
}
62+
}
63+
}

0 commit comments

Comments
 (0)