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.



751
752
753
754
# File 'lib/dbus/bus.rb', line 751

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

Instance Method Details

#<<(bus) ⇒ Object

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



757
758
759
# File 'lib/dbus/bus.rb', line 757

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

#quitObject

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



762
763
764
# File 'lib/dbus/bus.rb', line 762

def quit
  @quitting = true
end

#runObject

Run the main loop. This is a blocking call!



767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
# File 'lib/dbus/bus.rb', line 767

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