Class: Twib::TwibConnection

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

Overview

A connection to twibd.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(socket) ⇒ TwibConnection

Creates a TwibConnection using the specified socket

Parameters:

  • socket (BasicSocket)


105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# 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

Instance Attribute Details

#cb_queueObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



150
151
152
# File 'lib/twib.rb', line 150

def cb_queue
  @cb_queue
end

#mutexObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



148
149
150
# File 'lib/twib.rb', line 148

def mutex
  @mutex
end

Class Method Details

.connect_unixTwibConnection

Connects to twibd using the standard UNIX socket address.

Returns:



99
100
101
# File 'lib/twib.rb', line 99

def self.connect_unix
  return self.new(UNIXSocket.new("/var/run/twibd.sock"))
end

Instance Method Details

#closeObject

Closes the socket and stops internal threads



153
154
155
156
157
158
159
# File 'lib/twib.rb', line 153

def close
  @alive = false
  @socket.close
  @thread.join
  @cb_queue.close
  @cb_thread.join
end

#list_devicesArray<Hash>

Returns a list of devices connected to twibd.

tc.list_devices
# => [{"device_id"=>507914862, "identification"=>{...}}]

Use #open_device to connect to one.

Returns:

  • (Array<Hash>)


186
187
188
# File 'lib/twib.rb', line 186

def list_devices
  @itmi.list_devices
end

#open_device(device_id) ⇒ Interfaces::ITwibDeviceInterface

Opens a device specified by one of the device IDs returned from #list_devices.



192
193
194
# 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) ⇒ ActiveRequest

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:



163
164
165
166
167
168
169
170
171
172
173
174
175
176
# 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