Base for sending function and command messages

This commit is contained in:
2014-11-01 00:03:37 +01:00
parent ec03419e7d
commit 5a37474988

View File

@@ -12,7 +12,7 @@ function VNBMessageTransform(opts) {
Transform.call(this,opts); Transform.call(this,opts);
var buffer; var buffer;
/** /**
* Transform stream Write implementation * Transform stream Write implementation
* @param {string} data Upstream data already converted as string * @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 //The first message must be an authentication one
if (message.startsWith("AUTH ") && authentication(message.substring(message.indexOf(" ") + 1))) { if (message.startsWith("AUTH ") && authentication(message.substring(message.indexOf(" ") + 1))) {
connected = true; connected = true;
messageTransform.removeAllListeners("data"); messageTransform.removeAllListeners("data");
messageTransform.on("data", function(message) { messageTransform.on("data", function(message) {
@@ -222,7 +222,7 @@ function VNBClient(socket, authentication) {
}); });
messageTransform.on("error", function (error) { messageTransform.on("error", function (error) {
}); });
messageTransform.on("end",function () { messageTransform.on("end",function () {
@@ -232,79 +232,115 @@ function VNBClient(socket, authentication) {
messageTransform.on("close", function () { messageTransform.on("close", function () {
}); });
self.sendCommand = function (name, buffer) {
}; /**
* @function sendCommand
self.callFunction = function (name, buffer) { * 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; let seqno = currentSeqno;
currentSeqno += 1; 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); socket.pipe(messageTransform);
} }
function NetbeansColor (value) { function color (value) {
"use strict"; "use strict";
this.convert = function() { return value ? value : "none";
return value ? value : "none";
};
} }
function NetbeansBool (value) { function bool (value) {
"use strict"; "use strict";
this.convert = function () { return value ? "T" : "F";
return value ? "T" : "F";
};
} }
function NetbeansPosition (line, col) { function position (line, col) {
"use strict"; "use strict";
this.convert = function () { return line+"/"+col;
return line+"/"+col;
};
} }
function NetbeansString (value) { function string (value) {
"use strict"; "use strict";
this.convert = function () { var buffer = "\"";
var buffer = "\"";
for (var i = 0 ; i < value.length ; ++i){ for (var i = 0 ; i < value.length ; ++i){
switch (value[i]) { switch (value[i]) {
case "\n" : case "\n" :
buffer += "\\n"; buffer += "\\n";
break; break;
case "\t" : case "\t" :
buffer += "\\t"; buffer += "\\t";
break; break;
case "\r" : case "\r" :
buffer += "\\r"; buffer += "\\r";
break; break;
case "\\" : case "\\" :
buffer += "\\\\"; buffer += "\\\\";
break; break;
case "\"" : case "\"" :
buffer += "\\\""; buffer += "\\\"";
break; break;
default : default :
buffer += value[i]; buffer += value[i];
break; break;
} }
} }
buffer += "\""; buffer += "\"";
return buffer; return buffer;
}
} }