Skip to content

Commit ddb01db

Browse files
committed
Rearranging source code, removing DUMMY_CALLBACK. When we receive data from a namespace we're not listening on, an error is thrown.
1 parent ec34856 commit ddb01db

File tree

1 file changed

+107
-140
lines changed

1 file changed

+107
-140
lines changed

src/io/socket/IOConnection.java

Lines changed: 107 additions & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -195,39 +195,6 @@ public void run() {
195195
connectTransport();
196196
}
197197

198-
/**
199-
* Handshake.
200-
*
201-
*/
202-
private void handshake() {
203-
URL url;
204-
String response;
205-
URLConnection connection;
206-
try {
207-
setState(STATE_HANDSHAKE);
208-
url = new URL(IOConnection.this.url.toString() + SOCKET_IO_1);
209-
connection = url.openConnection();
210-
connection.setConnectTimeout(connectTimeout);
211-
connection.setReadTimeout(connectTimeout);
212-
213-
/* Setting the request headers */
214-
for (Entry<Object, Object> entry : headers.entrySet()) {
215-
connection.setRequestProperty((String) entry.getKey(),
216-
(String) entry.getValue());
217-
}
218-
219-
InputStream stream = connection.getInputStream();
220-
Scanner in = new Scanner(stream);
221-
response = in.nextLine();
222-
String[] data = response.split(":");
223-
sessionId = data[0];
224-
heartbeatTimeout = Long.parseLong(data[1]) * 1000;
225-
closingTimeout = Long.parseLong(data[2]) * 1000;
226-
protocols = Arrays.asList(data[3].split(","));
227-
} catch (Exception e) {
228-
error(new SocketIOException("Error while handshaking", e));
229-
}
230-
}
231198

232199
};
233200

@@ -257,6 +224,77 @@ static public IOConnection register(String origin, SocketIO socket) {
257224
return connection;
258225
}
259226

227+
/**
228+
* Connects a socket to the IOConnection.
229+
*
230+
* @param socket
231+
* the socket to be connected
232+
* @return true, if successfully registered on this transport, otherwise
233+
* false.
234+
*/
235+
public boolean register(SocketIO socket) {
236+
String namespace = socket.getNamespace();
237+
if (sockets.containsKey(namespace))
238+
return false;
239+
sockets.put(namespace, socket);
240+
socket.setHeaders(headers);
241+
IOMessage connect = new IOMessage(IOMessage.TYPE_CONNECT,
242+
socket.getNamespace(), "");
243+
sendPlain(connect.toString());
244+
return true;
245+
}
246+
247+
/**
248+
* Disconnect a socket from the IOConnection. Shuts down this IOConnection
249+
* if no further connections are available for this IOConnection.
250+
*
251+
* @param socket
252+
* the socket to be shut down
253+
*/
254+
public void unregister(SocketIO socket) {
255+
sendPlain("0::" + socket.getNamespace());
256+
sockets.remove(socket.getNamespace());
257+
socket.getCallback().onDisconnect();
258+
259+
if (sockets.size() == 0) {
260+
cleanup();
261+
}
262+
}
263+
264+
/**
265+
* Handshake.
266+
*
267+
*/
268+
private void handshake() {
269+
URL url;
270+
String response;
271+
URLConnection connection;
272+
try {
273+
setState(STATE_HANDSHAKE);
274+
url = new URL(IOConnection.this.url.toString() + SOCKET_IO_1);
275+
connection = url.openConnection();
276+
connection.setConnectTimeout(connectTimeout);
277+
connection.setReadTimeout(connectTimeout);
278+
279+
/* Setting the request headers */
280+
for (Entry<Object, Object> entry : headers.entrySet()) {
281+
connection.setRequestProperty((String) entry.getKey(),
282+
(String) entry.getValue());
283+
}
284+
285+
InputStream stream = connection.getInputStream();
286+
Scanner in = new Scanner(stream);
287+
response = in.nextLine();
288+
String[] data = response.split(":");
289+
sessionId = data[0];
290+
heartbeatTimeout = Long.parseLong(data[1]) * 1000;
291+
closingTimeout = Long.parseLong(data[2]) * 1000;
292+
protocols = Arrays.asList(data[3].split(","));
293+
} catch (Exception e) {
294+
error(new SocketIOException("Error while handshaking", e));
295+
}
296+
}
297+
260298
/**
261299
* Connect transport.
262300
*/
@@ -350,39 +388,6 @@ private IOConnection(String url, SocketIO socket) {
350388
new ConnectThread().start();
351389
}
352390

353-
/**
354-
* Populates an error to the connected {@link IOCallback}s and shuts down.
355-
*
356-
* @param e
357-
* an exception
358-
*/
359-
protected void error(SocketIOException e) {
360-
for (SocketIO socket : sockets.values()) {
361-
socket.getCallback().onError(e);
362-
}
363-
cleanup();
364-
}
365-
366-
/**
367-
* Connects a socket to the IOConnection.
368-
*
369-
* @param socket
370-
* the socket to be connected
371-
* @return true, if successfully registered on this transport, otherwise
372-
* false.
373-
*/
374-
public boolean register(SocketIO socket) {
375-
String namespace = socket.getNamespace();
376-
if (sockets.containsKey(namespace))
377-
return false;
378-
sockets.put(namespace, socket);
379-
socket.setHeaders(headers);
380-
IOMessage connect = new IOMessage(IOMessage.TYPE_CONNECT,
381-
socket.getNamespace(), "");
382-
sendPlain(connect.toString());
383-
return true;
384-
}
385-
386391
/**
387392
* Cleanup. IOConnection is not usable after this calling this.
388393
*/
@@ -403,20 +408,16 @@ private void cleanup() {
403408
}
404409

405410
/**
406-
* Disconnect a socket from the IOConnection. Shuts down this IOConnection
407-
* if no further connections are available for this IOConnection.
411+
* Populates an error to the connected {@link IOCallback}s and shuts down.
408412
*
409-
* @param socket
410-
* the socket to be shut down
413+
* @param e
414+
* an exception
411415
*/
412-
public void unregister(SocketIO socket) {
413-
sendPlain("0::" + socket.getNamespace());
414-
sockets.remove(socket.getNamespace());
415-
socket.getCallback().onDisconnect();
416-
417-
if (sockets.size() == 0) {
418-
cleanup();
416+
private void error(SocketIOException e) {
417+
for (SocketIO socket : sockets.values()) {
418+
socket.getCallback().onError(e);
419419
}
420+
cleanup();
420421
}
421422

422423
/**
@@ -462,6 +463,26 @@ private void resetTimeout() {
462463
+ heartbeatTimeout);
463464
}
464465

466+
/**
467+
* finds the corresponding callback object to an incoming message. Returns a
468+
* dummy callback if no corresponding callback can be found
469+
*
470+
* @param message
471+
* the message
472+
* @return the iO callback
473+
* @throws SocketIOException
474+
*/
475+
private IOCallback findCallback(IOMessage message) throws SocketIOException {
476+
if("".equals(message.getEndpoint()))
477+
return this;
478+
SocketIO socket = sockets.get(message.getEndpoint());
479+
if (socket == null) {
480+
throw new SocketIOException("Cannot find socket for '" + message.getEndpoint()
481+
+ "'");
482+
}
483+
return socket.getCallback();
484+
}
485+
465486
/**
466487
* Transport connected.
467488
*
@@ -632,7 +653,7 @@ public void transportMessage(String text) {
632653
case IOMessage.TYPE_EVENT:
633654
try {
634655
JSONObject event = new JSONObject(message.getData());
635-
Object[] argsArray = empty;
656+
Object[] argsArray;
636657
if (event.has("args")) {
637658
JSONArray args = event.getJSONArray("args");
638659
argsArray = new Object[args.length()];
@@ -641,6 +662,8 @@ public void transportMessage(String text) {
641662
argsArray[i] = args.get(i);
642663
}
643664
}
665+
else
666+
argsArray = new Object[0];
644667
String eventName = event.getString("name");
645668
try {
646669
findCallback(message).on(eventName,
@@ -681,8 +704,12 @@ public void transportMessage(String text) {
681704
}
682705
break;
683706
case IOMessage.TYPE_ERROR:
684-
findCallback(message).onError(
685-
new SocketIOException(message.getData()));
707+
try {
708+
findCallback(message).onError(
709+
new SocketIOException(message.getData()));
710+
} catch (SocketIOException e) {
711+
error(e);
712+
}
686713
if (message.getData().endsWith("+0")) {
687714
// We are advised to disconnect
688715
cleanup();
@@ -714,26 +741,6 @@ public void reconnect() {
714741
}
715742
}
716743

717-
/**
718-
* finds the corresponding callback object to an incoming message. Returns a
719-
* dummy callback if no corresponding callback can be found
720-
*
721-
* @param message
722-
* the message
723-
* @return the iO callback
724-
*/
725-
private IOCallback findCallback(IOMessage message) {
726-
if("".equals(message.getEndpoint()))
727-
return this;
728-
SocketIO socket = sockets.get(message.getEndpoint());
729-
if (socket == null) {
730-
logger.warning("Cannot find socket for '" + message.getEndpoint()
731-
+ "'");
732-
return DUMMY_CALLBACK;
733-
}
734-
return socket.getCallback();
735-
}
736-
737744
/**
738745
* Returns the session id. This should be called from a {@link IOTransport}
739746
*
@@ -842,46 +849,6 @@ public IOTransport getTransport() {
842849
return transport;
843850
}
844851

845-
/** A dummy callback used when IOConnection receives a unexpected message. */
846-
final static public IOCallback DUMMY_CALLBACK = new IOCallback() {
847-
@Override
848-
public void onDisconnect() {
849-
logger.info("Disconnect");
850-
}
851-
852-
@Override
853-
public void onConnect() {
854-
logger.info("Connect");
855-
}
856-
857-
@Override
858-
public void onMessage(String data, IOAcknowledge ack) {
859-
logger.info("Message:\n" + data + "\n-------------");
860-
}
861-
862-
@Override
863-
public void onMessage(JSONObject json, IOAcknowledge ack) {
864-
logger.info("Message:\n" + json.toString() + "\n-------------");
865-
}
866-
867-
@Override
868-
public void on(String event, IOAcknowledge ack, Object... args) {
869-
logger.info("Event '" + event + "':\n");
870-
for (Object arg : args)
871-
logger.info(arg.toString());
872-
logger.info("\n-------------");
873-
}
874-
875-
@Override
876-
public void onError(SocketIOException socketIOException) {
877-
logger.info("Error");
878-
throw new RuntimeException(socketIOException);
879-
}
880-
881-
};
882-
883-
private static final Object[] empty = new Object[0];
884-
885852
@Override
886853
public void onDisconnect() {
887854
SocketIO socket = sockets.get("");

0 commit comments

Comments
 (0)