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) {
|
||||
"use strict";
|
||||
|
||||
console.log("Connected");
|
||||
|
||||
var connected = true;
|
||||
console.log("Ready to serve");
|
||||
|
||||
process.stdin.removeAllListeners("data");
|
||||
|
||||
process.stdin.on("data", function(data) {
|
||||
var cmd = data.toString();
|
||||
console.log("CMD "+cmd);
|
||||
|
||||
controler.on("fileOpened", function() {
|
||||
console.log("Filed opened dude !");
|
||||
});
|
||||
|
||||
try
|
||||
{
|
||||
@@ -61,7 +65,6 @@ server.createServer(3219, "127.0.0.1", function (password) {
|
||||
});
|
||||
break;
|
||||
default :
|
||||
console.log("CMD : "+data.toString());
|
||||
console.log("RETURNED : "+eval(data.toString()));
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -1,12 +1,16 @@
|
||||
var EventEmitter = require("events").EventEmitter;
|
||||
var util = require("util");
|
||||
var Types = require("./types.js");
|
||||
|
||||
//A controler owned buffer representation
|
||||
function Buffer (client, buffId) {
|
||||
function Buffer (client, buffId, path) {
|
||||
"use strict";
|
||||
|
||||
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 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
|
||||
this.setTitle = function (name) {
|
||||
self.title = name;
|
||||
client.sendCommand("setTitle", buffId, Types.string(name));
|
||||
};
|
||||
|
||||
@@ -138,6 +143,7 @@ function Buffer (client, buffId) {
|
||||
|
||||
//Edit the file in the current buffer
|
||||
this.editFile = function (pathName){
|
||||
self.name = 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;
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
var Transform = require("stream").Transform;
|
||||
var EventEmitter = require("events").EventEmitter;
|
||||
var util = require("util");
|
||||
var Types = require("./types.js");
|
||||
var Buffer = require("./buffer.js");
|
||||
var events = require("./event.js");
|
||||
|
||||
var event = require("./event.js");
|
||||
|
||||
//A stream Transform implementation returning full netbeans messages
|
||||
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
|
||||
//This callback just get the password sent by Vim. It must return true to allow connexion
|
||||
function NetbeansClient(socket, authentication, onEvent, onError, onDisconnected) {
|
||||
"use strict";
|
||||
|
||||
EventEmitter.call(this);
|
||||
|
||||
var self = this;
|
||||
var callbacks = {};
|
||||
var currentSeqno = 0;
|
||||
@@ -59,7 +57,6 @@ function NetbeansClient(socket, authentication, onEvent, onError, onDisconnected
|
||||
|
||||
messageTransform.on("data",function (data) {
|
||||
var message = data.toString();
|
||||
console.log("MSG : "+message);
|
||||
|
||||
/* The first message must be an authentication one */
|
||||
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.on("data", function(data) {
|
||||
var message = data.toString();
|
||||
console.log("MSG "+message);
|
||||
|
||||
try {
|
||||
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) {
|
||||
isEvent = true;
|
||||
args.push("event");
|
||||
args.push(parseInt(message.substr(0, iName), 10));
|
||||
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) {
|
||||
seqno = parseInt(message.substr(0, i), 10);
|
||||
@@ -170,7 +165,6 @@ function NetbeansClient(socket, authentication, onEvent, onError, onDisconnected
|
||||
}
|
||||
}
|
||||
else {
|
||||
console.log("ARG : "+argument);
|
||||
args.push(parseInt(argument));
|
||||
}
|
||||
}
|
||||
@@ -252,8 +246,6 @@ function NetbeansClient(socket, authentication, onEvent, onError, onDisconnected
|
||||
|
||||
data += "\n";
|
||||
|
||||
console.log("CALL : "+data);
|
||||
|
||||
socket.write(data);
|
||||
|
||||
};
|
||||
@@ -261,8 +253,6 @@ function NetbeansClient(socket, authentication, onEvent, onError, onDisconnected
|
||||
socket.pipe(messageTransform);
|
||||
}
|
||||
|
||||
NetbeansClient.prototype = EventEmitter.prototype;
|
||||
|
||||
//Wrapper around the controler exposing functions and commands as methods
|
||||
//Takes an incomming connexion (socket:Socket), an authentication callback (authentication:function(string))
|
||||
//Returns a Controler (Controler)
|
||||
@@ -270,21 +260,30 @@ function Controler(socket, authentication) {
|
||||
"use strict";
|
||||
|
||||
var self = this;
|
||||
var unmanagedFiles=[];
|
||||
|
||||
EventEmitter.call(this);
|
||||
|
||||
var client = new NetbeansClient(socket, authentication, function(name, buffId) {
|
||||
var client = new NetbeansClient(socket, authentication, function(buffId, name) {
|
||||
switch (name) {
|
||||
case events.disconnect :
|
||||
case event.disconnect :
|
||||
//Connexion closed by Vim
|
||||
socket.destroy();
|
||||
self.emit("disconnected");
|
||||
break;
|
||||
case events.killed :
|
||||
case event.killed :
|
||||
//Remove the buffer if it as been killed
|
||||
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];
|
||||
}
|
||||
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 :
|
||||
var target = self;
|
||||
|
||||
@@ -292,29 +291,42 @@ function Controler(socket, authentication) {
|
||||
target = buffers[buffId];
|
||||
}
|
||||
|
||||
target.emit.apply(target, Array.prototype.slice(arguments, 1));
|
||||
target.emit.apply(target, Array.prototype.slice.call(arguments, 1));
|
||||
break;
|
||||
}
|
||||
},
|
||||
|
||||
function (error) {
|
||||
console.log("ERROR : "+error);
|
||||
self.emit("error",error);
|
||||
},
|
||||
|
||||
function () {
|
||||
console.log("DISCONNECTED");
|
||||
self.emit("disconnected");
|
||||
});
|
||||
|
||||
var buffId = 0;
|
||||
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;
|
||||
var buffer = new Buffer(client, buffId);
|
||||
var buffer = new Buffer(client, buffId, pathname);
|
||||
Object.defineProperty(buffers, buffId, {value: buffer, configurable: true});
|
||||
return buffer;
|
||||
}
|
||||
|
||||
function error (exception) {
|
||||
self.emit("error", exception);
|
||||
}
|
||||
|
||||
/* GLOBAL COMMANDS */
|
||||
|
||||
@@ -335,25 +347,40 @@ function Controler(socket, authentication) {
|
||||
|
||||
//Create a new buffer. Return the buffer created
|
||||
this.create = function() {
|
||||
try {
|
||||
var buffer = newBuffer();
|
||||
client.sendCommand("create", buffId);
|
||||
return buffer;
|
||||
}
|
||||
catch (exception) {
|
||||
error(exception);
|
||||
}
|
||||
};
|
||||
|
||||
//Associate already opened buffer on Vim to a controler owned buffer
|
||||
//Return the newly created buffer
|
||||
this.putBufferNumber = function (pathname) {
|
||||
var buffer = newBuffer();
|
||||
client.sendCommand("putBufferNumber", buffId, Types.string(pathname)); //TODO : May be better on the buffer
|
||||
try {
|
||||
var buffer = newBuffer(pathname);
|
||||
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
|
||||
//Return the newly created buffer
|
||||
this.setBufferNumber = function (pathname) {
|
||||
var buffer = newBuffer();
|
||||
try {
|
||||
var buffer = newBuffer(pathname);
|
||||
client.sendCommand("setBufferNumber", buffId, Types.string(pathname));
|
||||
return buffer;
|
||||
}
|
||||
catch (exception) {
|
||||
error(exception);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -388,11 +415,23 @@ function Controler(socket, authentication) {
|
||||
self.emit("disconnected");
|
||||
};
|
||||
|
||||
//Get a buffer using its name or its id
|
||||
this.getBuffer = function(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;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
var events = {
|
||||
var event = {
|
||||
balloonText : "balloonText",
|
||||
buttonRelease : "buttonRelease",
|
||||
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");
|
||||
|
||||
var ctrl = new Controler(connexion, function(pwd) {
|
||||
return authentication.call(null, pwd);
|
||||
ctrl.on("startupDone", function() {
|
||||
connected.call(null, ctrl);
|
||||
});
|
||||
|
||||
connected.call(null, ctrl);
|
||||
return authentication.call(null, pwd);
|
||||
});
|
||||
});
|
||||
|
||||
host = host || "127.0.0.1";
|
||||
|
||||
Reference in New Issue
Block a user