major continued work on the server side of the debug broker
diff --git a/nodejs/app.js b/nodejs/app.js
index 4dec88f..ade9913 100644
--- a/nodejs/app.js
+++ b/nodejs/app.js
@@ -2,12 +2,22 @@
express = require('express'),
app = express(),
expressWs = require('express-ws')(app),
+ bodyParser = require('body-parser'),
+ invoker = require('./lib/invoker'),
db = require('./lib/db');
db.init(function() {
- app.listen(port, function() {
- app.ws('/register', db.registerDebugClient_route);
+ app.use(bodyParser.json());
- console.log("OWDBG listening on " + port);
+ var router = express.Router();
+ router.ws('/client/register', db.registerDebugClient);
+ app.use('/ws', router);
+
+ app.get('/ping', function(req, res) { res.send("OK") });
+ app.post('/invoke/begin', invoker.invoke);
+ app.get('/invoke/status/:activationId', invoker.status);
+
+ app.listen(port, function() {
+ console.log("OWDBG Broker Ready");
});
});
diff --git "a/nodejs/lib/\043db.js\043" "b/nodejs/lib/\043db.js\043"
deleted file mode 100644
index c4b0ed4..0000000
--- "a/nodejs/lib/\043db.js\043"
+++ /dev/null
@@ -1,101 +0,0 @@
-var Cloudant = require("cloudant"),
- VCAP_SERVICES = JSON.parse(process.env.VCAP_SERVICES),
- instanceName = 'OWDBG',
- dbName = 'owdbg-registry';
-
-// these variables are initialized below
-var cloudant, cloudantCreds;
-
-try {
- cloudantCreds = VCAP_SERVICES.cloudantNoSQLDB.filter(function(env) {
- return env.name == instanceName;
- })[0].credentials;
- cloudant = Cloudant(cloudantCreds.url);
-} catch (e) {
- console.log("Could not initialize cloudant");
- cloudant = undefined;
-}
-
-/*var WebSocketServer = require('ws').Server;
-function startWebSocket(onMessage, docRev) {
- var serverInstance = new WebSocketServer();
- console.log("Listening on port " + serverInstance.port);
-
- serverInstance.on('connection', function connection(ws) {
- ws.on('message', function _onMessage(message) {
- onMessage(message, ws, docRev);
- });
- });
-}*/
-
-exports.init = function(next) {
- if (!cloudant) return next(); // error in initialization
-
- cloudant.db.create(dbName, next);
-}
-
-function registerDebugClient(key, next, nextOnErr) {
- var db = cloudant.db.use(dbName);
-
- db.insert({}, key, function(err, body, header) {
- if (err) {
- // TODO log the error
- console.log("Error inserting registration into DB " + e);
- nextOnErr();
- } else {
- next(body.rev);
- }
- });
-}
-function unregisterDebugClient(key, next, nextOnErr) {
- var db = cloudant.db.use(dbName);
-
- db.get(key, function(err, body, header) {
- if (err) {
- nextOnErr();
- } else {
- db.destroy(key, body.rev, function(err, body, header) {
- if (err) {
- // TODO log the error
- console.log("Error inserting registration into DB " + e);
- nextOnErr();
- } else {
- next();
- }
- });
- }
- });
-}
-
-function ok(ws) {
- ws.send(JSON.stringify({ status: "ok" }));
-}
-function oops(ws) {
- ws.send(JSON.stringify({ status: "error" }));
-}
-
-function handleClientMessage(ws) {
- var _ok = ok.bind(undefined, ws);
- var _oops = oops.bind(undefined, ws);
-
- return function onMessage(message) {
- try {
- message = JSON.parse(message);
- switch (message.action) {
- case "init":
- registerDebugClient(message.key, _ok, _oops);
- break;
-
- case "end":
- unregisterDebugClient(message.key, _ok, _oops);
- break;
- }
- } catch (e) {
- }
- };
-}
-
-exports.registerDebugClient_route = function(ws, res) {
- ws.on('message', handleClientMessage(ws));
- ws.on('close',
-}
diff --git a/nodejs/lib/db.js b/nodejs/lib/db.js
index 5d20731..3fa8c04 100644
--- a/nodejs/lib/db.js
+++ b/nodejs/lib/db.js
@@ -1,4 +1,4 @@
-var Cloudant = require("cloudant"),
+/*var Cloudant = require("cloudant"),
VCAP_SERVICES = JSON.parse(process.env.VCAP_SERVICES),
instanceName = 'OWDBG',
dbName = 'owdbg-registry';
@@ -14,7 +14,7 @@
} catch (e) {
console.log("Could not initialize cloudant");
cloudant = undefined;
-}
+}*/
/*var WebSocketServer = require('ws').Server;
function startWebSocket(onMessage, docRev) {
@@ -29,13 +29,15 @@
}*/
exports.init = function(next) {
- if (!cloudant) return next(); // error in initialization
+/* if (!cloudant) return next(); // error in initialization
- cloudant.db.create(dbName, next);
+ cloudant.db.create(dbName, next);*/
+ next();
}
-function registerDebugClient(key, next, nextOnErr) {
- var db = cloudant.db.use(dbName);
+var db = {};
+function registerDebugClient(key, ws, next, nextOnErr) {
+/* var db = cloudant.db.use(dbName);
db.insert({}, key, function(err, body, header) {
if (err) {
@@ -45,10 +47,15 @@
} else {
next(body.rev);
}
- });
+ });*/
+ db[key] = {
+ ws: ws,
+ activations: {}
+ }
+ next();
}
-function unregisterDebugClient(key, next, nextOnErr) {
- var db = cloudant.db.use(dbName);
+function unregisterDebugClient(key, activationId, result, next, nextOnErr) {
+/* var db = cloudant.db.use(dbName);
db.get(key, function(err, body, header) {
if (err) {
@@ -64,7 +71,22 @@
}
});
}
- });
+ });*/
+ console.log('UNREGISTER ' + key + ' ' + activationId);
+ var client = db[key];
+ if (client) {
+ console.log('UNREGISTER:GotClient ' + JSON.stringify(client.activations));
+ var activation = client.activations[activationId];
+ if (activation) {
+ console.log('UNREGISTER:GotActivation => ' + result);
+ activation.result = result;
+ }
+ delete db[key];
+ }
+}
+
+exports.getClient = function getClient(key) {
+ return db[key];
}
function ok(ws) {
@@ -79,22 +101,26 @@
var _oops = oops.bind(undefined, ws);
return function onMessage(message) {
+ console.log('MESSAGE');
try {
message = JSON.parse(message);
- switch (message.action) {
- case "init":
- registerDebugClient(message.key, _ok, _oops);
+ console.log('MESSAGE:TYPE ' + message.type + " " + JSON.stringify(message, undefined, 4));
+ switch (message.type) {
+ case 'init':
+ registerDebugClient(message.key, ws, _ok, _oops);
break;
- case "end":
- unregisterDebugClient(message.key, _ok, _oops);
+ case 'end':
+ unregisterDebugClient(message.key, message.activationId, message.result, _ok, _oops);
break;
}
} catch (e) {
+ console.log('WS:handleCLientMessage:Error ' + JSON.stringify(e));
}
};
}
-exports.registerDebugClient_route = function(ws, res) {
+exports.registerDebugClient = function(ws, req) {
+ console.log("FFFFFFFFFFFFFFFFFFFFFFFFFFF");
ws.on('message', handleClientMessage(ws));
}
diff --git a/nodejs/lib/invoker.js b/nodejs/lib/invoker.js
new file mode 100644
index 0000000..f72b438
--- /dev/null
+++ b/nodejs/lib/invoker.js
@@ -0,0 +1,79 @@
+var getClient = require('./db').getClient,
+ uuid = require('uuid'),
+ request = require('request');
+
+var activations = {};
+
+exports.invoke = function(req, res) {
+ var key = req.body.key;
+ var action = req.body.action;
+ var namespace = req.body.namespace || "_";
+
+ console.log("INVOKE:Begin " + key + " " + action);
+
+ var client = getClient(key);
+ if (client) {
+ request({
+ url: "https://openwhisk.ng.bluemix.net/api/v1"
+ + "/namespaces/" + encodeURIComponent(namespace)
+ + "/actions/" + encodeURIComponent(action),
+ method: "GET",
+ headers: {
+ "Authorization": 'basic ' + new Buffer(key).toString('base64')
+ }
+ }, function(err, response, body) {
+ if (err || response.statusCode != 200) {
+ if (err) console.log("INVOKE:ErrorFetchingAction " + JSON.stringify(err));
+ else console.log("INVOKE:ErrorFetchingAction_b " + JSON.stringify(response) + " " + JSON.stringify(body));
+ res.status(response.statusCode || 500).send(body);
+ } else {
+ var activationId = uuid.v4();
+
+ client.ws.send(JSON.stringify({
+ type: "invoke",
+ key: key,
+ activationId: activationId,
+ onDone_trigger: req.body.onDone_trigger,
+ actualParameters: req.body.actualParameters,
+ action: JSON.parse(body)
+ }), function onError(error) {
+ console.log("INVOKE:ErrorSendingToClient " + JSON.stringify(error) + " " + client.ws.readyState);
+ });
+
+ client.activations[activationId] = activations[activationId] = {
+ result: undefined,
+ action: JSON.parse(body)
+ };
+
+ res.status(200).send({
+ activationId: activationId
+ });
+ }
+ });
+
+ } else {
+ console.log("INVOKE:ClientNotFound");
+ res.sendStatus(404);
+ }
+}
+
+exports.status = function(req, res) {
+ var key = req.headers.authkey;
+ var activationId = req.params.activationId;
+
+ console.log("INVOKE:Status " + key + " " + activationId + " " + JSON.stringify(req.headers));
+
+ var activation = activations[activationId];
+ if (activation) {
+ console.log("INVOKE:Status:Result " + JSON.stringify(activation.result));
+ res.status(200).send(JSON.stringify({
+ result: activation.result
+ }));
+ } else {
+ nope(res, "Could not find activationId for this debug client " + activationId);
+ }
+}
+
+function nope(res, message) {
+ res.status(404).send(JSON.stringify({ message: message }));
+}
diff --git a/nodejs/package.json b/nodejs/package.json
index 86cd662..8defaae 100644
--- a/nodejs/package.json
+++ b/nodejs/package.json
@@ -9,11 +9,14 @@
},
"author": "IBM",
"dependencies": {
+ "body-parser": "^1.15.2",
"bufferutil": "^1.2.1",
"cloudant": "^1.4.3",
"express": "^4.14.0",
"express-ws": "^2.0.0",
"properties-parser": "^0.3.1",
+ "request": "^2.75.0",
+ "uuid": "^2.0.3",
"ws": "^1.1.1"
}
}
diff --git a/nodejs/push.sh b/nodejs/push.sh
index 1f37d89..51cfd92 100755
--- a/nodejs/push.sh
+++ b/nodejs/push.sh
@@ -1,5 +1,5 @@
#!/bin/bash
-cf push owdbg --no-start
-cf bind-service owdbg OWDBG
-cf start owdbg
+cf push owdbg-broker --no-start
+#cf bind-service owdbg OWDBG
+cf start owdbg-broker