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.



111
112
113
114
115
116
117
118
# File 'lib/lightning/plugin.rb', line 111

def initialize
  methods[:init] = Method.new(:init)
  @options = {}
  @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.



107
108
109
# File 'lib/lightning/plugin.rb', line 107

def lightning_dir
  @lightning_dir
end

#logObject (readonly)

Returns the value of attribute log.



106
107
108
# File 'lib/lightning/plugin.rb', line 106

def log
  @log
end

#optionsObject (readonly)

Returns the value of attribute options.



103
104
105
# File 'lib/lightning/plugin.rb', line 103

def options
  @options
end

#rpcObject

Returns the value of attribute rpc.



109
110
111
# File 'lib/lightning/plugin.rb', line 109

def rpc
  @rpc
end

#rpc_filenameObject

Returns the value of attribute rpc_filename.



108
109
110
# File 'lib/lightning/plugin.rb', line 108

def rpc_filename
  @rpc_filename
end

#stdinObject (readonly)

Returns the value of attribute stdin.



105
106
107
# File 'lib/lightning/plugin.rb', line 105

def stdin
  @stdin
end

#stdoutObject (readonly)

Returns the value of attribute stdout.



104
105
106
# File 'lib/lightning/plugin.rb', line 104

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)


69
70
71
72
73
74
75
76
77
# File 'lib/lightning/plugin.rb', line 69

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.



60
61
62
63
64
# File 'lib/lightning/plugin.rb', line 60

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)


93
94
95
96
97
98
99
# File 'lib/lightning/plugin.rb', line 93

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

.subscribe(event, lambda) ⇒ Object

Define Event notification handler.

Parameters:

  • event (Symbol)

    the event name.

  • lambda (Proc)

    Lambda which is the event handler.

Raises:

  • (ArgumentError)


82
83
84
85
86
87
88
# File 'lib/lightning/plugin.rb', line 82

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.



132
133
134
135
136
137
138
139
140
# File 'lib/lightning/plugin.rb', line 132

def getmanifest
  log.info("getmanifest")
  {
      options: options.values,
      rpcmethods: rpc_methods.map(&:to_h),
      subscriptions: subscriptions,
      hooks: hook_methods.map(&:name),
  }
end

#init(options, configuration) ⇒ Object



120
121
122
123
124
125
126
127
128
# File 'lib/lightning/plugin.rb', line 120

def init(options, 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, log)
  @options.merge!(options)
  nil
end

#runObject

run plugin.



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/lightning/plugin.rb', line 148

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 Exception => e
    if e.is_a?(Interrupt)
      shutdown
    else
      log.error e
      throw e
    end
  end
  log.info("Plugin end.")
end

#shutdownObject

shutdown this plugin.



143
144
145
# File 'lib/lightning/plugin.rb', line 143

def shutdown
  log.info "Plugin shutdown"
end