Github user d2r commented on a diff in the pull request:
https://github.com/apache/incubator-storm/pull/84#discussion_r12665246
--- Diff: storm-core/src/jvm/backtype/storm/multilang/JsonSerializer.java
---
@@ -0,0 +1,164 @@
+package backtype.storm.multilang;
+
+import java.io.BufferedReader;
+import java.io.DataOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.OutputStream;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+
+import org.json.simple.JSONObject;
+import org.json.simple.JSONValue;
+
+import backtype.storm.task.TopologyContext;
+import backtype.storm.tuple.Tuple;
+import backtype.storm.utils.Utils;
+
+/**
+ * JsonSerializer implements the JSON multilang protocol.
+ */
+public class JsonSerializer implements ISerializer {
+ private DataOutputStream processIn;
+ private BufferedReader processOut;
+
+ public void initialize(OutputStream processIn, InputStream processOut)
{
+ this.processIn = new DataOutputStream(processIn);
+ this.processOut = new BufferedReader(new
InputStreamReader(processOut));
+ }
+
+ public Number connect(Map conf, TopologyContext context)
+ throws IOException, NoOutputException {
+ JSONObject setupInfo = new JSONObject();
+ setupInfo.put("pidDir", context.getPIDDir());
+ setupInfo.put("conf", conf);
+ setupInfo.put("context", context);
+ writeMessage(setupInfo);
+
+ Number pid = (Number) ((JSONObject) readMessage()).get("pid");
+ return pid;
+ }
+
+ public void writeBoltMsg(BoltMsg boltMsg) throws IOException {
+ JSONObject obj = new JSONObject();
+ obj.put("id", boltMsg.getId());
+ obj.put("comp", boltMsg.getComp());
+ obj.put("stream", boltMsg.getStream());
+ obj.put("task", boltMsg.getTask());
+ obj.put("tuple", boltMsg.getTuple());
+ writeMessage(obj);
+ }
+
+ public void writeSpoutMsg(SpoutMsg msg) throws IOException {
+ JSONObject obj = new JSONObject();
+ obj.put("command", msg.getCommand());
+ obj.put("id", msg.getId());
+ writeMessage(obj);
+ }
+
+ public void writeTaskIds(List<Integer> taskIds) throws IOException {
+ writeMessage(taskIds);
+ }
+
+ private void writeMessage(Object msg) throws IOException {
+ writeString(JSONValue.toJSONString(msg));
+ }
+
+ private void writeString(String str) throws IOException {
+ byte[] strBytes = str.getBytes("UTF-8");
+ processIn.write(strBytes, 0, strBytes.length);
+ processIn.writeBytes("\nend\n");
+ processIn.flush();
+ }
+
+ public ShellMsg readShellMsg() throws IOException, NoOutputException {
+ JSONObject msg = (JSONObject) readMessage();
+ ShellMsg shellMsg = new ShellMsg();
+
+ String command = (String) msg.get("command");
+ shellMsg.setCommand(command);
+
+ Object id = msg.get("id");
+ shellMsg.setId(id);
+
+ String log = (String) msg.get("msg");
+ shellMsg.setMsg(log);
+
+ String stream = (String) msg.get("stream");
+ if (stream == null)
+ stream = Utils.DEFAULT_STREAM_ID;
--- End diff --
Tiny nit: Can we put this in a block?
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---