c9-core/plugins/c9.cli.bridge/bridge-client.js

95 wiersze
2.6 KiB
JavaScript
Czysty Zwykły widok Historia

2015-02-10 19:41:24 +00:00
/**
* File Finder module for the Cloud9 that uses nak
*
2015-02-15 02:12:39 +00:00
* @copyright 2013, Ajax.org B.V.
2015-02-10 19:41:24 +00:00
*/
define(function(require, exports, module) {
main.consumes = ["c9", "Plugin", "net"];
2015-05-01 13:02:28 +00:00
main.provides = ["bridge.client"];
2015-02-10 19:41:24 +00:00
return main;
function main(options, imports, register) {
var Plugin = imports.Plugin;
var c9 = imports.c9;
var net = imports.net;
2015-04-30 04:01:13 +00:00
var JSONStream = require("./json-stream");
2015-02-10 19:41:24 +00:00
/***** Initialization *****/
var plugin = new Plugin("Ajax.org", main.consumes);
// var emit = plugin.getEmitter();
2015-04-30 04:01:13 +00:00
var counter = 0;
2015-05-01 13:36:36 +00:00
var SOCKET = c9.platform == "win32"
2015-05-01 18:08:46 +00:00
? "\\\\.\\pipe\\.c9\\bridge.socket"
2015-05-01 13:36:36 +00:00
: c9.home + "/.c9/bridge.socket";
2015-02-10 19:41:24 +00:00
/***** Methods *****/
function send(message, callback) {
2015-04-30 04:01:13 +00:00
net.connect(SOCKET, {}, function(err, stream) {
2015-02-10 19:41:24 +00:00
if (err)
return callback(err);
2015-04-30 04:01:13 +00:00
var jstream = new JSONStream(stream);
var msgId = generateMessageId();
var done;
2015-04-30 22:30:29 +00:00
jstream.write({
2015-04-30 04:01:13 +00:00
id: msgId,
message: message
2015-04-30 22:30:29 +00:00
});
2015-04-30 04:01:13 +00:00
jstream.on("data", function(payload){
if (payload.id == msgId && !done) {
done = true;
callback(null, payload.message);
stream.end();
}
});
2015-02-10 19:41:24 +00:00
2015-04-30 04:01:13 +00:00
jstream.on("error", function(err){
if (done) return;
callback(err);
done = true;
});
jstream.on("close", function(){
if (done) return;
callback(new Error("No Response"));
done = true;
})
2015-02-10 19:41:24 +00:00
});
}
2015-04-30 04:01:13 +00:00
function generateMessageId(){
// Use vfs token
return Math.random() + "-" + ++counter;
}
2015-02-10 19:41:24 +00:00
/***** Lifecycle *****/
plugin.on("load", function(){
});
plugin.on("unload", function(){
});
/***** Register and define API *****/
/**
* Bridge To Communicate from CLI to IDE
**/
plugin.freezePublicAPI({
/**
*
*/
send: send
});
register(null, {
2015-05-01 13:02:28 +00:00
"bridge.client": plugin
2015-02-10 19:41:24 +00:00
});
}
});