Class: OverSIP::SIP::InviteClientTransaction

Inherits:
ClientTransaction show all
Defined in:
lib/oversip/sip/client_transaction.rb

Instance Attribute Summary

Attributes inherited from ClientTransaction

#connection, #core, #request, #state

Instance Method Summary collapse

Methods inherited from ClientTransaction

get_class

Methods included from Logger

close, fg_system_msg2str, init_logger_mq, load_methods, #log_id, syslog_system_msg2str, syslog_user_msg2str

Constructor Details

#initialize(core, request, transaction_conf, transport, ip = nil, ip_type = nil, port = nil) ⇒ InviteClientTransaction

Returns a new instance of InviteClientTransaction.



78
79
80
81
82
83
84
# File 'lib/oversip/sip/client_transaction.rb', line 78

def initialize core, request, transaction_conf, transport, ip=nil, ip_type=nil, port=nil
  super
  @log_id = "ICT #{@transaction_id}"

  # Can be :calling, :proceeding, :completed, :accepted or :terminated.
  @state = :calling
end

Instance Method Details

#connection_failedObject



255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/oversip/sip/client_transaction.rb', line 255

def connection_failed
  # This avoid the case in which the TCP connection timeout raises after the transaction timeout.
  # Neither we react if the transaction has been canceled and the CANCEL cannot be sent due to
  # TCP disconnection.
  return unless @state == :calling or not @cancel

  @timer_A.cancel  if @timer_A
  @timer_B.cancel
  @timer_C.cancel
  terminate_transaction

  @core.connection_failed
end

#do_cancel(cancel = nil) ⇒ Object

It receives the received CANCEL request as parameter so it can check the existence of Reason header and act according (RFC 3326). This method is also called (without argument) when Timer C expires (INVITE).



310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# File 'lib/oversip/sip/client_transaction.rb', line 310

def do_cancel cancel=nil
  return if @cancel

  @cancel = "CANCEL #{@request.ruri} SIP/2.0\r\n"
  @cancel << "Via: #{@top_via}\r\n"

  @request.hdr_route.each do |route|
    @cancel << "Route: " << route << CRLF
  end  if @request.hdr_route

  # RFC 3326. Copy Reason headers if present in the received CANCEL.
  cancel.header_all("Reason").each do |reason|
    @cancel << "Reason: " << reason << CRLF
  end  if cancel

  @cancel << "From: " << @request.hdr_from << CRLF
  @cancel << "To: " << @request.hdr_to << CRLF
  @cancel << "Call-ID: " << @request.call_id << CRLF
  @cancel << "CSeq: " << @request.cseq.to_s << " CANCEL\r\n"
  @cancel << "Content-Length: 0\r\n"
  @cancel << HDR_USER_AGENT << CRLF
  @cancel << CRLF

  # Just send the ACK inmediately if the branch has replied a 1XX response.
  send_cancel  if @state == :proceeding
end

#receive_response(response) ⇒ Object



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/oversip/sip/client_transaction.rb', line 182

def receive_response response
  # Set the request attribute to the response so we can access the related outgoing request.
  response.request = @request

  # Set server transaction variables to the response.
  response.tvars = @request.tvars

  # Provisional response
  if response.status_code < 200
    case @state
    when :calling
      @state = :proceeding
      @timer_A.cancel  if @timer_A
      @timer_B.cancel
      @core.receive_response(response) unless response.status_code == 100
      # RFC 3261 - 9.1 states that a CANCEL must be sent after receiving a 1XX response.
      send_cancel if @cancel
      return true
    when :proceeding
      @core.receive_response(response) unless response.status_code == 100
      return true
    else
      log_system_notice "received a provisional response #{response.status_code} while in #{@state} state"
      return false
    end

  # [3456]XX final response.
  elsif response.status_code >= 300
    case @state
    when :calling, :proceeding
      @state = :completed
      @timer_A.cancel  if @timer_A
      @timer_B.cancel
      @timer_C.cancel
      if @transport == :udp
        start_timer_D
      else
        terminate_transaction
      end
      send_ack(response)
      @core.receive_response(response)
      return true
    when :completed
      send_ack(response)
      return false
    when :accepted
      log_system_notice "received a [3456]XX response while in accepted state, ignoring it"
      return false
    end

  # 2XX final response.
  else
    case @state
    when :calling, :proceeding
      @state = :accepted
      @timer_A.cancel  if @timer_A
      @timer_B.cancel
      @timer_C.cancel
      start_timer_M
      @core.receive_response(response)
      return true
    when :accepted
      @core.receive_response(response)
      return true
    when :completed
      ### NOTE: It could be accepted and bypassed to the UAC, but makes no sense.
      log_system_notice "received 2XX response while in completed state, ignoring it"
      return false
    end

  end
end

#receive_response_to_cancel(response) ⇒ Object



369
370
371
372
373
374
375
376
377
378
379
# File 'lib/oversip/sip/client_transaction.rb', line 369

def receive_response_to_cancel(response)
  unless @state == :terminated
    log_system_debug "our CANCEL got a #{response.status_code} response, transaction terminated"  if $oversip_debug

    @timer_E_cancel.cancel  if @timer_E_cancel
    @timer_F_cancel.cancel
    # We MUST ensure that we end the client transaction, so after sending a CANCEL and get a response
    # for it, ensure the transaction is terminated after a while.
    ::EM.add_timer(4) { terminate_transaction }
  end
end

#retransmit_cancelObject



365
366
367
# File 'lib/oversip/sip/client_transaction.rb', line 365

def retransmit_cancel
  @connection.send_sip_msg @cancel, @ip, @port
end

#retransmit_requestObject



178
179
180
# File 'lib/oversip/sip/client_transaction.rb', line 178

def retransmit_request
  @connection.send_sip_msg @outgoing_request_str, @ip, @port
end

#send_ack(response) ⇒ Object



280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'lib/oversip/sip/client_transaction.rb', line 280

def send_ack response
  unless @ack
    @ack = "ACK #{@request.ruri} SIP/2.0\r\n"
    @ack << "Via: #{@top_via}\r\n"

    @request.hdr_route.each do |route|
      @ack << "Route: " << route << CRLF
    end  if @request.hdr_route

    @ack << "From: " << @request.hdr_from << CRLF
    @ack << "To: " << @request.hdr_to
    unless @request.to_tag
      @ack << ";tag=#{response.to_tag}"  if response.to_tag
    end
    @ack << CRLF

    @ack << "Call-ID: " << @request.call_id << CRLF
    @ack << "CSeq: " << @request.cseq.to_s << " ACK\r\n"
    @ack << "Content-Length: 0\r\n"
    @ack << HDR_USER_AGENT << CRLF
    @ack << CRLF
  end

  log_system_debug "sending ACK for [3456]XX response"  if $oversip_debug
  @connection.send_sip_msg @ack, @ip, @port
end

#send_cancelObject



337
338
339
340
341
342
343
344
# File 'lib/oversip/sip/client_transaction.rb', line 337

def send_cancel
  log_system_debug "sending CANCEL"  if $oversip_debug

  @connection.send_sip_msg @cancel, @ip, @port

  start_timer_E_cancel  if @transport == :udp
  start_timer_F_cancel
end

#send_requestObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/oversip/sip/client_transaction.rb', line 86

def send_request
  @client_transactions = @server_klass.invite_client_transactions
  # Store the new client transaction.
  @client_transactions[@transaction_id] = self

  @top_via = "#{@server_klass.via_core};branch=z9hG4bK#{@transaction_id};rport"
  @request.insert_header "Via", @top_via

  case @request.in_rr
  # Add a second Record-Route just in case there is transport change.
  when :rr
    unless @request.connection.is_a?(@server_klass)
      @out_rr = :rr
      @request.insert_header "Record-Route", @server_klass.record_route
    end
  # When there is outgoing Outbound always add a second Record-Route header.
  when :outgoing_outbound_rr
    @out_rr = :rr
    @request.insert_header "Record-Route", @server_klass.record_route
  # When there is incoming Outbound always add a second Record-Route header containing the flow token.
  when :incoming_outbound_rr
    @out_rr = :rr
    @request.insert_header "Record-Route", "<sip:" << @request.route_outbound_flow_token << @server_klass.outbound_record_route_fragment
  # When there is both incoming and outgoing Outbound always add a second Record-Route header containing the flow token.
  when :both_outbound_rr
    @out_rr = :rr
    @request.insert_header "Record-Route", "<sip:" << @request.route_outbound_flow_token << @server_klass.outbound_record_route_fragment
  end  if @request.in_rr

  @outgoing_request_str = @request.to_s

  @request.delete_header_top "Via"
  if @out_rr == :rr
    @request.delete_header_top "Record-Route"
  end

  @connection.send_sip_msg @outgoing_request_str, @ip, @port

  start_timer_A  if @transport == :udp
  start_timer_B
  start_timer_C
end

#start_timer_AObject



129
130
131
132
133
134
135
136
# File 'lib/oversip/sip/client_transaction.rb', line 129

def start_timer_A
  @timer_A_interval = TIMER_A
  @timer_A = ::EM::PeriodicTimer.new(@timer_A_interval) do
    log_system_debug "timer A expires, retransmitting request"  if $oversip_debug
    retransmit_request
    @timer_A_interval = @timer_A.interval = 2*@timer_A_interval
  end
end

#start_timer_BObject



138
139
140
141
142
143
144
145
146
# File 'lib/oversip/sip/client_transaction.rb', line 138

def start_timer_B
  @timer_B = ::EM::Timer.new(@transaction_conf[:timer_B] || TIMER_B) do
    log_system_debug "timer B expires, transaction timeout"  if $oversip_debug
    @timer_A.cancel  if @timer_A
    @timer_C.cancel
    terminate_transaction
    @core.client_timeout
  end
end

#start_timer_CObject



148
149
150
151
152
153
154
155
156
# File 'lib/oversip/sip/client_transaction.rb', line 148

def start_timer_C
  @timer_C = ::EM::Timer.new(@transaction_conf[:timer_C] || TIMER_C) do
    log_system_debug "timer C expires, transaction timeout"  if $oversip_debug
    @timer_A.cancel  if @timer_A
    @timer_B.cancel
    do_cancel
    @core.invite_timeout
  end
end

#start_timer_DObject



158
159
160
161
162
163
# File 'lib/oversip/sip/client_transaction.rb', line 158

def start_timer_D
  ::EM.add_timer(TIMER_D_UDP) do
    log_system_debug "timer D expires, transaction terminated"  if $oversip_debug
    terminate_transaction
  end
end

#start_timer_E_cancelObject



346
347
348
349
350
351
352
353
# File 'lib/oversip/sip/client_transaction.rb', line 346

def start_timer_E_cancel
  @timer_E_cancel_interval = TIMER_E
  @timer_E_cancel = ::EM::PeriodicTimer.new(@timer_E_cancel_interval) do
    log_system_debug "timer E expires, retransmitting CANCEL"  if $oversip_debug
    retransmit_cancel
    @timer_E_cancel_interval = @timer_E_cancel.interval = [2*@timer_E_cancel_interval, T2].min
  end
end

#start_timer_F_cancelObject



355
356
357
358
359
360
361
362
363
# File 'lib/oversip/sip/client_transaction.rb', line 355

def start_timer_F_cancel
  @timer_F_cancel = ::EM::Timer.new(@transaction_conf[:timer_F] || TIMER_F) do
    unless @state == :terminated
      log_system_debug "timer F expires, CANCEL timeout, transaction terminated"  if $oversip_debug
      @timer_E_cancel.cancel  if @timer_E_cancel
      terminate_transaction
    end
  end
end

#start_timer_MObject



165
166
167
168
169
170
# File 'lib/oversip/sip/client_transaction.rb', line 165

def start_timer_M
  ::EM.add_timer(TIMER_M) do
    log_system_debug "timer M expires, transaction terminated"  if $oversip_debug
    terminate_transaction
  end
end

#terminate_transactionObject

Terminate current transaction and delete from the list of transactions.



173
174
175
176
# File 'lib/oversip/sip/client_transaction.rb', line 173

def terminate_transaction
  @state = :terminated
  @client_transactions.delete(@transaction_id)
end

#tls_validation_failedObject



269
270
271
272
273
274
275
276
277
278
# File 'lib/oversip/sip/client_transaction.rb', line 269

def tls_validation_failed
  return unless @state == :calling or not @cancel

  @timer_A.cancel  if @timer_A
  @timer_B.cancel
  @timer_C.cancel
  terminate_transaction

  @core.tls_validation_failed
end