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.



873
874
875
876
877
878
879
# File 'lib/dbus/bus.rb', line 873

def initialize
  @buses = Hash.new
  @buses_thread = Array.new
  @quit_queue = Queue.new
  @quitting = false
  $mainclass = self
end

Instance Method Details

#<<(bus) ⇒ Object

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



896
897
898
# File 'lib/dbus/bus.rb', line 896

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

#quitObject

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



901
902
903
904
905
906
907
908
# File 'lib/dbus/bus.rb', line 901

def quit
  if(ENV["DBUS_THREADED_ACCESS"] || false)
    @quitting = true
    quit_imediately
  else
    @quitting = true
  end
end

#quit_imediatelyObject

the standar quit method didn’t quit imediately and wait for a last message. This methodes allow to quit imediately



883
884
885
886
887
888
889
890
891
892
893
# File 'lib/dbus/bus.rb', line 883

def quit_imediately 
  @buses.each_value do |b|
    #b.read_thread.exit
  end
  @buses_thread.each do |th|
    @buses_thread_id.delete(th.object_id)
    #th.exit
  end
  @quit_queue << "quit"      

end

#runObject

Run the main loop. This is a blocking call!



911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
# File 'lib/dbus/bus.rb', line 911

def run
  # before blocking, empty the buffers
  # https://bugzilla.novell.com/show_bug.cgi?id=537401
  @buses_thread_id = Array.new
  @buses_thread = Array.new
  @thread_as_quit = Queue.new

  if(ENV["DBUS_THREADED_ACCESS"] || false)
    @buses.each_value do |b|
      
      b.rescuemethod = self.method(:quit_imediately)
      th= Thread.new{
        b.main_thread = true
        while m = b.pop_message
          b.process(m)
        end

        while not b.nil? and not @quitting
          m = b.main_message_queue.pop
          b.process(m)
        end

        @thread_as_quit << Thread.current.object_id
      }
      @buses_thread_id.push th.object_id
      @buses_thread.push th
    end
  
    popping_thread =  Thread.new{
      @quit_queue.pop
    }
    popping_thread.join # main thread - sleep for this thread waiting for poping thread
    
   else
    @buses.each_value do |b|

      while m = b.pop_message
        b.process(m)
      end
      
    end
    while not @quitting and not @buses.empty?
      io_ready = false
      while not io_ready and not @quitting and not @buses.empty? do
        ready, dum, dum = IO.select(@buses.keys, nil, nil, 0.1)
        io_ready = !(ready.nil? or ready.first.nil?)
      end

      next if ready.nil?

      ready.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 # if($threaded)
end