class DBus::MessageQueue

Constants

MSG_BUF_SIZE

The buffer size for messages.

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, line 18
def initialize(address)
  @address = address
  @buffer = ""
  @is_tcp = false
  connect
end

Public Instance Methods

<<(message)
Alias for: push
buffer_from_socket_nonblock() click to toggle source

Fill (append) the buffer from data that might be available on the socket. EOFError may be raised

# File lib/dbus/message_queue.rb, line 153
def buffer_from_socket_nonblock
  @buffer += @socket.read_nonblock(MSG_BUF_SIZE)
rescue EOFError
  raise                     # the caller expects it
rescue Errno::EAGAIN
  # fine, would block
rescue Exception => e
  puts "Oops:", e
  raise if @is_tcp          # why?
  puts "WARNING: read_nonblock failed, falling back to .recv"
  @buffer += @socket.recv(MSG_BUF_SIZE)
end
message_from_buffer_nonblock() click to toggle source

Get and remove one message from the buffer. Return the message or nil.

# File lib/dbus/message_queue.rb, line 135
def message_from_buffer_nonblock
  return nil if @buffer.empty?
  ret = nil
  begin
    ret, size = Message.new.unmarshall_buffer(@buffer)
    @buffer.slice!(0, size)
  rescue IncompleteBufferException
    # fall through, let ret be null
  end
  ret
end
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, line 29
def pop(non_block = false)
  buffer_from_socket_nonblock
  message = message_from_buffer_nonblock
  unless non_block
    # we can block
    while message.nil?
      r, d, d = IO.select([@socket])
      if r and r[0] == @socket
        buffer_from_socket_nonblock
        message = message_from_buffer_nonblock
      end
    end
  end
  message
end
push(message) click to toggle source
# File lib/dbus/message_queue.rb, line 45
def push(message)
  @socket.write(message.marshall)
end
Also aliased as: <<