Skip to content

Commit 3f9a384

Browse files
robertroeserstevegury
authored andcommitted
embedded Aeron MediaDriver in the client (#6)
* aeron client can run embededded media driver so you don't have to run the external driver * added toString methods for duplex connections for debugging and access log purposes
1 parent dad3019 commit 3f9a384

9 files changed

Lines changed: 126 additions & 3 deletions

File tree

reactivesocket-aeron/src/main/java/io/reactivesocket/aeron/client/AeronClientDuplexConnection.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,5 +126,15 @@ public CopyOnWriteArrayList<Observer<Frame>> getSubjects() {
126126
return subjects;
127127
}
128128

129+
public String toString() {
130+
if (publication == null) {
131+
return getClass().getName() + ":publication=null";
132+
}
133+
134+
return getClass().getName() + ":publication=[" +
135+
"channel=" + publication.channel() + "," +
136+
"streamId=" + publication.streamId() + "," +
137+
"sessionId=" + publication.sessionId() + "]";
129138

139+
}
130140
}

reactivesocket-aeron/src/main/java/io/reactivesocket/aeron/client/ClientAeronManager.java

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,19 @@
1515
*/
1616
package io.reactivesocket.aeron.client;
1717

18-
import io.reactivesocket.aeron.internal.Loggable;
19-
import rx.Scheduler;
20-
import rx.schedulers.Schedulers;
2118
import io.aeron.Aeron;
2219
import io.aeron.FragmentAssembler;
2320
import io.aeron.Image;
2421
import io.aeron.Subscription;
22+
import io.aeron.driver.MediaDriver;
23+
import io.aeron.driver.ThreadingMode;
2524
import io.aeron.logbuffer.FragmentHandler;
25+
import io.reactivesocket.aeron.internal.Constants;
26+
import io.reactivesocket.aeron.internal.Loggable;
27+
import org.agrona.concurrent.BackoffIdleStrategy;
28+
import org.agrona.concurrent.SleepingIdleStrategy;
29+
import rx.Scheduler;
30+
import rx.schedulers.Schedulers;
2631

2732
import java.util.concurrent.CopyOnWriteArrayList;
2833
import java.util.concurrent.TimeUnit;
@@ -33,6 +38,24 @@
3338
public class ClientAeronManager implements Loggable {
3439
private static final ClientAeronManager INSTANCE = new ClientAeronManager();
3540

41+
/**
42+
* Enables running the client with an embedded Aeron {@link MediaDriver} so you don't have to run
43+
* the driver in a separate process. To enable this option you need to set the reactivesocket.aeron.clientEmbeddedDriver
44+
* to true
45+
*/
46+
static {
47+
if (Constants.CLIENT_EMBEDDED_AERON_DRIVER) {
48+
System.out.println("+++ Launching embedded media driver");
49+
final MediaDriver.Context context = new MediaDriver.Context();
50+
context.dirsDeleteOnStart(true);
51+
context.threadingMode(ThreadingMode.SHARED_NETWORK);
52+
context.conductorIdleStrategy(new SleepingIdleStrategy(TimeUnit.MILLISECONDS.toNanos(10)));
53+
context.senderIdleStrategy(new BackoffIdleStrategy(5, 10, 100, 1000));
54+
context.receiverIdleStrategy(new BackoffIdleStrategy(5, 10, 100, 1000));
55+
MediaDriver.launch(context);
56+
}
57+
}
58+
3659
private final CopyOnWriteArrayList<ClientAction> clientActions;
3760

3861
private final CopyOnWriteArrayList<SubscriptionGroup> subscriptionGroups;

reactivesocket-aeron/src/main/java/io/reactivesocket/aeron/internal/Constants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public final class Constants {
3939
public static final int SERVER_TIMER_WHEEL_TICK_DURATION_MS = 10;
4040
public static final int SERVER_TIMER_WHEEL_BUCKETS = 128;
4141
public static final int DEFAULT_OFFER_TO_AERON_TIMEOUT_MS = 30_000;
42+
public static final boolean CLIENT_EMBEDDED_AERON_DRIVER = Boolean.getBoolean("reactivesocket.aeron.clientEmbeddedDriver");
4243

4344
static {
4445
String idlStrategy = System.getProperty("idleStrategy");

reactivesocket-aeron/src/main/java/io/reactivesocket/aeron/server/AeronServerDuplexConnection.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,16 @@ public void close() {
107107
publication.close();
108108
} catch (Throwable t) {}
109109
}
110+
111+
public String toString() {
112+
if (publication == null) {
113+
return getClass().getName() + ":publication=null";
114+
}
115+
116+
return getClass().getName() + ":publication=[" +
117+
"channel=" + publication.channel() + "," +
118+
"streamId=" + publication.streamId() + "," +
119+
"sessionId=" + publication.sessionId() + "]";
120+
121+
}
110122
}

reactivesocket-jsr-356/src/main/java/io/reactivesocket/javax/websocket/WebSocketDuplexConnection.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,13 @@ public void onSubscribe(Subscription s) {
8383
public void close() throws IOException {
8484
session.close();
8585
}
86+
87+
public String toString() {
88+
if (session == null) {
89+
return getClass().getName() + ":session=null";
90+
}
91+
92+
return getClass().getName() + ":session=[" + session.toString() + "]";
93+
94+
}
8695
}

reactivesocket-netty/src/main/java/io/reactivesocket/netty/tcp/client/ClientTcpDuplexConnection.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,4 +151,20 @@ public void onComplete() {
151151
public void close() throws IOException {
152152
channel.close();
153153
}
154+
155+
public String toString() {
156+
if (channel == null) {
157+
return getClass().getName() + ":channel=null";
158+
}
159+
160+
return getClass().getName() + ":channel=[" +
161+
"remoteAddress=" + channel.remoteAddress() + "," +
162+
"isActive=" + channel.isActive() + "," +
163+
"isOpen=" + channel.isOpen() + "," +
164+
"isRegistered=" + channel.isRegistered() + "," +
165+
"isWritable=" + channel.isWritable() + "," +
166+
"channelId=" + channel.id().asLongText() +
167+
"]";
168+
169+
}
154170
}

reactivesocket-netty/src/main/java/io/reactivesocket/netty/tcp/server/ServerTcpDuplexConnection.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import io.netty.buffer.ByteBuf;
1919
import io.netty.buffer.Unpooled;
20+
import io.netty.channel.Channel;
2021
import io.netty.channel.ChannelFuture;
2122
import io.netty.channel.ChannelHandlerContext;
2223
import io.reactivesocket.DuplexConnection;
@@ -97,4 +98,21 @@ public void onComplete() {
9798
public void close() throws IOException {
9899

99100
}
101+
102+
public String toString() {
103+
if (ctx ==null || ctx.channel() == null) {
104+
return getClass().getName() + ":channel=null";
105+
}
106+
107+
Channel channel = ctx.channel();
108+
return getClass().getName() + ":channel=[" +
109+
"remoteAddress=" + channel.remoteAddress() + "," +
110+
"isActive=" + channel.isActive() + "," +
111+
"isOpen=" + channel.isOpen() + "," +
112+
"isRegistered=" + channel.isRegistered() + "," +
113+
"isWritable=" + channel.isWritable() + "," +
114+
"channelId=" + channel.id().asLongText() +
115+
"]";
116+
117+
}
100118
}

reactivesocket-netty/src/main/java/io/reactivesocket/netty/websocket/client/ClientWebSocketDuplexConnection.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,4 +166,20 @@ public void onComplete() {
166166
public void close() throws IOException {
167167
channel.close();
168168
}
169+
170+
public String toString() {
171+
if (channel == null) {
172+
return getClass().getName() + ":channel=null";
173+
}
174+
175+
return getClass().getName() + ":channel=[" +
176+
"remoteAddress=" + channel.remoteAddress() + "," +
177+
"isActive=" + channel.isActive() + "," +
178+
"isOpen=" + channel.isOpen() + "," +
179+
"isRegistered=" + channel.isRegistered() + "," +
180+
"isWritable=" + channel.isWritable() + "," +
181+
"channelId=" + channel.id().asLongText() +
182+
"]";
183+
184+
}
169185
}

reactivesocket-netty/src/main/java/io/reactivesocket/netty/websocket/server/ServerWebSocketDuplexConnection.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import io.netty.buffer.ByteBuf;
1919
import io.netty.buffer.Unpooled;
20+
import io.netty.channel.Channel;
2021
import io.netty.channel.ChannelFuture;
2122
import io.netty.channel.ChannelHandlerContext;
2223
import io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame;
@@ -99,4 +100,21 @@ public void onComplete() {
99100
public void close() throws IOException {
100101

101102
}
103+
104+
public String toString() {
105+
if (ctx ==null || ctx.channel() == null) {
106+
return getClass().getName() + ":channel=null";
107+
}
108+
109+
Channel channel = ctx.channel();
110+
return getClass().getName() + ":channel=[" +
111+
"remoteAddress=" + channel.remoteAddress() + "," +
112+
"isActive=" + channel.isActive() + "," +
113+
"isOpen=" + channel.isOpen() + "," +
114+
"isRegistered=" + channel.isRegistered() + "," +
115+
"isWritable=" + channel.isWritable() + "," +
116+
"channelId=" + channel.id().asLongText() +
117+
"]";
118+
119+
}
102120
}

0 commit comments

Comments
 (0)