Class: RJR::Nodes::Easy

Inherits:
RJR::Node show all
Defined in:
lib/rjr/nodes/easy.rb

Overview

Easy node definition.

Clients should specify the transports that they would like to use and their relevant config upon instantating this call. After which invocations and notifications will be routed via the correct transport depending on the format of the destination.

All nodes managed locally will share the same dispatcher so that json-rpc methods only need to be registered once, with the multi-node itself.

Examples:

invoking requests via multiple protocols

easy = RJR::Nodes::Easy.new :tcp  => { :host   => 'localhost', :port => 8999 },
                            :amqp => { :broker => 'localhost' }

easy.invoke 'tcp://localhost:9000/', 'hello world'
# => sent via tcp

easy.notify 'dest-queue', 'hello world'
# => sent via amqp

Instance Attribute Summary

Attributes inherited from RJR::Node

#dispatcher, #message_headers, #node_id

Instance Method Summary collapse

Methods inherited from RJR::Node

em, #halt, #join, #node_type, #on, tp

Constructor Details

#initialize(args = {}) ⇒ Easy

Easy Node initializer

Parameters:

  • args (Hash) (defaults to: {})

    the options to create the node with

Options Hash (args):

  • :amqp (Hash)

    options to create the amqp node with

  • :ws (Hash)

    options to create the ws node with

  • :tcp (Hash)

    options to create the ws node with

  • :web (Hash)

    options to create the web node with



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/rjr/nodes/easy.rb', line 79

def initialize(args = {})
   super(args)

   nodes = []
   args.keys.each { |n|
     node = 
     case n
     when :amqp then
       RJR::Nodes::AMQP.new  args[:amqp].merge(args)
     when :ws then
       RJR::Nodes::WS.new    args[:ws].merge(args)
     when :tcp then
       RJR::Nodes::TCP.new   args[:tcp].merge(args)
     when :web then
       RJR::Nodes::Web.new   args[:web].merge(args)
     end

     if node
       nodes << node
     end
   }

   @multi_node = RJR::Nodes::Multi.new :nodes => nodes
   @dispatcher = @multi_node.dispatcher
end

Instance Method Details

#invoke(dst, rpc_method, *args) ⇒ Object

Instructs node to send rpc request, and wait for and return response.

Implementation of RJR::Node#invoke

Parameters:

  • dst (String)

    destination send request to

  • rpc_method (String)

    json-rpc method to invoke on destination

  • args (Array)

    array of arguments to convert to json and invoke remote method wtih

Returns:

  • (Object)

    the json result retrieved from destination converted to a ruby object

Raises:

  • (Exception)

    if the destination raises an exception, it will be converted to json and re-raised here



128
129
130
131
132
# File 'lib/rjr/nodes/easy.rb', line 128

def invoke(dst, rpc_method, *args)
  n = get_node(dst)
  # TODO raise exception if n.nil?
  n.invoke dst, rpc_method, *args
end

#listenObject

Instruct Nodes to start listening for and dispatching rpc requests

Implementation of RJR::Node#listen



115
116
117
# File 'lib/rjr/nodes/easy.rb', line 115

def listen
  @multi_node.listen
end

#notify(dst, rpc_method, *args) ⇒ Object

Instructs node to send rpc notification (immadiately returns / no response is generated)

Implementation of RJR::Node#notify

Parameters:

  • dst (String)

    destination to send notification to

  • rpc_method (String)

    json-rpc method to invoke on destination

  • args (Array)

    array of arguments to convert to json and invoke remote method wtih



141
142
143
144
# File 'lib/rjr/nodes/easy.rb', line 141

def notify(dst, rpc_method, *args)
  n = get_node(dst)
  n.notify dst, rpc_method, *args
end

#send_msg(data, connection) ⇒ Object

Send data using specified connection

Implementation of RJR::Node#send_msg



108
109
110
# File 'lib/rjr/nodes/easy.rb', line 108

def send_msg(data, connection)
# TODO
end

#stop_on(signal) ⇒ Object

Stop node on the specified signal

Parameters:

  • signal (Singnal)

    signal to stop the node on

Returns:

  • self



150
151
152
153
154
155
# File 'lib/rjr/nodes/easy.rb', line 150

def stop_on(signal)
  Signal.trap(signal) {
    @multi_node.stop
  }
  self
end