-
Notifications
You must be signed in to change notification settings - Fork 43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow user to add custom codecEngine #27
Open
AUT0CRAT
wants to merge
3
commits into
sacOO7:master
Choose a base branch
from
AUT0CRAT:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package io.github.sac.codec; | ||
|
||
import io.github.sac.codec.CodecEngine; | ||
import org.json.JSONException; | ||
import org.json.JSONObject; | ||
|
||
import java.nio.charset.Charset; | ||
|
||
/** | ||
* <p>Created by AUT0CRAT on 2/2/18. | ||
*/ | ||
public class BasicCodecEngine extends CodecEngine { | ||
@Override | ||
public byte[] encode(JSONObject data) { | ||
return data.toString().getBytes(); | ||
} | ||
|
||
@Override | ||
public byte[] encode(String data) { | ||
return data.getBytes(); | ||
} | ||
|
||
@Override | ||
public String decode(byte[] data) { | ||
return new String(data, Charset.defaultCharset()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package io.github.sac.codec; | ||
|
||
import org.json.JSONObject; | ||
|
||
/** | ||
* <p>Created by AUT0CRAT on 2/2/18. | ||
*/ | ||
public abstract class CodecEngine { | ||
abstract public byte[] encode(JSONObject data); | ||
|
||
abstract public byte[] encode(String data); | ||
|
||
abstract public String decode(byte[] data); | ||
} |
200 changes: 200 additions & 0 deletions
200
src/main/java/io/github/sac/codec/MessagePacketCodec.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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"); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should probably be fixed as:
a.add(dataObject.optJSONObject("data"));