class DBus::MessageQueue

Encapsulates a socket so that we can {#push} and {#pop} {Message}s.

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
19 def initialize(address)
20   @address = address
21   @buffer = ""
22   @is_tcp = false
23   connect
24 end

Public Instance Methods

<<(message)
Alias for: push
pop(non_block = false) click to toggle source

@param non_block [Boolean] if true, return nil instead of waiting @return [Message,nil] one message or nil if unavailable @raise EOFError @todo failure modes

   # File lib/dbus/message_queue.rb
30 def pop(non_block = false)
31   buffer_from_socket_nonblock
32   message = message_from_buffer_nonblock
33   unless non_block
34     # we can block
35     while message.nil?
36       r, _d, _d = IO.select([@socket])
37       if r && r[0] == @socket
38         buffer_from_socket_nonblock
39         message = message_from_buffer_nonblock
40       end
41     end
42   end
43   message
44 end
push(message) click to toggle source
   # File lib/dbus/message_queue.rb
46 def push(message)
47   @socket.write(message.marshall)
48 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
54 def connect
55   addresses = @address.split ";"
56   # connect to first one that succeeds
57   worked = addresses.find do |a|
58     transport, keyvaluestring = a.split ":"
59     kv_list = keyvaluestring.split ","
60     kv_hash = {}
61     kv_list.each do |kv|
62       key, escaped_value = kv.split "="
63       value = escaped_value.gsub(/%(..)/) { |_m| [Regexp.last_match(1)].pack "H2" }
64       kv_hash[key] = value
65     end
66     case transport
67     when "unix"
68       connect_to_unix kv_hash
69     when "tcp"
70       connect_to_tcp kv_hash
71     when "launchd"
72       connect_to_launchd kv_hash
73     else
74       # ignore, report?
75     end
76   end
77   worked
78   # returns the address that worked or nil.
79   # how to report failure?
80 end
connect_to_launchd(params) click to toggle source
    # File lib/dbus/message_queue.rb
120 def connect_to_launchd(params)
121   socket_var = params["env"]
122   socket = `launchctl getenv #{socket_var}`.chomp
123   connect_to_unix "path" => socket
124 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
 83 def connect_to_tcp(params)
 84   # check if the path is sufficient
 85   if params.key?("host") && params.key?("port")
 86     begin
 87       # initialize the tcp socket
 88       @socket = TCPSocket.new(params["host"], params["port"].to_i)
 89       @socket.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC)
 90       init_connection
 91       @is_tcp = true
 92     rescue Exception => e
 93       puts "Oops:", e
 94       puts "Error: Could not establish connection to: #{@path}, will now exit."
 95       exit(1) # a little harsh
 96     end
 97   else
 98     # Danger, Will Robinson: the specified "path" is not usable
 99     puts "Error: supplied path: #{@path}, unusable! sorry."
100   end
101 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
104 def connect_to_unix(params)
105   @socket = Socket.new(Socket::Constants::PF_UNIX, Socket::Constants::SOCK_STREAM, 0)
106   @socket.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC)
107   if !params["abstract"].nil?
108     sockaddr = if HOST_END == LIL_END
109                  "\1\0\0#{params["abstract"]}"
110                else
111                  "\0\1\0#{params["abstract"]}"
112                end
113   elsif !params["path"].nil?
114     sockaddr = Socket.pack_sockaddr_un(params["path"])
115   end
116   @socket.connect(sockaddr)
117   init_connection
118 end
init_connection() click to toggle source

Initialize the connection to the bus.

    # File lib/dbus/message_queue.rb
127 def init_connection
128   client = Client.new(@socket)
129   client.authenticate
130 end