Class: DBus::MessageQueue
- Inherits:
-
Object
- Object
- DBus::MessageQueue
- Defined in:
- lib/dbus/message_queue.rb
Overview
Constant Summary collapse
- MSG_BUF_SIZE =
The buffer size for messages.
4096
Instance Attribute Summary collapse
-
#socket ⇒ Object
readonly
The socket that is used to connect with the bus.
Instance Method Summary collapse
-
#buffer_from_socket_nonblock ⇒ void
Fill (append) the buffer from data that might be available on the socket.
-
#initialize(address) ⇒ MessageQueue
constructor
A new instance of MessageQueue.
-
#message_from_buffer_nonblock ⇒ Message?
Get and remove one message from the buffer.
-
#pop(blocking: true) ⇒ Message?
One message or nil if unavailable.
- #push(message) ⇒ Object (also: #<<)
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
#socket ⇒ Object (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_nonblock ⇒ void
This method returns an undefined value.
Fill (append) the buffer from data that might be available on the socket.
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_nonblock ⇒ Message?
Get and remove one message from the buffer.
140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/dbus/message_queue.rb', line 140 def 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.
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 = if blocking # we can block while .nil? r, _d, _d = IO.select([@socket]) if r && r[0] == @socket buffer_from_socket_nonblock = end end end end |
#push(message) ⇒ Object Also known as: <<
50 51 52 |
# File 'lib/dbus/message_queue.rb', line 50 def push() @socket.write(.marshall) end |