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.



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

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.



32
33
34
# File 'lib/libvirt_async/handle.rb', line 32

def events
  @events
end

#fdObject (readonly)

Returns the value of attribute fd.



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

def fd
  @fd
end

#handle_idObject (readonly)

Returns the value of attribute handle_id.



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

def handle_id
  @handle_id
end

#monitorObject (readonly)

Returns the value of attribute monitor.



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

def monitor
  @monitor
end

#opaqueObject (readonly)

Returns the value of attribute opaque.



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

def opaque
  @opaque
end

Instance Method Details

#inspectObject



111
112
113
# File 'lib/libvirt_async/handle.rb', line 111

def inspect
  to_s
end

#registerObject



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
92
93
# File 'lib/libvirt_async/handle.rb', line 44

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



107
108
109
# File 'lib/libvirt_async/handle.rb', line 107

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

#unregisterObject



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

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