Sha256: 53c9e013853dfb41a9b68c7029700e8ea58b7e8429cc787cbac0e8730d48048e
Contents?: true
Size: 1.4 KB
Versions: 1
Compression:
Stored size: 1.4 KB
Contents
require 'thread' module Adhearsion ## # This manages the list of calls the Adhearsion service receives class Calls attr_reader :semaphore, :calls def initialize @semaphore = Monitor.new @calls = {} end def from_offer(offer) Call.new(offer).tap do |call| self << call end end def <<(call) atomically do calls[call.id] = call end self end def any? atomically { !calls.empty? } end def size atomically { calls.size } end def remove_inactive_call(call) atomically { calls.delete call.id } end # Searches all active calls by their id def find(id) atomically { calls[id] } end alias :[] :find def clear! atomically { calls.clear } end def with_tag(tag) atomically do calls.inject([]) do |calls_with_tag,(key,call)| call.tagged_with?(tag) ? calls_with_tag << call : calls_with_tag end end end def each(&block) atomically { calls.values.each &block } end def each_pair calls.each_pair { |id, call| yield id, call } end def to_a calls.values end def to_h calls end def method_missing(m, *args) atomically { calls.send m.to_sym, *args } end private def atomically(&block) semaphore.synchronize(&block) end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
adhearsion-2.0.0.alpha3 | lib/adhearsion/calls.rb |