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.



704
705
706
707
# File 'lib/dbus/bus.rb', line 704

def initialize
  @buses = {}
  @quitting = false
end

Instance Method Details

#<<(bus) ⇒ Object

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



710
711
712
# File 'lib/dbus/bus.rb', line 710

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

#quitObject

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



715
716
717
# File 'lib/dbus/bus.rb', line 715

def quit
  @quitting = true
end

#runObject

Run the main loop. This is a blocking call!



720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
# File 'lib/dbus/bus.rb', line 720

def run
  # before blocking, empty the buffers
  # https://bugzilla.novell.com/show_bug.cgi?id=537401
  @buses.each_value do |b|
    while (m = b.message_queue.message_from_buffer_nonblock)
      b.process(m)
    end
  end
  while !@quitting && !@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.message_queue.buffer_from_socket_nonblock
      rescue EOFError, SystemCallError
        @buses.delete socket # this bus died
        next
      end
      while (m = b.message_queue.message_from_buffer_nonblock)
        b.process(m)
      end
    end
  end
end