From e59a3df7f43fec6bc51a4c40ba0d5f6910b2b3d9 Mon Sep 17 00:00:00 2001 From: boudin Date: Sun, 2 Nov 2014 18:45:42 +0100 Subject: [PATCH] Full implementation of netbeans protocol --- lib/buffer.js | 7 ++++- lib/controler.js | 80 ++++++++++++++++++++++++++++++++---------------- lib/events.js | 19 ++++++++++++ 3 files changed, 79 insertions(+), 27 deletions(-) create mode 100644 lib/events.js diff --git a/lib/buffer.js b/lib/buffer.js index b7f1166..16e6ed8 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -1,9 +1,12 @@ -var Types = require("types.js"); +var EventEmitter = require("EventEmitter"); +var Types = require("types"); //A controler owned buffer representation function Buffer (client, buffId) { "use strict"; + EventEmitter.call(this); + 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 @@ -174,4 +177,6 @@ function Buffer (client, buffId) { }; } +Buffer.prototype = EventEmitter; + module.exports = Buffer; diff --git a/lib/controler.js b/lib/controler.js index 7fe8ba1..772313a 100644 --- a/lib/controler.js +++ b/lib/controler.js @@ -2,7 +2,7 @@ var Transform = require("stream").Transform; var EventEmitter = require("events").EventEmitter; var Types = require("types"); var Buffer = require("buffer"); - +var events = require("events"); //A stream Transform implementation returning full netbeans messages function MessageTransform(opts) { @@ -42,17 +42,16 @@ function MessageTransform(opts) { //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 -function Controler(socket, authentication) { +function NetbeansClient(socket, authentication) { "use strict"; EventEmitter.call(this); var self = this; - var callbacks = new Map(); + var callbacks = {}; var currentSeqno = 0; var connected = false; - /* TODO check if this is needed */ socket.setEncoding("utf8"); var messageTransform = new MessageTransform(); @@ -89,7 +88,7 @@ function Controler(socket, authentication) { args.push("event"); 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)); + args.push(Number.parseInt(message.substring(iSeqno + 1, i), 10)); } else if (iName === 0 && iSeqno === 0) { seqno = Number.parseInt(message.substr(0, i), 10); @@ -173,12 +172,11 @@ function Controler(socket, authentication) { } if (isEvent) { - //Emit the event - self.emit(...args); + self.emit.apply(self, args); } - else if (callbacks.has(seqno)) { - callbacks.get(seqno)(...args); - callbacks.delete(seqno); + else if (callbacks.hasOwnProperty(seqno)) { + callbacks[seqno].apply(null, args); + delete callbacks[seqno]; } else { throw "Received reply to unknown function call"; @@ -197,15 +195,15 @@ function Controler(socket, authentication) { }); messageTransform.on("error", function (error) { - + self.emit("error", error); }); messageTransform.on("end",function () { - + self.emit("disconnect"); }); messageTransform.on("close", function () { - + self.emit("disconnect"); }); //Send a command message. @@ -234,7 +232,7 @@ function Controler(socket, authentication) { var lastArg = arguments.length - 1; if (typeof(arguments[lastArg]) === "function") { - callbacks[currentSeqno] = arguments[lastArg]; + callbacks.defineProperty(currentSeqno,arguments[lastArg]); lastArg -= 1; } @@ -248,21 +246,51 @@ function Controler(socket, authentication) { socket.write(data); - }; + }; socket.pipe(messageTransform); } -InternalClient.prototype = EventEmitter; +NetbeansClient.prototype = EventEmitter; //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 -function Client(socket, authentication) { +//Takes an incomming connexion (socket:Socket), an authentication callback (authentication:function(string)) +//Returns a Controler (Controler) +function Controler(socket, authentication) { "use strict"; - var client = new Controler(socket, authentication); + EventEmitter.call(this); + + var client = new NetbeansClient(socket, authentication); 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 */ //Bring Vim to the front (GVim only) @@ -282,8 +310,7 @@ function Client(socket, authentication) { //Create a new buffer. Return the buffer created this.create = function() { - var buffer = new Buffer(client, buffId); - buffId += 1; + var buffer = getBuffer(); client.sendCommand("create", buffId); return buffer; }; @@ -291,8 +318,7 @@ function Client(socket, authentication) { //Associate already opened buffer on Vim to a controler owned buffer //Return the newly created buffer this.putBufferNumber = function (pathname) { - var buffer = new Buffer(client, buffId); - buffId += 1; + var buffer = getBuffer(); client.sendCommand("putBufferNumber", buffId, Types.string(pathname)); //TODO : May be better on the 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 //Return the newly created buffer this.setBufferNumber = function (pathname) { - var buffer = new Buffer(client, buffId); - buffId += 1; + var buffer = getBuffer(); client.sendCommand("setBufferNumber", buffId, Types.string(pathname)); 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; diff --git a/lib/events.js b/lib/events.js new file mode 100644 index 0000000..5eb3282 --- /dev/null +++ b/lib/events.js @@ -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;