Module: Nutella::Net

Defined in:
lib/nutella_lib/net.rb

Overview

This module implements the pub/sub and request/response APIs at the run level

Class Method Summary collapse

Class Method Details

.async_request(channel, message = nil, callback) ⇒ Object

Performs an asynchronous request

Parameters:

  • channel (String)

    we want to make the request to. CANNOT contain wildcard(s)!

  • message (Object) (defaults to: nil)

    the body of request. This can be, nil/empty (default), a string, a hash and, in general, anything with a .to_json method.

  • callback (Proc)

    the callback that is fired whenever a response is received. It takes one parameter (response).



64
65
66
# File 'lib/nutella_lib/net.rb', line 64

def self.async_request( channel, message=nil, callback )
  async_request_to(channel, message, callback, Nutella.app_id, Nutella.run_id)
end

.callbacksObject

Provides access to callbacks



14
# File 'lib/nutella_lib/net.rb', line 14

def self.callbacks; @callbacks end

.handle_requests(channel, callback) ⇒ Object

Handles requests on a certain channel

Parameters:

  • channel (String)

    we want to listen for requests on. Can contain wildcard(s).

  • callback (Proc)

    a lambda expression that is fired whenever a message is received. The passed callback takes the following parameters:

    • String

      the received message (payload). Messages that are not JSON are discarded.

    • Hash

      the sender’s identifiers (run_id, app_id, component_id and optionally resource_id)

    • 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.



78
79
80
# File 'lib/nutella_lib/net.rb', line 78

def self.handle_requests( channel, callback )
  handle_requests_on(channel, callback, Nutella.app_id, Nutella.run_id)
end

.listenObject

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.



99
100
101
102
103
104
105
# File 'lib/nutella_lib/net.rb', line 99

def self.listen
  begin
    sleep
  rescue Interrupt
    # Simply returns once interrupted
  end
end

.publish(channel, message = nil) ⇒ Object

Publishes a message to a channel

Parameters:

  • channel (String)

    we want to publish the message to. CANNOT contain wildcard(s)!

  • message (Object) (defaults to: nil)

    the message we are publishing. This can be, nil/empty (default), a string, a hash and, in general, anything with a .to_json method.



43
44
45
# File 'lib/nutella_lib/net.rb', line 43

def self.publish( channel, message=nil )
  publish_to( channel, message, Nutella.app_id, Nutella.run_id)
end

.start_pingingObject

Sends a ping every 5 seconds to pings channel of the proper level



86
87
88
89
90
91
92
93
# File 'lib/nutella_lib/net.rb', line 86

def self.start_pinging
  Nutella.ping_thread = Thread.new do
    loop do
      publish_to('pings', 'ping', Nutella.app_id, Nutella.run_id)
      sleep(5)
    end
  end
end

.subscribe(channel, callback) ⇒ Object

Subscribes to a channel or to a set of channels.

Parameters:

  • channel (String)

    the channel or filter we are subscribing to. Can contain wildcard(s)

  • callback (Proc)

    a lambda expression that is fired whenever a message is received. The passed callback takes the following parameters:

    • String

      message: the received message. Messages that are not JSON are discarded.

    • String

      channel: the channel the message was received on (optional, only for wildcard subscriptions)

    • Hash

      from: the sender’s identifiers (run_id, app_id, component_id and optionally resource_id)



25
26
27
# File 'lib/nutella_lib/net.rb', line 25

def self.subscribe( channel, callback )
  subscribe_to( channel, callback, Nutella.app_id, Nutella.run_id)
end

.subscriptionsObject

Provides access to the subscriptions



11
# File 'lib/nutella_lib/net.rb', line 11

def self.subscriptions; @subscriptions end

.sync_request(channel, message = nil) ⇒ Object

Performs a synchronous request.

Parameters:

  • channel (String)

    we want to make the request to. CANNOT contain wildcard(s)!

  • message (Object) (defaults to: nil)

    the body of request. This can be, nil/empty (default), a string, a hash and, in general, anything with a .to_json method.



53
54
55
# File 'lib/nutella_lib/net.rb', line 53

def self.sync_request( channel, message=nil )
  sync_request_to(channel, message, Nutella.app_id, Nutella.run_id)
end

.unsubscribe(channel) ⇒ Object

Un-subscribes from a channel

Parameters:

  • channel (String)

    we want to unsubscribe from. Can contain wildcard(s).



33
34
35
# File 'lib/nutella_lib/net.rb', line 33

def self.unsubscribe( channel )
  unsubscribe_to( channel, Nutella.app_id, Nutella.run_id)
end