diff --git a/lib/vim-nb-protocol.js b/lib/vim-nb-protocol.js index 1bb5178..022b049 100644 --- a/lib/vim-nb-protocol.js +++ b/lib/vim-nb-protocol.js @@ -12,7 +12,7 @@ function VNBMessageTransform(opts) { Transform.call(this,opts); var buffer; - + /** * Transform stream Write implementation * @param {string} data Upstream data already converted as string @@ -87,7 +87,7 @@ function VNBClient(socket, authentication) { //The first message must be an authentication one if (message.startsWith("AUTH ") && authentication(message.substring(message.indexOf(" ") + 1))) { connected = true; - + messageTransform.removeAllListeners("data"); messageTransform.on("data", function(message) { @@ -222,7 +222,7 @@ function VNBClient(socket, authentication) { }); messageTransform.on("error", function (error) { - + }); messageTransform.on("end",function () { @@ -232,79 +232,115 @@ function VNBClient(socket, authentication) { messageTransform.on("close", function () { }); - - self.sendCommand = function (name, buffer) { - }; - - self.callFunction = function (name, buffer) { + /** + * @function sendCommand + * Send a command message + * @param {string} name Name of the command + * @param {number} buffer Name of the buffer + * @param {...string} arguments Parameters of the command + */ + function sendCommand(name, buffer) { let seqno = currentSeqno; currentSeqno += 1; - - socket.write() - }; + var data = buffer + ":" + name + ":" + seqno; + + for (var i = 2 ; i < arguments.length ; ++i) { + data += " " + arguments[i]; + } + + data += "\n"; + + socket.write(data); + } + + /** + * @function callFunction + * Send a function message + * @param {string} name Name of the function + * @param {number} buffer ID of the buffer + * @param {...string} arguments Parameters of the function. Use the string, bool, color and position function + * of the method to convert javascript types to string + * @param {function} callback Callback function called with reply value + * */ + function callFunction (name, buffer) { + let seqno = currentSeqno; + currentSeqno += 1; + + var lastArg = arguments.length - 1; + + //If the function is called with a callback (wich should always be the case) + //add the calback to the map so it's fired when the reply is received + if (typeof(arguments[lastArg]) === "function") { + callbacks[currentSeqno] = arguments[lastArg]; + lastArg -= 1; + } + + var data = buffer + ":" + name + "/" + seqno; + + for (var i = 2 ; i < lastArg ; ++i) { + data += " " + arguments[i]; + } + + data += "\n"; + + socket.write(data); + + } socket.pipe(messageTransform); } -function NetbeansColor (value) { +function color (value) { "use strict"; - this.convert = function() { - return value ? value : "none"; - }; + return value ? value : "none"; } -function NetbeansBool (value) { +function bool (value) { "use strict"; - this.convert = function () { - return value ? "T" : "F"; - }; + return value ? "T" : "F"; } -function NetbeansPosition (line, col) { +function position (line, col) { "use strict"; - this.convert = function () { - return line+"/"+col; - }; + return line+"/"+col; } -function NetbeansString (value) { +function string (value) { "use strict"; - this.convert = function () { - var buffer = "\""; + var buffer = "\""; - for (var i = 0 ; i < value.length ; ++i){ - switch (value[i]) { - case "\n" : - buffer += "\\n"; - break; - case "\t" : - buffer += "\\t"; - break; - case "\r" : - buffer += "\\r"; - break; - case "\\" : - buffer += "\\\\"; - break; - case "\"" : - buffer += "\\\""; - break; - default : - buffer += value[i]; - break; + for (var i = 0 ; i < value.length ; ++i){ + switch (value[i]) { + case "\n" : + buffer += "\\n"; + break; + case "\t" : + buffer += "\\t"; + break; + case "\r" : + buffer += "\\r"; + break; + case "\\" : + buffer += "\\\\"; + break; + case "\"" : + buffer += "\\\""; + break; + default : + buffer += value[i]; + break; - } - } + } + } - buffer += "\""; - return buffer; - } + buffer += "\""; + return buffer; }