Class: LanGrove::Daemon::Base

Inherits:
Base
  • Object
show all
Defined in:
lib/langrove/daemon/daemon_base.rb

Constant Summary collapse

@@type =
:daemon

Instance Attribute Summary collapse

Attributes inherited from Base

#logger

Instance Method Summary collapse

Methods inherited from Base

#config_exception, #type

Constructor Details

#initialize(root, config_hash, daemon_name) ⇒ Base

Returns a new instance of Base.



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
219
220
221
222
223
224
225
226
227
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
276
277
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
# File 'lib/langrove/daemon/daemon_base.rb', line 183

def initialize( root, config_hash, daemon_name )
  
  super( root )
  
  @config      = config_hash
  @daemon_name = daemon_name
  
  #
  # TODO: The client
  #

  client  = nil
  @client = nil

  
  begin

    @logger.info "Starting daemon: #{@daemon_name}"

  rescue

    config_exception(
    
      "Requires a logger"
      
    )   
    
  end
  
  
  begin
    
    @my_config = @config[ :daemons ][ @daemon_name ]

    root.config.system = @my_config[:system] unless @my_config[:system].nil?

    root.config.application = @my_config[:application] unless @my_config[:application].nil?
         
    raise LanGrove::DaemonConfigException.new(

      'Daemon requires :system: and :application: defined.'

    ) if root.config.system.nil? or root.config.application.nil?

  rescue
    
    error = "Missing config for daemon: #{@daemon_name}"
    
    @logger.error( error )
    
    config_exception( 
    
      error
      
    )
     
  end
  
  
  #
  # Instanciate the Server module
  # 
  # - Contains the Handler collection (of attached clients)
  #
  
  server = LanGrove::ModuleLoader.create( { 
    
    :key => :server,
    :config => @my_config,
    :mandatory => false,
    :logger => @logger
    
  } )
  
  @server = server.new( 
  
    root, @config, @daemon_name 
    
  ) unless server.nil?
  
  
  #
  # Instanciate the Adaptor module
  #
  # - Interface to listen()
  #
  adaptor = LanGrove::ModuleLoader.create( { 
    
    :key => :adaptor,
    :config => @my_config,
    :mandatory => true,
    :logger => @logger
    
  } )
  
  @adaptor = adaptor.new( 
  
    root, @my_config[ :adaptor ], @daemon_name
    
  ) unless adaptor.nil?

  
  #
  # Load the Handler if present in config
  # 
  #  - Dont create instance.
  #
  #  - These are started up on socket connect
  # 
  #  - These will each have an attached client
  # 
  #  - These will reside in the Server.@handlers
  #    collection.  
  #
  @handler = LanGrove::ModuleLoader.create( { 
    
    :key => :handler,
    :config => @my_config,
    
    #
    # Mandatory if @server is loaded
    # 
    :mandatory => ( not @server.nil? ),
    :logger => @logger
    
  } )
  
  
  
  #
  # Laod the Protocol module
  #
  #  - Dont create instance.
  # 
  #  - These are started up, one each, in
  #    the Handler, as the socket connects.
  #
  @protocol = LanGrove::ModuleLoader.create( { 
    
    :key => :protocol,
    :config => @my_config,
    :mandatory =>  ( not @server.nil? ),
    :logger => @logger
    
  } )
  
  @server.start_server

end

Instance Attribute Details

#adaptorObject (readonly)

Returns the value of attribute adaptor.



20
21
22
# File 'lib/langrove/daemon/daemon_base.rb', line 20

def adaptor
  @adaptor
end

#serverObject (readonly)

Returns the value of attribute server.



19
20
21
# File 'lib/langrove/daemon/daemon_base.rb', line 19

def server
  @server
end

Instance Method Details

#connectObject



22
23
24
25
26
27
28
# File 'lib/langrove/daemon/daemon_base.rb', line 22

def connect

  #
  # UNIMPLEMENTED (Client Daemon)
  #
  
end

#listenObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/langrove/daemon/daemon_base.rb', line 31

def listen
  
  @adaptor.listen(

    #
    # Handler and Protocol are instanciated at socket connect,
    # they need the config then...
    #
    
    { :class => @handler, :config => @my_config[ :handler ] },
    
    { :class => @protocol, :config => @my_config[ :protocol ] },
    
    @server
    
  )
  
end

#reload_daemonObject



61
62
63
64
65
66
67
68
69
# File 'lib/langrove/daemon/daemon_base.rb', line 61

def reload_daemon
  
  @logger.info "#{self} received reload_daemon"

  @adaptor.reload_adaptor

  @server.reload_server
  
end

#runObject



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/langrove/daemon/daemon_base.rb', line 137

def run
  
  begin
    
    case @adaptor.type
      
      when :not_eventmachine
      
        start

      else

        EventMachine::run do
        
          start
        
        end
      
    end
  
  rescue LanGrove::DaemonStopException => e
    
    stop_daemon
    
  end
  
end

#schedule_behavioursObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/langrove/daemon/daemon_base.rb', line 72

def schedule_behaviours
  
  @server.schedule do |behaviour, at|
    
    at.each do |event, event_config|
      
      @root.schedule( 
      
        event, event_config, behaviour
        
      ) unless event == :interval
      
      
      if not defined?( EventMachine )
        
        @logger.warn( "Adaptor does not support :interval: scheduling" )
        
        return
        
      end if event == :interval
        
      EM::add_periodic_timer( event_config ) do 
        
        #
        # at_interval behaviours only act
        # on handlers - for now
        # 

        begin
         
          unless behaviour.running then
            
            #
            # Behaviour mey be scheduled at a shorter
            # interval than it takes to run.
            # 
            # This protects from multiple runnings
            #
            
            behaviour.running = true
            behaviour.call( @server, :handler )
            behaviour.running = false

          else
        
            @logger.warn("#{behaviour} already running at interval:#{event_config}")
        
          end
      
        rescue Exception => err
        
          puts err
          behaviour.running = false
        
        end 
      
      end if event == :interval
    
    end unless at.nil?
    
  end
       
end

#startObject



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/langrove/daemon/daemon_base.rb', line 165

def start
  
  schedule_behaviours unless @server.nil?

  #
  # Start the introspector
  #
  @root.metrix.start( self )
  
  #
  # TODO: The client
  # 
  connect unless @client.nil?
  
  listen unless @handler.nil?
  
end

#stop_daemonObject



51
52
53
54
55
56
57
58
59
# File 'lib/langrove/daemon/daemon_base.rb', line 51

def stop_daemon
  
  @logger.info "#{self} received stop_daemon"

  @adaptor.stop_adaptor

  @server.stop_server
  
end