Class: DBus::Main

Inherits:
Object
  • Object
show all
Defined in:
lib/dbus/bus.rb

Overview

Main event loop class.

Class that takes care of handling message and signal events asynchronously. Note: This is a native implement and therefore does not integrate with a graphical widget set main loop.

Instance Method Summary collapse

Constructor Details

#initializeMain

Create a new main event loop.



824
825
826
827
# File 'lib/dbus/bus.rb', line 824

def initialize
  @buses = Hash.new
  @quitting = false
end

Instance Method Details

#<<(bus) ⇒ Object

Add a bus to the list of buses to watch for events.



830
831
832
# File 'lib/dbus/bus.rb', line 830

def <<(bus)
  @buses[bus.socket] = bus
end

#quitObject

Quit a running main loop, to be used eg. from a signal handler



835
836
837
# File 'lib/dbus/bus.rb', line 835

def quit
  @quitting = true
end

#runObject

Run the main loop. This is a blocking call!



840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
# File 'lib/dbus/bus.rb', line 840

def run
  # before blocking, empty the buffers
  # https://bugzilla.novell.com/show_bug.cgi?id=537401
  @buses.each_value do |b|
    while m = b.pop_message
      b.process(m)
    end
  end
  while not @quitting and not @buses.empty?
    ready = IO.select(@buses.keys, [], [], 5) # timeout 5 seconds
    next unless ready # timeout exceeds so continue unless quitting
    ready.first.each do |socket|
      b = @buses[socket]
      begin
        b.update_buffer
      rescue EOFError, SystemCallError
        @buses.delete socket # this bus died
        next
      end
      while m = b.pop_message
        b.process(m)
      end
    end
  end
end