Class: LanGrove::Behaviour::Base

Inherits:
LanGrove::Base show all
Defined in:
lib/langrove/behaviour/behaviour_base.rb

Instance Attribute Summary collapse

Attributes inherited from LanGrove::Base

#logger

Instance Method Summary collapse

Methods inherited from LanGrove::Base

#config_exception, #type

Constructor Details

#initialize(root, config, name) ⇒ Base

Returns a new instance of Base.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/langrove/behaviour/behaviour_base.rb', line 18

def initialize( root, config, name )

  @config = config
  @running = false
  
  super( root )
  
  @requires = {
     
    :enactor => {
      
       :actions => [
         
         #
         # Array of actions this behaviour can
         # perform. 
         # 
         # 1st is the default.
         #
         
         :act,
         
         :alt_act
         
        ],
       
       :version => 0
       
       # 
       # SIGNIFICANT DECISION
       #
       # Yes or No to binding plugins to behaviours
       # so TIGHTLY (with version locking)
       # 
       # I vote yes.
       # 
       # -- Versions will likely very seldom change
       # 
       # -- When they do it will be for significant
       #    reasons. Therefore suggesting back-ward-
       #    compatable-less-ness.
       # 
       # -- The interactions between behaviour and 
       #    the actioning plugin will be deeply int-
       #    erwoven into daemon inner workings. Un-
       #    noticed interface incompatibilites could
       #    significanly scramble things - possibly
       #    all the way into databases.
       # 
       # -- Users can define their own plugins.
       #    
       #    After a langrove upgrade that modified 
       #    the plugin interface expectations by the
       #    behaviour. Their daemons may start up
       #    without incident on account of ruby's
       #    type-less-ness, but they may not be fun-
       #    ctioning anticipatedly. 
       # 
       #    Hense the scramble. 
       #  
  
    }
     
  } if @requires.nil?
  
  load_actions
  
  default_plugin
  
  load_plugin

end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(symbol, *args, &block) ⇒ Object

Expose functions on the plugin



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/langrove/behaviour/behaviour_base.rb', line 100

def method_missing( symbol, *args, &block )
  
  if @plugin.respond_to? symbol

    return @plugin.send(
    
      symbol, *args
    
    )
    
  end
  
  super
  
end

Instance Attribute Details

#runningObject

behave |verb| ~ a present tense behad

eg. The ability to sing was being had
    by this one time,

    At band camp.


16
17
18
# File 'lib/langrove/behaviour/behaviour_base.rb', line 16

def running
  @running
end

Instance Method Details

#call(recipient, recipient_type, action = nil) ⇒ Object

call( server_object, :server, :action ) -> calls action on server call( handler_object, :handler, :action ) -> calls action on handler call( server_object, :handler, :action ) -> calls action on ALL handlers

-------------   -------                               ---


228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/langrove/behaviour/behaviour_base.rb', line 228

def call( recipient, recipient_type, action = nil )
  
  Unimplemented.notify( 
  
    @logger, 
    "IMPORTANT!!! mechanism to stop multiple instances of the behaviour", 
    [:vital,:metrix] 
  
  )
  
  @logger.debug( "#{self.class}.call(#{recipient}, #{recipient_type}, #{action})" )
  
  begin
    
    @logger.debug "#{recipient}.type undefined - skipping event"

    return
    
  end unless recipient.respond_to?( :type )
  
  if recipient_type == recipient.type
      
      do_action( recipient, action )
      
      return
            
  end 
  
  case recipient.type 
    
    when :server
      
      recipient.each_handler do |handler|
        
        #
        # Call to action on only handlers of recipient_type
        # Multiplexers are contained in the servers handler 
        # collection, but should not be actioned on events 
        # intended for ordinary handlers.
        # 

        do_action( handler, action ) if handler.type == recipient_type

      end if recipient_type == :handler   
    
  end
  
end

#default_pluginObject



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/langrove/behaviour/behaviour_base.rb', line 136

def default_plugin
  
  unless @config[:plugin].nil?
    
    return if @config[:plugin].is_a?( Hash )
    
  end
  
  type = self.requires.keys.first
  
  unless @config[:plugin].nil?
      
    @plugin = @root.config.get_plugin( @config[:plugin] )
    
    return
    
  end
  
  #
  # Plugin has not been specified, get by type
  #
  @plugin = @root.config.get_plugin( nil, type )
  
end

#do_action(handler, function = nil) ⇒ Object



278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# File 'lib/langrove/behaviour/behaviour_base.rb', line 278

def do_action( handler, function = nil )
  
  function = @default_function if function.nil?
  
  #
  # Call the behaviour.
  #
  raise LanGrove::PluginException.new(
  
    "#{@plugin.class}.#{function}() not implemented."
  
  ) unless @plugin.respond_to?( function )
  
  if not handler.respond_to?( "pending_#{function}" ) or
      handler.send( "pending_#{function}".to_sym )
    
    #
    # Optionally, the handler can define
    # pending_<action_name> to flag an
    # action bypass.
    # 
    # eg. Persistable behaviour calls 
    #     <plugin>.store()
    # 
    #     But will not do so if 
    #     <handler>.pending_store == false
    #
    result = @plugin.send( 
  
      "#{function}_handler".to_sym , handler 
    
    )
    
    if result != false and 

        handler.respond_to?( "pending_#{function}".to_sym )
        
        #
        # If present and the action did not return false
        # Reset the pending_<action> to false.
        #
      
      handler.send( "pending_#{function}=".to_sym, false )
    
    else
      
      #
      # Action Failed
      #
      I.notify( @logger, "no mitigation on plugin failing behaviour" )
    
    end
    
    return result
    
  end
 
end

#load_actionsObject



129
130
131
132
133
134
# File 'lib/langrove/behaviour/behaviour_base.rb', line 129

def load_actions
  
  @functions =  @requires.first[1][:actions]
  @default_function = @functions[0]
  
end

#load_pluginObject



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/langrove/behaviour/behaviour_base.rb', line 161

def load_plugin
  
  if @plugin.nil?

    config_exception(
    
        "#{self.class} requires a :plugin: subconfig."
    
    ) unless ( 
    
      @config.has_key?( :plugin ) and
      @config[ :plugin ].is_a?( Hash )
      
    )
    
    @logger.info "#{self.class} loading plugin."
    
    @plugin = LanGrove::ModuleLoader.create( {
      
      :key => :plugin,
      :config => @config
      
    } ).new( @root, @config[:plugin], nil )
  
  end
  
  #
  # ((Currently)) only supports 
  # one plugin per behaviour
  #
  
  required_type = requires.first[0]
  
  config_exception(
    
    "#{self.class} requires a #{required_type} plugin."
    
  ) unless @plugin.provides[ required_type ]
  
  
  #
  # test plugin for supported behaviour 
  # interface version 
  #
  
  required_version = requires.first[1][:version]
  
  config_exception(
    
    "#{self.class} requires a version #{required_version} #{required_type} plugin."
    
  ) unless @plugin.provides[ 
    
    required_type 
    
  ][:version] == required_version
  
end

#requiresObject



91
92
93
94
95
# File 'lib/langrove/behaviour/behaviour_base.rb', line 91

def requires
  
  @requires
  
end

#respond_to?(symbol, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


116
117
118
119
120
121
122
123
124
125
126
# File 'lib/langrove/behaviour/behaviour_base.rb', line 116

def respond_to?( symbol, include_private = false )
  
  if @plugin.respond_to? symbol
    
    return true
    
  end
  
  super
  
end