app/models/extface/driver.rb in extface-0.1.4 vs app/models/extface/driver.rb in extface-0.1.5

- old
+ new

@@ -31,49 +31,73 @@ true end end end - def handle(buffer) # handle push messages from device outside active session - return false + # called on every push message received, buffer contains all not processed data + def handle(buffer) + $stdout.puts "Extface:#{device.uuid} PUSH #{buffer}" + return buffer.length # return number of bytes processed end def push(buffer) - - if @job - Timeout.timeout(Extface.device_timeout) do - Extface.redis_block do |r| - r.subscribe(@job.id) do |on| #blocking until delivered - on.subscribe do |channel, subscriptions| - @job.rpush buffer - end - on.message do |event, data| - r.unsubscribe - @job.connected! - end + if @job + Timeout.timeout(Extface.device_timeout) do + Extface.redis_block do |r| + r.subscribe(@job.id) do |on| #blocking until delivered + on.subscribe do |channel, subscriptions| + @job.rpush buffer end + on.message do |event, data| + r.unsubscribe + @job.connected! + end end end - else - raise "No job given" end - + else + raise "No job given" + end end def pull(timeout = nil) + element = nil Extface.redis_block do |r| - list, element = r.blpop(device.uuid, :timeout => timeout) + list, element = r.blpop(buffer_key, :timeout => timeout) end element end + def flush + Extface.redis_block do |r| + r.del device.uuid + r.del buffer_key + end + end + + def rpush(buffer) + Extface.redis_block do |r| + r.rpush buffer_key, buffer + end + end + def notify(message) raise "No job given" unless @job @job.notify(message) end def set_job(job) @job = job + return check_status end + + def check_status + errors.add :base, :not_implemented + false + end + private + def buffer_key + "#{device.uuid}:#{self.id}" + end end end