Fix event sent by EventEmitter classes
This commit is contained in:
@@ -8,14 +8,18 @@ server.createServer(3219, "127.0.0.1", function (password) {
|
|||||||
}, function (controler) {
|
}, function (controler) {
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
console.log("Connected");
|
var connected = true;
|
||||||
|
|
||||||
console.log("Ready to serve");
|
console.log("Ready to serve");
|
||||||
|
|
||||||
|
process.stdin.removeAllListeners("data");
|
||||||
|
|
||||||
process.stdin.on("data", function(data) {
|
process.stdin.on("data", function(data) {
|
||||||
var cmd = data.toString();
|
var cmd = data.toString();
|
||||||
console.log("CMD "+cmd);
|
console.log("CMD "+cmd);
|
||||||
|
|
||||||
|
controler.on("fileOpened", function() {
|
||||||
|
console.log("Filed opened dude !");
|
||||||
|
});
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@@ -61,7 +65,6 @@ server.createServer(3219, "127.0.0.1", function (password) {
|
|||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
default :
|
default :
|
||||||
console.log("CMD : "+data.toString());
|
|
||||||
console.log("RETURNED : "+eval(data.toString()));
|
console.log("RETURNED : "+eval(data.toString()));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +1,16 @@
|
|||||||
var EventEmitter = require("events").EventEmitter;
|
var EventEmitter = require("events").EventEmitter;
|
||||||
|
var util = require("util");
|
||||||
var Types = require("./types.js");
|
var Types = require("./types.js");
|
||||||
|
|
||||||
//A controler owned buffer representation
|
//A controler owned buffer representation
|
||||||
function Buffer (client, buffId) {
|
function Buffer (client, buffId, path) {
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
EventEmitter.call(this);
|
EventEmitter.call(this);
|
||||||
|
|
||||||
|
var self = this;
|
||||||
|
this.path = path;
|
||||||
|
this.title = undefined;
|
||||||
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
|
||||||
|
|
||||||
@@ -117,6 +121,7 @@ function Buffer (client, buffId) {
|
|||||||
|
|
||||||
//Set the title of the buffer. The title is just an internal name, it's not visible by the user
|
//Set the title of the buffer. The title is just an internal name, it's not visible by the user
|
||||||
this.setTitle = function (name) {
|
this.setTitle = function (name) {
|
||||||
|
self.title = name;
|
||||||
client.sendCommand("setTitle", buffId, Types.string(name));
|
client.sendCommand("setTitle", buffId, Types.string(name));
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -138,6 +143,7 @@ function Buffer (client, buffId) {
|
|||||||
|
|
||||||
//Edit the file in the current buffer
|
//Edit the file in the current buffer
|
||||||
this.editFile = function (pathName){
|
this.editFile = function (pathName){
|
||||||
|
self.name = pathName;
|
||||||
client.sendCommand("editFile", buffId, Types.string(pathName));
|
client.sendCommand("editFile", buffId, Types.string(pathName));
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -177,6 +183,6 @@ function Buffer (client, buffId) {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
Buffer.prototype = EventEmitter.prototype;
|
util.inherits(Buffer, EventEmitter);
|
||||||
|
|
||||||
module.exports = Buffer;
|
module.exports = Buffer;
|
||||||
|
|||||||
123
lib/controler.js
123
lib/controler.js
@@ -1,9 +1,9 @@
|
|||||||
var Transform = require("stream").Transform;
|
var Transform = require("stream").Transform;
|
||||||
var EventEmitter = require("events").EventEmitter;
|
var EventEmitter = require("events").EventEmitter;
|
||||||
|
var util = require("util");
|
||||||
var Types = require("./types.js");
|
var Types = require("./types.js");
|
||||||
var Buffer = require("./buffer.js");
|
var Buffer = require("./buffer.js");
|
||||||
var events = require("./event.js");
|
var event = require("./event.js");
|
||||||
|
|
||||||
|
|
||||||
//A stream Transform implementation returning full netbeans messages
|
//A stream Transform implementation returning full netbeans messages
|
||||||
function MessageTransform(opts) {
|
function MessageTransform(opts) {
|
||||||
@@ -39,15 +39,13 @@ function MessageTransform(opts) {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
MessageTransform.prototype = Transform.prototype;
|
util.inherits(MessageTransform, Transform);
|
||||||
|
|
||||||
//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 NetbeansClient(socket, authentication, onEvent, onError, onDisconnected) {
|
function NetbeansClient(socket, authentication, onEvent, onError, onDisconnected) {
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
EventEmitter.call(this);
|
|
||||||
|
|
||||||
var self = this;
|
var self = this;
|
||||||
var callbacks = {};
|
var callbacks = {};
|
||||||
var currentSeqno = 0;
|
var currentSeqno = 0;
|
||||||
@@ -59,7 +57,6 @@ function NetbeansClient(socket, authentication, onEvent, onError, onDisconnected
|
|||||||
|
|
||||||
messageTransform.on("data",function (data) {
|
messageTransform.on("data",function (data) {
|
||||||
var message = data.toString();
|
var message = data.toString();
|
||||||
console.log("MSG : "+message);
|
|
||||||
|
|
||||||
/* The first message must be an authentication one */
|
/* The first message must be an authentication one */
|
||||||
if (message.indexOf("AUTH ") === 0 && authentication(message.substring(message.indexOf(" ") + 1))) {
|
if (message.indexOf("AUTH ") === 0 && authentication(message.substring(message.indexOf(" ") + 1))) {
|
||||||
@@ -68,7 +65,6 @@ function NetbeansClient(socket, authentication, onEvent, onError, onDisconnected
|
|||||||
messageTransform.removeAllListeners("data");
|
messageTransform.removeAllListeners("data");
|
||||||
messageTransform.on("data", function(data) {
|
messageTransform.on("data", function(data) {
|
||||||
var message = data.toString();
|
var message = data.toString();
|
||||||
console.log("MSG "+message);
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
var iName = 0; /*Index of the name separator in case of an Event */
|
var iName = 0; /*Index of the name separator in case of an Event */
|
||||||
@@ -90,10 +86,9 @@ function NetbeansClient(socket, authentication, onEvent, onError, onDisconnected
|
|||||||
|
|
||||||
if (iName > 0 && iSeqno > 0) {
|
if (iName > 0 && iSeqno > 0) {
|
||||||
isEvent = true;
|
isEvent = true;
|
||||||
args.push("event");
|
|
||||||
args.push(parseInt(message.substr(0, iName), 10));
|
args.push(parseInt(message.substr(0, iName), 10));
|
||||||
args.push(message.substring(iName + 1, iSeqno));
|
args.push(message.substring(iName + 1, iSeqno));
|
||||||
args.push(parseInt(message.substring(iSeqno + 1, i), 10));
|
//args.push(parseInt(message.substring(iSeqno + 1, i), 10));
|
||||||
}
|
}
|
||||||
else if (iName === 0 && iSeqno === 0) {
|
else if (iName === 0 && iSeqno === 0) {
|
||||||
seqno = parseInt(message.substr(0, i), 10);
|
seqno = parseInt(message.substr(0, i), 10);
|
||||||
@@ -170,7 +165,6 @@ function NetbeansClient(socket, authentication, onEvent, onError, onDisconnected
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
console.log("ARG : "+argument);
|
|
||||||
args.push(parseInt(argument));
|
args.push(parseInt(argument));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -252,8 +246,6 @@ function NetbeansClient(socket, authentication, onEvent, onError, onDisconnected
|
|||||||
|
|
||||||
data += "\n";
|
data += "\n";
|
||||||
|
|
||||||
console.log("CALL : "+data);
|
|
||||||
|
|
||||||
socket.write(data);
|
socket.write(data);
|
||||||
|
|
||||||
};
|
};
|
||||||
@@ -261,8 +253,6 @@ function NetbeansClient(socket, authentication, onEvent, onError, onDisconnected
|
|||||||
socket.pipe(messageTransform);
|
socket.pipe(messageTransform);
|
||||||
}
|
}
|
||||||
|
|
||||||
NetbeansClient.prototype = EventEmitter.prototype;
|
|
||||||
|
|
||||||
//Wrapper around the controler exposing functions and commands as methods
|
//Wrapper around the controler exposing functions and commands as methods
|
||||||
//Takes an incomming connexion (socket:Socket), an authentication callback (authentication:function(string))
|
//Takes an incomming connexion (socket:Socket), an authentication callback (authentication:function(string))
|
||||||
//Returns a Controler (Controler)
|
//Returns a Controler (Controler)
|
||||||
@@ -270,51 +260,73 @@ function Controler(socket, authentication) {
|
|||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
var self = this;
|
var self = this;
|
||||||
|
var unmanagedFiles=[];
|
||||||
|
|
||||||
EventEmitter.call(this);
|
EventEmitter.call(this);
|
||||||
|
|
||||||
var client = new NetbeansClient(socket, authentication, function(name, buffId) {
|
var client = new NetbeansClient(socket, authentication, function(buffId, name) {
|
||||||
switch (name) {
|
switch (name) {
|
||||||
case events.disconnect :
|
case event.disconnect :
|
||||||
|
//Connexion closed by Vim
|
||||||
socket.destroy();
|
socket.destroy();
|
||||||
self.emit("disconnected");
|
self.emit("disconnected");
|
||||||
break;
|
break;
|
||||||
case events.killed :
|
case event.killed :
|
||||||
|
//Remove the buffer if it as been killed
|
||||||
if (buffers.hasOwnProperty(buffId)) {
|
if (buffers.hasOwnProperty(buffId)) {
|
||||||
buffers[buffId].emit.apply(buffers[buffId], Array.prototype.slice(arguments, 1));
|
buffers[buffId].emit.apply(buffers[buffId], Array.prototype.slice.call(arguments, 1));
|
||||||
delete buffers[buffId];
|
delete buffers[buffId];
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
case event.fileOpened :
|
||||||
|
//File opened. Check if it's already managed by controler.
|
||||||
|
if (self.getBuffer(arguments[2]) === undefined) {
|
||||||
|
unmanagedFiles.push(arguments[2]);
|
||||||
|
}
|
||||||
|
/* fall through */
|
||||||
default :
|
default :
|
||||||
var target = self;
|
var target = self;
|
||||||
|
|
||||||
if (buffId > 0 && buffers.hasOwnProperty(buffId)) {
|
if (buffId > 0 && buffers.hasOwnProperty(buffId)) {
|
||||||
target = buffers[buffId];
|
target = buffers[buffId];
|
||||||
}
|
}
|
||||||
|
|
||||||
target.emit.apply(target, Array.prototype.slice(arguments, 1));
|
target.emit.apply(target, Array.prototype.slice.call(arguments, 1));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
function (error) {
|
function (error) {
|
||||||
console.log("ERROR : "+error);
|
|
||||||
self.emit("error",error);
|
self.emit("error",error);
|
||||||
},
|
},
|
||||||
|
|
||||||
function () {
|
function () {
|
||||||
console.log("DISCONNECTED");
|
|
||||||
self.emit("disconnected");
|
self.emit("disconnected");
|
||||||
});
|
});
|
||||||
|
|
||||||
var buffId = 0;
|
var buffId = 0;
|
||||||
var buffers = {};
|
var buffers = {};
|
||||||
|
|
||||||
function newBuffer() {
|
function newBuffer(pathname) {
|
||||||
|
if (pathname !== undefined) {
|
||||||
|
var indexFile = unmanagedFiles.indexOf(pathname);
|
||||||
|
|
||||||
|
if (indexFile < 0 ) {
|
||||||
|
throw "File not opened by client";
|
||||||
|
}
|
||||||
|
|
||||||
|
unmanagedFiles.splice(indexFile, 1);
|
||||||
|
}
|
||||||
|
|
||||||
buffId += 1;
|
buffId += 1;
|
||||||
var buffer = new Buffer(client, buffId);
|
var buffer = new Buffer(client, buffId, pathname);
|
||||||
Object.defineProperty(buffers, buffId, {value: buffer, configurable: true});
|
Object.defineProperty(buffers, buffId, {value: buffer, configurable: true});
|
||||||
return buffer;
|
return buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function error (exception) {
|
||||||
|
self.emit("error", exception);
|
||||||
|
}
|
||||||
|
|
||||||
/* GLOBAL COMMANDS */
|
/* GLOBAL COMMANDS */
|
||||||
|
|
||||||
@@ -335,25 +347,40 @@ function Controler(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 = newBuffer();
|
try {
|
||||||
client.sendCommand("create", buffId);
|
var buffer = newBuffer();
|
||||||
return buffer;
|
client.sendCommand("create", buffId);
|
||||||
|
return buffer;
|
||||||
|
}
|
||||||
|
catch (exception) {
|
||||||
|
error(exception);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
//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 = newBuffer();
|
try {
|
||||||
client.sendCommand("putBufferNumber", buffId, Types.string(pathname)); //TODO : May be better on the buffer
|
var buffer = newBuffer(pathname);
|
||||||
return buffer;
|
client.sendCommand("putBufferNumber", buffId, Types.string(pathname));
|
||||||
|
return buffer;
|
||||||
|
}
|
||||||
|
catch (exception) {
|
||||||
|
error(exception);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
//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 = newBuffer();
|
try {
|
||||||
client.sendCommand("setBufferNumber", buffId, Types.string(pathname));
|
var buffer = newBuffer(pathname);
|
||||||
return buffer;
|
client.sendCommand("setBufferNumber", buffId, Types.string(pathname));
|
||||||
|
return buffer;
|
||||||
|
}
|
||||||
|
catch (exception) {
|
||||||
|
error(exception);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@@ -388,11 +415,23 @@ function Controler(socket, authentication) {
|
|||||||
self.emit("disconnected");
|
self.emit("disconnected");
|
||||||
};
|
};
|
||||||
|
|
||||||
|
//Get a buffer using its name or its id
|
||||||
this.getBuffer = function(buffId) {
|
this.getBuffer = function(buffId) {
|
||||||
return buffers[buffId];
|
if (typeof(buffId) === "string" || buffId instanceof String) {
|
||||||
|
for (var id in buffers) {
|
||||||
|
if (buffers[id].name === buffId) {
|
||||||
|
return buffers[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return buffers[buffId];
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
Controler.prototype = EventEmitter.prototype;
|
util.inherits(Controler, EventEmitter);
|
||||||
|
|
||||||
module.exports.Controler = Controler;
|
module.exports.Controler = Controler;
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
var events = {
|
var event = {
|
||||||
balloonText : "balloonText",
|
balloonText : "balloonText",
|
||||||
buttonRelease : "buttonRelease",
|
buttonRelease : "buttonRelease",
|
||||||
disconnect : "disconnect",
|
disconnect : "disconnect",
|
||||||
@@ -16,4 +16,4 @@ var events = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
module.exports.events = events;
|
module.exports = event;
|
||||||
|
|||||||
@@ -9,10 +9,12 @@ function createServer(port, host, authentication, connected) {
|
|||||||
console.log("Incomming connexion");
|
console.log("Incomming connexion");
|
||||||
|
|
||||||
var ctrl = new Controler(connexion, function(pwd) {
|
var ctrl = new Controler(connexion, function(pwd) {
|
||||||
|
ctrl.on("startupDone", function() {
|
||||||
|
connected.call(null, ctrl);
|
||||||
|
});
|
||||||
|
|
||||||
return authentication.call(null, pwd);
|
return authentication.call(null, pwd);
|
||||||
});
|
});
|
||||||
|
|
||||||
connected.call(null, ctrl);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
host = host || "127.0.0.1";
|
host = host || "127.0.0.1";
|
||||||
|
|||||||
Reference in New Issue
Block a user