class Twib::TwibConnection
A connection to twibd.
Attributes
cb_queue[R]
@api private
mutex[R]
@api private
Public Class Methods
connect_unix()
click to toggle source
Connects to twibd using the standard UNIX socket address. @return [TwibConnection]
# File lib/twib.rb, line 99 def self.connect_unix return self.new(UNIXSocket.new(ENV["TWIB_UNIX_FRONTEND_PATH"] || "/var/run/twibd.sock")) end
new(socket)
click to toggle source
Creates a TwibConnection
using the specified socket @param [BasicSocket] socket
# File lib/twib.rb, line 105 def initialize(socket) @socket = socket @itmi = Interfaces::ITwibMetaInterface.new(self, 0, 0) @alive = true @active_requests = {} @mutex = Mutex.new @thread = Thread.new do loop do header = @socket.recv(32) header = header.unpack("L<L<L<L<Q<L<") payload = String.new object_ids = [] if header[4] > 0 then payload = @socket.recv(header[4]) # payload size end if header[5] > 0 then object_ids = @socket.recv(header[5] * 4).unpack("L<*") # object IDs end rs = Response.new(header[0], header[1], header[2], header[3], payload, object_ids) @mutex.synchronize do rq = @active_requests.delete(rs.tag) if !rq then puts "WARNING: got response for bad tag" end rq.respond(rs) end end end @cb_queue = Queue.new @cb_thread = Thread.new do # to avoid deadlocks on I/O thread, we execute callbacks here while @alive do cb, response = @cb_queue.pop if cb then cb.call(response) end end end end
Public Instance Methods
close()
click to toggle source
Closes the socket and stops internal threads
# File lib/twib.rb, line 153 def close @alive = false @socket.close @thread.join @cb_queue.close @cb_thread.join end
list_devices()
click to toggle source
Returns a list of devices connected to twibd.
tc.list_devices # => [{"device_id"=>507914862, "identification"=>{...}}]
Use {#open_device} to connect to one.
@return [Array<Hash>]
# File lib/twib.rb, line 186 def list_devices @itmi.list_devices end
open_device(device_id)
click to toggle source
Opens a device specified by one of the device IDs returned from {#list_devices}. @return [Interfaces::ITwibDeviceInterface]
# File lib/twib.rb, line 192 def open_device(device_id) return Interfaces::ITwibDeviceInterface.new(self, device_id, 0) end
send(device_id, object_id, command_id, payload, &block)
click to toggle source
@api private @return [ActiveRequest]
# File lib/twib.rb, line 163 def send(device_id, object_id, command_id, payload, &block) tag = rand(0xffffffff) rq = ActiveRequest.new(self, device_id, object_id, command_id, tag, payload, &block) @active_requests[tag] = rq message = [device_id, object_id, command_id, tag, payload.size, 0, 0].pack("L<L<L<L<Q<L<L<") + payload sz = @socket.send(message, 0) if sz < message.size then raise "couldn't send entire message" end return rq end