nutella_framework 0.4.5 → 0.4.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (74) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +14 -15
  3. data/VERSION +1 -1
  4. data/framework_components/beacon-cloud-bot/README.md +27 -0
  5. data/framework_components/beacon-cloud-bot/beacon_cloud_bot.rb +154 -0
  6. data/framework_components/beacon-cloud-bot/nutella.json +6 -0
  7. data/framework_components/beacon-cloud-bot/startup +4 -0
  8. data/framework_components/beacon-cloud-interface/LICENSE +21 -0
  9. data/framework_components/beacon-cloud-interface/Readme.md +0 -0
  10. data/framework_components/beacon-cloud-interface/bower.json +29 -0
  11. data/framework_components/beacon-cloud-interface/bower_components/bower-mqttws/.bower.json +23 -0
  12. data/framework_components/beacon-cloud-interface/bower_components/bower-mqttws/bower.json +14 -0
  13. data/framework_components/beacon-cloud-interface/bower_components/bower-mqttws/mqttws31.js +2081 -0
  14. data/framework_components/beacon-cloud-interface/bower_components/bower-mqttws/readme.md +4 -0
  15. data/framework_components/beacon-cloud-interface/bower_components/nutella_lib/.bower.json +37 -0
  16. data/framework_components/beacon-cloud-interface/bower_components/nutella_lib/LICENSE +21 -0
  17. data/framework_components/beacon-cloud-interface/bower_components/nutella_lib/README.md +15 -0
  18. data/framework_components/beacon-cloud-interface/bower_components/nutella_lib/bower.json +28 -0
  19. data/framework_components/beacon-cloud-interface/bower_components/nutella_lib/examples/browser/mqtt_client_hello_world.html +23 -0
  20. data/framework_components/beacon-cloud-interface/bower_components/nutella_lib/examples/browser/nutella_hello_world.html +52 -0
  21. data/framework_components/beacon-cloud-interface/bower_components/nutella_lib/examples/node/mqtt_client_hello_world.js +14 -0
  22. data/framework_components/beacon-cloud-interface/bower_components/nutella_lib/examples/node/nutella_hello_world.js +38 -0
  23. data/framework_components/beacon-cloud-interface/bower_components/nutella_lib/nutella_lib.js +789 -0
  24. data/framework_components/beacon-cloud-interface/bower_components/nutella_lib/package.json +30 -0
  25. data/framework_components/beacon-cloud-interface/bower_components/nutella_lib/simple-js-mqtt-client.js +428 -0
  26. data/framework_components/beacon-cloud-interface/css/animation.css +17 -0
  27. data/framework_components/beacon-cloud-interface/css/cursor.css +16 -0
  28. data/framework_components/beacon-cloud-interface/css/page_layout.css +73 -0
  29. data/framework_components/beacon-cloud-interface/index.html +157 -0
  30. data/framework_components/beacon-cloud-interface/js/lib/nutella_lib.js +4039 -0
  31. data/framework_components/beacon-cloud-interface/js/react/beacon-add.js +102 -0
  32. data/framework_components/beacon-cloud-interface/js/react/beacon-table.js +73 -0
  33. data/framework_components/beacon-cloud-interface/js/react/beacon.js +97 -0
  34. data/framework_components/beacon-cloud-interface/nutella.json +6 -0
  35. data/framework_components/example_framework_web_interface/index.html +11 -2
  36. data/framework_components/example_framework_web_interface/node_modules/nutella_lib/.npmignore +10 -0
  37. data/framework_components/example_framework_web_interface/node_modules/nutella_lib/.travis.yml +5 -0
  38. data/framework_components/example_framework_web_interface/node_modules/nutella_lib/LICENSE +21 -0
  39. data/framework_components/example_framework_web_interface/node_modules/nutella_lib/README.md +27 -0
  40. data/framework_components/example_framework_web_interface/node_modules/nutella_lib/dist/nutella_lib.js +4039 -0
  41. data/framework_components/example_framework_web_interface/node_modules/nutella_lib/dist/nutella_lib.js.map +1 -0
  42. data/framework_components/example_framework_web_interface/node_modules/nutella_lib/examples/browser_hello_world.html +67 -0
  43. data/framework_components/example_framework_web_interface/node_modules/nutella_lib/examples/node_hello_world.js +51 -0
  44. data/framework_components/example_framework_web_interface/node_modules/nutella_lib/gulpfile.js +31 -0
  45. data/framework_components/example_framework_web_interface/node_modules/nutella_lib/package.json +41 -0
  46. data/framework_components/example_framework_web_interface/node_modules/nutella_lib/src/app_core.js +19 -0
  47. data/framework_components/example_framework_web_interface/node_modules/nutella_lib/src/app_core_browser.js +17 -0
  48. data/framework_components/example_framework_web_interface/node_modules/nutella_lib/src/app_log.js +50 -0
  49. data/framework_components/example_framework_web_interface/node_modules/nutella_lib/src/app_net.js +279 -0
  50. data/framework_components/example_framework_web_interface/node_modules/nutella_lib/src/app_persist.js +20 -0
  51. data/framework_components/example_framework_web_interface/node_modules/nutella_lib/src/fr_core_browser.js +17 -0
  52. data/framework_components/example_framework_web_interface/node_modules/nutella_lib/src/fr_log.js +50 -0
  53. data/framework_components/example_framework_web_interface/node_modules/nutella_lib/src/fr_net.js +499 -0
  54. data/framework_components/example_framework_web_interface/node_modules/nutella_lib/src/nutella_i.js +74 -0
  55. data/framework_components/example_framework_web_interface/node_modules/nutella_lib/src/nutella_i_browser.js +130 -0
  56. data/framework_components/example_framework_web_interface/node_modules/nutella_lib/src/nutella_lib.js +91 -0
  57. data/framework_components/example_framework_web_interface/node_modules/nutella_lib/src/nutella_lib_browser.js +90 -0
  58. data/framework_components/example_framework_web_interface/node_modules/nutella_lib/src/run_log.js +51 -0
  59. data/framework_components/example_framework_web_interface/node_modules/nutella_lib/src/run_net.js +84 -0
  60. data/framework_components/example_framework_web_interface/node_modules/nutella_lib/src/run_persist.js +20 -0
  61. data/framework_components/example_framework_web_interface/node_modules/nutella_lib/src/util/net.js +327 -0
  62. data/framework_components/example_framework_web_interface/node_modules/nutella_lib/test/nutella.test.js +16 -0
  63. data/framework_components/example_framework_web_interface/node_modules/nutella_lib/test/runner.html +22 -0
  64. data/framework_components/example_framework_web_interface/package.json +15 -0
  65. data/framework_components/{order.json.example → order.json} +0 -0
  66. data/framework_components/runs_list_bot/{app_runs_list_bot.rb → runs_list_bot.rb} +9 -3
  67. data/framework_components/runs_list_bot/startup +1 -1
  68. data/lib/commands/meta/run_command.rb +21 -36
  69. data/lib/commands/start.rb +9 -199
  70. data/lib/commands/util/components_list.rb +68 -0
  71. data/lib/commands/util/components_starter.rb +169 -0
  72. data/nutella_framework.gemspec +109 -47
  73. data/nutella_lib/framework_net.rb +17 -13
  74. metadata +84 -106
@@ -0,0 +1 @@
1
+ {"version":3,"names":[],"mappings":"","sources":["nutella_lib.js"],"sourcesContent":["(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require==\"function\"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error(\"Cannot find module '\"+o+\"'\");throw f.code=\"MODULE_NOT_FOUND\",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require==\"function\"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){\n/******************\n * nutella_lib.js *\n ******************/\nvar unique = require('uniq');\n\nvar pippo = [1, 2, 2, 3, 4, 5, 5, 5, 6];\n\nconsole.log(unique(pippo));\n},{\"uniq\":2}],2:[function(require,module,exports){\n\"use strict\"\n\nfunction unique_pred(list, compare) {\n var ptr = 1\n , len = list.length\n , a=list[0], b=list[0]\n for(var i=1; i<len; ++i) {\n b = a\n a = list[i]\n if(compare(a, b)) {\n if(i === ptr) {\n ptr++\n continue\n }\n list[ptr++] = a\n }\n }\n list.length = ptr\n return list\n}\n\nfunction unique_eq(list) {\n var ptr = 1\n , len = list.length\n , a=list[0], b = list[0]\n for(var i=1; i<len; ++i, b=a) {\n b = a\n a = list[i]\n if(a !== b) {\n if(i === ptr) {\n ptr++\n continue\n }\n list[ptr++] = a\n }\n }\n list.length = ptr\n return list\n}\n\nfunction unique(list, compare, sorted) {\n if(list.length === 0) {\n return list\n }\n if(compare) {\n if(!sorted) {\n list.sort(compare)\n }\n return unique_pred(list, compare)\n }\n if(!sorted) {\n list.sort()\n }\n return unique_eq(list)\n}\n\nmodule.exports = unique\n\n},{}]},{},[1]);\n"],"file":"nutella_lib.js","sourceRoot":"/source/"}
@@ -0,0 +1,67 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head lang="en">
4
+ <meta charset="UTF-8">
5
+ <title>Nutella hello world</title>
6
+ </head>
7
+ <body>
8
+
9
+ <script src="../dist/nutella_lib.js"></script>
10
+
11
+ <script>
12
+ // Initialize nutella
13
+ var nutella = NUTELLA.init('ltg.evl.uic.edu', 'my_app_id', 'my_run_id', 'demo_run_interface');
14
+ nutella.setResourceId('r_id');
15
+
16
+ // Initialize nutella app interface
17
+ // var nutella = NUTELLA.initApp('ltg.evl.uic.edu', 'test', 'demo_app_interface');
18
+ // nutella.setResourceId('r_id');
19
+ // nutella.app.net.publish_to_all_runs('channel', 'ale_message');
20
+
21
+ // Initialize nutella fr interface
22
+ // var nutella = NUTELLA.initFramework('ltg.evl.uic.edu', 'demo_fr_interface');
23
+ // nutella.setResourceId('r_id');
24
+ // nutella.f.net.publish_to_all_runs('channel', 'message');
25
+
26
+ // Simple channel pub/sub
27
+ // nutella.net.subscribe('channel', function(message, from) {
28
+ // console.log('Received: "' + message + '" from: ' + JSON.stringify(from));
29
+ // }, function() {
30
+ // nutella.net.publish('channel', 'message');
31
+ // });
32
+
33
+ // Simple channel req/res
34
+ // nutella.net.handle_requests('channel', function(req, from){
35
+ // console.log('Received: "' + req + '" from: ' + JSON.stringify(from));
36
+ // return {some: 'json'};
37
+ // }, function() {
38
+ // nutella.net.request('channel', 'hey you', function(res){
39
+ // console.log('Response is : "' + JSON.stringify(res));
40
+ // });
41
+ // });
42
+
43
+ // Wildcard pub/sub
44
+ // nutella.net.subscribe('#', function(message, channel, from) {
45
+ // console.log('Received: "' + JSON.stringify(message) + '" on channel: "'+ channel +'" from: ' + JSON.stringify(from));
46
+ // }, function() {
47
+ // nutella.net.publish('channel_1', 'message');
48
+ // nutella.net.publish('channel_2', 'message');
49
+ // });
50
+
51
+
52
+ // Wildcard req/res
53
+ // nutella.net.handle_requests('#', function(req, from){
54
+ // console.log('Received: "' + req + '" from: ' + JSON.stringify(from));
55
+ // return {some: 'json'};
56
+ // }, function() {
57
+ // nutella.net.request('channel_1', 'hey you', function(res){
58
+ // console.log('Response is : "' + JSON.stringify(res));
59
+ // });
60
+ // nutella.net.request('channel_2', 'hey you', function(res){
61
+ // console.log('Response is : "' + JSON.stringify(res));
62
+ // });
63
+ // });
64
+
65
+ </script>
66
+ </body>
67
+ </html>
@@ -0,0 +1,51 @@
1
+ var NUTELLA = require('../src/nutella_lib')
2
+
3
+ // Initialize nutella
4
+ //var p = NUTELLA.parseURLParameters(); // This only works in the browser
5
+ var p = NUTELLA.parseAppComponentArgs();
6
+ console.log(p)
7
+ var nutella = NUTELLA.init('ltg.evl.uic.edu', 'my_app_id', 'my_run_id', 'demo_node_bot');
8
+ nutella.setResourceId('r_id');
9
+ nutella.log.test();
10
+ nutella.net.publish('channel', 'message');
11
+ nutella.persist.test(); // this should only work in node
12
+ nutella = NUTELLA.initApp('ltg.evl.uic.edu', 'my_app_id', 'demo_node_bot');
13
+ nutella.app.net.test();
14
+ nutella.app.log.test();
15
+ nutella.app.persist.test();
16
+
17
+
18
+
19
+ // // Subscribe to a channel
20
+ // nutella.net.subscribe("demo1", function(message, c_id, r_id) {
21
+ // console.log('Received "' + JSON.stringify(message) + '" from ' + c_id + '/' + r_id);
22
+ // nutella.net.unsubscribe('demo1');
23
+ // });
24
+
25
+ // // Wildcard subscribe
26
+ // nutella.net.subscribe("demo2/#", function(message, channel, c_id, r_id) {
27
+ // console.log('Received "' + JSON.stringify(message) + '" on channel ' + channel + ' from ' + c_id + '/' + r_id);
28
+ // });
29
+
30
+ // // Publish some stuff
31
+ // nutella.net.publish('demo1');
32
+ // nutella.net.publish('demo1', 'just a string');
33
+ // nutella.net.publish('demo1', {a: 'proper', key: 'value'});
34
+ // nutella.setResourceId('a_particular_resource');
35
+ // nutella.net.publish('demo1');
36
+ // nutella.net.publish('demo1', 'just a string');
37
+ // nutella.net.publish('demo1', {a: 'proper', key: 'value'});
38
+
39
+ // Handle requests
40
+ //nutella.net.handle_requests('demo1', function(message, component_id, resource_id) {
41
+ // return 'this is the returned value';
42
+ //});
43
+ //
44
+ //// Perform a couple requests
45
+ //nutella.net.request('demo1', function(response) {
46
+ // console.log("This is the response to empty request (GET)");
47
+ //});
48
+ //
49
+ //nutella.net.request('demo1', 'my_request', function(response) {
50
+ // console.log("This is the response to non-empty request");
51
+ //});
@@ -0,0 +1,31 @@
1
+ var gulp = require('gulp');
2
+ var gutil = require('gulp-util');
3
+ var browserify = require('browserify');
4
+ var watchify = require('watchify');
5
+ var source = require('vinyl-source-stream');
6
+
7
+ // bundler used to run watchify and browserify
8
+ var bundler = watchify(browserify(watchify.args, {standalone: 'NUTELLA'}));
9
+ // add the lib file to bundle
10
+ bundler.add('./src/nutella_lib.js');
11
+
12
+
13
+ gulp.task('bundle', bundle); // so you can run `gulp js` to build the file
14
+ bundler.on('update', bundle); // on any dep update, runs the bundler
15
+ bundler.on('log', gutil.log); // output build logs to terminal
16
+
17
+ function bundle() {
18
+ return bundler.bundle()
19
+ // log errors if they happen
20
+ .on('error', gutil.log.bind(gutil, 'Browserify Error'))
21
+ .pipe(source('nutella_lib.js'))
22
+ .pipe(gulp.dest('./dist'));
23
+ }
24
+
25
+
26
+ gulp.task('default', function() {
27
+ // place code for your default task here
28
+ });
29
+
30
+
31
+
@@ -0,0 +1,41 @@
1
+ {
2
+ "name": "nutella_lib",
3
+ "version": "0.4.3",
4
+ "description": "Nutella library for JavaScript",
5
+ "main": "src/nutella_lib.js",
6
+ "browser": "src/nutella_lib_browser.js",
7
+ "scripts": {
8
+ "test": "mocha"
9
+ },
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "https://github.com/nutella-framework/nutella_lib.js"
13
+ },
14
+ "devDependencies": {
15
+ "browserify": "^9.0.3",
16
+ "chai": "^2.1.2",
17
+ "gulp": "^3.8.11",
18
+ "gulp-util": "^3.0.4",
19
+ "mocha": "^2.2.1",
20
+ "vinyl-source-stream": "^1.1.0",
21
+ "watchify": "^2.6.2"
22
+ },
23
+ "keywords": [
24
+ "nutella",
25
+ "framework"
26
+ ],
27
+ "author": {
28
+ "name": "Alessandro Gnoli"
29
+ },
30
+ "license": "MIT",
31
+ "bugs": {
32
+ "url": "https://github.com/nutella-framework/nutella_lib.js/issues"
33
+ },
34
+ "homepage": "https://github.com/nutella-framework/nutella_lib.js",
35
+ "readme": "[![Build Status](https://travis-ci.org/nutella-framework/nutella_lib.js.svg?branch=master)](https://travis-ci.org/nutella-framework/nutella_lib.js)\n\n# nutella library for node.js and the browser\n\n## Installation\nFor node.js projects do\n```\nnpm install nutella_lib\n```\n\nFor browser projects either:\n\n1. `npm install nutella_lib` and then use [browserify](http://browserify.org/) OR\n2. use the bundled `nutella_lib.js` in `dist`\n\n\n## Building the project\nFor developers working on the library. We are using gulp + browserify + watchify to continuously and incrementally build the library as we develop. \n\n**To contribute**: Clone the repo and `gulp bundle` inside the project directory. Every time you make a change to any of the files required by the library gulp will rebuild it. \n\n\n## Releasing a new version\nFor developers working on the library, to release a new version:\n\n- Update the version in the `package.json`\n- Publish to npm by doing `npm publish`",
36
+ "readmeFilename": "README.md",
37
+ "gitHead": "92101787b67f169eb26aa2139110c6a0ab307abf",
38
+ "_id": "nutella_lib@0.4.3",
39
+ "_shasum": "21dbfd227fab6a500dc6ccb1bdc2a764908910e2",
40
+ "_from": "nutella_lib@*"
41
+ }
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Application-level APIs for nutella, node version
3
+ */
4
+
5
+ // Require various sub-modules
6
+ var AppNetSubModule = require('./app_net');
7
+ var AppLogSubModule = require('./app_log');
8
+ var AppPersistSubModule = require('./app_persist');
9
+
10
+
11
+ var AppSubModule = function(main_nutella) {
12
+ // Initialized the various sub-modules
13
+ this.net = new AppNetSubModule(main_nutella);
14
+ this.log = new AppLogSubModule(main_nutella);
15
+ this.persist = new AppPersistSubModule(main_nutella);
16
+ };
17
+
18
+
19
+ module.exports = AppSubModule;
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Application-level APIs for nutella, browser version
3
+ */
4
+
5
+ // Require various sub-modules
6
+ var AppNetSubModule = require('./app_net');
7
+ var AppLogSubModule = require('./app_log');
8
+
9
+
10
+ var AppSubModule = function(main_nutella) {
11
+ // Initialized the various sub-modules
12
+ this.net = new AppNetSubModule(main_nutella);
13
+ this.log = new AppLogSubModule(main_nutella);
14
+ };
15
+
16
+
17
+ module.exports = AppSubModule;
@@ -0,0 +1,50 @@
1
+ /**
2
+ * App-level log APIs for nutella
3
+ */
4
+
5
+ var AppNetSubModule = require('./app_net');
6
+
7
+ var AppLogSubModule = function(main_nutella) {
8
+ this.net = new AppNetSubModule(main_nutella);
9
+ };
10
+
11
+
12
+
13
+ AppLogSubModule.prototype.debug = function(message, code) {
14
+ console.debug(message);
15
+ this.net.publish('logging', logToJson(message, code, 'debug'));
16
+ return code;
17
+ };
18
+
19
+ AppLogSubModule.prototype.info = function(message, code) {
20
+ console.info(message);
21
+ this.net.publish('logging', logToJson(message, code, 'info'));
22
+ return code;
23
+ };
24
+
25
+ AppLogSubModule.prototype.success = function(message, code) {
26
+ console.log('%c '+ message , 'color: #009933');
27
+ this.net.publish('logging', logToJson(message, code, 'success'));
28
+ return code;
29
+ };
30
+
31
+ AppLogSubModule.prototype.warn = function(message, code) {
32
+ console.warn(message);
33
+ this.net.publish('logging', logToJson(message, code, 'warn'));
34
+ return code;
35
+ };
36
+
37
+ AppLogSubModule.prototype.error = function(message, code) {
38
+ console.error(message);
39
+ this.net.publish('logging', logToJson(message, code, 'error'));
40
+ return code;
41
+ };
42
+
43
+
44
+ function logToJson( message, code, level) {
45
+ return (code === undefined) ? {level: level, message: message} : {level: level, message: message, code: code};
46
+ }
47
+
48
+
49
+
50
+ module.exports = AppLogSubModule;
@@ -0,0 +1,279 @@
1
+ /**
2
+ * App-level Networking APIs for nutella
3
+ */
4
+
5
+
6
+ var AbstractNet = require('./util/net');
7
+
8
+
9
+ /**
10
+ * App-level network APIs for nutella
11
+ * @param main_nutella
12
+ * @constructor
13
+ */
14
+ var AppNetSubModule = function(main_nutella) {
15
+ this.net = new AbstractNet(main_nutella);
16
+ };
17
+
18
+
19
+
20
+ /**
21
+ * Subscribes to a channel or filter.
22
+ *
23
+ * @param channel
24
+ * @param callback
25
+ * @param done_callback
26
+ */
27
+ AppNetSubModule.prototype.subscribe = function(channel, callback, done_callback) {
28
+ this.net.subscribe_to(channel, callback, this.net.nutella.appId, undefined, done_callback);
29
+ };
30
+
31
+
32
+
33
+ /**
34
+ * Unsubscribes from a channel
35
+ *
36
+ * @param channel
37
+ * @param done_callback
38
+ */
39
+ AppNetSubModule.prototype.unsubscribe = function(channel, done_callback) {
40
+ this.net.unsubscribe_from(channel, this.net.nutella.appId, undefined, done_callback);
41
+ };
42
+
43
+
44
+
45
+ /**
46
+ * Publishes a message to a channel
47
+ *
48
+ * @param channel
49
+ * @param message
50
+ */
51
+ AppNetSubModule.prototype.publish = function(channel, message) {
52
+ this.net.publish_to(channel, message, this.net.nutella.appId, undefined);
53
+ };
54
+
55
+
56
+
57
+ /**
58
+ * Sends a request.
59
+ *
60
+ * @param channel
61
+ * @param message
62
+ * @param callback
63
+ */
64
+ AppNetSubModule.prototype.request = function(channel, message, callback) {
65
+ this.net.request_to(channel, message, callback, this.net.nutella.appId, undefined);
66
+ };
67
+
68
+
69
+
70
+ /**
71
+ * Handles requests.
72
+ *
73
+ * @param channel
74
+ * @param callback
75
+ * @param done_callback
76
+ */
77
+ AppNetSubModule.prototype.handle_requests = function (channel, callback, done_callback) {
78
+ this.net.handle_requests_on(channel, callback, this.net.nutella.appId, undefined, done_callback);
79
+ };
80
+
81
+
82
+
83
+ //----------------------------------------------------------------------------------------------------------------
84
+ // Application-level APIs to communicate at the run-level
85
+ //----------------------------------------------------------------------------------------------------------------
86
+
87
+ /**
88
+ * Allows application-level APIs to subscribe to a run-level channel within a specific run
89
+ *
90
+ * @param run_id
91
+ * @param channel
92
+ * @param callback
93
+ * @param done_callback
94
+ */
95
+ AppNetSubModule.prototype.subscribe_to_run = function(run_id, channel, callback, done_callback) {
96
+ this.net.subscribe_to(channel,callback,this.net.nutella.appId,run_id,done_callback);
97
+ };
98
+
99
+
100
+ /**
101
+ * Allows application-level APIs to unsubscribe from a run-level channel within a specific run
102
+ *
103
+ * @param run_id
104
+ * @param channel
105
+ * @param done_callback
106
+ */
107
+ AppNetSubModule.prototype.unsubscribe_from_run = function(run_id, channel, done_callback) {
108
+ this.net.unsubscribe_from(channel,this.net.nutella.appId,run_id,done_callback);
109
+ };
110
+
111
+
112
+ /**
113
+ * Allows application-level APIs to publish to a run-level channel within a specific run
114
+ *
115
+ * @param run_id
116
+ * @param channel
117
+ * @param message
118
+ */
119
+ AppNetSubModule.prototype.publish_to_run = function( run_id, channel, message ) {
120
+ this.net.publish_to(channel,message,this.net.nutella.appId, run_id);
121
+ };
122
+
123
+
124
+ /**
125
+ * Allows application-level APIs to make a request to a run-level channel within a specific run
126
+ *
127
+ * @param run_id
128
+ * @param channel
129
+ * @param request
130
+ * @param callback
131
+ */
132
+ AppNetSubModule.prototype.request_to_run = function( run_id, channel, request, callback) {
133
+ this.net.request_to(channel,request,callback,this.net.nutella.appId,run_id);
134
+ };
135
+
136
+
137
+ /**
138
+ * Allows application-level APIs to handle requests on a run-level channel within a specific run
139
+ *
140
+ * @param run_id
141
+ * @param channel
142
+ * @param callback
143
+ * @param done_callback
144
+ */
145
+ AppNetSubModule.prototype.handle_requests_on_run = function( run_id, channel, callback, done_callback ) {
146
+ this.net.handle_requests_on(channel,callback,this.net.nutella.appId,run_id,done_callback);
147
+ };
148
+
149
+
150
+ //----------------------------------------------------------------------------------------------------------------
151
+ // Application-level APIs to communicate at the run-level (broadcast)
152
+ //----------------------------------------------------------------------------------------------------------------
153
+
154
+ /**
155
+ * Fired whenever a message is received on the specified channel for any of the runs in the application
156
+ *
157
+ * @callback all_runs_cb
158
+ * @param {string} message - the received message. Messages that are not JSON are discarded.
159
+ * @param {string} run_id - the run_id of the channel the message was sent on
160
+ * @param {Object} from - the sender's identifiers (run_id, app_id, component_id and optionally resource_id)
161
+ */
162
+
163
+ /**
164
+ * Allows application-level APIs to subscribe to a run-level channel *for ALL runs*
165
+ *
166
+ * @param {string} channel - the run-level channel we are subscribing to. Can be wildcard
167
+ * @param {all_runs_cb} callback - the callback that is fired whenever a message is received on the channel
168
+ */
169
+ AppNetSubModule.prototype.subscribe_to_all_runs = function(channel, callback, done_callback) {
170
+ var app_id = this.net.nutella.appId;
171
+ //Pad channel
172
+ var padded_channel = this.net.pad_channel(channel, app_id, '+');
173
+ var mqtt_cb = function(mqtt_message, mqtt_channel) {
174
+ try {
175
+ var f = JSON.parse(mqtt_message);
176
+ var run_id = extractRunId(app_id, mqtt_channel);
177
+ if(f.type==='publish')
178
+ callback(f.payload, run_id, f.from);
179
+ } catch(e) {
180
+ if (e instanceof SyntaxError) {
181
+ // Message is not JSON, drop it
182
+ } else {
183
+ // Bubble up whatever exception is thrown
184
+ throw e;
185
+ }
186
+ }
187
+ };
188
+ // Add to subscriptions, save mqtt callback and subscribe
189
+ this.net.subscriptions.push(padded_channel);
190
+ this.net.callbacks.push(mqtt_cb);
191
+ this.net.nutella.mqtt_client.subscribe(padded_channel, mqtt_cb, done_callback);
192
+ // Notify subscription
193
+ this.net.publish_to('subscriptions', {type: 'subscribe', channel: padded_channel}, this.net.nutella.appId, undefined);
194
+ };
195
+
196
+
197
+ /**
198
+ * Allows application-level APIs to publish a message to a run-level channel *for ALL runs*
199
+ *
200
+ * @param channel
201
+ * @param message
202
+ */
203
+ AppNetSubModule.prototype.publish_to_all_runs = function(channel, message) {
204
+ this.net.nutella.runs_list.forEach(function(run_id){
205
+ this.net.publish_to(channel,message,this.net.nutella.appId,run_id);
206
+ }.bind(this));
207
+ };
208
+
209
+
210
+ /**
211
+ * Allows application-level APIs to send a request to a run-level channel *for ALL runs*
212
+ *
213
+ * @param channel
214
+ * @param request
215
+ * @param callback
216
+ */
217
+ AppNetSubModule.prototype.request_to_all_runs = function(channel, request, callback) {
218
+ this.net.nutella.runs_list.forEach(function(run_id){
219
+ this.net.request_to(channel,request,callback,this.net.nutella.appId,run_id);
220
+ }.bind(this));
221
+ };
222
+
223
+
224
+ /**
225
+ * This callback is used to handle all runs
226
+ * @callback handle_all_run
227
+ * @param {string} message - the received message. Messages that are not JSON are discarded.
228
+ * @param {string} run_id - the run_id of the channel the message was sent on
229
+ * @param {Object} from - the sender's identifiers (run_id, app_id, component_id and optionally resource_id)
230
+ * @return {Object} the response sent back to the client that performed the request. Whatever is returned by the callback is marshaled into a JSON string and sent via MQTT.
231
+ */
232
+
233
+ /**
234
+ * Allows application-level APIs to handle requests to a run-level channel *for ALL runs*
235
+ *
236
+ * @param channel
237
+ * @param callback
238
+ * @param done_callback
239
+ */
240
+ AppNetSubModule.prototype.handle_requests_on_all_runs = function(channel, callback, done_callback) {
241
+ var app_id = this.net.nutella.appId;
242
+ // Pad channel
243
+ var padded_channel = this.net.pad_channel(channel, app_id, '+');
244
+ var ln = this.net;
245
+ var mqtt_cb = function(mqtt_message, mqtt_channel) {
246
+ try {
247
+ var f = JSON.parse(mqtt_message);
248
+ var run_id = extractRunId(app_id, mqtt_channel);
249
+ // Only handle requests that have proper id set
250
+ if(f.type!=='request' || f.id===undefined) return;
251
+ // Execute callback and send response
252
+ var m = ln.prepare_message_for_response(callback(f.payload, run_id, f.from), f.id);
253
+ ln.nutella.mqtt_client.publish( padded_channel, m );
254
+ } catch(e) {
255
+ if (e instanceof SyntaxError) {
256
+ // Message is not JSON, drop it
257
+ } else {
258
+ // Bubble up whatever exception is thrown
259
+ throw e;
260
+ }
261
+ }
262
+ };
263
+ this.net.nutella.mqtt_client.subscribe( padded_channel, mqtt_cb, done_callback);
264
+ // Notify subscription
265
+ this.net.publish_to('subscriptions', {type: 'handle_requests', channel: padded_channel}, this.net.nutella.appId, undefined);
266
+ };
267
+
268
+
269
+
270
+ // Utility function
271
+
272
+ function extractRunId(app_id, mqtt_channel) {
273
+ var pc = '/nutella/apps/' + app_id + '/runs/';
274
+ var sp = mqtt_channel.replace(pc, '').split('/');
275
+ return sp[0];
276
+ }
277
+
278
+
279
+ module.exports = AppNetSubModule;
@@ -0,0 +1,20 @@
1
+ /**
2
+ * App-level persistence APIs for nutella
3
+ */
4
+
5
+
6
+
7
+ var AppPersistSubModule = function(main_nutella) {
8
+ // Store a reference to the main module
9
+ this.main_nutella = main_nutella;
10
+ };
11
+
12
+
13
+
14
+ AppPersistSubModule.prototype.test = function () {
15
+ console.log("This is just a test method for the APP persist sub-module");
16
+ };
17
+
18
+
19
+
20
+ module.exports = AppPersistSubModule;
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Framework-level APIs for nutella, browser version
3
+ */
4
+
5
+ // Require various sub-modules
6
+ var FrNetSubModule = require('./fr_net');
7
+ var FrLogSubModule = require('./fr_log');
8
+
9
+
10
+ var FrSubModule = function(main_nutella) {
11
+ // Initialized the various sub-modules
12
+ this.net = new FrNetSubModule(main_nutella);
13
+ this.log = new FrLogSubModule(main_nutella);
14
+ };
15
+
16
+
17
+ module.exports = FrSubModule;
@@ -0,0 +1,50 @@
1
+ /**
2
+ * Framework-level log APIs for nutella
3
+ */
4
+
5
+ var FrNetSubModule = require('./app_net');
6
+
7
+ var FrLogSubModule = function(main_nutella) {
8
+ this.net = new FrNetSubModule(main_nutella);
9
+ };
10
+
11
+
12
+
13
+ FrLogSubModule.prototype.debug = function(message, code) {
14
+ console.debug(message);
15
+ this.net.publish('logging', logToJson(message, code, 'debug'));
16
+ return code;
17
+ };
18
+
19
+ FrLogSubModule.prototype.info = function(message, code) {
20
+ console.info(message);
21
+ this.net.publish('logging', logToJson(message, code, 'info'));
22
+ return code;
23
+ };
24
+
25
+ FrLogSubModule.prototype.success = function(message, code) {
26
+ console.log('%c '+ message , 'color: #009933');
27
+ this.net.publish('logging', logToJson(message, code, 'success'));
28
+ return code;
29
+ };
30
+
31
+ FrLogSubModule.prototype.warn = function(message, code) {
32
+ console.warn(message);
33
+ this.net.publish('logging', logToJson(message, code, 'warn'));
34
+ return code;
35
+ };
36
+
37
+ FrLogSubModule.prototype.error = function(message, code) {
38
+ console.error(message);
39
+ this.net.publish('logging', logToJson(message, code, 'error'));
40
+ return code;
41
+ };
42
+
43
+
44
+ function logToJson( message, code, level) {
45
+ return (code === undefined) ? {level: level, message: message} : {level: level, message: message, code: code};
46
+ }
47
+
48
+
49
+
50
+ module.exports = FrLogSubModule;