Class: LibvirtAsync::Handle

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

Defined Under Namespace

Classes: Monitor

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(handle_id, fd, events, opaque) ⇒ Handle

Returns a new instance of Handle.



32
33
34
35
36
37
38
39
40
# File 'lib/libvirt_async/handle.rb', line 32

def initialize(handle_id, fd, events, opaque)
  dbg { "#{self.class}#initialize handle_id=#{handle_id}, fd=#{fd}, events=#{events}" }

  @handle_id = handle_id
  @fd = fd
  @events = events
  @opaque = opaque
  @monitor = nil
end

Instance Attribute Details

#eventsObject

Returns the value of attribute events.



30
31
32
# File 'lib/libvirt_async/handle.rb', line 30

def events
  @events
end

#fdObject (readonly)

Returns the value of attribute fd.



29
30
31
# File 'lib/libvirt_async/handle.rb', line 29

def fd
  @fd
end

#handle_idObject (readonly)

Returns the value of attribute handle_id.



29
30
31
# File 'lib/libvirt_async/handle.rb', line 29

def handle_id
  @handle_id
end

#monitorObject (readonly)

Returns the value of attribute monitor.



29
30
31
# File 'lib/libvirt_async/handle.rb', line 29

def monitor
  @monitor
end

#opaqueObject (readonly)

Returns the value of attribute opaque.



29
30
31
# File 'lib/libvirt_async/handle.rb', line 29

def opaque
  @opaque
end

Instance Method Details

#inspectObject



109
110
111
# File 'lib/libvirt_async/handle.rb', line 109

def inspect
  to_s
end

#registerObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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
89
90
91
# File 'lib/libvirt_async/handle.rb', line 42

def register
  dbg { "#{self.class}#register handle_id=#{handle_id}, fd=#{fd}" }

  if (events & Libvirt::EVENT_HANDLE_ERROR) != 0
    dbg { "#{self.class}#register skip EVENT_HANDLE_ERROR handle_id=#{handle_id}, fd=#{fd}" }
  end
  if (events & Libvirt::EVENT_HANDLE_HANGUP) != 0
    dbg { "#{self.class}#register skip EVENT_HANDLE_HANGUP handle_id=#{handle_id}, fd=#{fd}" }
  end

  interest = events_to_interest(events)
  dbg { "#{self.class}#register parse handle_id=#{handle_id}, fd=#{fd}, events=#{events}, interest=#{interest}" }

  if interest.nil?
    dbg { "#{self.class}#register no interest handle_id=#{handle_id}, fd=#{fd}" }
    return
  end

  task = Util.create_task do
    dbg { "#{self.class}#register_handle Async start handle_id=#{handle_id}, fd=#{fd}" }
    io_mode = interest_to_io_mode(interest)

    io = IO.new(fd, io_mode, autoclose: false)
    @monitor = Monitor.new(io)

    while @monitor.readiness == nil
      cancelled = wait_io(interest)

      if cancelled
        dbg { "#{self.class}#register_handle async cancel handle_id=#{handle_id}, fd=#{fd}" }
        break
      end

      dbg { "#{self.class}#register_handle async resumes readiness=#{@monitor.readiness}, handle_id=#{handle_id}, fd=#{fd}" }
      events = readiness_to_events(@monitor.readiness)

      unless events.nil?
        dispatch(events)
        break
      end

      dbg { "#{self.class}#register_handle async not ready readiness=#{@monitor.readiness}, handle_id=#{handle_id}, fd=#{fd}" }
    end

  end

  dbg { "#{self.class}#register_handle invokes fiber=0x#{task.fiber.object_id.to_s(16)} handle_id=#{handle_id}, fd=#{fd}" }
  task.run
  dbg { "#{self.class}#register_handle ends handle_id=#{handle_id}, fd=#{fd}" }
end

#to_sObject



105
106
107
# File 'lib/libvirt_async/handle.rb', line 105

def to_s
  "#<#{self.class}:0x#{object_id.to_s(16)} handle_id=#{handle_id} fd=#{fd} events=#{events} monitor=#{monitor}>"
end

#unregisterObject



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

def unregister
  dbg { "#{self.class}#unregister handle_id=#{handle_id}, fd=#{fd}" }

  if @monitor.nil?
    dbg { "#{self.class}#unregister already unregistered handle_id=#{handle_id}, fd=#{fd}" }
    return
  end

  @monitor.close
  @monitor = nil
end