class DBus::MessageQueue
Attributes
socket[R]
The socket that is used to connect with the bus.
Public Class Methods
new(address)
click to toggle source
# File lib/dbus/message_queue.rb 18 def initialize(address) 19 @address = address 20 @buffer = "" 21 @is_tcp = false 22 connect 23 end
Public Instance Methods
pop(non_block = false)
click to toggle source
TODO: failure modes
If non_block is true, return nil instead of waiting EOFError may be raised
# File lib/dbus/message_queue.rb 29 def pop(non_block = false) 30 buffer_from_socket_nonblock 31 message = message_from_buffer_nonblock 32 unless non_block 33 # we can block 34 while message.nil? 35 r, _d, _d = IO.select([@socket]) 36 if r && r[0] == @socket 37 buffer_from_socket_nonblock 38 message = message_from_buffer_nonblock 39 end 40 end 41 end 42 message 43 end
push(message)
click to toggle source
# File lib/dbus/message_queue.rb 45 def push(message) 46 @socket.write(message.marshall) 47 end
Also aliased as: <<
Private Instance Methods
connect()
click to toggle source
Connect to the bus and initialize the connection.
# File lib/dbus/message_queue.rb 53 def connect 54 addresses = @address.split ";" 55 # connect to first one that succeeds 56 worked = addresses.find do |a| 57 transport, keyvaluestring = a.split ":" 58 kv_list = keyvaluestring.split "," 59 kv_hash = {} 60 kv_list.each do |kv| 61 key, escaped_value = kv.split "=" 62 value = escaped_value.gsub(/%(..)/) { |_m| [Regexp.last_match(1)].pack "H2" } 63 kv_hash[key] = value 64 end 65 case transport 66 when "unix" 67 connect_to_unix kv_hash 68 when "tcp" 69 connect_to_tcp kv_hash 70 when "launchd" 71 connect_to_launchd kv_hash 72 else 73 # ignore, report? 74 end 75 end 76 worked 77 # returns the address that worked or nil. 78 # how to report failure? 79 end
connect_to_launchd(params)
click to toggle source
# File lib/dbus/message_queue.rb 119 def connect_to_launchd(params) 120 socket_var = params["env"] 121 socket = `launchctl getenv #{socket_var}`.chomp 122 connect_to_unix "path" => socket 123 end
connect_to_tcp(params)
click to toggle source
Connect to a bus over tcp and initialize the connection.
# File lib/dbus/message_queue.rb 82 def connect_to_tcp(params) 83 # check if the path is sufficient 84 if params.key?("host") && params.key?("port") 85 begin 86 # initialize the tcp socket 87 @socket = TCPSocket.new(params["host"], params["port"].to_i) 88 @socket.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC) 89 init_connection 90 @is_tcp = true 91 rescue Exception => e 92 puts "Oops:", e 93 puts "Error: Could not establish connection to: #{@path}, will now exit." 94 exit(1) # a little harsh 95 end 96 else 97 # Danger, Will Robinson: the specified "path" is not usable 98 puts "Error: supplied path: #{@path}, unusable! sorry." 99 end 100 end
connect_to_unix(params)
click to toggle source
Connect to an abstract unix bus and initialize the connection.
# File lib/dbus/message_queue.rb 103 def connect_to_unix(params) 104 @socket = Socket.new(Socket::Constants::PF_UNIX, Socket::Constants::SOCK_STREAM, 0) 105 @socket.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC) 106 if !params["abstract"].nil? 107 sockaddr = if HOST_END == LIL_END 108 "\1\0\0#{params["abstract"]}" 109 else 110 "\0\1\0#{params["abstract"]}" 111 end 112 elsif !params["path"].nil? 113 sockaddr = Socket.pack_sockaddr_un(params["path"]) 114 end 115 @socket.connect(sockaddr) 116 init_connection 117 end
init_connection()
click to toggle source
Initialize the connection to the bus.
# File lib/dbus/message_queue.rb 126 def init_connection 127 @client = Client.new(@socket) 128 @client.authenticate 129 end