Class: Switest::Client
- Inherits:
-
Object
- Object
- Switest::Client
- Defined in:
- lib/switest/client.rb
Instance Attribute Summary collapse
-
#calls ⇒ Object
readonly
Returns the value of attribute calls.
Instance Method Summary collapse
- #active_calls ⇒ Object
- #connected? ⇒ Boolean
- #dial(to:, from: nil, timeout: nil, headers: {}) ⇒ Object
- #hangup_all(cause: "NORMAL_CLEARING", timeout: 5) ⇒ Object
-
#initialize ⇒ Client
constructor
A new instance of Client.
- #on_offer(&block) ⇒ Object
- #start ⇒ Object
- #stop ⇒ Object
Constructor Details
#initialize ⇒ Client
Returns a new instance of Client.
12 13 14 15 16 17 |
# File 'lib/switest/client.rb', line 12 def initialize @calls = {} @offer_handlers = [] @session = nil @client_task = nil end |
Instance Attribute Details
#calls ⇒ Object (readonly)
Returns the value of attribute calls.
10 11 12 |
# File 'lib/switest/client.rb', line 10 def calls @calls end |
Instance Method Details
#active_calls ⇒ Object
109 110 111 |
# File 'lib/switest/client.rb', line 109 def active_calls @calls.select { |_k, v| v.alive? } end |
#connected? ⇒ Boolean
54 55 56 |
# File 'lib/switest/client.rb', line 54 def connected? !@session.nil? end |
#dial(to:, from: nil, timeout: nil, headers: {}) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/switest/client.rb', line 73 def dial(to:, from: nil, timeout: nil, headers: {}) uuid = SecureRandom.uuid vars = { origination_uuid: uuid, return_ring_ready: true } vars.merge!(FromParser.parse(from)) if from vars[:originate_timeout] = timeout if timeout var_string = Escaper.build_var_string(vars, headers) call = Call.new( id: uuid, direction: :outbound, to: to, from: from, headers: headers, session: @session ) @calls[uuid] = call begin @session.bgapi("originate #{var_string}#{to} &park()") rescue @calls.delete(uuid) raise end call end |
#hangup_all(cause: "NORMAL_CLEARING", timeout: 5) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/switest/client.rb', line 58 def hangup_all(cause: "NORMAL_CLEARING", timeout: 5) active = @calls.values.reject(&:ended?) active.each do |call| call.hangup(cause, wait: false) rescue nil end deadline = Time.now + timeout active.each do |call| remaining = deadline - Time.now break if remaining <= 0 call.wait_for_end(timeout: remaining) unless call.ended? end end |
#on_offer(&block) ⇒ Object
105 106 107 |
# File 'lib/switest/client.rb', line 105 def on_offer(&block) @offer_handlers << block end |
#start ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/switest/client.rb', line 19 def start config = Switest.configuration # Configure Session class with client state Session.call_registry = @calls Session.offer_handler = method(:handle_inbound_offer) Session.connection_promise = Async::Promise.new endpoint = IO::Endpoint.tcp(config.host, config.port) client = Librevox::Client.new(Session, endpoint, auth: config.password) @client_task = Async { client.run } # Wait for the session to be established @session = Async::Task.current.with_timeout(config.default_timeout) do Session.connection_promise.wait end self rescue Async::TimeoutError @client_task&.stop @client_task = nil raise Switest::ConnectionError, "Timed out connecting to FreeSWITCH" end |
#stop ⇒ Object
43 44 45 46 47 48 49 50 51 52 |
# File 'lib/switest/client.rb', line 43 def stop hangup_all @client_task&.stop @client_task = nil @session = nil @calls.clear Session.call_registry = nil Session.offer_handler = nil Session.connection_promise = nil end |