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.



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

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.



46
47
48
# File 'lib/libvirt_async/timer.rb', line 46

def interval
  @interval
end

#last_firedObject

Returns the value of attribute last_fired.



46
47
48
# File 'lib/libvirt_async/timer.rb', line 46

def last_fired
  @last_fired
end

#monitorObject (readonly)

Returns the value of attribute monitor.



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

def monitor
  @monitor
end

#opaqueObject (readonly)

Returns the value of attribute opaque.



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

def opaque
  @opaque
end

#timer_idObject (readonly)

Returns the value of attribute timer_id.



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

def timer_id
  @timer_id
end

Instance Method Details

#inspectObject



108
109
110
# File 'lib/libvirt_async/timer.rb', line 108

def inspect
  to_s
end

#registerObject



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

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



104
105
106
# File 'lib/libvirt_async/timer.rb', line 104

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



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

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



58
59
60
61
# File 'lib/libvirt_async/timer.rb', line 58

def wait_time
  return if interval < 0
  last_fired + interval
end