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

#connection_event_handlers, #dispatcher, #message_headers, #node_id

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from RJR::Node

#clear_event_handlers, em, #em, #halt, #indirect?, indirect?, #join, #node_type, #on, persistent?, #persistent?, tp, #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



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/rjr/nodes/easy.rb', line 91

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

   nodes = 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

Class Method Details

.node_type_for(dst) ⇒ Object

Publically available helper, retrieve the rjr node type based on dst format



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/rjr/nodes/easy.rb', line 42

def self.node_type_for(dst)
  type = nil
  if dst.is_a?(String)
    if /tcp:\/\/.*/      =~ dst ||
       /jsonrpc:\/\/.*/  =~ dst ||
       /json-rpc:\/\/.*/ =~ dst
        type = RJR::Nodes::TCP

    elsif /ws:\/\/.*/    =~ dst
      type = RJR::Nodes::WS

    elsif /http:\/\/.*/   =~ dst
      type = RJR::Nodes::Web

    elsif /.*-queue$/   =~ dst
      type = RJR::Nodes::AMQP

    # else # TODO
    # type = RJR::Nodes::Local

    end
  end

  type
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



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

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



127
128
129
# File 'lib/rjr/nodes/easy.rb', line 127

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



153
154
155
156
# File 'lib/rjr/nodes/easy.rb', line 153

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



120
121
122
# File 'lib/rjr/nodes/easy.rb', line 120

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



162
163
164
165
166
167
# File 'lib/rjr/nodes/easy.rb', line 162

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