class Lightning::Plugin
Attributes
Public Class Methods
Define RPC
method. @param [String] name the name of rpc method. @param [Proc] lambda Lambda which is the substance of RPC
method.
# File lib/lightning/plugin.rb, line 83 def define_rpc(name, lambda) m = name.to_sym raise ArgumentError, 'method must be implemented using lambda.' unless lambda.is_a?(Proc) && lambda.lambda? raise ArgumentError, "#{m} was already defined." if methods[m] raise ArgumentError, "usage for #{m} dose not defined." unless @usage raise ArgumentError, "description for #{m} dose not defined." unless @desc define_method(m, lambda) methods[m] = Method.new(m, @usage, @desc, long_desc: @long_desc) end
Define the definition of RPC
method @param [String] usage the usage of RPC
method. @param [String] desc the description of RPC
method. @param [String] long_desc (Optional) the long description of RPC
method.
# File lib/lightning/plugin.rb, line 74 def desc(usage, desc, long_desc = nil) @usage = usage @desc = desc @long_desc = long_desc end
Define hook handler. @param [Symbol] event the event name. @param [Proc] lambda Lambda which is the event handler.
# File lib/lightning/plugin.rb, line 107 def hook(event, lambda) e = event.to_sym raise ArgumentError, 'handler must be implemented using lambda.' unless lambda.is_a?(Proc) && lambda.lambda? raise ArgumentError, "Hook #{e} was already registered." if methods[e] define_method(e, lambda) methods[e] = Method.new(e, type: Method::TYPE[:hook]) end
get RPM methods. @return [Hash] the hash of RPC
method.
# File lib/lightning/plugin.rb, line 46 def methods @methods ||= {} end
# File lib/lightning/plugin.rb, line 124 def initialize methods[:init] = Method.new(:init) @stdout = STDOUT @stdin = STDIN methods[:getmanifest] = Method.new(:getmanifest) @log = Lightning::Logger.create(:plugin) end
Define option for lightningd command line. @param [String] name option name. @param [String] default default value. @param [String] description description.
# File lib/lightning/plugin.rb, line 65 def option(name, default, description) # currently only string options are supported. options << {name: name, default: default, description: description, type: 'string'} end
get options
# File lib/lightning/plugin.rb, line 57 def options @options ||= [] end
Define Event notification handler. @param [Symbol] event the event name. @param [Proc] lambda Lambda which is the event handler.
# File lib/lightning/plugin.rb, line 96 def subscribe(event, lambda) e = event.to_sym raise ArgumentError, 'handler must be implemented using lambda.' unless lambda.is_a?(Proc) && lambda.lambda? raise ArgumentError, "Topic #{e} already has a handler." if subscriptions.include?(e) define_method(e, lambda) subscriptions << e end
get subscriptions @return [Hash] the hash of subscriptions
# File lib/lightning/plugin.rb, line 52 def subscriptions @subscriptions ||= [] end
Public Instance Methods
get manifest information. @return [Hash] the manifest.
# File lib/lightning/plugin.rb, line 146 def getmanifest log.info("getmanifest") { options: options, rpcmethods: rpc_methods.map(&:to_h), subscriptions: subscriptions, hooks: hook_methods.map(&:name), } end
# File lib/lightning/plugin.rb, line 132 def init(opts, configuration) log.info("init") @lightning_dir = configuration['lightning-dir'] @rpc_filename = configuration['rpc-file'] socket_path = (Pathname.new(lightning_dir) + rpc_filename).to_path @rpc = Lightning::RPC.new(socket_path) opts.each do |key, value| options.find{|o|o[:name] == key}[:value] = value end nil end
run plugin.
# File lib/lightning/plugin.rb, line 162 def run log.info("Plugin run.") begin partial = '' stdin.each_line do |l| partial << l msgs = partial.split("\n\n", -1) next if msgs.size < 2 partial = multi_dispatch(msgs) end rescue Interrupt log.info "Interrupt occur." shutdown end log.info("Plugin end.") end
shutdown this plugin.
# File lib/lightning/plugin.rb, line 157 def shutdown log.info "Plugin shutdown" end
Private Instance Methods
# File lib/lightning/plugin.rb, line 223 def dispatch_notification(request) begin name = request.method raise ArgumentError, "No handler #{request.method} found." unless subscriptions.include?(request.method) request.method_args.empty? ? send(name) : send(name, *request.method_args) rescue Exception => e log.error e end end
# File lib/lightning/plugin.rb, line 211 def dispatch_request(request) begin method = methods[request.method] raise ArgumentError, "No method #{request.method} found." unless method result = request.method_args.empty? ? send(method.name) : send(method.name, *request.method_args) request.apply_result(result) if result rescue Exception => e log.error e request.write_error(e) end end
# File lib/lightning/plugin.rb, line 237 def hook_methods methods.values.select(&:hook?) end
get method list @return [Hash] the hash of method.
# File lib/lightning/plugin.rb, line 183 def methods self.class.methods # delegate to class instance end
# File lib/lightning/plugin.rb, line 197 def multi_dispatch(msgs) msgs[0...-1].each do |payload| json = JSON.parse(payload) log.info("receive payload = #{json}") request = Lightning::Request.parse_from_json(self, json) if request.id dispatch_request(request) else dispatch_notification(request) end end msgs[-1] end
get options
# File lib/lightning/plugin.rb, line 193 def options self.class.options # delegate to class instance end
# File lib/lightning/plugin.rb, line 233 def rpc_methods methods.values.select(&:rpc?) end
get subscriptions
# File lib/lightning/plugin.rb, line 188 def subscriptions self.class.subscriptions # delegate to class instance end