Class: Lightning::Plugin

Inherits:
Object
  • Object
show all
Defined in:
lib/lightning/plugin.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePlugin

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_dirObject

Returns the value of attribute lightning_dir.



120
121
122
# File 'lib/lightning/plugin.rb', line 120

def lightning_dir
  @lightning_dir
end

#logObject (readonly)

Returns the value of attribute log.



119
120
121
# File 'lib/lightning/plugin.rb', line 119

def log
  @log
end

#rpcObject

Returns the value of attribute rpc.



122
123
124
# File 'lib/lightning/plugin.rb', line 122

def rpc
  @rpc
end

#rpc_filenameObject

Returns the value of attribute rpc_filename.



121
122
123
# File 'lib/lightning/plugin.rb', line 121

def rpc_filename
  @rpc_filename
end

#stdinObject (readonly)

Returns the value of attribute stdin.



118
119
120
# File 'lib/lightning/plugin.rb', line 118

def stdin
  @stdin
end

#stdoutObject (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.

Parameters:

  • name (String)

    the name of rpc method.

  • lambda (Proc)

    Lambda which is the substance of RPC method.

Raises:

  • (ArgumentError)


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

Parameters:

  • usage (String)

    the usage of RPC method.

  • desc (String)

    the description of RPC method.

  • long_desc (String) (defaults to: nil)

    (Optional) the long description 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.

Parameters:

  • event (Symbol)

    the event name.

  • lambda (Proc)

    Lambda which is the event handler.

Raises:

  • (ArgumentError)


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

.methodsHash

get RPM methods.

Returns:

  • (Hash)

    the hash of RPC method.



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.

Parameters:

  • name (String)

    option name.

  • default (String)

    default value.

  • description (String)

    description.



65
66
67
68
# 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

.optionsObject

get options



57
58
59
# File 'lib/lightning/plugin.rb', line 57

def options
  @options ||= []
end

.subscribe(event, lambda) ⇒ Object

Define Event notification handler.

Parameters:

  • event (Symbol)

    the event name.

  • lambda (Proc)

    Lambda which is the event handler.

Raises:

  • (ArgumentError)


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

.subscriptionsHash

get subscriptions

Returns:

  • (Hash)

    the hash of subscriptions



52
53
54
# File 'lib/lightning/plugin.rb', line 52

def subscriptions
  @subscriptions ||= []
end

Instance Method Details

#getmanifestHash

get manifest information.

Returns:

  • (Hash)

    the manifest.



146
147
148
149
150
151
152
153
154
# 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

#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|
    options.find{|o|o[:name] == key}[:value] = value
  end
  nil
end

#runObject

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

#shutdownObject

shutdown this plugin.



157
158
159
# File 'lib/lightning/plugin.rb', line 157

def shutdown
  log.info "Plugin shutdown"
end