Module: Nutella::Framework::Net
- Defined in:
- lib/nutella_lib/framework_net.rb
Framework-level communication APIs collapse
-
.async_request(channel, message = nil, callback) ⇒ Object
Performs an asynchronous request at the framework-level.
-
.handle_requests(channel, callback) ⇒ Object
Handles requests on a certain framework-level channel.
-
.publish(channel, message = nil) ⇒ Object
Publishes a message to an framework-level channel.
-
.subscribe(channel, callback) ⇒ Object
Subscribes to a channel or to a set of channels at the framework-level.
-
.sync_request(channel, message = nil) ⇒ Object
Performs a synchronous request at the framework-level.
-
.unsubscribe(channel) ⇒ Object
Un-subscribes from a framework-level channel.
Framework-level APIs to communicate at the run-level collapse
-
.async_request_to_run(app_id, run_id, channel, request, callback) ⇒ Object
Allows framework-level APIs to make an asynchronous request to a run-level channel within a specific run.
-
.handle_requests_on_run(app_id, run_id, channel, callback) ⇒ Object
Allows framework-level APIs to handle requests on a run-level channel within a specific run.
-
.publish_to_run(app_id, run_id, channel, message) ⇒ Object
Allows framework-level APIs to publish to a run-level channel within a specific run.
-
.subscribe_to_run(app_id, run_id, channel, callback) ⇒ Object
Allows framework-level APIs to subscribe to a run-level channel within a specific run.
-
.sync_request_to_run(app_id, run_id, channel, request) ⇒ Object
Allows framework-level APIs to make a synchronous request to a run-level channel within a specific run.
-
.unsubscribe_to_run(app_id, run_id, channel) ⇒ Object
Allows framework-level APIs to unsubscribe from a run-level channel within a specific run.
Framework-level APIs to communicate at the run-level (broadcast) collapse
-
.async_request_to_all_runs(channel, request, callback) ⇒ Object
Allows framework-level APIs to send a request to a run-level channel *for ALL runs*.
- .catch_requests_on_all_runs_wildcard(callback) ⇒ Object
-
.handle_requests_on_all_runs(channel, callback) ⇒ Object
Allows framework-level APIs to handle requests to a run-level channel *for ALL runs*.
-
.publish_to_all_runs(channel, message) ⇒ Object
Allows framework-level APIs to publish a message to a run-level channel *for ALL runs*.
-
.subscribe_to_all_runs(channel, callback) ⇒ Object
Allows framework-level APIs to subscribe to a run-level channel *for ALL runs*.
- .un_pad_wildcard_channel(channel) ⇒ Object
-
.unsubscribe_from_all_runs(channel) ⇒ Object
Allows framework-level APIs to unsubscribe from a run-level channel *for ALL runs*.
Framework-level APIs to communicate at the application-level collapse
-
.async_request_to_app(app_id, channel, request, callback) ⇒ Object
Allows framework-level APIs to make an asynchronous request to a run-level channel within a specific run.
-
.handle_requests_on_app(app_id, channel, callback) ⇒ Object
Allows framework-level APIs to handle requests on a run-level channel within a specific run.
-
.publish_to_app(app_id, channel, message) ⇒ Object
Allows framework-level APIs to publish to an app-level channel within a specific run.
-
.subscribe_to_app(app_id, channel, callback) ⇒ Object
Allows framework-level APIs to subscribe to an app-level channel within a specific run.
-
.sync_request_to_app(app_id, channel, request) ⇒ Object
Allows framework-level APIs to make a synchronous request to a run-level channel within a specific run.
-
.unsubscribe_to_app(app_id, channel) ⇒ Object
Allows framework-level APIs to unsubscribe from an app-level channel within a specific run.
Framework-level APIs to communicate at the application-level (broadcast) collapse
-
.async_request_to_all_apps(channel, request, callback) ⇒ Object
Allows framework-level APIs to send a request to a run-level channel *for ALL runs*.
-
.handle_requests_on_all_apps(channel, callback) ⇒ Object
Allows framework-level APIs to handle requests to a run-level channel *for ALL runs*.
-
.publish_to_all_apps(channel, message) ⇒ Object
Allows framework-level APIs to publish a message to an app-level channel *for ALL apps*.
-
.subscribe_to_all_apps(channel, callback) ⇒ Object
Allows framework-level APIs to subscribe to an app-level channel *for ALL apps*.
-
.unsubscribe_from_all_apps(channel) ⇒ Object
Allows framework-level APIs to unsubscribe from an app-level channel *for ALL apps*.
Class Method Summary collapse
-
.listen ⇒ Object
Listens for incoming messages.
Class Method Details
.async_request(channel, message = nil, callback) ⇒ Object
Performs an asynchronous request at the framework-level
56 57 58 |
# File 'lib/nutella_lib/framework_net.rb', line 56 def self.async_request ( channel, =nil, callback ) Nutella::Net.async_request_to(channel, , callback, nil, nil) end |
.async_request_to_all_apps(channel, request, callback) ⇒ Object
Allows framework-level APIs to send a request to a run-level channel *for ALL runs*
461 462 463 464 465 |
# File 'lib/nutella_lib/framework_net.rb', line 461 def self.async_request_to_all_apps(channel, request, callback) Nutella.runlist.all_runs.each do |app_id, _| Nutella::Net.async_request_to(channel, request, callback, app_id, nil) end end |
.async_request_to_all_runs(channel, request, callback) ⇒ Object
Allows framework-level APIs to send a request to a run-level channel *for ALL runs*
235 236 237 238 239 240 241 |
# File 'lib/nutella_lib/framework_net.rb', line 235 def self.async_request_to_all_runs(channel, request, callback) Nutella.runlist.all_runs.each do |app_id, _| Nutella.runlist.runs_for_app(app_id).each do |run_id| Nutella::Net.async_request_to(channel, request, callback, app_id, run_id) end end end |
.async_request_to_app(app_id, channel, request, callback) ⇒ Object
Allows framework-level APIs to make an asynchronous request to a run-level channel within a specific run
371 372 373 |
# File 'lib/nutella_lib/framework_net.rb', line 371 def self.async_request_to_app( app_id, channel, request, callback) Nutella::Net.async_request_to(channel, request, callback, app_id, nil) end |
.async_request_to_run(app_id, run_id, channel, request, callback) ⇒ Object
Allows framework-level APIs to make an asynchronous request to a run-level channel within a specific run
136 137 138 |
# File 'lib/nutella_lib/framework_net.rb', line 136 def self.async_request_to_run( app_id, run_id, channel, request, callback) Nutella::Net.async_request_to(channel, request, callback, app_id, run_id) end |
.catch_requests_on_all_runs_wildcard(callback) ⇒ Object
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 |
# File 'lib/nutella_lib/framework_net.rb', line 283 def self.catch_requests_on_all_runs_wildcard(callback) # Pad channel padded_channel = Nutella::Net.pad_channel("#", '+', '+') mqtt_cb = lambda do |request, mqtt_channel| begin # Extract nutella fields type, from, payload, id = Nutella::Net. request app_id, run_id = self.extract_run_id_and_app_id mqtt_channel # Only handle requests that have proper id set return if type!='request' || id.nil? # Execute callback and send response regex = Regexp.new '/nutella/apps/[^"\/"]+/runs/[^"\/"]+/' unpadded_channel = mqtt_channel.gsub(regex, '') callback.call( unpadded_channel, payload, app_id, run_id, from) rescue JSON::ParserError # Make sure that request contains JSON, if not drop the message return rescue ArgumentError # Check the passed callback has the right number of arguments STDERR.puts "The callback you passed to subscribe has the #{$!}: it needs 'request', 'app_id', 'run_id' and 'from'" end end # Subscribe to the channel Nutella.mqtt.subscribe( padded_channel, mqtt_cb ) end |
.handle_requests(channel, callback) ⇒ Object
Handles requests on a certain framework-level channel
69 70 71 |
# File 'lib/nutella_lib/framework_net.rb', line 69 def self.handle_requests( channel, callback ) Nutella::Net.handle_requests_on(channel, callback, nil, nil) end |
.handle_requests_on_all_apps(channel, callback) ⇒ Object
Allows framework-level APIs to handle requests to a run-level channel *for ALL runs*
477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 |
# File 'lib/nutella_lib/framework_net.rb', line 477 def self.handle_requests_on_all_apps(channel, callback) # Check the passed callback has the right number of arguments raise 'You need to pass a callback with 3 parameters (request, run_id, from) when handling requests!' if callback.parameters.length!=3 # Pad channel padded_channel = Nutella::Net.pad_channel(channel, '+', nil) mqtt_cb = lambda do |request, mqtt_channel| begin # Extract nutella fields type, from, payload, id = Nutella::Net. request app_id = self.extract_app_id mqtt_channel # Only handle requests that have proper id set return if type!='request' || id.nil? # Execute callback and send response m = Nutella::Net.( callback.call( payload, app_id, from), id ) Nutella.mqtt.publish( mqtt_channel, m ) rescue JSON::ParserError # Make sure that request contains JSON, if not drop the message return rescue ArgumentError # Check the passed callback has the right number of arguments STDERR.puts "The callback you passed to subscribe has the #{$!}: it needs 'request', 'app_id' and 'from'" end end # Subscribe to the channel Nutella.mqtt.subscribe( padded_channel, mqtt_cb ) # Notify subscription Nutella::Net.publish_to('subscriptions', {'type' => 'handle_requests', 'channel' => padded_channel}, nil, nil) end |
.handle_requests_on_all_runs(channel, callback) ⇒ Object
Allows framework-level APIs to handle requests to a run-level channel *for ALL runs*
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 |
# File 'lib/nutella_lib/framework_net.rb', line 254 def self.handle_requests_on_all_runs(channel, callback) # Check the passed callback has the right number of arguments raise 'You need to pass a callback with 4 parameters (request, run_id, from) when handling requests!' if callback.parameters.length!=4 # Pad channel padded_channel = Nutella::Net.pad_channel(channel, '+', '+') mqtt_cb = lambda do |request, mqtt_channel| begin # Extract nutella fields type, from, payload, id = Nutella::Net. request app_id, run_id = self.extract_run_id_and_app_id mqtt_channel # Only handle requests that have proper id set return if type!='request' || id.nil? # Execute callback and send response m = Nutella::Net.( callback.call( payload, app_id, run_id, from), id ) Nutella.mqtt.publish( mqtt_channel, m ) rescue JSON::ParserError # Make sure that request contains JSON, if not drop the message return rescue ArgumentError # Check the passed callback has the right number of arguments STDERR.puts "The callback you passed to subscribe has the #{$!}: it needs 'request', 'app_id', 'run_id' and 'from'" end end # Subscribe to the channel Nutella.mqtt.subscribe( padded_channel, mqtt_cb ) # Notify subscription Nutella::Net.publish_to('subscriptions', {'type' => 'handle_requests', 'channel' => padded_channel}, nil, nil) end |
.handle_requests_on_app(app_id, channel, callback) ⇒ Object
Allows framework-level APIs to handle requests on a run-level channel within a specific run
386 387 388 |
# File 'lib/nutella_lib/framework_net.rb', line 386 def self.handle_requests_on_app(app_id, channel, callback) Nutella::Net.handle_requests_on(channel, callback, app_id, nil) end |
.handle_requests_on_run(app_id, run_id, channel, callback) ⇒ Object
Allows framework-level APIs to handle requests on a run-level channel within a specific run
151 152 153 |
# File 'lib/nutella_lib/framework_net.rb', line 151 def self.handle_requests_on_run( app_id, run_id, channel, callback ) Nutella::Net.handle_requests_on(channel, callback, app_id, run_id) end |
.listen ⇒ Object
Listens for incoming messages. All this function does is to put the thread to sleep and wait for something to happen over the network to wake up.
515 516 517 |
# File 'lib/nutella_lib/framework_net.rb', line 515 def self.listen Nutella::Net.listen end |
.publish(channel, message = nil) ⇒ Object
Publishes a message to an framework-level channel
35 36 37 |
# File 'lib/nutella_lib/framework_net.rb', line 35 def self.publish(channel, =nil) Nutella::Net.publish_to(channel, , nil, nil) end |
.publish_to_all_apps(channel, message) ⇒ Object
Allows framework-level APIs to publish a message to an app-level channel *for ALL apps*
448 449 450 451 452 |
# File 'lib/nutella_lib/framework_net.rb', line 448 def self.publish_to_all_apps(channel, ) Nutella.runlist.all_runs.each do |app_id, _| Nutella::Net.publish_to(channel, , app_id, nil) end end |
.publish_to_all_runs(channel, message) ⇒ Object
Allows framework-level APIs to publish a message to a run-level channel *for ALL runs*
220 221 222 223 224 225 226 |
# File 'lib/nutella_lib/framework_net.rb', line 220 def self.publish_to_all_runs( channel, ) Nutella.runlist.all_runs.each do |app_id, _| Nutella.runlist.runs_for_app(app_id).each do |run_id| Nutella::Net.publish_to(channel, , app_id, run_id) end end end |
.publish_to_app(app_id, channel, message) ⇒ Object
Allows framework-level APIs to publish to an app-level channel within a specific run
346 347 348 |
# File 'lib/nutella_lib/framework_net.rb', line 346 def self.publish_to_app(app_id, channel, ) Nutella::Net.publish_to(channel, , app_id, nil) end |
.publish_to_run(app_id, run_id, channel, message) ⇒ Object
Allows framework-level APIs to publish to a run-level channel within a specific run
111 112 113 |
# File 'lib/nutella_lib/framework_net.rb', line 111 def self.publish_to_run( app_id, run_id, channel, ) Nutella::Net.publish_to(channel, , app_id, run_id) end |
.subscribe(channel, callback) ⇒ Object
Subscribes to a channel or to a set of channels at the framework-level.
17 18 19 |
# File 'lib/nutella_lib/framework_net.rb', line 17 def self.subscribe (channel, callback) Nutella::Net.subscribe_to(channel, callback, nil, nil) end |
.subscribe_to_all_apps(channel, callback) ⇒ Object
Allows framework-level APIs to subscribe to an app-level channel *for ALL apps*
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 |
# File 'lib/nutella_lib/framework_net.rb', line 408 def self.subscribe_to_all_apps(channel, callback) # Check the passed callback has the right number of arguments raise 'You need to pass a callback with 3 parameters (payload, app_id, from) when subscribing to all apps!' if callback.parameters.length!=3 # Pad channel padded_channel = Nutella::Net.pad_channel(channel, '+', nil) mqtt_cb = lambda do |, mqtt_channel| begin type, from, payload, _ = Nutella::Net. app_id = self.extract_app_id mqtt_channel callback.call(payload, app_id, from) if type=='publish' rescue JSON::ParserError # Make sure the message is JSON, if not drop the message return rescue ArgumentError # Check the passed callback has the right number of arguments STDERR.puts "The callback you passed to subscribe has the #{$!}: it needs 'payload', 'app_id' and 'from'" end end # Add to subscriptions, save mqtt callback and subscribe Nutella::Net.subscriptions.push padded_channel Nutella::Net.callbacks.push mqtt_cb Nutella.mqtt.subscribe( padded_channel, mqtt_cb ) # Notify subscription Nutella::Net.publish_to('subscriptions', {'type' => 'subscribe', 'channel' => padded_channel}, nil, nil) end |
.subscribe_to_all_runs(channel, callback) ⇒ Object
Allows framework-level APIs to subscribe to a run-level channel *for ALL runs*
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/nutella_lib/framework_net.rb', line 170 def self.subscribe_to_all_runs( channel, callback ) # Check the passed callback has the right number of arguments raise 'You need to pass a callback with 4 parameters (payload, app_id, run_id, from) when subscribing to all runs!' if callback.parameters.length!=4 && callback.parameters.length!=5 # Pad channel padded_channel = Nutella::Net.pad_channel(channel, '+', '+') mqtt_cb = lambda do |, mqtt_channel| begin type, from, payload, _ = Nutella::Net. app_id, run_id = self.extract_run_id_and_app_id mqtt_channel if channel.include?('#') unpadded_channel = un_pad_wildcard_channel(mqtt_channel) callback.call(unpadded_channel, payload, app_id, run_id, from) if type=='publish' else callback.call(payload, app_id, run_id, from) if type=='publish' end rescue JSON::ParserError # Make sure the message is JSON, if not drop the message return rescue ArgumentError # Check the passed callback has the right number of arguments STDERR.puts "The callback you passed to subscribe has the #{$!}: it needs 'payload', 'app_id', 'run_id' and 'from'" end end # Add to subscriptions, save mqtt callback and subscribe Nutella::Net.subscriptions.push padded_channel Nutella::Net.callbacks.push mqtt_cb Nutella.mqtt.subscribe( padded_channel, mqtt_cb ) # Notify subscription Nutella::Net.publish_to('subscriptions', {'type' => 'subscribe', 'channel' => padded_channel}, nil, nil) end |
.subscribe_to_app(app_id, channel, callback) ⇒ Object
Allows framework-level APIs to subscribe to an app-level channel within a specific run
325 326 327 |
# File 'lib/nutella_lib/framework_net.rb', line 325 def self.subscribe_to_app(app_id, channel, callback) Nutella::Net.subscribe_to(channel, callback, app_id, nil) end |
.subscribe_to_run(app_id, run_id, channel, callback) ⇒ Object
Allows framework-level APIs to subscribe to a run-level channel within a specific run
89 90 91 |
# File 'lib/nutella_lib/framework_net.rb', line 89 def self.subscribe_to_run( app_id, run_id, channel, callback ) Nutella::Net.subscribe_to(channel, callback, app_id, run_id) end |
.sync_request(channel, message = nil) ⇒ Object
Performs a synchronous request at the framework-level
45 46 47 |
# File 'lib/nutella_lib/framework_net.rb', line 45 def self.sync_request ( channel, =nil ) Nutella::Net.sync_request_to(channel, , nil, nil) end |
.sync_request_to_app(app_id, channel, request) ⇒ Object
Allows framework-level APIs to make a synchronous request to a run-level channel within a specific run
358 359 360 |
# File 'lib/nutella_lib/framework_net.rb', line 358 def self.sync_request_to_app( app_id, channel, request) Nutella::Net.sync_request_to(channel, request, app_id, nil) end |
.sync_request_to_run(app_id, run_id, channel, request) ⇒ Object
Allows framework-level APIs to make a synchronous request to a run-level channel within a specific run
123 124 125 |
# File 'lib/nutella_lib/framework_net.rb', line 123 def self.sync_request_to_run( app_id, run_id, channel, request) Nutella::Net.sync_request_to(channel, request, app_id, run_id) end |
.un_pad_wildcard_channel(channel) ⇒ Object
201 202 203 204 |
# File 'lib/nutella_lib/framework_net.rb', line 201 def self.un_pad_wildcard_channel(channel) regex = Regexp.new '/nutella/apps/[^"\/"]+/runs/[^"\/"]+/' channel.gsub(regex, '') end |
.unsubscribe(channel) ⇒ Object
Un-subscribes from a framework-level channel
25 26 27 |
# File 'lib/nutella_lib/framework_net.rb', line 25 def self.unsubscribe( channel ) Nutella::Net.unsubscribe_to(channel, nil, nil) end |
.unsubscribe_from_all_apps(channel) ⇒ Object
Allows framework-level APIs to unsubscribe from an app-level channel *for ALL apps*
438 439 440 |
# File 'lib/nutella_lib/framework_net.rb', line 438 def self.unsubscribe_from_all_apps( channel ) Nutella::Net.unsubscribe_to(channel, '+', nil) end |
.unsubscribe_from_all_runs(channel) ⇒ Object
Allows framework-level APIs to unsubscribe from a run-level channel *for ALL runs*
210 211 212 |
# File 'lib/nutella_lib/framework_net.rb', line 210 def self.unsubscribe_from_all_runs( channel ) Nutella::Net.unsubscribe_to(channel, '+', '+') end |
.unsubscribe_to_app(app_id, channel) ⇒ Object
Allows framework-level APIs to unsubscribe from an app-level channel within a specific run
335 336 337 |
# File 'lib/nutella_lib/framework_net.rb', line 335 def self.unsubscribe_to_app( app_id, channel ) Nutella::Net.unsubscribe_to(channel, app_id, nil) end |
.unsubscribe_to_run(app_id, run_id, channel) ⇒ Object
Allows framework-level APIs to unsubscribe from a run-level channel within a specific run
99 100 101 |
# File 'lib/nutella_lib/framework_net.rb', line 99 def self.unsubscribe_to_run( app_id, run_id, channel ) Nutella::Net.unsubscribe_to(channel, app_id, run_id) end |