nutella_lib 0.4.0 → 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +1 -0
- data/VERSION +1 -1
- data/lib/nutella_lib/app_core.rb +57 -0
- data/lib/nutella_lib/app_log.rb +49 -0
- data/lib/nutella_lib/{net_app.rb → app_net.rb} +36 -22
- data/lib/nutella_lib/core.rb +18 -49
- data/lib/nutella_lib/log.rb +46 -0
- data/lib/nutella_lib/net.rb +25 -27
- data/lib/nutella_lib.rb +9 -5
- data/nutella_lib.gemspec +11 -4
- data/test/test_logger.rb +26 -0
- metadata +27 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2016c67f84255831a77a366b0e60feb41d533933
|
4
|
+
data.tar.gz: 5249682d97501eda22d951916126f3e539aaf425
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 68d73aba562d55c2f487fcedd3ffbbb0081dd342486fd6079722d2d718fbcc06b79ffeaa1c28301347f479d664fb37c52c3d04dd3f39b3ed1f4fe801ade57f0e
|
7
|
+
data.tar.gz: d9d0434e9ac3dec3bcfb2e3a79aca838b4eeda46313bbddebe44f9059c60e4de39538f9e3a20badd0820493d74f09ac1afd3c265e21a26ceac755abbc0aea8cf
|
data/Gemfile
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.4.
|
1
|
+
0.4.1
|
@@ -0,0 +1,57 @@
|
|
1
|
+
module Nutella
|
2
|
+
|
3
|
+
module App
|
4
|
+
|
5
|
+
# Initializes this component as an application component
|
6
|
+
# @param [String] broker_hostname
|
7
|
+
# @param [String] component_id
|
8
|
+
def self.init( broker_hostname, app_id, component_id )
|
9
|
+
Nutella.app_id = app_id
|
10
|
+
Nutella.run_id = nil
|
11
|
+
Nutella.component_id = component_id
|
12
|
+
Nutella.resource_id = nil
|
13
|
+
Nutella.mqtt = SimpleMQTTClient.new broker_hostname
|
14
|
+
# Fetch the `run_id`s list for this application and subscribe to its updates
|
15
|
+
# @app_runs_list = net.sync_request('app_runs_list')
|
16
|
+
# self.net.subscribe('app_runs_list', lambda {|message, _| Nutella.app.app_runs_list = message })
|
17
|
+
end
|
18
|
+
|
19
|
+
# Setter/getter for runs_list
|
20
|
+
def self.app_runs_list=(val) @app_runs_list=val; end
|
21
|
+
def self.app_runs_list; @app_runs_list end
|
22
|
+
|
23
|
+
# Accessors for sub-modules
|
24
|
+
def self.net; Nutella::App::Net; end
|
25
|
+
def self.log; Nutella::App::Log; end
|
26
|
+
|
27
|
+
|
28
|
+
# Parse command line arguments for app level components
|
29
|
+
#
|
30
|
+
# @param [Array] args command line arguments array
|
31
|
+
# @return [String, String] broker and app_id
|
32
|
+
def self.parse_args(args)
|
33
|
+
if args.length < 2
|
34
|
+
STDERR.puts 'Couldn\'t read broker address and app_id from the command line, impossible to initialize component!'
|
35
|
+
return
|
36
|
+
end
|
37
|
+
return args[0], args[1]
|
38
|
+
end
|
39
|
+
|
40
|
+
# Extracts the component name from the folder where the code for this component is located
|
41
|
+
#
|
42
|
+
# @return [String] the component name
|
43
|
+
def self.extract_component_id
|
44
|
+
Nutella.extract_component_id
|
45
|
+
end
|
46
|
+
|
47
|
+
# Sets the resource id
|
48
|
+
#
|
49
|
+
# @param [String] resource_id the resource id (i.e. the particular instance of this component)
|
50
|
+
def self.set_resource_id( resource_id )
|
51
|
+
Nutella.set_resource_id resource_id
|
52
|
+
end
|
53
|
+
|
54
|
+
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'ansi'
|
2
|
+
|
3
|
+
module Nutella
|
4
|
+
|
5
|
+
module App
|
6
|
+
|
7
|
+
module Log
|
8
|
+
|
9
|
+
def self.debug(message, code=nil)
|
10
|
+
puts( ANSI.cyan + message + ANSI.reset )
|
11
|
+
Nutella.app.net.publish( 'logging', log_to_json(message, code, __method__) )
|
12
|
+
code
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.info(message, code=nil)
|
16
|
+
puts( message )
|
17
|
+
Nutella.app.net.publish( 'logging', log_to_json(message, code, __method__) )
|
18
|
+
code
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.success(message, code=nil)
|
22
|
+
puts( ANSI.green + message + ANSI.reset )
|
23
|
+
Nutella.app.net.publish( 'logging', log_to_json(message, code, __method__) )
|
24
|
+
code
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.warn(message, code=nil)
|
28
|
+
puts( ANSI.yellow + message + ANSI.reset )
|
29
|
+
Nutella.app.net.publish( 'logging', log_to_json(message, code, __method__) )
|
30
|
+
code
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.error(message, code=nil)
|
34
|
+
puts( ANSI.red + message + ANSI.reset )
|
35
|
+
Nutella.app.net.publish( 'logging', log_to_json(message, code, __method__) )
|
36
|
+
code
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
|
41
|
+
def self.log_to_json( message, code, level)
|
42
|
+
code.nil? ? {level: level, message: message} : {level: level, message: message, code: code}
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
@@ -1,8 +1,9 @@
|
|
1
1
|
module Nutella
|
2
|
-
|
2
|
+
|
3
|
+
module App
|
3
4
|
|
4
5
|
# This module implements the pub/sub and request/response APIs at the application level
|
5
|
-
module
|
6
|
+
module Net
|
6
7
|
|
7
8
|
|
8
9
|
# @!group Application-level communication APIs
|
@@ -15,7 +16,7 @@ module Nutella
|
|
15
16
|
# - [String] message: the received message. Messages that are not JSON are discarded.
|
16
17
|
# - [String] channel: the application-level channel the message was received on (optional, only for wildcard subscriptions)
|
17
18
|
# - [Hash] from: the sender's identifiers (run_id, app_id, component_id and optionally resource_id)
|
18
|
-
def
|
19
|
+
def self.subscribe (channel, callback)
|
19
20
|
Nutella::Net.subscribe_to(channel, callback, Nutella.app_id, nil)
|
20
21
|
end
|
21
22
|
|
@@ -23,7 +24,7 @@ module Nutella
|
|
23
24
|
# Un-subscribes from an application-level channel
|
24
25
|
#
|
25
26
|
# @param [String] channel the application level channel we want to unsubscribe from. Can contain wildcard(s).
|
26
|
-
def
|
27
|
+
def self.unsubscribe( channel )
|
27
28
|
Nutella::Net.unsubscribe_to(channel, Nutella.app_id, nil)
|
28
29
|
end
|
29
30
|
|
@@ -33,7 +34,7 @@ module Nutella
|
|
33
34
|
# @param [String] channel the application-level channel we want to publish the message to. *CANNOT* contain wildcard(s)!
|
34
35
|
# @param [Object] message the message we are publishing. This can be,
|
35
36
|
# nil/empty (default), a string, a hash and, in general, anything with a .to_json method.
|
36
|
-
def
|
37
|
+
def self.publish(channel, message=nil)
|
37
38
|
Nutella::Net.publish_to(channel, message, Nutella.app_id, nil)
|
38
39
|
end
|
39
40
|
|
@@ -43,7 +44,7 @@ module Nutella
|
|
43
44
|
# @param [String] channel the application-level channel we want to make the request to. *CANNOT* contain wildcard(s)!
|
44
45
|
# @param [Object] message the body of request. This can be,
|
45
46
|
# nil/empty (default), a string, a hash and, in general, anything with a .to_json method.
|
46
|
-
def
|
47
|
+
def self.sync_request ( channel, message=nil )
|
47
48
|
Nutella::Net.sync_request_to(channel, message, Nutella.app_id, nil)
|
48
49
|
end
|
49
50
|
|
@@ -54,7 +55,7 @@ module Nutella
|
|
54
55
|
# @param [Object] message the body of request. This can be,
|
55
56
|
# nil/empty (default), a string, a hash and, in general, anything with a .to_json method.
|
56
57
|
# @param [Proc] callback the callback that is fired whenever a response is received. It takes one parameter (response).
|
57
|
-
def
|
58
|
+
def self.async_request ( channel, message=nil, callback )
|
58
59
|
Nutella::Net.async_request_to(channel, message, callback, Nutella.app_id, nil)
|
59
60
|
end
|
60
61
|
|
@@ -67,7 +68,7 @@ module Nutella
|
|
67
68
|
# - [String] the received message (payload). Messages that are not JSON are discarded.
|
68
69
|
# - [Hash] the sender's identifiers (run_id, app_id, component_id and optionally resource_id)
|
69
70
|
# - [*returns* Hash] 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.
|
70
|
-
def
|
71
|
+
def self.handle_requests( channel, callback )
|
71
72
|
Nutella::Net.handle_requests_on(channel, callback, Nutella.app_id, nil)
|
72
73
|
end
|
73
74
|
|
@@ -85,7 +86,7 @@ module Nutella
|
|
85
86
|
# - [String] message: the received message. Messages that are not JSON are discarded.
|
86
87
|
# - [String] channel: the application-level channel the message was received on (optional, only for wildcard subscriptions)
|
87
88
|
# - [Hash] from: the sender's identifiers (run_id, app_id, component_id and optionally resource_id)
|
88
|
-
def
|
89
|
+
def self.subscribe_to_run( run_id, channel, callback )
|
89
90
|
Nutella::Net.subscribe_to(channel, callback, Nutella.app_id, run_id)
|
90
91
|
end
|
91
92
|
|
@@ -94,7 +95,7 @@ module Nutella
|
|
94
95
|
#
|
95
96
|
# @param [String] run_id the specific run we are un-subscribing from
|
96
97
|
# @param [String] channel the run-level channel we want to unsubscribe from. Can contain wildcard(s).
|
97
|
-
def
|
98
|
+
def self.unsubscribe_to_run( run_id, channel )
|
98
99
|
Nutella::Net.unsubscribe_to(channel, Nutella.app_id, run_id)
|
99
100
|
end
|
100
101
|
|
@@ -105,7 +106,7 @@ module Nutella
|
|
105
106
|
# @param [String] channel the run-level channel we want to publish the message to. *CANNOT* contain wildcard(s)!
|
106
107
|
# @param [String] message the message we are publishing. This can be,
|
107
108
|
# nil/empty (default), a string, a hash and, in general, anything with a .to_json method.
|
108
|
-
def
|
109
|
+
def self.publish_to_run( run_id, channel, message )
|
109
110
|
Nutella::Net.publish_to(channel, message, Nutella.app_id, run_id)
|
110
111
|
end
|
111
112
|
|
@@ -116,7 +117,7 @@ module Nutella
|
|
116
117
|
# @param [String] channel the channel we want to make the request to. *CANNOT* contain wildcard(s)!
|
117
118
|
# @param [Object] request the body of request. This can be,
|
118
119
|
# nil/empty (default), a string, a hash and, in general, anything with a .to_json method.
|
119
|
-
def
|
120
|
+
def self.sync_request_to_run( run_id, channel, request)
|
120
121
|
Nutella::Net.sync_request_to(channel, request, Nutella.app_id, run_id)
|
121
122
|
end
|
122
123
|
|
@@ -128,7 +129,7 @@ module Nutella
|
|
128
129
|
# @param [Object] request the body of request. This can be,
|
129
130
|
# nil/empty (default), a string, a hash and, in general, anything with a .to_json method.
|
130
131
|
# @param [Proc] callback the callback that is fired whenever a response is received. It takes one parameter (response).
|
131
|
-
def
|
132
|
+
def self.async_request_to_run( run_id, channel, request, callback)
|
132
133
|
Nutella::Net.async_request_to(channel, request, callback, Nutella.app_id, run_id)
|
133
134
|
end
|
134
135
|
|
@@ -142,7 +143,7 @@ module Nutella
|
|
142
143
|
# - [String] the received message (payload). Messages that are not JSON are discarded.
|
143
144
|
# - [Hash] the sender's identifiers (run_id, app_id, component_id and optionally resource_id)
|
144
145
|
# - [*returns* Hash] 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.
|
145
|
-
def
|
146
|
+
def self.handle_requests_on_run( run_id, channel, callback )
|
146
147
|
Nutella::Net.handle_requests_on(channel, callback, Nutella.app_id, run_id)
|
147
148
|
end
|
148
149
|
|
@@ -159,7 +160,7 @@ module Nutella
|
|
159
160
|
# - [String] message: the received message. Messages that are not JSON are discarded.
|
160
161
|
# - [String] run_id: the run_id of the channel the message was sent on
|
161
162
|
# - [Hash] from: the sender's identifiers (run_id, app_id, component_id and optionally resource_id)
|
162
|
-
def
|
163
|
+
def self.subscribe_to_all_runs( channel, callback )
|
163
164
|
# Check the passed callback has the right number of arguments
|
164
165
|
raise 'You need to pass a callback with 3 parameters (payload, run_id, from) when subscribing to all runs!' if callback.parameters.length!=3
|
165
166
|
# Pad channel
|
@@ -187,7 +188,7 @@ module Nutella
|
|
187
188
|
# Allows application-level APIs to unsubscribe from a run-level channel *for ALL runs*
|
188
189
|
#
|
189
190
|
# @param [String] channel the run-level channel we want to unsubscribe from. Can contain wildcard(s).
|
190
|
-
def
|
191
|
+
def self.unsubscribe_from_all_runs( channel )
|
191
192
|
Nutella::Net.unsubscribe_to(channel, Nutella.app_id, '+')
|
192
193
|
end
|
193
194
|
|
@@ -197,8 +198,8 @@ module Nutella
|
|
197
198
|
# @param [String] channel the run-level channel we want to publish the message to. *CANNOT* contain wildcard(s)!
|
198
199
|
# @param [Object] message the message we are publishing. This can be,
|
199
200
|
# nil/empty (default), a string, a hash and, in general, anything with a .to_json method.
|
200
|
-
def
|
201
|
-
Nutella.app_runs_list.each do |run_id|
|
201
|
+
def self.publish_to_all_runs( channel, message )
|
202
|
+
Nutella.app.app_runs_list.each do |run_id|
|
202
203
|
Nutella::Net.publish_to(channel, message, Nutella.app_id, run_id)
|
203
204
|
end
|
204
205
|
end
|
@@ -210,8 +211,8 @@ module Nutella
|
|
210
211
|
# @param [Object] request the body of request. This can be,
|
211
212
|
# nil/empty (default), a string, a hash and, in general, anything with a .to_json method.
|
212
213
|
# @param [Proc] callback the callback that is fired whenever a response is received. It takes one parameter (response).
|
213
|
-
def
|
214
|
-
Nutella.app_runs_list.each do |run_id|
|
214
|
+
def self.async_request_to_all_runs(channel, request, callback)
|
215
|
+
Nutella.app.app_runs_list.each do |run_id|
|
215
216
|
Nutella::Net.async_request_to(channel, request, callback, Nutella.app_id, run_id)
|
216
217
|
end
|
217
218
|
end
|
@@ -226,7 +227,7 @@ module Nutella
|
|
226
227
|
# - [String] run_id: the run_id of the channel the message was sent on
|
227
228
|
# - [Hash] the sender's identifiers (from containing, run_id, app_id, component_id and optionally resource_id)
|
228
229
|
# - [*returns* Hash] 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.
|
229
|
-
def
|
230
|
+
def self.handle_requests_on_all_runs(channel, callback)
|
230
231
|
# Check the passed callback has the right number of arguments
|
231
232
|
raise 'You need to pass a callback with 3 parameters (request, run_id, from) when handling requests!' if callback.parameters.length!=3
|
232
233
|
# Pad channel
|
@@ -257,9 +258,21 @@ module Nutella
|
|
257
258
|
# @!endgroup
|
258
259
|
|
259
260
|
|
261
|
+
# Listens for incoming messages. All this function
|
262
|
+
# does is to put the thread to sleep and wait for something to
|
263
|
+
# happen over the network to wake up.
|
264
|
+
def self.listen
|
265
|
+
begin
|
266
|
+
sleep
|
267
|
+
rescue Interrupt
|
268
|
+
# Simply returns once interrupted
|
269
|
+
end
|
270
|
+
end
|
271
|
+
|
272
|
+
|
260
273
|
private
|
261
274
|
|
262
|
-
def
|
275
|
+
def self.extract_run_id_from_ch( app_id, mqtt_channel )
|
263
276
|
head = "/nutella/apps/#{app_id}/runs/"
|
264
277
|
mqtt_channel.gsub(head, '').split('/')[0]
|
265
278
|
end
|
@@ -268,4 +281,5 @@ module Nutella
|
|
268
281
|
end
|
269
282
|
|
270
283
|
end
|
284
|
+
|
271
285
|
end
|
data/lib/nutella_lib/core.rb
CHANGED
@@ -15,50 +15,32 @@ module Nutella
|
|
15
15
|
end
|
16
16
|
|
17
17
|
|
18
|
-
#
|
19
|
-
# @param [String] broker_hostname
|
20
|
-
# @param [String] component_id
|
21
|
-
def self.init_as_app_component( broker_hostname, app_id, component_id )
|
22
|
-
@app_id = app_id
|
23
|
-
@run_id = nil
|
24
|
-
@component_id = component_id
|
25
|
-
@resource_id = nil
|
26
|
-
@mqtt = SimpleMQTTClient.new broker_hostname
|
27
|
-
# Fetch the `run_id`s list for this application and subscribe to its updates
|
28
|
-
@app_runs_list = net.app.sync_request('app_runs_list')
|
29
|
-
net.app.subscribe('app_runs_list', lambda {|message, _| @app_runs_list = message })
|
30
|
-
end
|
31
|
-
|
32
|
-
|
33
|
-
# Accessors for app_id
|
18
|
+
# Variables accessors
|
34
19
|
def self.app_id; @app_id end
|
35
|
-
|
36
|
-
# Accessors for run_id
|
37
20
|
def self.run_id; @run_id end
|
38
|
-
|
39
|
-
# Accessors for mqtt client
|
40
|
-
def self.mqtt;
|
41
|
-
raise 'Nutella has not been initialized: you need to call the proper init method before you can start using nutella' if @mqtt.nil?
|
42
|
-
@mqtt
|
43
|
-
end
|
44
|
-
|
45
|
-
# Accessors for component_id
|
21
|
+
def self.resource_id; @resource_id end
|
46
22
|
def self.component_id;
|
47
23
|
raise 'Nutella has not been initialized: you need to call the proper init method before you can start using nutella' if @component_id.nil?
|
48
24
|
@component_id
|
49
25
|
end
|
26
|
+
def self.mqtt;
|
27
|
+
raise 'Nutella has not been initialized: you need to call the proper init method before you can start using nutella' if @mqtt.nil?
|
28
|
+
@mqtt
|
29
|
+
end
|
50
30
|
|
51
|
-
#
|
52
|
-
def self.
|
53
|
-
|
54
|
-
|
55
|
-
def self.
|
31
|
+
# Variables Setters
|
32
|
+
def self.app_id=(val); @app_id=val; end
|
33
|
+
def self.run_id=(val); @run_id=val; end
|
34
|
+
def self.resource_id=(val); @resource_id=val; end
|
35
|
+
def self.component_id=(val); @component_id=val; end
|
36
|
+
def self.mqtt=(val); @mqtt=val; end
|
56
37
|
|
57
|
-
# Accessor for the net module
|
58
|
-
def self.net; Nutella::Net end
|
59
38
|
|
60
|
-
#
|
61
|
-
def self.
|
39
|
+
# Accessors for sub-modules
|
40
|
+
def self.app; Nutella::App; end
|
41
|
+
def self.net; Nutella::Net; end
|
42
|
+
def self.log; Nutella::Log; end
|
43
|
+
def self.persist; Nutella::Persist; end
|
62
44
|
|
63
45
|
|
64
46
|
# Utility functions
|
@@ -68,7 +50,7 @@ module Nutella
|
|
68
50
|
#
|
69
51
|
# @param [Array] args command line arguments array
|
70
52
|
# @return [String, String, String] broker, app_id and run_id
|
71
|
-
def self.
|
53
|
+
def self.parse_args(args)
|
72
54
|
if args.length < 3
|
73
55
|
STDERR.puts 'Couldn\'t read broker address, app_id and run_id from the command line, impossible to initialize component!'
|
74
56
|
return
|
@@ -77,19 +59,6 @@ module Nutella
|
|
77
59
|
end
|
78
60
|
|
79
61
|
|
80
|
-
# Parse command line arguments for app level components
|
81
|
-
#
|
82
|
-
# @param [Array] args command line arguments array
|
83
|
-
# @return [String, String] broker and app_id
|
84
|
-
def self.parse_app_component_args(args)
|
85
|
-
if args.length < 2
|
86
|
-
STDERR.puts 'Couldn\'t read broker address and app_id from the command line, impossible to initialize component!'
|
87
|
-
return
|
88
|
-
end
|
89
|
-
return args[0], args[1]
|
90
|
-
end
|
91
|
-
|
92
|
-
|
93
62
|
# Extracts the component name from the folder where the code for this component is located
|
94
63
|
#
|
95
64
|
# @return [String] the component name
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'ansi'
|
2
|
+
|
3
|
+
module Nutella
|
4
|
+
|
5
|
+
module Log
|
6
|
+
|
7
|
+
def self.debug(message, code=nil)
|
8
|
+
puts( ANSI.cyan + message + ANSI.reset )
|
9
|
+
Nutella.net.publish( 'logging', log_to_json(message, code, __method__) )
|
10
|
+
code
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.info(message, code=nil)
|
14
|
+
puts( message )
|
15
|
+
Nutella.net.publish( 'logging', log_to_json(message, code, __method__) )
|
16
|
+
code
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.success(message, code=nil)
|
20
|
+
puts( ANSI.green + message + ANSI.reset )
|
21
|
+
Nutella.net.publish( 'logging', log_to_json(message, code, __method__) )
|
22
|
+
code
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.warn(message, code=nil)
|
26
|
+
puts( ANSI.yellow + message + ANSI.reset )
|
27
|
+
Nutella.net.publish( 'logging', log_to_json(message, code, __method__) )
|
28
|
+
code
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.error(message, code=nil)
|
32
|
+
puts( ANSI.red + message + ANSI.reset )
|
33
|
+
Nutella.net.publish( 'logging', log_to_json(message, code, __method__) )
|
34
|
+
code
|
35
|
+
end
|
36
|
+
|
37
|
+
private
|
38
|
+
|
39
|
+
def self.log_to_json( message, code, level)
|
40
|
+
code.nil? ? {level: level, message: message} : {level: level, message: message, code: code}
|
41
|
+
end
|
42
|
+
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
data/lib/nutella_lib/net.rb
CHANGED
@@ -7,14 +7,11 @@ module Nutella
|
|
7
7
|
@subscriptions = []
|
8
8
|
@callbacks = []
|
9
9
|
|
10
|
-
# Provides access to the net.app sub-module
|
11
|
-
def Net.app; Nutella::Net::App end
|
12
|
-
|
13
10
|
# Provides access to the subscriptions
|
14
|
-
def
|
11
|
+
def self.subscriptions; @subscriptions end
|
15
12
|
|
16
13
|
# Provides access to callbacks
|
17
|
-
def
|
14
|
+
def self.callbacks; @callbacks end
|
18
15
|
|
19
16
|
|
20
17
|
# Subscribes to a channel or to a set of channels.
|
@@ -25,7 +22,7 @@ module Nutella
|
|
25
22
|
# - [String] message: the received message. Messages that are not JSON are discarded.
|
26
23
|
# - [String] channel: the channel the message was received on (optional, only for wildcard subscriptions)
|
27
24
|
# - [Hash] from: the sender's identifiers (run_id, app_id, component_id and optionally resource_id)
|
28
|
-
def
|
25
|
+
def self.subscribe( channel, callback )
|
29
26
|
subscribe_to( channel, callback, Nutella.app_id, Nutella.run_id)
|
30
27
|
end
|
31
28
|
|
@@ -33,7 +30,7 @@ module Nutella
|
|
33
30
|
# Un-subscribes from a channel
|
34
31
|
#
|
35
32
|
# @param [String] channel we want to unsubscribe from. Can contain wildcard(s).
|
36
|
-
def
|
33
|
+
def self.unsubscribe( channel )
|
37
34
|
unsubscribe_to( channel, Nutella.app_id, Nutella.run_id)
|
38
35
|
end
|
39
36
|
|
@@ -43,7 +40,7 @@ module Nutella
|
|
43
40
|
# @param [String] channel we want to publish the message to. *CANNOT* contain wildcard(s)!
|
44
41
|
# @param [Object] message the message we are publishing. This can be,
|
45
42
|
# nil/empty (default), a string, a hash and, in general, anything with a .to_json method.
|
46
|
-
def
|
43
|
+
def self.publish( channel, message=nil )
|
47
44
|
publish_to( channel, message, Nutella.app_id, Nutella.run_id)
|
48
45
|
end
|
49
46
|
|
@@ -53,7 +50,7 @@ module Nutella
|
|
53
50
|
# @param [String] channel we want to make the request to. *CANNOT* contain wildcard(s)!
|
54
51
|
# @param [Object] message the body of request. This can be,
|
55
52
|
# nil/empty (default), a string, a hash and, in general, anything with a .to_json method.
|
56
|
-
def
|
53
|
+
def self.sync_request( channel, message=nil )
|
57
54
|
sync_request_to(channel, message, Nutella.app_id, Nutella.run_id)
|
58
55
|
end
|
59
56
|
|
@@ -64,7 +61,7 @@ module Nutella
|
|
64
61
|
# @param [Object] message the body of request. This can be,
|
65
62
|
# nil/empty (default), a string, a hash and, in general, anything with a .to_json method.
|
66
63
|
# @param [Proc] callback the callback that is fired whenever a response is received. It takes one parameter (response).
|
67
|
-
def
|
64
|
+
def self.async_request( channel, message=nil, callback )
|
68
65
|
async_request_to(channel, message, callback, Nutella.app_id, Nutella.run_id)
|
69
66
|
end
|
70
67
|
|
@@ -78,7 +75,7 @@ module Nutella
|
|
78
75
|
# - [Hash] the sender's identifiers (run_id, app_id, component_id and optionally resource_id)
|
79
76
|
# - [*returns* Hash] 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.
|
80
77
|
#
|
81
|
-
def
|
78
|
+
def self.handle_requests( channel, callback )
|
82
79
|
handle_requests_on(channel, callback, Nutella.app_id, Nutella.run_id)
|
83
80
|
end
|
84
81
|
|
@@ -86,7 +83,7 @@ module Nutella
|
|
86
83
|
# Listens for incoming messages. All this function
|
87
84
|
# does is to put the thread to sleep and wait for something to
|
88
85
|
# happen over the network to wake up.
|
89
|
-
def
|
86
|
+
def self.listen
|
90
87
|
begin
|
91
88
|
sleep
|
92
89
|
rescue Interrupt
|
@@ -98,26 +95,26 @@ module Nutella
|
|
98
95
|
private
|
99
96
|
|
100
97
|
|
101
|
-
def
|
98
|
+
def self.extract_fields_from_message(message)
|
102
99
|
mh = JSON.parse message
|
103
100
|
return mh['type'], mh['from'], mh['payload'], mh['id']
|
104
101
|
end
|
105
102
|
|
106
|
-
def
|
103
|
+
def self.pad_channel( channel, app_id, run_id )
|
107
104
|
raise 'If the run_id is specified, app_id needs to also be specified' if (!run_id.nil? && app_id.nil?)
|
108
105
|
return "/nutella/#{channel}" if (app_id.nil? && run_id.nil?)
|
109
106
|
return "/nutella/apps/#{app_id}/#{channel}" if (!app_id.nil? && run_id.nil?)
|
110
107
|
"/nutella/apps/#{app_id}/runs/#{run_id}/#{channel}"
|
111
108
|
end
|
112
109
|
|
113
|
-
def
|
110
|
+
def self.un_pad_channel( channel, app_id, run_id )
|
114
111
|
raise 'If the run_id is specified, app_id needs to also be specified' if (!run_id.nil? && app_id.nil?)
|
115
112
|
return channel.gsub('/nutella/', '') if (app_id.nil? && run_id.nil?)
|
116
113
|
return channel.gsub("/nutella/apps/#{app_id}/", '') if (!app_id.nil? && run_id.nil?)
|
117
114
|
channel.gsub("/nutella/apps/#{app_id}/runs/#{run_id}/", '')
|
118
115
|
end
|
119
116
|
|
120
|
-
def
|
117
|
+
def self.assemble_from
|
121
118
|
from = Hash.new
|
122
119
|
if Nutella.run_id.nil?
|
123
120
|
from['type'] = 'app'
|
@@ -131,14 +128,14 @@ module Nutella
|
|
131
128
|
from
|
132
129
|
end
|
133
130
|
|
134
|
-
def
|
131
|
+
def self.prepare_message_for_publish( message )
|
135
132
|
if message.nil?
|
136
133
|
return {type: 'publish', from: assemble_from}.to_json
|
137
134
|
end
|
138
135
|
{type: 'publish', from: assemble_from, payload: message}.to_json
|
139
136
|
end
|
140
137
|
|
141
|
-
def
|
138
|
+
def self.prepare_message_for_request( message )
|
142
139
|
id = message.hash + Random.rand(100)
|
143
140
|
if message.nil?
|
144
141
|
return {id: id, type: 'request', from: assemble_from}.to_json, id
|
@@ -146,7 +143,7 @@ module Nutella
|
|
146
143
|
return {id: id, type: 'request', from: assemble_from, payload: message}.to_json, id
|
147
144
|
end
|
148
145
|
|
149
|
-
def
|
146
|
+
def self.prepare_message_for_response( message, id )
|
150
147
|
if message.nil?
|
151
148
|
return {id: id, type: 'response', from: assemble_from}.to_json
|
152
149
|
end
|
@@ -164,7 +161,7 @@ module Nutella
|
|
164
161
|
# - [Hash] from: the sender's identifiers (run_id, app_id, component_id and optionally resource_id)
|
165
162
|
# @param [String] app_id used to pad channels
|
166
163
|
# @param [String] run_id used to pad channels
|
167
|
-
def
|
164
|
+
def self.subscribe_to( channel, callback, app_id, run_id )
|
168
165
|
# Check the passed callback has the right number of arguments
|
169
166
|
if Nutella.mqtt.is_channel_wildcard?(channel)
|
170
167
|
raise 'You need to pass a callback with 3 parameters (message, channel, from) when subscribing to a wildcard channel!' if callback.parameters.length!=3
|
@@ -216,7 +213,7 @@ module Nutella
|
|
216
213
|
# @param [String] channel we want to unsubscribe from. Can contain wildcard(s).
|
217
214
|
# @param [String] app_id used to pad channels
|
218
215
|
# @param [String] run_id used to pad channels
|
219
|
-
def
|
216
|
+
def self.unsubscribe_to( channel, app_id, run_id )
|
220
217
|
# Pad channel
|
221
218
|
padded_channel = pad_channel(channel, app_id, run_id)
|
222
219
|
idx = @subscriptions.index padded_channel
|
@@ -238,15 +235,16 @@ module Nutella
|
|
238
235
|
# nil/empty (default), a string, a hash and, in general, anything with a .to_json method.
|
239
236
|
# @param [String] app_id used to pad channels
|
240
237
|
# @param [String] run_id used to pad channels
|
241
|
-
def
|
238
|
+
def self.publish_to( channel, message=nil, app_id, run_id )
|
242
239
|
# Pad channel
|
243
240
|
padded_channel = pad_channel(channel, app_id, run_id)
|
244
241
|
# Throw exception if trying to publish something that is not JSON
|
245
242
|
begin
|
246
|
-
m =
|
243
|
+
m = self.prepare_message_for_publish message
|
247
244
|
Nutella.mqtt.publish( padded_channel, m )
|
248
245
|
rescue
|
249
246
|
STDERR.puts 'Error: you are trying to publish something that is not JSON'
|
247
|
+
STDERR.puts $!
|
250
248
|
end
|
251
249
|
end
|
252
250
|
|
@@ -258,7 +256,7 @@ module Nutella
|
|
258
256
|
# nil/empty (default), a string, a hash and, in general, anything with a .to_json method.
|
259
257
|
# @param [String] app_id used to pad channels
|
260
258
|
# @param [String] run_id used to pad channels
|
261
|
-
def
|
259
|
+
def self.sync_request_to( channel, message=nil, app_id, run_id )
|
262
260
|
# Pad channel
|
263
261
|
padded_channel = pad_channel(channel, app_id, run_id)
|
264
262
|
# Prepare message
|
@@ -291,7 +289,7 @@ module Nutella
|
|
291
289
|
# @param [Proc] callback the callback that is fired whenever a response is received. It takes one parameter (response).
|
292
290
|
# @param [String] app_id used to pad channels
|
293
291
|
# @param [String] run_id used to pad channels
|
294
|
-
def
|
292
|
+
def self.async_request_to( channel, message=nil, callback, app_id, run_id )
|
295
293
|
# Check the passed callback has the right number of arguments
|
296
294
|
raise 'You need to pass a callback with 1 parameter (response) when making an asynchronous request!' if callback.parameters.length!=1
|
297
295
|
# Pad channel
|
@@ -323,7 +321,7 @@ module Nutella
|
|
323
321
|
# - [*returns* Hash] 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.
|
324
322
|
# @param [String] app_id used to pad channels
|
325
323
|
# @param [String] run_id used to pad channels
|
326
|
-
def
|
324
|
+
def self.handle_requests_on( channel, callback, app_id, run_id )
|
327
325
|
# Check the passed callback has the right number of arguments
|
328
326
|
raise 'You need to pass a callback with 2 parameter (request, from) when handling requests!' if callback.parameters.length!=2
|
329
327
|
# Pad channel
|
@@ -335,7 +333,7 @@ module Nutella
|
|
335
333
|
# Only handle requests that have proper id set
|
336
334
|
return if type!='request' || id.nil?
|
337
335
|
# Execute callback and send response
|
338
|
-
m =
|
336
|
+
m = self.prepare_message_for_response( callback.call( payload, from), id )
|
339
337
|
Nutella.mqtt.publish( padded_channel, m )
|
340
338
|
rescue JSON::ParserError
|
341
339
|
# Make sure that request contains JSON, if not drop the message
|
data/lib/nutella_lib.rb
CHANGED
@@ -1,12 +1,16 @@
|
|
1
|
+
require 'json'
|
2
|
+
require 'set'
|
3
|
+
|
4
|
+
require 'simple_mqtt_client/simple_mqtt_client'
|
5
|
+
|
1
6
|
require 'nutella_lib/core'
|
2
7
|
require 'nutella_lib/net'
|
3
|
-
require 'nutella_lib/
|
8
|
+
require 'nutella_lib/log'
|
4
9
|
require 'nutella_lib/persist'
|
5
|
-
require 'simple_mqtt_client/simple_mqtt_client'
|
6
10
|
|
7
|
-
|
8
|
-
require '
|
9
|
-
require '
|
11
|
+
require 'nutella_lib/app_core'
|
12
|
+
require 'nutella_lib/app_net'
|
13
|
+
require 'nutella_lib/app_log'
|
10
14
|
|
11
15
|
# NO_EXT gets defined when you require "nutella_lib/noext", which
|
12
16
|
# signals that you don't want any extensions.
|
data/nutella_lib.gemspec
CHANGED
@@ -2,16 +2,16 @@
|
|
2
2
|
# DO NOT EDIT THIS FILE DIRECTLY
|
3
3
|
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
4
|
# -*- encoding: utf-8 -*-
|
5
|
-
# stub: nutella_lib 0.4.
|
5
|
+
# stub: nutella_lib 0.4.1 ruby lib
|
6
6
|
|
7
7
|
Gem::Specification.new do |s|
|
8
8
|
s.name = "nutella_lib"
|
9
|
-
s.version = "0.4.
|
9
|
+
s.version = "0.4.1"
|
10
10
|
|
11
11
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
12
12
|
s.require_paths = ["lib"]
|
13
13
|
s.authors = ["Alessandro Gnoli"]
|
14
|
-
s.date = "2015-03-
|
14
|
+
s.date = "2015-03-16"
|
15
15
|
s.description = "Implements the nutella protocol and exposes it natively to ruby developers"
|
16
16
|
s.email = "tebemis@gmail.com"
|
17
17
|
s.extra_rdoc_files = [
|
@@ -27,15 +27,19 @@ Gem::Specification.new do |s|
|
|
27
27
|
"Rakefile",
|
28
28
|
"VERSION",
|
29
29
|
"lib/nutella_lib.rb",
|
30
|
+
"lib/nutella_lib/app_core.rb",
|
31
|
+
"lib/nutella_lib/app_log.rb",
|
32
|
+
"lib/nutella_lib/app_net.rb",
|
30
33
|
"lib/nutella_lib/core.rb",
|
31
34
|
"lib/nutella_lib/ext/kernel.rb",
|
35
|
+
"lib/nutella_lib/log.rb",
|
32
36
|
"lib/nutella_lib/net.rb",
|
33
|
-
"lib/nutella_lib/net_app.rb",
|
34
37
|
"lib/nutella_lib/noext.rb",
|
35
38
|
"lib/nutella_lib/persist.rb",
|
36
39
|
"lib/simple_mqtt_client/simple_mqtt_client.rb",
|
37
40
|
"nutella_lib.gemspec",
|
38
41
|
"test/helper.rb",
|
42
|
+
"test/test_logger.rb",
|
39
43
|
"test/test_nutella_net.rb",
|
40
44
|
"test/test_nutella_net_app.rb",
|
41
45
|
"test/test_simple_mqtt_client.rb"
|
@@ -50,6 +54,7 @@ Gem::Specification.new do |s|
|
|
50
54
|
|
51
55
|
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
52
56
|
s.add_runtime_dependency(%q<mqtt>, [">= 0.3", "~> 0.3"])
|
57
|
+
s.add_runtime_dependency(%q<ansi>, [">= 1.4", "~> 1.5"])
|
53
58
|
s.add_development_dependency(%q<shoulda>, [">= 3", "~> 3"])
|
54
59
|
s.add_development_dependency(%q<minitest>, [">= 5", "~> 5.4"])
|
55
60
|
s.add_development_dependency(%q<yard>, [">= 0.8.7", "~> 0.8"])
|
@@ -59,6 +64,7 @@ Gem::Specification.new do |s|
|
|
59
64
|
s.add_development_dependency(%q<simplecov>, [">= 0", "~> 0"])
|
60
65
|
else
|
61
66
|
s.add_dependency(%q<mqtt>, [">= 0.3", "~> 0.3"])
|
67
|
+
s.add_dependency(%q<ansi>, [">= 1.4", "~> 1.5"])
|
62
68
|
s.add_dependency(%q<shoulda>, [">= 3", "~> 3"])
|
63
69
|
s.add_dependency(%q<minitest>, [">= 5", "~> 5.4"])
|
64
70
|
s.add_dependency(%q<yard>, [">= 0.8.7", "~> 0.8"])
|
@@ -69,6 +75,7 @@ Gem::Specification.new do |s|
|
|
69
75
|
end
|
70
76
|
else
|
71
77
|
s.add_dependency(%q<mqtt>, [">= 0.3", "~> 0.3"])
|
78
|
+
s.add_dependency(%q<ansi>, [">= 1.4", "~> 1.5"])
|
72
79
|
s.add_dependency(%q<shoulda>, [">= 3", "~> 3"])
|
73
80
|
s.add_dependency(%q<minitest>, [">= 5", "~> 5.4"])
|
74
81
|
s.add_dependency(%q<yard>, [">= 0.8.7", "~> 0.8"])
|
data/test/test_logger.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class TestNutellaNet < MiniTest::Test
|
4
|
+
|
5
|
+
# nutella.init( 'ltg.evl.uic.edu', 'ale_app_1', 'ale_run_1', 'ale_component_2' )
|
6
|
+
#
|
7
|
+
# def test_logger
|
8
|
+
# p nutella.log.success 'success', 401
|
9
|
+
# p nutella.log.info 'info', 401
|
10
|
+
# p nutella.log.warn 'warn', 401
|
11
|
+
# p nutella.log.error 'error', 401
|
12
|
+
# p nutella.log.debug 'debug', 401
|
13
|
+
# end
|
14
|
+
|
15
|
+
|
16
|
+
# nutella.app.init( 'ltg.evl.uic.edu', 'ale_app_1', 'ale_component_2' )
|
17
|
+
#
|
18
|
+
# def test_logger
|
19
|
+
# p nutella.app.log.success 'success', 401
|
20
|
+
# p nutella.app.log.info 'info', 401
|
21
|
+
# p nutella.app.log.warn 'warn', 401
|
22
|
+
# p nutella.app.log.error 'error', 401
|
23
|
+
# p nutella.app.log.debug 'debug', 401
|
24
|
+
# end
|
25
|
+
|
26
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nutella_lib
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alessandro Gnoli
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-03-
|
11
|
+
date: 2015-03-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: mqtt
|
@@ -30,6 +30,26 @@ dependencies:
|
|
30
30
|
- - "~>"
|
31
31
|
- !ruby/object:Gem::Version
|
32
32
|
version: '0.3'
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: ansi
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '1.4'
|
40
|
+
- - "~>"
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '1.5'
|
43
|
+
type: :runtime
|
44
|
+
prerelease: false
|
45
|
+
version_requirements: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '1.4'
|
50
|
+
- - "~>"
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '1.5'
|
33
53
|
- !ruby/object:Gem::Dependency
|
34
54
|
name: shoulda
|
35
55
|
requirement: !ruby/object:Gem::Requirement
|
@@ -186,15 +206,19 @@ files:
|
|
186
206
|
- Rakefile
|
187
207
|
- VERSION
|
188
208
|
- lib/nutella_lib.rb
|
209
|
+
- lib/nutella_lib/app_core.rb
|
210
|
+
- lib/nutella_lib/app_log.rb
|
211
|
+
- lib/nutella_lib/app_net.rb
|
189
212
|
- lib/nutella_lib/core.rb
|
190
213
|
- lib/nutella_lib/ext/kernel.rb
|
214
|
+
- lib/nutella_lib/log.rb
|
191
215
|
- lib/nutella_lib/net.rb
|
192
|
-
- lib/nutella_lib/net_app.rb
|
193
216
|
- lib/nutella_lib/noext.rb
|
194
217
|
- lib/nutella_lib/persist.rb
|
195
218
|
- lib/simple_mqtt_client/simple_mqtt_client.rb
|
196
219
|
- nutella_lib.gemspec
|
197
220
|
- test/helper.rb
|
221
|
+
- test/test_logger.rb
|
198
222
|
- test/test_nutella_net.rb
|
199
223
|
- test/test_nutella_net_app.rb
|
200
224
|
- test/test_simple_mqtt_client.rb
|