|
1 | 1 | package org.java_websocket; |
2 | 2 |
|
3 | | -import java.net.InetSocketAddress; |
4 | | - |
5 | 3 | import org.java_websocket.drafts.Draft; |
6 | 4 | import org.java_websocket.exceptions.InvalidDataException; |
7 | 5 | import org.java_websocket.exceptions.InvalidHandshakeException; |
| 6 | +import org.java_websocket.framing.CloseFrame; |
8 | 7 | import org.java_websocket.framing.Framedata; |
9 | 8 | import org.java_websocket.framing.Framedata.Opcode; |
10 | 9 | import org.java_websocket.framing.FramedataImpl1; |
|
13 | 12 | import org.java_websocket.handshake.ServerHandshake; |
14 | 13 | import org.java_websocket.handshake.ServerHandshakeBuilder; |
15 | 14 |
|
| 15 | +import java.net.InetSocketAddress; |
| 16 | +import java.util.Collection; |
| 17 | +import java.util.Timer; |
| 18 | +import java.util.TimerTask; |
| 19 | + |
16 | 20 | /** |
17 | 21 | * This class default implements all methods of the WebSocketListener that can be overridden optionally when advances functionalities is needed.<br> |
18 | 22 | **/ |
19 | 23 | public abstract class WebSocketAdapter implements WebSocketListener { |
20 | 24 |
|
| 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 | + for (WebSocket conn: connections()) { |
| 95 | + conn.sendPing(); |
| 96 | + } |
| 97 | + } |
| 98 | + }; |
| 99 | + connectionLostTimer.scheduleAtFixedRate( connectionLostTimerTask,connectionLostTimeout * 1000, connectionLostTimeout * 1000 ); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Getter to get all the currently available connections |
| 104 | + * @return the currently available connections |
| 105 | + */ |
| 106 | + protected abstract Collection<WebSocket> connections(); |
| 107 | + |
| 108 | + /** |
| 109 | + * Cancel any running timer for the connection lost detection |
| 110 | + */ |
| 111 | + private void cancelConnectionLostTimer() { |
| 112 | + if( connectionLostTimer != null ) { |
| 113 | + connectionLostTimer.cancel(); |
| 114 | + connectionLostTimer = null; |
| 115 | + } |
| 116 | + if( connectionLostTimerTask != null ) { |
| 117 | + connectionLostTimerTask.cancel(); |
| 118 | + connectionLostTimerTask = null; |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Tests if TCP_NODELAY is enabled. |
| 124 | + * |
| 125 | + * @return a boolean indicating whether or not TCP_NODELAY is enabled for new connections. |
| 126 | + */ |
| 127 | + public boolean isTcpNoDelay() { |
| 128 | + return tcpNoDelay; |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Setter for tcpNoDelay |
| 133 | + * <p> |
| 134 | + * Enable/disable TCP_NODELAY (disable/enable Nagle's algorithm) for new connections |
| 135 | + * |
| 136 | + * @param tcpNoDelay true to enable TCP_NODELAY, false to disable. |
| 137 | + */ |
| 138 | + public void setTcpNoDelay( boolean tcpNoDelay ) { |
| 139 | + this.tcpNoDelay = tcpNoDelay; |
| 140 | + } |
| 141 | + |
| 142 | + |
21 | 143 | /** |
22 | 144 | * This default implementation does not do anything. Go ahead and overwrite it. |
23 | 145 | * |
@@ -75,25 +197,26 @@ public void onWebsocketPong( WebSocket conn, Framedata f ) { |
75 | 197 | /** |
76 | 198 | * Gets the XML string that should be returned if a client requests a Flash |
77 | 199 | * security policy. |
78 | | - * |
| 200 | + * <p> |
79 | 201 | * The default implementation allows access from all remote domains, but |
80 | 202 | * only on the port that this WebSocketServer is listening on. |
81 | | - * |
| 203 | + * <p> |
82 | 204 | * This is specifically implemented for gitime's WebSocket client for Flash: |
83 | 205 | * http://github.com/gimite/web-socket-js |
84 | | - * |
| 206 | + * |
85 | 207 | * @return An XML String that comforts to Flash's security policy. You MUST |
86 | | - * not include the null char at the end, it is appended automatically. |
| 208 | + * not include the null char at the end, it is appended automatically. |
87 | 209 | * @throws InvalidDataException thrown when some data that is required to generate the flash-policy like the websocket local port could not be obtained e.g because the websocket is not connected. |
88 | 210 | */ |
89 | 211 | @Override |
90 | 212 | public String getFlashPolicy( WebSocket conn ) throws InvalidDataException { |
91 | 213 | InetSocketAddress adr = conn.getLocalSocketAddress(); |
92 | | - if(null == adr){ |
| 214 | + if( null == adr ) { |
93 | 215 | throw new InvalidHandshakeException( "socket not bound" ); |
94 | 216 | } |
95 | 217 |
|
96 | | - return "<cross-domain-policy><allow-access-from domain=\"*\" to-ports=\"" + adr.getPort() +"\" /></cross-domain-policy>\0"; |
| 218 | + return "<cross-domain-policy><allow-access-from domain=\"*\" to-ports=\"" + adr.getPort() + "\" /></cross-domain-policy>\0"; |
97 | 219 | } |
98 | 220 |
|
| 221 | + |
99 | 222 | } |
0 commit comments