# class DBus::Main

# `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.

### Public Class Methods

new() click to toggle source

Create a new main event loop.

       # File lib/dbus/main.rb
    20 def initialize
    21   @buses = {}
    22   @quitting = false
    23 end

### Public Instance Methods

<<(bus) click to toggle source

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

       # File lib/dbus/main.rb
    26 def <<(bus)
    27   @buses[bus.message_queue.socket] = bus
    28 end

quit() click to toggle source

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

       # File lib/dbus/main.rb
    31 def quit
    32   @quitting = true
    33 end

run() click to toggle source

Run the main loop. This is a blocking call!

       # File lib/dbus/main.rb
    36 def run
    37   # before blocking, empty the buffers
    38   # https://bugzilla.novell.com/show_bug.cgi?id=537401
    39   @buses.each_value do |b|
    40     while (m = b.message_queue.message_from_buffer_nonblock)
    41       b.process(m)
    42     end
    43   end
    44   while !@quitting && !@buses.empty?
    45     ready = IO.select(@buses.keys, [], [], 5) # timeout 5 seconds
    46     next unless ready # timeout exceeds so continue unless quitting
    47
    48     ready.first.each do |socket|
    49       b = @buses[socket]
    50       begin
    51         b.message_queue.buffer_from_socket_nonblock
    52       rescue EOFError, SystemCallError => e
    53         DBus.logger.debug "Got #{e.inspect} from #{socket.inspect}"
    54         @buses.delete socket # this bus died
    55         next
    56       end
    57       while (m = b.message_queue.message_from_buffer_nonblock)
    58         b.process(m)
    59       end
    60     end
    61   end
    62   DBus.logger.debug "Main loop quit" if @quitting
    63   DBus.logger.debug "Main loop quit, no connections left" if @buses.empty?
    64 end