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

+1022
-292
lines changed

src/main/example/ServerStressTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import java.awt.event.ActionListener;
3030
import java.net.URI;
3131
import java.net.URISyntaxException;
32-
import java.nio.channels.NotYetConnectedException;
3332
import java.util.Collections;
3433
import java.util.LinkedList;
3534
import java.util.List;
@@ -47,6 +46,7 @@
4746
import javax.swing.event.ChangeListener;
4847

4948
import org.java_websocket.client.WebSocketClient;
49+
import org.java_websocket.exceptions.WebsocketNotConnectedException;
5050

5151
public class ServerStressTest extends JFrame {
5252
private JSlider clients;
@@ -225,7 +225,7 @@ public void send() {
225225
for( WebSocketClient cl : websockets ) {
226226
try {
227227
cl.send( payload );
228-
} catch ( NotYetConnectedException e ) {
228+
} catch ( WebsocketNotConnectedException e ) {
229229
notyetconnected++;
230230
}
231231
}

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
Original file line numberDiff line numberDiff line change
@@ -27,30 +27,16 @@
2727

2828
import java.net.InetSocketAddress;
2929
import java.nio.ByteBuffer;
30-
import java.nio.channels.NotYetConnectedException;
3130
import java.util.Collection;
3231

3332
import org.java_websocket.drafts.Draft;
3433
import org.java_websocket.enums.Opcode;
3534
import org.java_websocket.enums.ReadyState;
35+
import org.java_websocket.exceptions.WebsocketNotConnectedException;
3636
import org.java_websocket.framing.Framedata;
3737

3838
public interface WebSocket {
3939

40-
/**
41-
* The default port of WebSockets, as defined in the spec. If the nullary
42-
* constructor is used, DEFAULT_PORT will be the port the WebSocketServer
43-
* is binded to. Note that ports under 1024 usually require root permissions.
44-
*/
45-
int DEFAULT_PORT = 80;
46-
47-
/**
48-
* The default wss port of WebSockets, as defined in the spec. If the nullary
49-
* constructor is used, DEFAULT_WSS_PORT will be the port the WebSocketServer
50-
* is binded to. Note that ports under 1024 usually require root permissions.
51-
*/
52-
int DEFAULT_WSS_PORT = 443;
53-
5440
/**
5541
* sends the closing handshake.
5642
* may be send in response to an other handshake.
@@ -81,7 +67,7 @@ public interface WebSocket {
8167
* Send Text data to the other end.
8268
*
8369
* @param text the text data to send
84-
* @throws NotYetConnectedException websocket is not yet connected
70+
* @throws WebsocketNotConnectedException websocket is not yet connected
8571
*/
8672
void send( String text );
8773

@@ -90,7 +76,7 @@ public interface WebSocket {
9076
*
9177
* @param bytes the binary data to send
9278
* @throws IllegalArgumentException the data is null
93-
* @throws NotYetConnectedException websocket is not yet connected
79+
* @throws WebsocketNotConnectedException websocket is not yet connected
9480
*/
9581
void send( ByteBuffer bytes );
9682

@@ -99,7 +85,7 @@ public interface WebSocket {
9985
*
10086
* @param bytes the byte array to send
10187
* @throws IllegalArgumentException the data is null
102-
* @throws NotYetConnectedException websocket is not yet connected
88+
* @throws WebsocketNotConnectedException websocket is not yet connected
10389
*/
10490
void send( byte[] bytes );
10591

@@ -117,9 +103,9 @@ public interface WebSocket {
117103

118104
/**
119105
* Send a ping to the other end
120-
* @throws NotYetConnectedException websocket is not yet connected
106+
* @throws WebsocketNotConnectedException websocket is not yet connected
121107
*/
122-
void sendPing() throws NotYetConnectedException;
108+
void sendPing();
123109

124110
/**
125111
* Allows to send continuous/fragmented frames conveniently. <br>

0 commit comments

Comments
 (0)