Added missing files

pull/85/head
Ruben Daniels 2015-04-30 04:01:13 +00:00
rodzic 0a86081af0
commit c78aba36b7
6 zmienionych plików z 184 dodań i 77 usunięć

Wyświetl plik

@ -618,7 +618,6 @@ module.exports = function(options) {
}, },
{ {
packagePath: "plugins/c9.cli.bridge/bridge", packagePath: "plugins/c9.cli.bridge/bridge",
port: 17123,
startBridge: options.startBridge startBridge: options.startBridge
}, },
{ {

Wyświetl plik

@ -13,25 +13,57 @@ define(function(require, exports, module) {
var c9 = imports.c9; var c9 = imports.c9;
var net = imports.net; var net = imports.net;
var JSONStream = require("./json-stream");
/***** Initialization *****/ /***** Initialization *****/
var plugin = new Plugin("Ajax.org", main.consumes); var plugin = new Plugin("Ajax.org", main.consumes);
// var emit = plugin.getEmitter(); // var emit = plugin.getEmitter();
var PORT = options.port || 17123; var counter = 0;
var SOCKET = c9.home + "/.c9/bridge.socket";
/***** Methods *****/ /***** Methods *****/
function send(message, callback) { function send(message, callback) {
net.connect(PORT, {}, function(err, stream) { net.connect(SOCKET, {}, function(err, stream) {
if (err) if (err)
return callback(err); return callback(err);
stream.write(JSON.stringify(message)); var jstream = new JSONStream(stream);
stream.end(); var msgId = generateMessageId();
var done;
callback(); stream.write(JSON.stringify({
id: msgId,
message: message
}));
jstream.on("data", function(payload){
if (payload.id == msgId && !done) {
done = true;
callback(null, payload.message);
stream.end();
}
}); });
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;
})
});
}
function generateMessageId(){
// Use vfs token
return Math.random() + "-" + ++counter;
} }
/***** Lifecycle *****/ /***** Lifecycle *****/

Wyświetl plik

@ -1,37 +1,114 @@
module.exports = function (vfs, options, register) { module.exports = function (vfs, options, register) {
var stream;
var net = require("net");
var Stream = require('stream'); var Stream = require('stream');
var stream, server;
var SOCKET = process.env.HOME + "/.c9/bridge.socket";
function createListenClient(api){
var client = net.connect(SOCKET, function(data){
if (data) api.onData(data);
});
client.setEncoding("utf8");
client.unref();
client.on("data", function(data){
if (data) api.onData(data);
});
client.on("error", function(err){
if (err.code == "ECONNREFUSED") {
require("fs").unlink(SOCKET, function(){
createListenServer(api);
});
}
else
api.onError(err);
});
client.on("end", function(){
createListenServer(api);
});
api.onConnect(client);
api.disconnect = function(){
client.end();
};
return client;
}
function createListenServer(api){
var unixServer = net.createServer(function(client) {
client.setEncoding("utf8");
client.on("data", function(data){
if (data) api.onData(data);
});
client.on("error", function(data){
// console.error("ERROR", api.id, data);
});
api.onConnect(client);
});
unixServer.listen(SOCKET);
unixServer.on("error", function(err){
if (err.code == "EADDRINUSE") {
createListenClient(api);
}
else
api.onError(err);
});
api.disconnect = function(){
unixServer.close();
};
}
register(null, { register(null, {
connect: function (port, callback) { connect: function (callback) {
if (stream) return callback(null, { stream: stream }); if (stream) return callback(null, { stream: stream });
server = require('net').createServer(function(c) {
var buffer = "";
c.on("data", function(chunk) {
buffer += chunk;
});
c.on("end", function(){
stream.emit("data", buffer);
});
});
server.on("error", function(err) {
callback(err);
});
server.listen(port, process.env.OPENSHIFT_DIY_IP || "localhost", function(err) {
if (err) return callback(err);
callback(null, { stream: stream });
});
stream = new Stream(); stream = new Stream();
stream.readable = true; stream.readable = true;
stream.writable = true;
var client;
var sent = false;
var api = this.api = {
id: Math.random(),
onConnect: function(c){
client = c;
if (sent) return;
callback(null, { stream: stream });
sent = true;
},
onData: function(data){
stream.emit("data", data);
},
onError: function(err){
stream.emit("error", err);
}
};
createListenServer(api);
stream.on("data", function(data){
if (client) client.write(data);
});
}, },
disconnect: function(){ disconnect: function(){
try { server && server.close(); } try { this.api && this.api.disconnect(); }
catch (e) {} catch (e) {}
stream = null; stream = null;
server = null; delete this.api;
} }
}); });
}; };

Wyświetl plik

@ -1,4 +1,3 @@
define(function(require, exports, module) { define(function(require, exports, module) {
main.consumes = ["c9", "Plugin", "ext"]; main.consumes = ["c9", "Plugin", "ext"];
main.provides = ["bridge"]; main.provides = ["bridge"];
@ -9,21 +8,18 @@ define(function(require, exports, module) {
var c9 = imports.c9; var c9 = imports.c9;
var ext = imports.ext; var ext = imports.ext;
var JSONStream = require("./json-stream");
/***** Initialization *****/ /***** Initialization *****/
var plugin = new Plugin("Ajax.org", main.consumes); var plugin = new Plugin("Ajax.org", main.consumes);
var emit = plugin.getEmitter(); var emit = plugin.getEmitter();
var ENABLED = options.startBridge !== false; var ENABLED = options.startBridge !== false;
var PORT = options.port || 17123;
var stream, api; var stream, api;
var loaded = false;
function load(){ function load(){
if (loaded) return;
loaded = true;
if (!ENABLED) return; if (!ENABLED) return;
ext.loadRemotePlugin("bridge", { ext.loadRemotePlugin("bridge", {
@ -35,58 +31,52 @@ define(function(require, exports, module) {
api = remote; api = remote;
api.connect(PORT, function(err, meta) { api.connect(function(err, meta) {
if (err) { if (err)
loaded = false; return console.error(err); // this should never happen
if (err.code == "EADDRINUSE") { stream = new JSONStream(meta.stream);
console.warn("Another Application is using port "
+ PORT + ". CLI client interface disabled. Restart Cloud9 to retry connecting."); stream.on("error", function(err) {
}
else
console.error(err); console.error(err);
});
return; stream.on("data", function(payload) {
} var response = emit("message", { message: payload.message });
stream = meta.stream; stream.write({
id: payload.id,
stream.on("data", function(chunk) { message: response
try { var message = JSON.parse(chunk); } });
catch (e) {
setTimeout(function(){
loaded = false;
load();
}, 60000);
return;
}
emit("message", { message: message });
}); });
stream.on("close", function(){ stream.on("close", function(){
loaded = false; load();
}); });
}); });
}); });
window.addEventListener("unload", unload); window.addEventListener("unload", function(){
api && api.disconnect();
});
} }
function unload() { function write(json){
api && api.disconnect(); if (!stream) return false;
api = stream = null; stream.write(JSON.stringify(json));
loaded = false; return true;
} }
/***** Methods *****/ /***** Methods *****/
plugin.on("load", function(){ plugin.on("load", function(){
c9.on("connect", load, plugin); c9.on("connect", load, plugin);
c9.on("disconnect", unload, plugin);
}); });
plugin.on("unload", function(){ plugin.on("unload", function(){
api && api.disconnect(); api && api.disconnect();
stream = null;
api = null;
}); });
/***** Register and define API *****/ /***** Register and define API *****/
@ -94,7 +84,12 @@ define(function(require, exports, module) {
/** /**
* Bridge To Communicate from CLI to IDE * Bridge To Communicate from CLI to IDE
**/ **/
plugin.freezePublicAPI({ }); plugin.freezePublicAPI({
/**
*
*/
write: write
});
register(null, { register(null, {
bridge: plugin bridge: plugin

Wyświetl plik

@ -23,22 +23,20 @@ define(function(require, exports, module) {
var BASEPATH = options.basePath; var BASEPATH = options.basePath;
var loaded = false;
function load(){ function load(){
if (loaded) return;
loaded = true;
bridge.on("message", function(e) { bridge.on("message", function(e) {
var message = e.message; var message = e.message;
switch (message.type) { switch (message.type) {
case "open": case "open":
open(message); open(message);
break; return true;
case "ping": case "ping":
break; return true;
default:
return false;
} }
}); }, plugin);
} }
/***** Methods *****/ /***** Methods *****/
@ -61,7 +59,7 @@ define(function(require, exports, module) {
var node = favs.addFavorite(path); var node = favs.addFavorite(path);
tree.expand(path, function(err) { tree.expand(path, function() {
tree.select(node); //path || "/"); tree.select(node); //path || "/");
tree.scrollToSelection(); tree.scrollToSelection();
}); });

Wyświetl plik

@ -90,7 +90,7 @@ define(function(require, exports, module) {
paths: paths paths: paths
}; };
bridge.send(message, function cb(err) { bridge.send(message, function cb(err, response) {
if (err) { if (err) {
if (err.code == "ECONNREFUSED") { if (err.code == "ECONNREFUSED") {
// Seems Cloud9 is not running, lets start it up // Seems Cloud9 is not running, lets start it up
@ -111,6 +111,9 @@ define(function(require, exports, module) {
console.log(err.message); console.log(err.message);
} }
if (response !== true)
console.log("Could not open ", paths);
process.exit(); // I don't get why this is needed process.exit(); // I don't get why this is needed
}); });
} }
@ -129,13 +132,16 @@ define(function(require, exports, module) {
var timed = Date.now(); var timed = Date.now();
(function retry(){ (function retry(){
bridge.send({ type: "ping" }, function(err) { bridge.send({ type: "ping" }, function(err, message) {
if (!err) if (!err)
return callback(true); return callback(true);
if (Date.now() - timed > 10000) if (Date.now() - timed > 10000)
return callback(false); return callback(false);
if (message !== true)
return callback(false);
setTimeout(retry, 100); setTimeout(retry, 100);
}); });
})(); })();