Permalink
Browse files

Add client-side socket support to nodeSumo plugin

  • Loading branch information...
mamacdon committed Dec 11, 2012
1 parent 5ef6b76 commit 8107535849f7173b371ee4c7db0bcc82f0268d32
Showing with 77 additions and 78 deletions.
  1. +77 −78 lib/orionode.client/plugins/nodeSumo.js
@@ -8,11 +8,54 @@
*
* Contributors: IBM Corporation - initial API and implementation
******************************************************************************/
-/*global console define XMLHttpRequest */
-define(['orion/plugin', 'orion/xhr'], function(PluginProvider, xhr) {
+/*global console define URL XMLHttpRequest window*/
+define(['orion/plugin', 'orion/xhr', 'orion/Deferred', 'orion/URL-shim', 'socketIO/socket.io'],
+ function(PluginProvider, xhr, Deferred, url, io) {
function fromJson(xhrResult) {
return JSON.parse(xhrResult.response);
}
+ function getSocketURL(path) {
+ var socketBaseUrl = new URL();
+ var location = window.location;
+ socketBaseUrl.protocol = location.protocol;
+ socketBaseUrl.hostname = location.hostname;
+ socketBaseUrl.port = location.port;
+ socketBaseUrl.pathname = path;
+ return socketBaseUrl.href;
+ }
+ /**
+ * Helper for managing Deferred progress
+ */
+ function SocketProcess(url) {
+ var socket = this.socket = io.connect(getSocketURL(url || ''), {
+ 'force new connection': true
+ });
+ var deferred = this.deferred = new Deferred();
+ var buf = [];
+ function addListenerOfType(type) {
+ return socket.on.bind(socket, type);
+ }
+ Object.defineProperties(this, {
+ 'connect': { set: addListenerOfType('connect') },
+ 'disconnect': { set: addListenerOfType('disconnect') },
+ 'error': { set: addListenerOfType('error') },
+ 'started': { set: addListenerOfType('started') },
+ 'stopped': { set: addListenerOfType('stopped') },
+ 'stdout': { set: addListenerOfType('stdout') },
+ 'stderr': { set: addListenerOfType('stderr') }
+ });
+ this.progress = function(data) {
+ buf.push(data);
+ deferred.progress(buf.join(''));
+ };
+ this.resolve = function() {
+ deferred.resolve(buf.join(''));
+ };
+ this.disconnect = this.resolve;
+ this.error = this.progress;
+ this.stdout = this.progress;
+ this.stderr = this.progress;
+ }
var provider = new PluginProvider({
name: "NodeSumo",
@@ -28,19 +71,22 @@ define(['orion/plugin', 'orion/xhr'], function(PluginProvider, xhr) {
provider.registerService('orion.shell.command', {
callback: function(commandArgs) {
var moduleFile = commandArgs.module, args = commandArgs.args;
- var lastPosition = 0;
- return xhr('POST', '/node/start', {
- progress: true,
- headers: {'Content-Type': 'application/json'},
- data: JSON.stringify({
+ var sockProc = new SocketProcess();
+ sockProc.connect = function(data) {
+ sockProc.socket.emit('start', {
modulePath: moduleFile.Location,
args: args
- })
- }).then(function(xhrResult) {
- // TODO only return progress no done
- var app = fromJson(xhrResult);
- return 'Started PID: ' + app.Id + ' at ' + app.Location;
- });
+ });
+ };
+ sockProc.started = function(app) {
+ sockProc.progress('Started app (PID: ' + app.Id + ')\n');
+ };
+ sockProc.stopped = function(app) {
+ // TODO unnecessary work, could just "resolve with progress" in one shot
+ sockProc.progress('App stopped: (PID: ' + app.Id + ')\n');
+ sockProc.resolve();
+ };
+ return sockProc.deferred;
}
}, {
name: 'node start',
@@ -64,8 +110,8 @@ define(['orion/plugin', 'orion/xhr'], function(PluginProvider, xhr) {
if (!data.Apps.length) {
return 'No running apps.';
}
- return 'PID\t\tLocation\n' + data.Apps.map(function(app) {
- return app.Id + '\t\t' + app.Location;
+ return data.Apps.map(function(app) {
+ return ['PID: ' + app.Id, 'URL: ' + app.Location, app.DebugURL ? ('Debug URL: ' + app.DebugURL) : ''].join(', ');
}).join('\n');
});
}
@@ -93,33 +139,23 @@ define(['orion/plugin', 'orion/xhr'], function(PluginProvider, xhr) {
}]
});
-
provider.registerService('orion.shell.command',{
callback: function(commandArgs) {
- var moduleFile = commandArgs.module, port = commandArgs.port;
- var lastPosition = 0;
- return xhr('POST', '/node/debug', {
- progress: true,
- headers: {'Content-Type': 'application/json'},
- data: JSON.stringify({
- modulePath: moduleFile.Location,
- port: port
- })
- }).then(function(xhrResult) {
- // TODO only return progress no done
- return xhrResult.response;
- }, null, function(progressEvent) {
- function getChunk() {
- var xmlHttpRequest = progressEvent.xhr;
- var position = progressEvent.position;
- var chunk = xmlHttpRequest.responseText.slice(lastPosition, position);
- console.log('got chunk ' + chunk.length + ' bytes [' + lastPosition + '..' + position + ']');
- lastPosition = position;
- return chunk;
- }
- var chunk = getChunk();
- return progressEvent;
- });
+ var sockProc = new SocketProcess();
+ sockProc.connect = function(data) {
+ sockProc.socket.emit('debug', {
+ modulePath: commandArgs.module.Location,
+ port: commandArgs.port
+ });
+ };
+ sockProc.started = function(app) {
+ sockProc.progress('Debugging app (PID: ' + app.Id + ')\nDebug URL: ' + app.DebugURL);
+ };
+ sockProc.stopped = function(app) {
+ sockProc.progress('App stopped (PID: ' + app.Id + ')');
+ sockProc.resolve();
+ };
+ return sockProc.deferred;
}
}, {
name: 'node debug',
@@ -135,44 +171,7 @@ define(['orion/plugin', 'orion/xhr'], function(PluginProvider, xhr) {
defaultValue: 5860
}]
});
-/*
- provider.registerService('orion.shell.command',{
- callback: function(commandArgs) {
- var port = commandArgs.port;
- var lastPosition = 0;
- return xhr('POST', '/node/debug_inspect', {
- progress: true,
- headers: {'Content-Type': 'application/json'},
- data: JSON.stringify({
- port: port
- })
- }).then(function(xhrResult) {
- // TODO only return progress no done
- return xhrResult.response;
- }, null, function(progressEvent) {
- function getChunk() {
- var xmlHttpRequest = progressEvent.xhr;
- var position = progressEvent.position;
- var chunk = xmlHttpRequest.responseText.slice(lastPosition, position);
- console.log('got chunk ' + chunk.length + ' bytes [' + lastPosition + '..' + position + ']');
- lastPosition = position;
- return chunk;
- }
- var chunk = getChunk();
- return progressEvent;
- });
- }
- }, {
- name: 'node inspect',
- description: 'Launch a node-inspector process with a given port. The node-inspector can be used as the format of the serverURL:portNumber/debug?port=debugPort. E.g., localhost:8082/debug?port=5861',
- parameters: [{
- name: 'port',
- type: { name: 'number', min: 1 },
- description: 'Port number to pass to the node-inspector process.',
- defaultValue: 8082
- }]
- });
-*/
+
provider.registerService('orion.shell.command',{
callback: function(args) {
return 'lol';

0 comments on commit 8107535

Please sign in to comment.