Full implementation of netbeans protocol

This commit is contained in:
2014-11-02 18:45:42 +01:00
parent c5709a403c
commit e59a3df7f4
3 changed files with 79 additions and 27 deletions

View File

@@ -1,9 +1,12 @@
var Types = require("types.js"); var EventEmitter = require("EventEmitter");
var Types = require("types");
//A controler owned buffer representation //A controler owned buffer representation
function Buffer (client, buffId) { function Buffer (client, buffId) {
"use strict"; "use strict";
EventEmitter.call(this);
var typeNum = 0; //WARNING : check if this should be unique to a buffer var typeNum = 0; //WARNING : check if this should be unique to a buffer
var serNum = 0; //WARNING : check if this should be unique to a buffer var serNum = 0; //WARNING : check if this should be unique to a buffer
@@ -174,4 +177,6 @@ function Buffer (client, buffId) {
}; };
} }
Buffer.prototype = EventEmitter;
module.exports = Buffer; module.exports = Buffer;

View File

@@ -2,7 +2,7 @@ var Transform = require("stream").Transform;
var EventEmitter = require("events").EventEmitter; var EventEmitter = require("events").EventEmitter;
var Types = require("types"); var Types = require("types");
var Buffer = require("buffer"); var Buffer = require("buffer");
var events = require("events");
//A stream Transform implementation returning full netbeans messages //A stream Transform implementation returning full netbeans messages
function MessageTransform(opts) { function MessageTransform(opts) {
@@ -42,17 +42,16 @@ function MessageTransform(opts) {
//Main netbeans object. Needs the socket of the incomming connexion and an authentication callback //Main netbeans object. Needs the socket of the incomming connexion and an authentication callback
//This callback just get the password sent by Vim. It must return true to allow connexion //This callback just get the password sent by Vim. It must return true to allow connexion
function Controler(socket, authentication) { function NetbeansClient(socket, authentication) {
"use strict"; "use strict";
EventEmitter.call(this); EventEmitter.call(this);
var self = this; var self = this;
var callbacks = new Map(); var callbacks = {};
var currentSeqno = 0; var currentSeqno = 0;
var connected = false; var connected = false;
/* TODO check if this is needed */
socket.setEncoding("utf8"); socket.setEncoding("utf8");
var messageTransform = new MessageTransform(); var messageTransform = new MessageTransform();
@@ -89,7 +88,7 @@ function Controler(socket, authentication) {
args.push("event"); args.push("event");
args.push(Number.parseInt(message.substr(0, iName), 10)); args.push(Number.parseInt(message.substr(0, iName), 10));
args.push(message.substring(iName + 1, iSeqno)); args.push(message.substring(iName + 1, iSeqno));
//args.push(Number.parseInt(message.substring(iSeqno + 1, i), 10)); args.push(Number.parseInt(message.substring(iSeqno + 1, i), 10));
} }
else if (iName === 0 && iSeqno === 0) { else if (iName === 0 && iSeqno === 0) {
seqno = Number.parseInt(message.substr(0, i), 10); seqno = Number.parseInt(message.substr(0, i), 10);
@@ -173,12 +172,11 @@ function Controler(socket, authentication) {
} }
if (isEvent) { if (isEvent) {
//Emit the event self.emit.apply(self, args);
self.emit(...args);
} }
else if (callbacks.has(seqno)) { else if (callbacks.hasOwnProperty(seqno)) {
callbacks.get(seqno)(...args); callbacks[seqno].apply(null, args);
callbacks.delete(seqno); delete callbacks[seqno];
} }
else { else {
throw "Received reply to unknown function call"; throw "Received reply to unknown function call";
@@ -197,15 +195,15 @@ function Controler(socket, authentication) {
}); });
messageTransform.on("error", function (error) { messageTransform.on("error", function (error) {
self.emit("error", error);
}); });
messageTransform.on("end",function () { messageTransform.on("end",function () {
self.emit("disconnect");
}); });
messageTransform.on("close", function () { messageTransform.on("close", function () {
self.emit("disconnect");
}); });
//Send a command message. //Send a command message.
@@ -234,7 +232,7 @@ function Controler(socket, authentication) {
var lastArg = arguments.length - 1; var lastArg = arguments.length - 1;
if (typeof(arguments[lastArg]) === "function") { if (typeof(arguments[lastArg]) === "function") {
callbacks[currentSeqno] = arguments[lastArg]; callbacks.defineProperty(currentSeqno,arguments[lastArg]);
lastArg -= 1; lastArg -= 1;
} }
@@ -248,21 +246,51 @@ function Controler(socket, authentication) {
socket.write(data); socket.write(data);
}; };
socket.pipe(messageTransform); socket.pipe(messageTransform);
} }
InternalClient.prototype = EventEmitter; NetbeansClient.prototype = EventEmitter;
//Wrapper around the controler exposing functions and commands as methods //Wrapper around the controler exposing functions and commands as methods
//Needs the incomming connexon socket and an authentication callback that get the password and return true to allow connexion //Takes an incomming connexion (socket:Socket), an authentication callback (authentication:function(string))
function Client(socket, authentication) { //Returns a Controler (Controler)
function Controler(socket, authentication) {
"use strict"; "use strict";
var client = new Controler(socket, authentication); EventEmitter.call(this);
var client = new NetbeansClient(socket, authentication);
var buffId = 0; var buffId = 0;
var buffers = new {};
function getBuffer() {
buffId += 1;
var buffer = new Buffer(client, buffId);
buffers.defineProperty(buffId, buffer);
return buffer;
}
client.on("event", function (buffId, name, seqno) {
var target = this;
if (buffId > 0 && buffers.hasOwnProperty(buffId)) {
target = buffers[buffId];
}
target.emit.apply(target, Array.prototype.slice(arguments, 1));
});
client.on("error", function (error) {
console.errort("Error : "+error);
});
client.on("disconnected", function () {
});
/* GLOBAL COMMANDS */ /* GLOBAL COMMANDS */
//Bring Vim to the front (GVim only) //Bring Vim to the front (GVim only)
@@ -282,8 +310,7 @@ function Client(socket, authentication) {
//Create a new buffer. Return the buffer created //Create a new buffer. Return the buffer created
this.create = function() { this.create = function() {
var buffer = new Buffer(client, buffId); var buffer = getBuffer();
buffId += 1;
client.sendCommand("create", buffId); client.sendCommand("create", buffId);
return buffer; return buffer;
}; };
@@ -291,8 +318,7 @@ function Client(socket, authentication) {
//Associate already opened buffer on Vim to a controler owned buffer //Associate already opened buffer on Vim to a controler owned buffer
//Return the newly created buffer //Return the newly created buffer
this.putBufferNumber = function (pathname) { this.putBufferNumber = function (pathname) {
var buffer = new Buffer(client, buffId); var buffer = getBuffer();
buffId += 1;
client.sendCommand("putBufferNumber", buffId, Types.string(pathname)); //TODO : May be better on the buffer client.sendCommand("putBufferNumber", buffId, Types.string(pathname)); //TODO : May be better on the buffer
return buffer; return buffer;
}; };
@@ -300,8 +326,7 @@ function Client(socket, authentication) {
//Associate already opened buffer on Vim to a controler owned buffer and bring it to front //Associate already opened buffer on Vim to a controler owned buffer and bring it to front
//Return the newly created buffer //Return the newly created buffer
this.setBufferNumber = function (pathname) { this.setBufferNumber = function (pathname) {
var buffer = new Buffer(client, buffId); var buffer = getBuffer();
buffId += 1;
client.sendCommand("setBufferNumber", buffId, Types.string(pathname)); client.sendCommand("setBufferNumber", buffId, Types.string(pathname));
return buffer; return buffer;
}; };
@@ -319,4 +344,7 @@ function Client(socket, authentication) {
}; };
} }
module.exports.Controler = Client; Controler.prototype = EventEmitter;
module.exports.events = events;
module.exports.Controler = Controler;

19
lib/events.js Normal file
View File

@@ -0,0 +1,19 @@
var events = {
balloonText : "balloonText",
buttonRelease : "buttonRelease",
disconnect : "disconnect",
fileOpened : "fileOpened",
geometry : "geometry",
insert : "insert",
keyCommand : "keyCommand",
keyAtPos : "keyAtPos",
killed : "killed",
newDotAndMark : "newDotAndMark",
remove : "remove",
save : "save",
startupDone : "startupDone",
version : "version"
};
module.exports.events = events;