Class: Distem::Events::EventManager

Inherits:
Object
  • Object
show all
Defined in:
lib/distem/events/eventmanager.rb

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (EventManager) initialize(trace = nil)

Returns a new instance of EventManager



10
11
12
13
# File 'lib/distem/events/eventmanager.rb', line 10

def initialize(trace = nil)
  @event_trace = trace
  @running_thread = nil
end

Instance Attribute Details

- (Object) event_trace (readonly)

Event trace



8
9
10
# File 'lib/distem/events/eventmanager.rb', line 8

def event_trace
  @event_trace
end

Instance Method Details

- (Object) run



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
# File 'lib/distem/events/eventmanager.rb', line 20

def run

  raise "No event trace is set" unless @event_trace
  raise "The event manager is already started!" if (@running_thread and @running_thread.alive?)

  event = nil
  date = 0

  runblock = Proc.new {
    init_time = Time.now

    begin
      event.trigger(@event_trace, date) if event
      date, event = @event_trace.pop_next_event
      if date
        sleep_time = init_time + date - Time.now
        sleep sleep_time if sleep_time > 0
      end
    end while event
  }

  @running_thread = Thread.new {
    runblock.call
  }

end

- (Object) set_trace(trace)



15
16
17
18
# File 'lib/distem/events/eventmanager.rb', line 15

def set_trace(trace)
  @running_thread.exit if @running_thread
  @event_trace = trace
end

- (Object) stop



47
48
49
50
51
# File 'lib/distem/events/eventmanager.rb', line 47

def stop
  @running_thread.exit if @running_thread
  @running_thread = nil
  @event_trace.clear
end