Sha256: 95cb678aa07357b132149edcc6783578fdbd526d891dd750b4a508cfbb0bf0c9

Contents?: true

Size: 1.36 KB

Versions: 1

Compression:

Stored size: 1.36 KB

Contents

# encoding: utf-8

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 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,(_,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.rc1 lib/adhearsion/calls.rb