-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathMessagePacketCodec.java
200 lines (169 loc) · 5.52 KB
/
MessagePacketCodec.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package io.github.sac.codec;
import io.github.sac.codec.CodecEngine;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.msgpack.core.MessageBufferPacker;
import org.msgpack.core.MessagePack;
import org.msgpack.core.MessageUnpacker;
import org.msgpack.value.ImmutableValue;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Implementation of Binary message packet code. It is based on <a href="https://github.com/SocketCluster/sc-codec-min-bin">min-bin codec</a>
*
* <p>Created by AUT0CRAT on 2/2/18.
*/
public class MessagePacketCodec extends CodecEngine {
@Override
public byte[] encode(JSONObject jsonObject) {
MessageBufferPacker packer = MessagePack.newDefaultBufferPacker();
ArrayList<Object> a = new ArrayList<>();
Map<String, Object[]> map = new HashMap<>();
encodePublish(jsonObject, map);
encodeEmit(jsonObject, map);
encodeResponse(jsonObject, map);
try {
JSONObject toEncode = new JSONObject(map);
System.out.println("to encode : " + toEncode.toString());
packer.packString(toEncode.toString());
} catch (IOException e) {
e.printStackTrace();
}
return packer.toByteArray();
}
@Override
public byte[] encode(String data) {
MessageBufferPacker packer = MessagePack.newDefaultBufferPacker();
try {
packer.packString(data);
} catch (IOException e) {
e.printStackTrace();
}
return packer.toByteArray();
}
private void encodeResponse(JSONObject jsonObject, Map<String, Object[]> map) {
String rid = jsonObject.optString("rid", null);
if (rid == null) {
return;
}
List<Object> a = new ArrayList<>();
a.add(rid);
a.add(jsonObject.optString("error"));
String cid = jsonObject.optString("data");
if (cid != null) {
a.add(cid);
}
map.put("r", a.toArray());
jsonObject.remove("rid");
jsonObject.remove("error");
jsonObject.remove("data");
}
private void encodeEmit(JSONObject jsonObject, Map<String, Object[]> map) {
String event = jsonObject.optString("event", null);
if (event == null) {
return;
}
List<Object> a = new ArrayList<>();
a.add(jsonObject.optString("event"));
a.add(jsonObject.optString("data"));
String cid = jsonObject.optString("cid");
if (cid != null) {
a.add(cid);
}
map.put("e", a.toArray());
jsonObject.remove("event");
jsonObject.remove("data");
jsonObject.remove("cid");
}
private void encodePublish(JSONObject jsonObject, Map<String, Object[]> map) {
String event = jsonObject.optString("event", null);
if (event == null || !event.equals("#publish")) {
return;
}
JSONObject dataObject = jsonObject.optJSONObject("data");
List<Object> a = new ArrayList<>();
a.add(dataObject.optString("channel"));
a.add(dataObject);
String cid = jsonObject.optString("cid");
if (cid != null) {
a.add(cid);
}
map.put("p", a.toArray());
jsonObject.remove("event");
jsonObject.remove("data");
jsonObject.remove("cid");
}
@Override
public String decode(byte[] data) {
MessageUnpacker unpacker = MessagePack.newDefaultUnpacker(data);
try {
ImmutableValue mapValue = unpacker.unpackValue();
try {
JSONObject object = new JSONObject(mapValue.toString());
decodeEmit(object);
decodePublish(object);
decodeResponse(object);
return object.toString();
} catch (JSONException e) {
return mapValue.toString();
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private void decodePublish(JSONObject object) {
JSONArray p = object.optJSONArray("p");
if (p == null) {
return;
}
try {
object.put("event", "#publish");
JSONObject data = new JSONObject();
int size = p.length();
data.put("channel", p.get(0));
data.put("data", p.get(1));
if (size > 2) {
data.put("cid", p.get(2));
}
object.put("data", data);
} catch (JSONException e) {
e.printStackTrace();
}
object.remove("p");
}
private void decodeEmit(JSONObject object) {
JSONArray e = object.optJSONArray("e");
if (e == null) {
return;
}
try {
object.put("event", e.get(0));
object.put("data", e.get(1));
if (e.length() > 2) {
object.put("cid", e.get(2));
}
} catch (JSONException ex) {
ex.printStackTrace();
}
object.remove("e");
}
private void decodeResponse(JSONObject object) {
JSONArray r = object.optJSONArray("r");
if (r == null) {
return;
}
try {
object.put("rid", r.get(0));
object.put("error", r.get(1));
object.put("data", r.get(2));
} catch (JSONException e) {
e.printStackTrace();
}
object.remove("r");
}
}