class RJR::Nodes::Easy

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.

@example 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

Public Class Methods

new(args = {}) click to toggle source

Easy Node initializer @param [Hash] args the options to create the node with @option args [Hash] :amqp options to create the amqp node with @option args [Hash] :ws options to create the ws node with @option args [Hash] :tcp options to create the ws node with @option args [Hash] :web options to create the web node with

Calls superclass method RJR::Node::new
# 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
node_type_for(dst) click to toggle source

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

# 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

Public Instance Methods

invoke(dst, rpc_method, *args) click to toggle source

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

Implementation of RJR::Node#invoke

@param [String] dst destination send request to @param [String] rpc_method json-rpc method to invoke on destination @param [Array] args array of arguments to convert to json and invoke remote method wtih @return [Object] the json result retrieved from destination converted to a ruby object @raise [Exception] if the destination raises an exception, it will be converted to json and re-raised here

# 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
listen() click to toggle source

Instruct Nodes to start listening for and dispatching rpc requests

Implementation of RJR::Node#listen

# File lib/rjr/nodes/easy.rb, line 127
def listen
  @multi_node.listen
end
notify(dst, rpc_method, *args) click to toggle source

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

Implementation of RJR::Node#notify

@param [String] dst destination to send notification to @param [String] rpc_method json-rpc method to invoke on destination @param [Array] args array of arguments to convert to json and invoke remote method wtih

# 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) click to toggle source

Send data using specified connection

Implementation of RJR::Node#send_msg

# File lib/rjr/nodes/easy.rb, line 120
def send_msg(data, connection)
# TODO
end
stop_on(signal) click to toggle source

Stop node on the specified signal

@param [Singnal] signal signal to stop the node on @return self

# File lib/rjr/nodes/easy.rb, line 162
def stop_on(signal)
  Signal.trap(signal) {
    @multi_node.stop
  }
  self
end

Private Instance Methods

get_node(dst) click to toggle source

Internal helper, retrieved the registered node depending on the type retrieved from the dst. If matching node type can’t be found, nil is returned

# File lib/rjr/nodes/easy.rb, line 73
def get_node(dst)
  type = self.class.node_type_for(dst)

  # TODO also add optional mechanism to class to load nodes of
  # new types on the fly as they are needed
  return @multi_node.nodes.find { |n| n.is_a?(type) } unless type.nil?

  nil
end