Class: OmfCommon::Eventloop

Inherits:
Object
  • Object
show all
Defined in:
omf_common/lib/omf_common/eventloop.rb,
omf_common/lib/omf_common/eventloop/em.rb,
omf_common/lib/omf_common/eventloop/local_evl.rb

Overview

Providing event loop support.

Direct Known Subclasses

EventMachine, Local

Defined Under Namespace

Classes: EventMachine, Local

Constant Summary

@@providers =
{
  em: {
    require: 'omf_common/eventloop/em',
    constructor: 'OmfCommon::Eventloop::EventMachine'
  },
  local: {
    require: 'omf_common/eventloop/local_evl',
    constructor: 'OmfCommon::Eventloop::Local'
  }
}
@@instance =
nil
@@on_stop_proc =
[]

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Eventloop) initialize(opts = {}, &block) (private)

Returns a new instance of Eventloop



119
120
121
# File 'omf_common/lib/omf_common/eventloop.rb', line 119

def initialize(opts = {}, &block)
  #run(&block) if block
end

Class Method Details

+ (Object) init(opts, &block)

opts:

:type - eventloop provider
:provider - custom provider (opts)
  :require - gem to load first (opts)
  :constructor - Class implementing provider


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'omf_common/lib/omf_common/eventloop.rb', line 30

def self.init(opts, &block)
  unless @@instance
    unless provider = opts[:provider]
      provider = @@providers[opts[:type].to_sym]
    end
    unless provider
      raise "Missing Eventloop provider declaration. Either define 'type' or 'provider'"
    end

    require provider[:require] if provider[:require]

    if class_name = provider[:constructor]
      provider_class = class_name.split('::').inject(Object) {|c,n| c.const_get(n) }
      inst = provider_class.new(opts, &block)
    else
      raise "Missing provider creation info - :constructor"
    end
    @@instance = inst
  end
  @@instance
end

+ (Object) instance



52
53
54
# File 'omf_common/lib/omf_common/eventloop.rb', line 52

def self.instance
  @@instance
end

Instance Method Details

- (Object) after(delay_sec, &block)

Execute block after some time

Parameters:

  • delay_sec (Float)

    in sec



60
61
62
# File 'omf_common/lib/omf_common/eventloop.rb', line 60

def after(delay_sec, &block)
  raise "Missing implementation 'after'"
end

- (Object) defer(&block)

Call 'block' in the context of a separate thread.



74
75
76
# File 'omf_common/lib/omf_common/eventloop.rb', line 74

def defer(&block)
  raise "Missing implementation 'defer'"
end

- (Object) every(interval_sec, &block)

Periodically call block every interval_sec

Parameters:

  • interval_sec (Float)

    in sec



68
69
70
# File 'omf_common/lib/omf_common/eventloop.rb', line 68

def every(interval_sec, &block)
  raise "Missing implementation 'every'"
end

- (Object) join

Block calling thread until eventloop exits



79
80
81
# File 'omf_common/lib/omf_common/eventloop.rb', line 79

def join()
  raise "Missing implementation 'join'"
end

- (Object) on_int_signal(&block)

Calling 'block' when having trapped an INT signal



106
107
108
109
# File 'omf_common/lib/omf_common/eventloop.rb', line 106

def on_int_signal(&block)
  # trap(:INT)
  warn "Missing implementation 'on_int_signal'"
end

- (Object) on_stop(&block)

Calling 'block' before stopping eventloop



100
101
102
# File 'omf_common/lib/omf_common/eventloop.rb', line 100

def on_stop(&block)
  @@on_stop_proc << block
end

- (Object) on_term_signal(&block)

Calling 'block' when having trapped a TERM signal



113
114
115
116
# File 'omf_common/lib/omf_common/eventloop.rb', line 113

def on_term_signal(&block)
  # trap(:TERM) {}
  warn "Missing implementation 'on_term_signal'"
end

- (Object) run



83
84
85
# File 'omf_common/lib/omf_common/eventloop.rb', line 83

def run()
  raise "Missing implementation 'run'"
end

- (Object) stop



87
88
89
90
91
92
93
94
95
96
# File 'omf_common/lib/omf_common/eventloop.rb', line 87

def stop()
  @@on_stop_proc.each do |block|
    begin
      block.call()
    rescue  => ex
      error "Exception '#{ex}'"
      debug "#{ex}\n\t#{ex.backtrace.join("\n\t")}"
    end
  end
end