Class: Lightning::Plugin
- Inherits:
-
Object
- Object
- Lightning::Plugin
- Defined in:
- lib/lightning/plugin.rb
Instance Attribute Summary collapse
-
#lightning_dir ⇒ Object
Returns the value of attribute lightning_dir.
-
#log ⇒ Object
readonly
Returns the value of attribute log.
-
#rpc ⇒ Object
Returns the value of attribute rpc.
-
#rpc_filename ⇒ Object
Returns the value of attribute rpc_filename.
-
#stdin ⇒ Object
readonly
Returns the value of attribute stdin.
-
#stdout ⇒ Object
readonly
Returns the value of attribute stdout.
Class Method Summary collapse
-
.define_rpc(name, lambda) ⇒ Object
Define RPC method.
-
.desc(usage, desc, long_desc = nil) ⇒ Object
Define the definition of RPC method.
-
.hook(event, lambda) ⇒ Object
Define hook handler.
-
.methods ⇒ Hash
get RPM methods.
-
.option(name, default, description) ⇒ Object
Define option for lightningd command line.
-
.options ⇒ Object
get options.
-
.subscribe(event, lambda) ⇒ Object
Define Event notification handler.
-
.subscriptions ⇒ Hash
get subscriptions.
Instance Method Summary collapse
-
#getmanifest ⇒ Hash
get manifest information.
- #init(opts, configuration) ⇒ Object
-
#initialize ⇒ Plugin
constructor
A new instance of Plugin.
-
#run ⇒ Object
run plugin.
-
#shutdown ⇒ Object
shutdown this plugin.
Constructor Details
#initialize ⇒ Plugin
Returns a new instance of Plugin.
124 125 126 127 128 129 130 |
# 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 |
Instance Attribute Details
#lightning_dir ⇒ Object
Returns the value of attribute lightning_dir.
120 121 122 |
# File 'lib/lightning/plugin.rb', line 120 def lightning_dir @lightning_dir end |
#log ⇒ Object (readonly)
Returns the value of attribute log.
119 120 121 |
# File 'lib/lightning/plugin.rb', line 119 def log @log end |
#rpc ⇒ Object
Returns the value of attribute rpc.
122 123 124 |
# File 'lib/lightning/plugin.rb', line 122 def rpc @rpc end |
#rpc_filename ⇒ Object
Returns the value of attribute rpc_filename.
121 122 123 |
# File 'lib/lightning/plugin.rb', line 121 def rpc_filename @rpc_filename end |
#stdin ⇒ Object (readonly)
Returns the value of attribute stdin.
118 119 120 |
# File 'lib/lightning/plugin.rb', line 118 def stdin @stdin end |
#stdout ⇒ Object (readonly)
Returns the value of attribute stdout.
117 118 119 |
# File 'lib/lightning/plugin.rb', line 117 def stdout @stdout end |
Class Method Details
.define_rpc(name, lambda) ⇒ Object
Define RPC method.
83 84 85 86 87 88 89 90 91 |
# 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 |
.desc(usage, desc, long_desc = nil) ⇒ Object
Define the definition of RPC method
74 75 76 77 78 |
# File 'lib/lightning/plugin.rb', line 74 def desc(usage, desc, long_desc = nil) @usage = usage @desc = desc @long_desc = long_desc end |
.hook(event, lambda) ⇒ Object
Define hook handler.
107 108 109 110 111 112 113 |
# 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 |
.methods ⇒ Hash
get RPM methods.
46 47 48 |
# File 'lib/lightning/plugin.rb', line 46 def methods @methods ||= {} end |
.option(name, default, description) ⇒ Object
Define option for lightningd command line.
65 66 67 68 |
# File 'lib/lightning/plugin.rb', line 65 def option(name, default, description) # currently only string options are supported. << {name: name, default: default, description: description, type: 'string'} end |
.options ⇒ Object
get options
57 58 59 |
# File 'lib/lightning/plugin.rb', line 57 def @options ||= [] end |
.subscribe(event, lambda) ⇒ Object
Define Event notification handler.
96 97 98 99 100 101 102 |
# 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 |
.subscriptions ⇒ Hash
get subscriptions
52 53 54 |
# File 'lib/lightning/plugin.rb', line 52 def subscriptions @subscriptions ||= [] end |
Instance Method Details
#getmanifest ⇒ Hash
get manifest information.
146 147 148 149 150 151 152 153 154 |
# File 'lib/lightning/plugin.rb', line 146 def getmanifest log.info("getmanifest") { options: , rpcmethods: rpc_methods.map(&:to_h), subscriptions: subscriptions, hooks: hook_methods.map(&:name), } end |
#init(opts, configuration) ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 |
# 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| .find{|o|o[:name] == key}[:value] = value end nil end |
#run ⇒ Object
run plugin.
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# 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 ⇒ Object
shutdown this plugin.
157 158 159 |
# File 'lib/lightning/plugin.rb', line 157 def shutdown log.info "Plugin shutdown" end |