Class: DBus::MessageQueue

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

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.



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

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.



16
17
18
# File 'lib/dbus/message_queue.rb', line 16

def socket
  @socket
end

Instance Method Details

#buffer_from_socket_nonblockObject

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



153
154
155
156
157
158
159
160
161
162
163
164
# 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_nonblockObject

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



135
136
137
138
139
140
141
142
143
144
145
# 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) ⇒ Object

TODO: failure modes

If non_block is true, return nil instead of waiting EOFError may be raised



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# 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 && r[0] == @socket
        buffer_from_socket_nonblock
        message = message_from_buffer_nonblock
      end
    end
  end
  message
end

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



45
46
47
# File 'lib/dbus/message_queue.rb', line 45

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