Class: DBus::MessageQueue

Inherits:
Object
  • Object
show all
Defined in:
lib/dbus/message_queue.rb

Overview

Encapsulates a socket so that we can #push and #pop Messages.

Constant Summary collapse

MSG_BUF_SIZE =

The buffer size for messages.

4096

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(address) ⇒ MessageQueue

Returns a new instance of MessageQueue.



21
22
23
24
25
26
# File 'lib/dbus/message_queue.rb', line 21

def initialize(address)
  @address = address
  @buffer = ""
  @is_tcp = false
  connect
end

Instance Attribute Details

#socketObject (readonly)

The socket that is used to connect with the bus.



19
20
21
# File 'lib/dbus/message_queue.rb', line 19

def socket
  @socket
end

Instance Method Details

#buffer_from_socket_nonblockvoid

This method returns an undefined value.

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

Raises:

  • EOFError



160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/dbus/message_queue.rb', line 160

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_nonblockMessage?

Get and remove one message from the buffer.

Returns:

  • (Message, nil)

    the message or nil if unavailable



140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/dbus/message_queue.rb', line 140

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 remain nil
  end
  ret
end

#pop(blocking: true) ⇒ Message?

TODO:

failure modes

Returns one message or nil if unavailable.

Parameters:

  • blocking (Boolean) (defaults to: true)

    true: wait to return a DBus::Message; false: may return ‘nil`

Returns:

  • (Message, nil)

    one message or nil if unavailable

Raises:

  • EOFError



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/dbus/message_queue.rb', line 34

def pop(blocking: true)
  buffer_from_socket_nonblock
  message = message_from_buffer_nonblock
  if blocking
    # we can block
    while message.nil?
      r, _d, _d = IO.select([@socket])
      if r && r[0] == @socket
        buffer_from_socket_nonblock
        message = message_from_buffer_nonblock
      end
    end
  end
  message
end

#push(message) ⇒ Object Also known as: <<



50
51
52
# File 'lib/dbus/message_queue.rb', line 50

def push(message)
  @socket.write(message.marshall)
end