Skip to content

Commit 92a6b78

Browse files
committed
Fix threading issues with Continuaitons. This fixes issue 3.
1 parent 1f4fbbf commit 92a6b78

File tree

7 files changed

+164
-140
lines changed

7 files changed

+164
-140
lines changed

src/com/glines/socketio/examples/broadcast/BroadcastSocketServlet.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
import javax.servlet.http.Cookie;
3131
import javax.servlet.http.HttpServletRequest;
3232

33+
import org.eclipse.jetty.util.log.Log;
34+
3335
import com.glines.socketio.common.DisconnectReason;
3436
import com.glines.socketio.server.SocketIOInbound;
3537
import com.glines.socketio.server.SocketIOServlet;
@@ -71,7 +73,7 @@ public void onMessage(int messageType, String message) {
7173
}
7274

7375
private void broadcast(int messageType, String message) {
74-
System.out.println("Broadcasting: " + message);
76+
Log.debug("Broadcasting: " + message);
7577
synchronized (connections) {
7678
for(BroadcastConnection c: connections) {
7779
if (c != this) {

src/com/glines/socketio/examples/chat/ChatSocketServlet.java

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@
2929
import java.util.Set;
3030
import java.util.concurrent.atomic.AtomicInteger;
3131

32-
import javax.servlet.http.Cookie;
3332
import javax.servlet.http.HttpServletRequest;
3433

3534
import org.eclipse.jetty.util.ajax.JSON;
35+
import org.eclipse.jetty.util.log.Log;
3636

3737
import com.glines.socketio.common.DisconnectReason;
3838
import com.glines.socketio.common.SocketIOException;
@@ -85,7 +85,7 @@ public void onDisconnect(DisconnectReason reason, String errorMessage) {
8585

8686
@Override
8787
public void onMessage(int messageType, String message) {
88-
System.out.println("Recieved: " + message);
88+
Log.debug("Recieved: " + message);
8989
if (message.equals("/rclose")) {
9090
outbound.close();
9191
} else if (message.equals("/rdisconnect")) {
@@ -107,6 +107,40 @@ public void onMessage(int messageType, String message) {
107107
} catch (SocketIOException e) {
108108
outbound.disconnect();
109109
}
110+
} else if (message.startsWith("/burst")) {
111+
int burstNum = 10;
112+
String parts[] = message.split("\\s+");
113+
if (parts.length == 2) {
114+
burstNum = Integer.parseInt(parts[1]);
115+
}
116+
try {
117+
for (int i = 0; i < burstNum; i++) {
118+
Log.debug("**************************************** Sending burst message: " + i);
119+
outbound.sendMessage(SocketIOFrame.JSON_MESSAGE_TYPE, JSON.toString(
120+
Collections.singletonMap("message",new String[]{"Server", "Hi " + i +
121+
"0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF" +
122+
"0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF" +
123+
"0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF" +
124+
"0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF" +
125+
"0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF" +
126+
"0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF" +
127+
"0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF" +
128+
"0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF"
129+
})));
130+
// outbound.sendMessage(SocketIOFrame.JSON_MESSAGE_TYPE, JSON.toString(
131+
// Collections.singletonMap("say","Hi " + i)));
132+
try {
133+
Thread.sleep(250);
134+
} catch (InterruptedException e) {
135+
// Do nothing
136+
}
137+
}
138+
} catch (Exception e) {
139+
Log.debug(e);
140+
// } catch (SocketIOException e) {
141+
// Log.debug(e);
142+
// outbound.disconnect();
143+
}
110144
} else {
111145
broadcast(SocketIOFrame.JSON_MESSAGE_TYPE, JSON.toString(
112146
Collections.singletonMap("message",
@@ -115,7 +149,7 @@ public void onMessage(int messageType, String message) {
115149
}
116150

117151
private void broadcast(int messageType, String message) {
118-
System.out.println("Broadcasting: " + message);
152+
Log.debug("Broadcasting: " + message);
119153
synchronized (connections) {
120154
for(ChatConnection c: connections) {
121155
if (c != this) {

src/com/glines/socketio/examples/gwtchat/GWTChatSocketServlet.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import javax.servlet.http.HttpServletRequest;
3434

3535
import org.eclipse.jetty.util.ajax.JSON;
36+
import org.eclipse.jetty.util.log.Log;
3637

3738
import com.glines.socketio.common.DisconnectReason;
3839
import com.glines.socketio.common.SocketIOException;
@@ -84,7 +85,7 @@ public void onDisconnect(DisconnectReason reason, String errorMessage) {
8485

8586
@Override
8687
public void onMessage(int messageType, String message) {
87-
System.out.println("Recieved: " + message);
88+
Log.debug("Recieved: " + message);
8889
if (message.equals("/rclose")) {
8990
outbound.close();
9091
} else if (message.equals("/rdisconnect")) {
@@ -114,7 +115,7 @@ public void onMessage(int messageType, String message) {
114115
}
115116

116117
private void broadcast(int messageType, String message) {
117-
System.out.println("Broadcasting: " + message);
118+
Log.debug("Broadcasting: " + message);
118119
synchronized (connections) {
119120
for(GWTChatConnection c: connections) {
120121
if (c != this) {

src/com/glines/socketio/server/SocketIOSessionManager.java

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
import java.util.concurrent.TimeUnit;
3434
import java.util.concurrent.atomic.AtomicLong;
3535

36+
import org.eclipse.jetty.util.log.Log;
37+
3638
import com.glines.socketio.common.ConnectionState;
3739
import com.glines.socketio.common.DisconnectReason;
3840
import com.glines.socketio.common.SocketIOException;
@@ -101,7 +103,7 @@ public SessionTransportHandler getTransportHandler() {
101103
}
102104

103105
private void onTimeout() {
104-
System.out.println("Session["+sessionId+"]: onTimeout");
106+
Log.debug("Session["+sessionId+"]: onTimeout");
105107
if (!timedout) {
106108
timedout = true;
107109
state = ConnectionState.CLOSED;
@@ -133,10 +135,11 @@ public void clearTimeoutTimer() {
133135

134136
private void sendPing() {
135137
String data = "" + messageId.incrementAndGet();
136-
System.out.println("Session["+sessionId+"]: sendPing " + data);
138+
Log.debug("Session["+sessionId+"]: sendPing " + data);
137139
try {
138140
handler.sendMessage(new SocketIOFrame(SocketIOFrame.FrameType.PING, 0, data));
139141
} catch (SocketIOException e) {
142+
Log.debug("handler.sendMessage failed: ", e);
140143
handler.abort();
141144
}
142145
startTimeoutTimer();
@@ -190,6 +193,7 @@ public void startClose() {
190193
try {
191194
handler.sendMessage(new SocketIOFrame(SocketIOFrame.FrameType.CLOSE, 0, closeId));
192195
} catch (SocketIOException e) {
196+
Log.debug("handler.sendMessage failed: ", e);
193197
handler.abort();
194198
}
195199
}
@@ -202,19 +206,19 @@ public void onMessage(SocketIOFrame message) {
202206
// Ignore these two messages types as they are only intended to be from server to client.
203207
break;
204208
case CLOSE:
205-
System.out.println("Session["+sessionId+"]: onClose: " + message.getData());
209+
Log.debug("Session["+sessionId+"]: onClose: " + message.getData());
206210
onClose(message.getData());
207211
break;
208212
case PING:
209-
System.out.println("Session["+sessionId+"]: onPing: " + message.getData());
213+
Log.debug("Session["+sessionId+"]: onPing: " + message.getData());
210214
onPing(message.getData());
211215
break;
212216
case PONG:
213-
System.out.println("Session["+sessionId+"]: onPong: " + message.getData());
217+
Log.debug("Session["+sessionId+"]: onPong: " + message.getData());
214218
onPong(message.getData());
215219
break;
216220
case DATA:
217-
System.out.println("Session["+sessionId+"]: onMessage: " + message.getData());
221+
Log.debug("Session["+sessionId+"]: onMessage: " + message.getData());
218222
onMessage(message.getData());
219223
break;
220224
default:
@@ -228,6 +232,7 @@ public void onPing(String data) {
228232
try {
229233
handler.sendMessage(new SocketIOFrame(SocketIOFrame.FrameType.PONG, 0, data));
230234
} catch (SocketIOException e) {
235+
Log.debug("handler.sendMessage failed: ", e);
231236
handler.abort();
232237
}
233238
}
@@ -248,6 +253,7 @@ public void onClose(String data) {
248253
try {
249254
handler.sendMessage(new SocketIOFrame(SocketIOFrame.FrameType.CLOSE, 0, data));
250255
} catch (SocketIOException e) {
256+
Log.debug("handler.sendMessage failed: ", e);
251257
handler.abort();
252258
}
253259
}
@@ -257,6 +263,7 @@ public void onClose(String data) {
257263
handler.sendMessage(new SocketIOFrame(SocketIOFrame.FrameType.CLOSE, 0, data));
258264
handler.disconnectWhenEmpty();
259265
} catch (SocketIOException e) {
266+
Log.debug("handler.sendMessage failed: ", e);
260267
handler.abort();
261268
}
262269
}
@@ -285,6 +292,7 @@ public void onConnect(SessionTransportHandler handler) {
285292
state = ConnectionState.CONNECTED;
286293
inbound.onConnect(handler);
287294
} catch (Throwable e) {
295+
Log.warn("Session["+sessionId+"]: Exception thrown by SocketIOInbound.onConnect()", e);
288296
state = ConnectionState.CLOSED;
289297
handler.abort();
290298
}
@@ -299,30 +307,30 @@ public void onMessage(String message) {
299307
try {
300308
inbound.onMessage(SocketIOFrame.TEXT_MESSAGE_TYPE, message);
301309
} catch (Throwable e) {
302-
// Ignore
310+
Log.warn("Session["+sessionId+"]: Exception thrown by SocketIOInbound.onMessage()", e);
303311
}
304312
}
305313
}
306314

307315
@Override
308316
public void onDisconnect(DisconnectReason reason) {
309-
System.out.println("Session["+sessionId+"]: onDisconnect: " + reason);
317+
Log.debug("Session["+sessionId+"]: onDisconnect: " + reason);
310318
clearTimeoutTimer();
311319
clearHeartbeatTimer();
312320
if (inbound != null) {
313321
state = ConnectionState.CLOSED;
314322
try {
315323
inbound.onDisconnect(reason, null);
316324
} catch (Throwable e) {
317-
// Ignore
325+
Log.warn("Session["+sessionId+"]: Exception thrown by SocketIOInbound.onDisconnect()", e);
318326
}
319327
inbound = null;
320328
}
321329
}
322330

323331
@Override
324332
public void onShutdown() {
325-
System.out.println("Session["+sessionId+"]: onShutdown");
333+
Log.debug("Session["+sessionId+"]: onShutdown");
326334
if (inbound != null) {
327335
if (state == ConnectionState.CLOSING) {
328336
if (closeId != null) {

src/com/glines/socketio/server/transport/WebSocketTransport.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import javax.servlet.http.HttpServletRequest;
3131
import javax.servlet.http.HttpServletResponse;
3232

33+
import org.eclipse.jetty.util.log.Log;
3334
import org.eclipse.jetty.websocket.WebSocket;
3435
import org.eclipse.jetty.websocket.WebSocketFactory;
3536

@@ -147,7 +148,7 @@ public ConnectionState getConnectionState() {
147148
@Override
148149
public void sendMessage(SocketIOFrame frame) throws SocketIOException {
149150
if (outbound.isOpen()) {
150-
System.out.println("Session["+session.getSessionId()+"]: sendMessage: [" + frame.getFrameType() + "]: " + frame.getData());
151+
Log.debug("Session["+session.getSessionId()+"]: sendMessage: [" + frame.getFrameType() + "]: " + frame.getData());
151152
try {
152153
outbound.sendMessage(frame.encode());
153154
} catch (IOException e) {

src/com/glines/socketio/server/transport/XHRMultipartTransport.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
import javax.servlet.http.HttpServletRequest;
3232
import javax.servlet.http.HttpServletResponse;
3333

34+
import org.eclipse.jetty.util.log.Log;
35+
3436
import com.glines.socketio.server.SocketIOFrame;
3537
import com.glines.socketio.server.SocketIOSession;
3638
import com.glines.socketio.server.transport.ConnectionTimeoutPreventor.IdleCheck;
@@ -65,14 +67,14 @@ protected void startSend(HttpServletResponse response) throws IOException {
6567

6668
protected void writeData(ServletResponse response, String data) throws IOException {
6769
idleCheck.activity();
68-
System.out.println("Session["+session.getSessionId()+"]: writeData(START): " + data);
70+
Log.debug("Session["+session.getSessionId()+"]: writeData(START): " + data);
6971
ServletOutputStream os = response.getOutputStream();
7072
os.println("Content-Type: text/plain");
7173
os.println();
7274
os.println(data);
7375
os.println(boundarySeperator);
7476
response.flushBuffer();
75-
System.out.println("Session["+session.getSessionId()+"]: writeData(END): " + data);
77+
Log.debug("Session["+session.getSessionId()+"]: writeData(END): " + data);
7678
}
7779

7880
protected void finishSend(ServletResponse response) throws IOException {

0 commit comments

Comments
 (0)