From 3a3477fe6b1f5680a34c7a48ef42fb93c2b47d51 Mon Sep 17 00:00:00 2001 From: boudin Date: Wed, 29 Oct 2014 19:38:26 +0100 Subject: [PATCH] Addin message parsing class --- README.md | 0 lib/message.js | 130 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 130 insertions(+) create mode 100644 README.md create mode 100644 lib/message.js diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/lib/message.js b/lib/message.js new file mode 100644 index 0000000..282d590 --- /dev/null +++ b/lib/message.js @@ -0,0 +1,130 @@ +var Transform = require("stream").Transform; + +/** + * A Transform Stream implementation that output full VNB protocol messages + * + * @constructor + * @this {VNBMessageParser} + */ +function VNBMessageTransform(opts) { + "use strict"; + Transform.call(this,opts); + + var buffer; + + /** + * Transform stream Write implementation + * @param {string} data Upstream data already converted as string + * @param {string} encoding String encoding (should be utf8, but should be checked upward) + * @param {function} callback Downstream Stream receiving only full VNB messages + **/ + this._transform = function(data, encoding, callback) { + var endpos = data.indexOf("\n"); + + if (endpos > -1) { + if (buffer !== undefined) { + this.push(buffer); + buffer = undefined; + } + + if (endpos > 0) { + this.push(data.substr(0, endpos)); + } + callback(); + + if (endpos < data.length - 1) { + buffer = data.substr(endpos + 1); + } + } + else { + buffer = buffer.concat(data); + } + }; + + /** + * Flush still data. Remaining data is lost since it can only be an incomplete message + * @param {function} callback Callback to call when data has been flushed + **/ + this._flush = function (callback) { + buffer = undefined; + callback(); + }; +} + + +/** + * VNB Client implementation (communication from vim to the netbeans server) + * @constructor + * @this {VNBMessageClient} + * @param {stream.Readable} data Incomming stream + * @param {function} onEvent Callback function called when an Event message is received + * @param {function} onReply Callback function called when a Reply to a previous Function called is received + */ +function VNBMessageClient(data, onEvent, onReply) { + "use strict"; + + data.setEncoding("utf8"); + + var messageTransform = new VNBMessageTransform(); + + messageTransform.on("data",function (message) { + var iName = 0; //Index of the name separator in case of an Event + var iSeqno = 0; //Index of the seqno separator in case of an Event + + //Header of the message + for (var i = 0 ; i < message.length && message[i] !== " "; ++i) { + if (message[i] === ":") { + iName = i; + } + else if (message[i] === "=") { + iSeqno = i; + } + } + + var isEvent = false; + var args = []; + + if (iName > 0 && iSeqno > 0) { + isEvent = true; + args.push(Number.parseInt(message.substr(0, iName), 10)); + args.push(message.substring(iName + 1, iSeqno)); + args.push(Number.parseInt(message.substring(iSeqno + 1, i), 10)); + } + else if (iName === 0 && iSeqno === 0) { + args.push(Number.parseInt(message.substr(0, i), 10)); + } + else { + //Unknown message type + } + + //Arguments of the message + for (++i ; i < message.length ; ++i) { + + } + + if (isEvent) { + onEvent(...args); + } + else { + onReply(...args); + } + }); + + messageTransform.on("error", function (error) { + + }); + + messageTransform.on("end",function () { + + }); + + messageTransform.on("close", function () { + + }); + + + data.pipe(messageTransform); + + +} +