Class: RJR::Request

Inherits:
Object show all
Defined in:
lib/rjr/dispatcher.rb

Overview

JSON-RPC request representation.

Registered request handlers will be invoked in the context of instances of this class, meaning all member variables will be available for use in the handler.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Request

RJR Request initializer

Parameters:

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

    options to set on request

Options Hash (args):

  • :rjr_method (String)

    name of the method which request is for

  • :rjr_method_args (Array)

    array of arguments which to pass to the rpc method handler

  • :rjr_headers (Hash)

    hash of keys/values corresponding to optional headers received as part of of the request

  • :rjr_client_ip (String)

    ip address of client which invoked the request (if applicable)

  • :rjr_client_port (String)

    port of client which invoked the request (if applicable)

  • :rjr_callback (RJR::Callback)

    callback through which requests/notifications can be sent to remote node

  • :rjr_node (RJR::Node)

    rjr node which request was received on

  • :rjr_node_id (String)

    id of the rjr node which request was received on

  • :rjr_node_type (Symbol)

    type of the rjr node which request was received on

  • :rjr_handler (Callable)

    callable object registered to the specified method which to invoke request on with arguments



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/rjr/dispatcher.rb', line 117

def initialize(args = {})
  @rjr_method      = args[:rjr_method]      || args['rjr_method']
  @rjr_method_args = args[:rjr_method_args] || args['rjr_method_args'] || []
  @rjr_headers     = args[:rjr_headers]     || args['rjr_headers']

  @rjr_client_ip   = args[:rjr_client_ip]
  @rjr_client_port = args[:rjr_client_port]

  @rjr_callback    = args[:rjr_callback]
  @rjr_node        = args[:rjr_node]
  @rjr_node_id     = args[:rjr_node_id]     || args['rjr_node_id']
  @rjr_node_type   = args[:rjr_node_type]   || args['rjr_node_type']

  @rjr_handler     = args[:rjr_handler]

  @result = nil
end

Instance Attribute Details

#resultObject

Result of the request operation, set by dispatcher



88
89
90
# File 'lib/rjr/dispatcher.rb', line 88

def result
  @result
end

#rjr_headersObject

Headers which came w/ request



97
98
99
# File 'lib/rjr/dispatcher.rb', line 97

def rjr_headers
  @rjr_headers
end

#rjr_methodObject

Method which request is for



91
92
93
# File 'lib/rjr/dispatcher.rb', line 91

def rjr_method
  @rjr_method
end

#rjr_method_argsObject

Arguments be passed to method



94
95
96
# File 'lib/rjr/dispatcher.rb', line 94

def rjr_method_args
  @rjr_method_args
end

#rjr_node_idObject

ID of node which request came in on



103
104
105
# File 'lib/rjr/dispatcher.rb', line 103

def rjr_node_id
  @rjr_node_id
end

#rjr_node_typeObject

Type of node which request came in on



100
101
102
# File 'lib/rjr/dispatcher.rb', line 100

def rjr_node_type
  @rjr_node_type
end

Class Method Details

.json_create(o) ⇒ Object

Create new request from json representation



170
171
172
173
174
175
# File 'lib/rjr/dispatcher.rb', line 170

def self.json_create(o)
  result  = Result.new(o['data']['result'])
  request = Request.new(o['data']['request'])
  request.result = result
  return request
end

Instance Method Details

#handleObject

Invoke the request by calling the registered handler with the registered method parameters in the local scope



137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/rjr/dispatcher.rb', line 137

def handle
  node_sig   = "#{@rjr_node_id}(#{@rjr_node_type})"
  method_sig = "#{@rjr_method}(#{@rjr_method_args.join(',')})"

  RJR::Logger.info "#{node_sig}->#{method_sig}"

  retval = instance_exec(*@rjr_method_args, &@rjr_handler)

  RJR::Logger.info \
    "#{node_sig}<-#{method_sig}<-#{retval.nil? ? "nil" : retval}"

  return retval
end

#to_json(*a) ⇒ Object

Convert request to json representation and return it



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/rjr/dispatcher.rb', line 152

def to_json(*a)
  {
    'json_class' => self.class.name,
    'data'       =>
      {:request => { :rjr_method      => @rjr_method,
                     :rjr_method_args => @rjr_method_args,
                     :rjr_headers     => @rjr_headers,
                     :rjr_node_type   => @rjr_node_type,
                     :rjr_node_id     => @rjr_node_id },

       :result  => { :result          => @result.result,
                     :error_code      => @result.error_code,
                     :error_msg       => @result.error_msg,
                     :error_class     => @result.error_class } }
  }.to_json(*a)
end