compatible with socket.io-parser 2.2.0, and supports protocol v4

This commit is contained in:
Naoyuki Kanezawa
2014-06-01 22:25:31 +09:00
parent e2f0af9816
commit ef3540707f
7 changed files with 207 additions and 149 deletions

View File

@@ -57,7 +57,7 @@
<dependency>
<groupId>com.github.nkzawa</groupId>
<artifactId>engine.io-client</artifactId>
<version>0.2.0</version>
<version>0.2.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.json</groupId>

View File

@@ -17,7 +17,7 @@ public class Binary {
public static DeconstructedPacket deconstructPacket(Packet packet) {
List<byte[]> buffers = new ArrayList<byte[]>();
packet.data = deconstructBinPackRecursive(packet.data, buffers);
packet.data = _deconstructPacket(packet.data, buffers);
packet.attachments = buffers.size();
DeconstructedPacket result = new DeconstructedPacket();
@@ -26,7 +26,7 @@ public class Binary {
return result;
}
private static Object deconstructBinPackRecursive(Object data, List<byte[]> buffers) {
private static Object _deconstructPacket(Object data, List<byte[]> buffers) {
if (data == null) return null;
if (data instanceof byte[]) {
@@ -40,7 +40,7 @@ public class Binary {
JSONArray _data = (JSONArray)data;
int len = _data.length();
for (int i = 0; i < len; i ++) {
newData.put(i, deconstructBinPackRecursive(_data.get(i), buffers));
newData.put(i, _deconstructPacket(_data.get(i), buffers));
}
return newData;
} else if (data instanceof JSONObject) {
@@ -49,7 +49,7 @@ public class Binary {
Iterator<?> iterator = _data.keys();
while (iterator.hasNext()) {
String key = (String)iterator.next();
newData.put(key, deconstructBinPackRecursive(_data.get(key), buffers));
newData.put(key, _deconstructPacket(_data.get(key), buffers));
}
return newData;
}
@@ -57,17 +57,17 @@ public class Binary {
}
public static Packet reconstructPacket(Packet packet, byte[][] buffers) {
packet.data = reconstructBinPackRecursive(packet.data, buffers);
packet.data = _reconstructPacket(packet.data, buffers);
packet.attachments = -1;
return packet;
}
private static Object reconstructBinPackRecursive(Object data, byte[][] buffers) {
private static Object _reconstructPacket(Object data, byte[][] buffers) {
if (data instanceof JSONArray) {
JSONArray _data = (JSONArray)data;
int len = _data.length();
for (int i = 0; i < len; i ++) {
_data.put(i, reconstructBinPackRecursive(_data.get(i), buffers));
_data.put(i, _reconstructPacket(_data.get(i), buffers));
}
return _data;
} else if (data instanceof JSONObject) {
@@ -79,7 +79,7 @@ public class Binary {
Iterator<?> iterator = _data.keys();
while (iterator.hasNext()) {
String key = (String)iterator.next();
_data.put(key, reconstructBinPackRecursive(_data.get(key), buffers));
_data.put(key, _reconstructPacket(_data.get(key), buffers));
}
return _data;
}

View File

@@ -43,7 +43,12 @@ public class Parser {
*/
public static final int BINARY_EVENT = 5;
public static int protocol = 3;
/**
* Packet type `binary ack`.
*/
public static final int BINARY_ACK = 6;
public static int protocol = 4;
/**
* Packet types.
@@ -54,6 +59,7 @@ public class Parser {
"EVENT",
"BINARY_EVENT",
"ACK",
"BINARY_ACK",
"ERROR",
};
@@ -72,7 +78,7 @@ public class Parser {
public void encode(Packet obj, Callback callback) {
logger.fine(String.format("encoding packet %s", obj));
if (BINARY_EVENT == obj.type || ACK == obj.type) {
if (BINARY_EVENT == obj.type || BINARY_ACK == obj.type) {
encodeAsBinary(obj, callback);
} else {
String encoding = encodeAsString(obj);
@@ -86,7 +92,7 @@ public class Parser {
str.append(obj.type);
if (BINARY_EVENT == obj.type || ACK == obj.type) {
if (BINARY_EVENT == obj.type || BINARY_ACK == obj.type) {
str.append(obj.attachments);
str.append("-");
}
@@ -140,7 +146,7 @@ public class Parser {
public void add(String obj) {
Packet packet = decodeString(obj);
if (BINARY_EVENT == packet.type || ACK == packet.type) {
if (BINARY_EVENT == packet.type || BINARY_ACK == packet.type) {
this.reconstructor = new BinaryReconstructor(packet);
if (this.reconstructor.reconPack.attachments == 0) {
@@ -170,7 +176,7 @@ public class Parser {
p.type = Character.getNumericValue(str.charAt(0));
if (p.type < 0 || p.type > types.length - 1) return error();
if (BINARY_EVENT == p.type || ACK == p.type) {
if (BINARY_EVENT == p.type || BINARY_ACK == p.type) {
StringBuilder attachments = new StringBuilder();
while (str.charAt(++i) != '-') {
attachments.append(str.charAt(i));

View File

@@ -0,0 +1,104 @@
package com.github.nkzawa.socketio.parser;
import com.github.nkzawa.emitter.Emitter;
import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import java.nio.charset.Charset;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
@RunWith(JUnit4.class)
public class ByteArrayTest {
private static Parser.Encoder encoder = new Parser.Encoder();
@Test
public void encodeByteArray() {
Packet<byte[]> packet = new Packet<byte[]>(Parser.BINARY_EVENT);
packet.data = "abc".getBytes(Charset.forName("UTF-8"));
packet.id = 23;
packet.nsp = "/cool";
Helpers.testBin(packet);
}
@Test
public void encodeByteArray2() {
Packet<byte[]> packet = new Packet<byte[]>(Parser.BINARY_EVENT);
packet.data = new byte[2];
packet.id = 0;
packet.nsp = "/";
Helpers.testBin(packet);
}
@Test
public void encodeByteArrayDeepInJson() {
JSONObject data = new JSONObject("{a: \"hi\", b: {}, c: {a: \"bye\", b: {}}}");
data.getJSONObject("b").put("why", new byte[3]);
data.getJSONObject("c").getJSONObject("b").put("a", new byte[6]);
Packet<JSONObject> packet = new Packet<JSONObject>(Parser.BINARY_EVENT);
packet.data = data;
packet.id = 999;
packet.nsp = "/deep";
Helpers.testBin(packet);
}
@Test
public void encodeDeepBinaryJSONWithNullValue() {
JSONObject data = new JSONObject("{a: \"b\", c: 4, e: {g: null}, h: null}");
data.put("h", new byte[9]);
Packet<JSONObject> packet = new Packet<JSONObject>(Parser.BINARY_EVENT);
packet.data = data;
packet.nsp = "/";
packet.id = 600;
Helpers.testBin(packet);
}
@Test
public void encodeBinaryAckWithByteArray() {
JSONArray data = new JSONArray("[a, null, {}]");
data.put(1, "xxx".getBytes(Charset.forName("UTF-8")));
Packet<JSONArray> packet = new Packet<JSONArray>(Parser.BINARY_ACK);
packet.data = data;
packet.id = 127;
packet.nsp = "/back";
Helpers.testBin(packet);
}
@Test
public void cleanItselfUpOnClose() {
JSONArray data = new JSONArray();
data.put(new byte[2]);
data.put(new byte[3]);
Packet<JSONArray> packet = new Packet<JSONArray>(Parser.BINARY_EVENT);
packet.data = data;
packet.id = 0;
packet.nsp = "/";
encoder.encode(packet, new Parser.Encoder.Callback() {
@Override
public void call(final Object[] encodedPackets) {
final Parser.Decoder decoder = new Parser.Decoder();
decoder.on(Parser.Decoder.EVENT_DECODED, new Emitter.Listener() {
@Override
public void call(Object... args) {
throw new RuntimeException("received a packet when not all binary data was sent.");
}
});
decoder.add((String)encodedPackets[0]);
decoder.add((byte[]) encodedPackets[1]);
decoder.destroy();
assertThat(decoder.reconstructor.buffers.size(), is(0));
}
});
}
}

View File

@@ -0,0 +1,76 @@
package com.github.nkzawa.socketio.parser;
import com.github.nkzawa.emitter.Emitter;
import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.skyscreamer.jsonassert.JSONAssert;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
@RunWith(JUnit4.class)
public class Helpers {
private static Parser.Encoder encoder = new Parser.Encoder();
public static void test(final Packet obj) {
encoder.encode(obj, new Parser.Encoder.Callback() {
@Override
public void call(Object[] encodedPackets) {
Parser.Decoder decoder = new Parser.Decoder();
decoder.on(Parser.Decoder.EVENT_DECODED, new Emitter.Listener() {
@Override
public void call(Object... args) {
Packet packet = (Packet)args[0];
assertPacket(packet, obj);
}
});
decoder.add((String)encodedPackets[0]);
}
});
}
public static void testBin(final Packet obj) {
final Object originalData = obj.data;
encoder.encode(obj, new Parser.Encoder.Callback() {
@Override
public void call(Object[] encodedPackets) {
Parser.Decoder decoder = new Parser.Decoder();
decoder.on(Parser.Decoder.EVENT_DECODED, new Emitter.Listener() {
@Override
public void call(Object... args) {
Packet packet = (Packet)args[0];
obj.data = originalData;
obj.attachments = -1;
assertPacket(packet, obj);
}
});
for (Object packet : encodedPackets) {
if (packet instanceof String) {
decoder.add((String)packet);
} else if (packet instanceof byte[]) {
decoder.add((byte[])packet);
}
}
}
});
}
public static void assertPacket(Packet expected, Packet actual) {
assertThat(actual.type, is(expected.type));
assertThat(actual.id, is(expected.id));
assertThat(actual.nsp, is(expected.nsp));
assertThat(actual.attachments, is(expected.attachments));
if (expected.data instanceof JSONArray) {
JSONAssert.assertEquals((JSONArray)expected.data, (JSONArray)actual.data, true);
} else if (expected.data instanceof JSONObject) {
JSONAssert.assertEquals((JSONObject)expected.data, (JSONObject)actual.data, true);
} else {
assertThat(actual.data, is(expected.data));
}
}
}

View File

@@ -1,17 +1,9 @@
package com.github.nkzawa.socketio.parser;
import com.github.nkzawa.emitter.Emitter;
import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.skyscreamer.jsonassert.JSONAssert;
import java.nio.charset.Charset;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
@RunWith(JUnit4.class)
public class ParserTest {
@@ -23,14 +15,14 @@ public class ParserTest {
public void encodeConnection() {
Packet packet = new Packet(Parser.CONNECT);
packet.nsp = "/woot";
test(packet);
Helpers.test(packet);
}
@Test
public void encodeDisconnect() {
public void encodeDisconnection() {
Packet packet = new Packet(Parser.DISCONNECT);
packet.nsp = "/woot";
test(packet);
Helpers.test(packet);
}
@Test
@@ -38,12 +30,12 @@ public class ParserTest {
Packet<JSONArray> packet1 = new Packet<JSONArray>(Parser.EVENT);
packet1.data = new JSONArray("[\"a\", 1, {}]");
packet1.nsp = "/";
test(packet1);
Helpers.test(packet1);
Packet<JSONArray> packet2 = new Packet<JSONArray>(Parser.EVENT);
packet2.data = new JSONArray("[\"a\", 1, {}]");
packet2.nsp = "/test";
test(packet2);
Helpers.test(packet2);
}
@Test
@@ -52,126 +44,6 @@ public class ParserTest {
packet.data = new JSONArray("[\"a\", 1, {}]");
packet.id = 123;
packet.nsp = "/";
test(packet);
}
@Test
public void encodeByteArray() {
Packet<byte[]> packet = new Packet<byte[]>(Parser.BINARY_EVENT);
packet.data = "abc".getBytes(Charset.forName("UTF-8"));
packet.id = 23;
packet.nsp = "/cool";
testBin(packet);
}
@Test
public void encodeByteArray2() {
Packet<byte[]> packet = new Packet<byte[]>(Parser.BINARY_EVENT);
packet.data = new byte[2];
packet.id = 0;
packet.nsp = "/";
testBin(packet);
}
@Test
public void encodeByteArrayDeep() {
JSONObject data = new JSONObject("{a: \"hi\", b: {}, c: {a: \"bye\", b: {}}}");
data.getJSONObject("b").put("why", new byte[3]);
data.getJSONObject("c").getJSONObject("b").put("a", new byte[6]);
Packet<JSONObject> packet = new Packet<JSONObject>(Parser.BINARY_EVENT);
packet.data = data;
packet.id = 999;
packet.nsp = "/deep";
testBin(packet);
}
@Test
public void cleanItselfUpOnClose() {
JSONArray data = new JSONArray();
data.put(new byte[2]);
data.put(new byte[3]);
Packet<JSONArray> packet = new Packet<JSONArray>(Parser.BINARY_EVENT);
packet.data = data;
packet.id = 0;
packet.nsp = "/";
encoder.encode(packet, new Parser.Encoder.Callback() {
@Override
public void call(final Object[] encodedPackets) {
final Parser.Decoder decoder = new Parser.Decoder();
decoder.on(Parser.Decoder.EVENT_DECODED, new Emitter.Listener() {
@Override
public void call(Object... args) {
throw new RuntimeException("received a packet when not all binary data was sent.");
}
});
decoder.add((String)encodedPackets[0]);
decoder.add((byte[]) encodedPackets[1]);
decoder.destroy();
assertThat(decoder.reconstructor.buffers.size(), is(0));
}
});
}
private void test(final Packet obj) {
encoder.encode(obj, new Parser.Encoder.Callback() {
@Override
public void call(Object[] encodedPackets) {
Parser.Decoder decoder = new Parser.Decoder();
decoder.on(Parser.Decoder.EVENT_DECODED, new Emitter.Listener() {
@Override
public void call(Object... args) {
Packet packet = (Packet)args[0];
assertPacket(packet, obj);
}
});
decoder.add((String)encodedPackets[0]);
}
});
}
private void testBin(final Packet obj) {
final Object originalData = obj.data;
encoder.encode(obj, new Parser.Encoder.Callback() {
@Override
public void call(Object[] encodedPackets) {
Parser.Decoder decoder = new Parser.Decoder();
decoder.on(Parser.Decoder.EVENT_DECODED, new Emitter.Listener() {
@Override
public void call(Object... args) {
Packet packet = (Packet)args[0];
obj.data = originalData;
obj.attachments = -1;
assertPacket(packet, obj);
}
});
for (Object packet : encodedPackets) {
if (packet instanceof String) {
decoder.add((String)packet);
} else if (packet instanceof byte[]) {
decoder.add((byte[])packet);
}
}
}
});
}
private void assertPacket(Packet expected, Packet actual) {
assertThat(actual.type, is(expected.type));
assertThat(actual.id, is(expected.id));
assertThat(actual.nsp, is(expected.nsp));
assertThat(actual.attachments, is(expected.attachments));
if (expected.data instanceof JSONArray) {
JSONAssert.assertEquals((JSONArray)expected.data, (JSONArray)actual.data, true);
} else if (expected.data instanceof JSONObject) {
JSONAssert.assertEquals((JSONObject)expected.data, (JSONObject)actual.data, true);
} else {
assertThat(actual.data, is(expected.data));
}
Helpers.test(packet);
}
}

View File

@@ -3,6 +3,6 @@
"version": "0.0.0",
"private": true,
"dependencies": {
"socket.io": "1.0.0-pre2"
"socket.io": "1.0.3"
}
}