Skip to content

Commit 047a3c1

Browse files
committed
Cleaner implementation
Added AbstractWebSocket for a cleaner implementation
1 parent ee40a5c commit 047a3c1

File tree

4 files changed

+148
-141
lines changed

4 files changed

+148
-141
lines changed
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
package org.java_websocket;
2+
3+
import org.java_websocket.framing.CloseFrame;
4+
5+
import java.util.Collection;
6+
import java.util.Timer;
7+
import java.util.TimerTask;
8+
9+
10+
/**
11+
* Base class for additional implementations for the server as well as the client
12+
*/
13+
public abstract class AbstractWebSocket extends WebSocketAdapter {
14+
15+
/**
16+
* Attribute which allows you to deactivate the Nagle's algorithm
17+
*/
18+
private boolean tcpNoDelay;
19+
20+
/**
21+
* Attribute for a timer allowing to check for lost connections
22+
*/
23+
private Timer connectionLostTimer;
24+
/**
25+
* Attribute for a timertask allowing to check for lost connections
26+
*/
27+
private TimerTask connectionLostTimerTask;
28+
29+
/**
30+
* Attribute for the lost connection check interval
31+
*/
32+
private int connectionLostTimeout = 60;
33+
34+
/**
35+
* Get the interval checking for lost connections
36+
* Default is 60 seconds
37+
* @return the interval
38+
*/
39+
public int getConnectionLostTimeout() {
40+
return connectionLostTimeout;
41+
}
42+
43+
/**
44+
* Setter for the interval checking for lost connections
45+
* A value >= 0 results in the check to be deactivated
46+
*
47+
* @param connectionLostTimeout the interval in seconds
48+
*/
49+
public void setConnectionLostTimeout( int connectionLostTimeout ) {
50+
this.connectionLostTimeout = connectionLostTimeout;
51+
if (this.connectionLostTimeout <= 0) {
52+
stopConnectionLostTimer();
53+
} else {
54+
startConnectionLostTimer();
55+
}
56+
}
57+
58+
/**
59+
* Stop the connection lost timer
60+
*/
61+
protected void stopConnectionLostTimer() {
62+
if (connectionLostTimer != null ||connectionLostTimerTask != null) {
63+
if( WebSocketImpl.DEBUG )
64+
System.out.println( "Connection lost timer stoped" );
65+
cancelConnectionLostTimer();
66+
}
67+
}
68+
/**
69+
* Start the connection lost timer
70+
*/
71+
protected void startConnectionLostTimer() {
72+
if (this.connectionLostTimeout <= 0) {
73+
if (WebSocketImpl.DEBUG)
74+
System.out.println("Connection lost timer deactivated");
75+
return;
76+
}
77+
if (WebSocketImpl.DEBUG)
78+
System.out.println("Connection lost timer started");
79+
cancelConnectionLostTimer();
80+
connectionLostTimer = new Timer();
81+
connectionLostTimerTask = new TimerTask() {
82+
@Override
83+
public void run() {
84+
Collection<WebSocket> con = connections();
85+
synchronized ( con ) {
86+
long current = (System.currentTimeMillis()-(connectionLostTimeout * 1500));
87+
for( WebSocket conn : con ) {
88+
if (conn instanceof WebSocketImpl) {
89+
if( ((WebSocketImpl)conn).getLastPong() < current ) {
90+
if (WebSocketImpl.DEBUG)
91+
System.out.println("Closing connection due to no pong received: " + conn.toString());
92+
conn.close( CloseFrame.ABNORMAL_CLOSE );
93+
} else {
94+
conn.sendPing();
95+
}
96+
}
97+
}
98+
}
99+
}
100+
};
101+
connectionLostTimer.scheduleAtFixedRate( connectionLostTimerTask,connectionLostTimeout * 1000, connectionLostTimeout * 1000 );
102+
}
103+
104+
/**
105+
* Getter to get all the currently available connections
106+
* @return the currently available connections
107+
*/
108+
protected abstract Collection<WebSocket> connections();
109+
110+
/**
111+
* Cancel any running timer for the connection lost detection
112+
*/
113+
private void cancelConnectionLostTimer() {
114+
if( connectionLostTimer != null ) {
115+
connectionLostTimer.cancel();
116+
connectionLostTimer = null;
117+
}
118+
if( connectionLostTimerTask != null ) {
119+
connectionLostTimerTask.cancel();
120+
connectionLostTimerTask = null;
121+
}
122+
}
123+
124+
/**
125+
* Tests if TCP_NODELAY is enabled.
126+
*
127+
* @return a boolean indicating whether or not TCP_NODELAY is enabled for new connections.
128+
*/
129+
public boolean isTcpNoDelay() {
130+
return tcpNoDelay;
131+
}
132+
133+
/**
134+
* Setter for tcpNoDelay
135+
* <p>
136+
* Enable/disable TCP_NODELAY (disable/enable Nagle's algorithm) for new connections
137+
*
138+
* @param tcpNoDelay true to enable TCP_NODELAY, false to disable.
139+
*/
140+
public void setTcpNoDelay( boolean tcpNoDelay ) {
141+
this.tcpNoDelay = tcpNoDelay;
142+
}
143+
144+
}

src/main/java/org/java_websocket/WebSocketAdapter.java

Lines changed: 0 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -22,136 +22,6 @@
2222
**/
2323
public abstract class WebSocketAdapter implements WebSocketListener {
2424

25-
/**
26-
* Attribute which allows you to deactivate the Nagle's algorithm
27-
*/
28-
private boolean tcpNoDelay;
29-
30-
/**
31-
* Attribute for a timer allowing to check for lost connections
32-
*/
33-
private Timer connectionLostTimer;
34-
/**
35-
* Attribute for a timertask allowing to check for lost connections
36-
*/
37-
private TimerTask connectionLostTimerTask;
38-
39-
/**
40-
* Attribute for the lost connection check interval
41-
*/
42-
private int connectionLostTimeout = 60;
43-
44-
/**
45-
* Get the interval checking for lost connections
46-
* Default is 60 seconds
47-
* @return the interval
48-
*/
49-
public int getConnectionLostTimeout() {
50-
return connectionLostTimeout;
51-
}
52-
53-
/**
54-
* Setter for the interval checking for lost connections
55-
* A value >= 0 results in the check to be deactivated
56-
*
57-
* @param connectionLostTimeout the interval in seconds
58-
*/
59-
public void setConnectionLostTimeout( int connectionLostTimeout ) {
60-
this.connectionLostTimeout = connectionLostTimeout;
61-
if (this.connectionLostTimeout <= 0) {
62-
stopConnectionLostTimer();
63-
} else {
64-
startConnectionLostTimer();
65-
}
66-
}
67-
68-
/**
69-
* Stop the connection lost timer
70-
*/
71-
protected void stopConnectionLostTimer() {
72-
if (connectionLostTimer != null ||connectionLostTimerTask != null) {
73-
if( WebSocketImpl.DEBUG )
74-
System.out.println( "Connection lost timer stoped" );
75-
cancelConnectionLostTimer();
76-
}
77-
}
78-
/**
79-
* Start the connection lost timer
80-
*/
81-
protected void startConnectionLostTimer() {
82-
if (this.connectionLostTimeout <= 0) {
83-
if (WebSocketImpl.DEBUG)
84-
System.out.println("Connection lost timer deactivated");
85-
return;
86-
}
87-
if (WebSocketImpl.DEBUG)
88-
System.out.println("Connection lost timer started");
89-
cancelConnectionLostTimer();
90-
connectionLostTimer = new Timer();
91-
connectionLostTimerTask = new TimerTask() {
92-
@Override
93-
public void run() {
94-
Collection<WebSocket> con = connections();
95-
synchronized ( con ) {
96-
long current = (System.currentTimeMillis()-(connectionLostTimeout * 1500));
97-
for( WebSocket conn : con ) {
98-
if (conn instanceof WebSocketImpl) {
99-
if( ((WebSocketImpl)conn).getLastPong() < current ) {
100-
if (WebSocketImpl.DEBUG)
101-
System.out.println("Closing connection due to no pong received: " + conn.toString());
102-
conn.close( CloseFrame.ABNORMAL_CLOSE );
103-
} else {
104-
conn.sendPing();
105-
}
106-
}
107-
}
108-
}
109-
}
110-
};
111-
connectionLostTimer.scheduleAtFixedRate( connectionLostTimerTask,connectionLostTimeout * 1000, connectionLostTimeout * 1000 );
112-
}
113-
114-
/**
115-
* Getter to get all the currently available connections
116-
* @return the currently available connections
117-
*/
118-
protected abstract Collection<WebSocket> connections();
119-
120-
/**
121-
* Cancel any running timer for the connection lost detection
122-
*/
123-
private void cancelConnectionLostTimer() {
124-
if( connectionLostTimer != null ) {
125-
connectionLostTimer.cancel();
126-
connectionLostTimer = null;
127-
}
128-
if( connectionLostTimerTask != null ) {
129-
connectionLostTimerTask.cancel();
130-
connectionLostTimerTask = null;
131-
}
132-
}
133-
134-
/**
135-
* Tests if TCP_NODELAY is enabled.
136-
*
137-
* @return a boolean indicating whether or not TCP_NODELAY is enabled for new connections.
138-
*/
139-
public boolean isTcpNoDelay() {
140-
return tcpNoDelay;
141-
}
142-
143-
/**
144-
* Setter for tcpNoDelay
145-
* <p>
146-
* Enable/disable TCP_NODELAY (disable/enable Nagle's algorithm) for new connections
147-
*
148-
* @param tcpNoDelay true to enable TCP_NODELAY, false to disable.
149-
*/
150-
public void setTcpNoDelay( boolean tcpNoDelay ) {
151-
this.tcpNoDelay = tcpNoDelay;
152-
}
153-
154-
15525
/**
15626
* This default implementation does not do anything. Go ahead and overwrite it.
15727
*

src/main/java/org/java_websocket/client/WebSocketClient.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import java.util.Map;
1515
import java.util.concurrent.CountDownLatch;
1616

17+
import org.java_websocket.AbstractWebSocket;
1718
import org.java_websocket.WebSocket;
1819
import org.java_websocket.WebSocketAdapter;
1920
import org.java_websocket.WebSocketImpl;
@@ -32,7 +33,7 @@
3233
* A subclass must implement at least <var>onOpen</var>, <var>onClose</var>, and <var>onMessage</var> to be
3334
* useful. At runtime the user is expected to establish a connection via {@link #connect()}, then receive events like {@link #onMessage(String)} via the overloaded methods and to {@link #send(String)} data to the server.
3435
*/
35-
public abstract class WebSocketClient extends WebSocketAdapter implements Runnable, WebSocket {
36+
public abstract class WebSocketClient extends AbstractWebSocket implements Runnable, WebSocket {
3637

3738
/**
3839
* The URI this channel is supposed to connect to.
@@ -61,8 +62,6 @@ public abstract class WebSocketClient extends WebSocketAdapter implements Runnab
6162

6263
private int connectTimeout = 0;
6364

64-
65-
6665
/**
6766
* Constructs a WebSocketClient instance and sets it to the connect to the
6867
* specified URI. The channel does not attampt to connect automatically. The connection

src/main/java/org/java_websocket/server/WebSocketServer.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import java.net.InetSocketAddress;
55
import java.net.ServerSocket;
66
import java.net.Socket;
7-
import java.net.UnknownHostException;
87
import java.nio.ByteBuffer;
98
import java.nio.channels.ByteChannel;
109
import java.nio.channels.CancelledKeyException;
@@ -28,12 +27,7 @@
2827
import java.util.concurrent.atomic.AtomicBoolean;
2928
import java.util.concurrent.atomic.AtomicInteger;
3029

31-
import org.java_websocket.SocketChannelIOHelper;
32-
import org.java_websocket.WebSocket;
33-
import org.java_websocket.WebSocketAdapter;
34-
import org.java_websocket.WebSocketFactory;
35-
import org.java_websocket.WebSocketImpl;
36-
import org.java_websocket.WrappedByteChannel;
30+
import org.java_websocket.*;
3731
import org.java_websocket.drafts.Draft;
3832
import org.java_websocket.exceptions.InvalidDataException;
3933
import org.java_websocket.framing.CloseFrame;
@@ -48,7 +42,7 @@
4842
* functionality/purpose to the server.
4943
*
5044
*/
51-
public abstract class WebSocketServer extends WebSocketAdapter implements Runnable {
45+
public abstract class WebSocketServer extends AbstractWebSocket implements Runnable {
5246

5347
public static int DECODERS = Runtime.getRuntime().availableProcessors();
5448

0 commit comments

Comments
 (0)