Fixed several issues related to the code quality · java-codehunger/Java-WebSocket@4094e3d · GitHub
Skip to content

Commit 4094e3d

Browse files
committed
Fixed several issues related to the code quality
Extended tests Less complexity in some methods
1 parent 64b7574 commit 4094e3d

33 files changed

Lines changed: 1022 additions & 292 deletions

src/main/example/ServerStressTest.java

Lines changed: 2 additions & 2 deletions

src/main/java/org/java_websocket/AbstractWebSocket.java

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -169,21 +169,8 @@ public void run() {
169169
try {
170170
connections.addAll( getConnections() );
171171
long current = ( System.currentTimeMillis() - ( connectionLostTimeout * 1500 ) );
172-
WebSocketImpl webSocketImpl;
173172
for( WebSocket conn : connections ) {
174-
if( conn instanceof WebSocketImpl ) {
175-
webSocketImpl = ( WebSocketImpl ) conn;
176-
if( webSocketImpl.getLastPong() < current ) {
177-
log.trace("Closing connection due to no pong received: {}", conn);
178-
webSocketImpl.closeConnection( CloseFrame.ABNORMAL_CLOSE, "The connection was closed because the other endpoint did not respond with a pong in time. For more information check: https://github.com/TooTallNate/Java-WebSocket/wiki/Lost-connection-detection" );
179-
} else {
180-
if( webSocketImpl.isOpen() ) {
181-
webSocketImpl.sendPing();
182-
} else {
183-
log.trace("Trying to ping a non open connection: {}", conn);
184-
}
185-
}
186-
}
173+
executeConnectionLostDetection(conn, current);
187174
}
188175
} catch ( Exception e ) {
189176
//Ignore this exception
@@ -195,6 +182,28 @@ public void run() {
195182

196183
}
197184

185+
/**
186+
* Send a ping to the endpoint or close the connection since the other endpoint did not respond with a ping
187+
* @param webSocket the websocket instance
188+
* @param current the current time in milliseconds
189+
*/
190+
private void executeConnectionLostDetection(WebSocket webSocket, long current) {
191+
if (!(webSocket instanceof WebSocketImpl)) {
192+
return;
193+
}
194+
WebSocketImpl webSocketImpl = (WebSocketImpl) webSocket;
195+
if( webSocketImpl.getLastPong() < current ) {
196+
log.trace("Closing connection due to no pong received: {}", webSocketImpl);
197+
webSocketImpl.closeConnection( CloseFrame.ABNORMAL_CLOSE, "The connection was closed because the other endpoint did not respond with a pong in time. For more information check: https://github.com/TooTallNate/Java-WebSocket/wiki/Lost-connection-detection" );
198+
} else {
199+
if( webSocketImpl.isOpen() ) {
200+
webSocketImpl.sendPing();
201+
} else {
202+
log.trace("Trying to ping a non open connection: {}", webSocketImpl);
203+
}
204+
}
205+
}
206+
198207
/**
199208
* Getter to get all the currently available connections
200209
* @return the currently available connections

src/main/java/org/java_websocket/AbstractWrappedByteChannel.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,26 @@
3030
import java.nio.channels.ByteChannel;
3131
import java.nio.channels.SocketChannel;
3232

33+
/*
34+
* @deprecated
35+
*/
3336
@Deprecated
3437
public class AbstractWrappedByteChannel implements WrappedByteChannel {
3538

3639
private final ByteChannel channel;
3740

41+
/*
42+
* @deprecated
43+
*/
44+
@Deprecated
3845
public AbstractWrappedByteChannel( ByteChannel towrap ) {
3946
this.channel = towrap;
4047
}
4148

49+
/*
50+
* @deprecated
51+
*/
52+
@Deprecated
4253
public AbstractWrappedByteChannel( WrappedByteChannel towrap ) {
4354
this.channel = towrap;
4455
}

src/main/java/org/java_websocket/SSLSocketChannel2.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,17 +115,14 @@ public SSLSocketChannel2( SocketChannel channel , SSLEngine sslEngine , Executor
115115

116116
private void consumeFutureUninterruptible( Future<?> f ) {
117117
try {
118-
boolean interrupted = false;
119118
while ( true ) {
120119
try {
121120
f.get();
122121
break;
123122
} catch ( InterruptedException e ) {
124-
interrupted = true;
123+
Thread.currentThread().interrupt();
125124
}
126125
}
127-
if( interrupted )
128-
Thread.currentThread().interrupt();
129126
} catch ( ExecutionException e ) {
130127
throw new RuntimeException( e );
131128
}

src/main/java/org/java_websocket/WebSocket.java

Lines changed: 6 additions & 20 deletions

0 commit comments

Comments
 (0)