|
| 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 | +} |
0 commit comments