Class: Adhearsion::Call

Inherits:
Object show all
Defined in:
lib/adhearsion/voip/call.rb

Overview

Encapsulates call-related data and behavior. For example, variables passed in on call initiation are accessible here as attributes

Defined Under Namespace

Modules: Variables Classes: CallMessageQueue

Constant Summary collapse

ASTERISK_FRAME_STATES =

This is basically a translation of ast_channel_reason2str() from main/channel.c and ast_control_frame_type in include/asterisk/frame.h in the Asterisk source code. When Asterisk jumps to the ‘failed’ extension, it sets a REASON channel variable to a number. The indexes of these symbols represent the possible numbers REASON could be.

[
  :failure,     # "Call Failure (not BUSY, and not NO_ANSWER, maybe Circuit busy or down?)"
  :hangup,      # Other end has hungup
  :ring,        # Local ring
  :ringing,     # Remote end is ringing
  :answer,      # Remote end has answered
  :busy,        # Remote end is busy
  :takeoffhook, # Make it go off hook
  :offhook,     # Line is off hook
  :congestion,  # Congestion (circuits busy)
  :flash,       # Flash hook
  :wink,        # Wink
  :option,      # Set a low-level option
  :radio_key,   # Key Radio
  :radio_unkey, # Un-Key Radio
  :progress,    # Indicate PROGRESS
  :proceeding,  # Indicate CALL PROCEEDING
  :hold,        # Indicate call is placed on hold
  :unhold,      # Indicate call is left from hold
  :vidupdate    # Indicate video frame update
]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io, variables) ⇒ Call

Returns a new instance of Call.



190
191
192
193
194
195
196
197
# File 'lib/adhearsion/voip/call.rb', line 190

def initialize(io, variables)
  @io, @variables = io, variables.symbolize_keys
  check_if_valid_call
  define_variable_accessors
  set_originating_voip_platform!
  @tag_mutex = Mutex.new
  @tags = []
end

Instance Attribute Details

#inboxObject

Returns the value of attribute inbox.



189
190
191
# File 'lib/adhearsion/voip/call.rb', line 189

def inbox
  @inbox
end

#ioObject

Returns the value of attribute io.



189
190
191
# File 'lib/adhearsion/voip/call.rb', line 189

def io
  @io
end

#originating_voip_platformObject

Returns the value of attribute originating_voip_platform.



189
190
191
# File 'lib/adhearsion/voip/call.rb', line 189

def originating_voip_platform
  @originating_voip_platform
end

#typeObject

Returns the value of attribute type.



189
190
191
# File 'lib/adhearsion/voip/call.rb', line 189

def type
  @type
end

#variablesObject

Returns the value of attribute variables.



189
190
191
# File 'lib/adhearsion/voip/call.rb', line 189

def variables
  @variables
end

Class Method Details

.receive_from(io, &block) ⇒ Object

The primary public interface for creating a Call instance. Given an IO (probably a socket accepted from an Asterisk service), creates a Call instance which encapsulates everything we know about that call.



176
177
178
179
180
# File 'lib/adhearsion/voip/call.rb', line 176

def receive_from(io, &block)
  new(io, variable_parser_for(io).variables).tap do |call|
    block.call(call) if block
  end
end

Instance Method Details

#can_use_messaging?Boolean

Returns:

  • (Boolean)


234
235
236
# File 'lib/adhearsion/voip/call.rb', line 234

def can_use_messaging?
  return inbox.open == true
end

#closed?Boolean

Returns:

  • (Boolean)


247
248
249
# File 'lib/adhearsion/voip/call.rb', line 247

def closed?
  io.closed?
end

#define_variable_accessors(recipient = self) ⇒ Object



278
279
280
281
282
# File 'lib/adhearsion/voip/call.rb', line 278

def define_variable_accessors(recipient=self)
  variables.each do |key, value|
    define_singleton_accessor_with_pair(key, value, recipient)
  end
end

#deliver_message(message) ⇒ Object Also known as: <<



229
230
231
# File 'lib/adhearsion/voip/call.rb', line 229

def deliver_message(message)
  inbox << message
end

#extract_failed_reason_from(environment) ⇒ Object



284
285
286
287
288
289
290
# File 'lib/adhearsion/voip/call.rb', line 284

def extract_failed_reason_from(environment)
  if originating_voip_platform == :asterisk
    failed_reason = environment.variable 'REASON'
    failed_reason &&= ASTERISK_FRAME_STATES[failed_reason.to_i]
    define_singleton_accessor_with_pair(:failed_reason, failed_reason, environment)
  end
end

#failed_call?Boolean

Asterisk sometimes uses the “failed” extension to indicate a failed dial attempt. Since it may be important to handle these, this flag helps the dialplan Manager figure that out.

Returns:

  • (Boolean)


254
255
256
# File 'lib/adhearsion/voip/call.rb', line 254

def failed_call?
  @failed_call
end

#hangup!Object



242
243
244
245
# File 'lib/adhearsion/voip/call.rb', line 242

def hangup!
  io.close
  Adhearsion.remove_inactive_call self
end

#hungup_call?Boolean

Returns:

  • (Boolean)


258
259
260
# File 'lib/adhearsion/voip/call.rb', line 258

def hungup_call?
  @hungup_call
end

#remove_tag(symbol) ⇒ Object



217
218
219
220
221
# File 'lib/adhearsion/voip/call.rb', line 217

def remove_tag(symbol)
  @tag_mutex.synchronize do
    @tags.reject! { |tag| tag == symbol }
  end
end

#tag(label) ⇒ Object

This may still be a symbol, but no longer requires the tag to be a symbol although beware that using a symbol would create a memory leak if used improperly

Parameters:

  • label (String, Symbol)

    String or Symbol with which to tag this call



208
209
210
211
212
213
214
215
# File 'lib/adhearsion/voip/call.rb', line 208

def tag(label)
  if ![String, Symbol].include?(label.class)
    raise ArgumentError, "Tag must be a String or Symbol"
  end
  @tag_mutex.synchronize do
    @tags << label
  end
end

#tagged_with?(symbol) ⇒ Boolean

Returns:

  • (Boolean)


223
224
225
226
227
# File 'lib/adhearsion/voip/call.rb', line 223

def tagged_with?(symbol)
  @tag_mutex.synchronize do
    @tags.include? symbol
  end
end

#tagsObject



199
200
201
202
203
# File 'lib/adhearsion/voip/call.rb', line 199

def tags
  @tag_mutex.synchronize do
    return @tags.clone
  end
end

#unique_identifierObject

Adhearsion indexes calls by this identifier so they may later be found and manipulated. For calls from Asterisk, this method uses the following properties for uniqueness, falling back to the next if one is for some reason unavailable:

Asterisk channel ID     ->        unique ID        -> Call#object_id

(e.g. SIP/mytrunk-jb12c88a) -> (e.g. 1215039989.47033) -> (e.g. 2792080)

Note: channel is used over unique ID because channel may be used to bridge two channels together.



269
270
271
272
273
274
275
276
# File 'lib/adhearsion/voip/call.rb', line 269

def unique_identifier
  case originating_voip_platform
    when :asterisk
      variables[:channel] || variables[:uniqueid] || object_id
    else
      raise NotImplementedError
  end
end