Skip to content

Commit b0062cc

Browse files
committed
Revert changes made to DummyStatsDServer files
1 parent 0e271e4 commit b0062cc

File tree

2 files changed

+31
-79
lines changed

2 files changed

+31
-79
lines changed

src/test/java/com/timgroup/statsd/UnixDatagramSocketDummyStatsDServer.java

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,54 +3,30 @@
33
import java.io.IOException;
44
import java.nio.ByteBuffer;
55
import java.nio.channels.DatagramChannel;
6-
import java.net.SocketAddress;
76
import jnr.unixsocket.UnixDatagramChannel;
87
import jnr.unixsocket.UnixSocketAddress;
98

109
import static com.timgroup.statsd.NonBlockingStatsDClient.DEFAULT_UDS_MAX_PACKET_SIZE_BYTES;
1110

1211
public class UnixDatagramSocketDummyStatsDServer extends DummyStatsDServer {
1312
private final DatagramChannel server;
14-
private volatile boolean serverRunning = true;
1513

1614
public UnixDatagramSocketDummyStatsDServer(String socketPath) throws IOException {
17-
if (ClientChannelUtils.hasNativeUdsSupport()) {
18-
try {
19-
Class<?> udsAddressClass = Class.forName("java.net.UnixDomainSocketAddress");
20-
Object udsAddress = udsAddressClass.getMethod("of", String.class).invoke(null, socketPath);
21-
22-
DatagramChannel nativeServer = DatagramChannel.open();
23-
nativeServer.bind((SocketAddress) udsAddress);
24-
this.server = nativeServer;
25-
} catch (ReflectiveOperationException e) {
26-
throw new IOException(e);
27-
}
28-
} else {
29-
UnixDatagramChannel jnrServer = UnixDatagramChannel.open();
30-
jnrServer.bind(new UnixSocketAddress(socketPath));
31-
this.server = jnrServer;
32-
}
15+
server = UnixDatagramChannel.open();
16+
server.bind(new UnixSocketAddress(socketPath));
3317
this.listen();
3418
}
3519

3620
@Override
3721
protected boolean isOpen() {
38-
return serverRunning && server.isOpen();
22+
return server.isOpen();
3923
}
4024

4125
protected void receive(ByteBuffer packet) throws IOException {
4226
server.receive(packet);
4327
}
4428

45-
@Override
4629
public void close() throws IOException {
47-
serverRunning = false;
48-
try {
49-
// Give the listening thread a chance to stop
50-
Thread.sleep(1000);
51-
} catch (InterruptedException e) {
52-
// ignore
53-
}
5430
try {
5531
server.close();
5632
} catch (Exception e) {

src/test/java/com/timgroup/statsd/UnixStreamSocketDummyStatsDServer.java

Lines changed: 28 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44
import java.nio.Buffer;
55
import java.nio.ByteBuffer;
66
import java.nio.ByteOrder;
7-
import java.nio.channels.ServerSocketChannel;
87
import java.nio.channels.SocketChannel;
9-
import java.net.SocketAddress;
108
import java.util.concurrent.ConcurrentLinkedQueue;
119
import java.util.logging.Logger;
1210
import jnr.unixsocket.UnixServerSocketChannel;
@@ -16,37 +14,21 @@
1614
import static com.timgroup.statsd.NonBlockingStatsDClient.DEFAULT_UDS_MAX_PACKET_SIZE_BYTES;
1715

1816
public class UnixStreamSocketDummyStatsDServer extends DummyStatsDServer {
19-
private final Object server; // Object is either ServerSocketChannel or UnixServerSocketChannel
20-
private final ConcurrentLinkedQueue<SocketChannel> channels = new ConcurrentLinkedQueue<>();
21-
private final boolean useNativeUds;
17+
private final UnixServerSocketChannel server;
18+
private final ConcurrentLinkedQueue<UnixSocketChannel> channels = new ConcurrentLinkedQueue<>();
2219

2320
private final Logger logger = Logger.getLogger(UnixStreamSocketDummyStatsDServer.class.getName());
2421

2522
public UnixStreamSocketDummyStatsDServer(String socketPath) throws IOException {
26-
this.useNativeUds = ClientChannelUtils.hasNativeUdsSupport();
27-
if (useNativeUds) {
28-
try {
29-
Class<?> udsAddressClass = Class.forName("java.net.UnixDomainSocketAddress");
30-
Object udsAddress = udsAddressClass.getMethod("of", String.class).invoke(null, socketPath);
31-
32-
ServerSocketChannel nativeServer = ServerSocketChannel.open();
33-
nativeServer.bind((SocketAddress) udsAddress);
34-
this.server = nativeServer;
35-
} catch (ReflectiveOperationException e) {
36-
throw new IOException(e);
37-
}
38-
} else {
39-
UnixServerSocketChannel jnrServer = UnixServerSocketChannel.open();
40-
jnrServer.configureBlocking(true);
41-
jnrServer.socket().bind(new UnixSocketAddress(socketPath));
42-
this.server = jnrServer;
43-
}
23+
server = UnixServerSocketChannel.open();
24+
server.configureBlocking(true);
25+
server.socket().bind(new UnixSocketAddress(socketPath));
4426
this.listen();
4527
}
4628

4729
@Override
4830
protected boolean isOpen() {
49-
return useNativeUds ? ((ServerSocketChannel)server).isOpen() : ((UnixServerSocketChannel)server).isOpen();
31+
return server.isOpen();
5032
}
5133

5234
@Override
@@ -56,43 +38,39 @@ protected void receive(ByteBuffer packet) throws IOException {
5638

5739
@Override
5840
protected void listen() {
59-
try {
60-
String localAddressMessage = useNativeUds ? "Listening on " + ((ServerSocketChannel)server).getLocalAddress() : "Listening on " + ((UnixServerSocketChannel)server).getLocalSocketAddress();
61-
logger.info(localAddressMessage);
62-
} catch (Exception e) {
63-
logger.warning("Failed to get local address: " + e);
64-
}
41+
logger.info("Listening on " + server.getLocalSocketAddress());
6542
Thread thread = new Thread(new Runnable() {
6643
@Override
6744
public void run() {
6845
while(isOpen()) {
6946
if (sleepIfFrozen()) {
7047
continue;
7148
}
72-
try {
73-
logger.info("Waiting for connection");
74-
SocketChannel clientChannel = null;
75-
clientChannel = useNativeUds ? ((ServerSocketChannel)server).accept() : ((UnixServerSocketChannel)server).accept();
76-
if (clientChannel != null) {
77-
clientChannel.configureBlocking(true);
78-
String connectionMessage = useNativeUds ? "Accepted connection from " + clientChannel.getRemoteAddress() : "Accepted connection from " + ((UnixSocketChannel)clientChannel).getRemoteSocketAddress();
79-
logger.info(connectionMessage);
80-
channels.add(clientChannel);
81-
readChannel(clientChannel);
49+
try {
50+
logger.info("Waiting for connection");
51+
UnixSocketChannel clientChannel = server.accept();
52+
if (clientChannel != null) {
53+
clientChannel.configureBlocking(true);
54+
try {
55+
logger.info("Accepted connection from " + clientChannel.getRemoteSocketAddress());
56+
} catch (Exception e) {
57+
logger.warning("Failed to get remote socket address");
58+
}
59+
channels.add(clientChannel);
60+
readChannel(clientChannel);
61+
}
62+
} catch (IOException e) {
8263
}
83-
} catch (Exception e) {
84-
// ignore
85-
}
8664
}
8765
}
8866
});
8967
thread.setDaemon(true);
9068
thread.start();
9169
}
9270

93-
public void readChannel(final SocketChannel clientChannel) {
94-
logger.info("Reading from " + clientChannel);
95-
Thread thread = new Thread(new Runnable() {
71+
public void readChannel(final UnixSocketChannel clientChannel) {
72+
logger.info("Reading from " + clientChannel);
73+
Thread thread = new Thread(new Runnable() {
9674
@Override
9775
public void run() {
9876
final ByteBuffer packet = ByteBuffer.allocate(DEFAULT_UDS_MAX_PACKET_SIZE_BYTES);
@@ -112,6 +90,7 @@ public void run() {
11290
logger.warning("Failed to close channel: " + e);
11391
}
11492
}
93+
11594
}
11695
logger.info("Disconnected from " + clientChannel);
11796
}
@@ -149,16 +128,13 @@ private boolean readPacket(SocketChannel channel, ByteBuffer packet) {
149128

150129
public void close() throws IOException {
151130
try {
152-
if (useNativeUds) {
153-
((ServerSocketChannel)server).close();
154-
} else {
155-
((UnixServerSocketChannel)server).close();
156-
}
157-
for (SocketChannel channel : channels) {
131+
server.close();
132+
for (UnixSocketChannel channel : channels) {
158133
channel.close();
159134
}
160135
} catch (Exception e) {
161136
//ignore
162137
}
163138
}
139+
164140
}

0 commit comments

Comments
 (0)