Class: LibvirtAsync::Timer

Inherits:
Object
  • Object
show all
Includes:
WithDbg
Defined in:
lib/libvirt_async/timer.rb

Defined Under Namespace

Classes: Monitor

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(timer_id, interval, opaque) ⇒ Timer

Returns a new instance of Timer.



46
47
48
49
50
51
52
53
54
# File 'lib/libvirt_async/timer.rb', line 46

def initialize(timer_id, interval, opaque)
  dbg { "#{self.class}#initialize timer_id=#{timer_id}, interval=#{interval}" }

  @timer_id = timer_id
  @interval = interval.to_f / 1000.to_f
  @opaque = opaque
  @last_fired = Time.now.to_f
  @monitor = nil
end

Instance Attribute Details

#intervalObject

Returns the value of attribute interval.



44
45
46
# File 'lib/libvirt_async/timer.rb', line 44

def interval
  @interval
end

#last_firedObject

Returns the value of attribute last_fired.



44
45
46
# File 'lib/libvirt_async/timer.rb', line 44

def last_fired
  @last_fired
end

#monitorObject (readonly)

Returns the value of attribute monitor.



43
44
45
# File 'lib/libvirt_async/timer.rb', line 43

def monitor
  @monitor
end

#opaqueObject (readonly)

Returns the value of attribute opaque.



43
44
45
# File 'lib/libvirt_async/timer.rb', line 43

def opaque
  @opaque
end

#timer_idObject (readonly)

Returns the value of attribute timer_id.



43
44
45
# File 'lib/libvirt_async/timer.rb', line 43

def timer_id
  @timer_id
end

Instance Method Details

#inspectObject



106
107
108
# File 'lib/libvirt_async/timer.rb', line 106

def inspect
  to_s
end

#registerObject



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
# File 'lib/libvirt_async/timer.rb', line 61

def register
  dbg { "#{self.class}#register starts timer_id=#{timer_id}, interval=#{interval}" }

  if wait_time.nil?
    dbg { "#{self.class}#register no wait time timer_id=#{timer_id}, interval=#{interval}" }
    return
  end

  task = Util.create_task do
    dbg { "#{self.class}#register async starts timer_id=#{timer_id}, interval=#{interval}" }
    now_time = Time.now.to_f
    timeout = wait_time > now_time ? wait_time - now_time : 0
    @monitor = Monitor.new
    cancelled = wait_timer(timeout)

    if cancelled
      dbg { "#{self.class}#register async cancel timer_id=#{timer_id}, interval=#{interval}" }
    else
      dbg { "#{self.class}#register async ready timer_id=#{timer_id}, interval=#{interval}" }
      self.last_fired = Time.now.to_f
      dispatch
    end
  end

  dbg { "#{self.class}#register invokes fiber=0x#{task.fiber.object_id.to_s(16)} timer_id=#{timer_id}, interval=#{interval}" }
  task.run
  dbg { "#{self.class}#register ends timer_id=#{timer_id}, interval=#{interval}" }
end

#to_sObject



102
103
104
# File 'lib/libvirt_async/timer.rb', line 102

def to_s
  "#<#{self.class}:0x#{object_id.to_s(16)} timer_id=#{timer_id} interval=#{interval} last_fired=#{last_fired} monitor=#{monitor}>"
end

#unregisterObject



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/libvirt_async/timer.rb', line 90

def unregister
  dbg { "#{self.class}#unregister_timer timer_id=#{timer_id}, interval=#{interval}" }

  if @monitor.nil?
    dbg { "#{self.class}#unregister_timer already unregistered timer_id=#{timer_id}, interval=#{interval}" }
    return
  end

  @monitor.close
  @monitor = nil
end

#wait_timeObject



56
57
58
59
# File 'lib/libvirt_async/timer.rb', line 56

def wait_time
  return if interval < 0
  last_fired + interval
end