Class: OverSIP::SIP::TcpClient

Inherits:
TcpConnection show all
Defined in:
lib/oversip/sip/listeners/tcp_client.rb

Direct Known Subclasses

IPv4TcpClient, IPv6TcpClient, TlsClient

Constant Summary

Constants inherited from TcpConnection

OverSIP::SIP::TcpConnection::HEADERS_MAX_SIZE

Constants included from MessageProcessor

MessageProcessor::MSG_TYPE

Class Attribute Summary collapse

Instance Attribute Summary collapse

Attributes inherited from Connection

#cvars

Instance Method Summary collapse

Methods inherited from TcpConnection

#get_body, #parse_headers, #process_received_data, #receive_data, #remote_ip, #remote_ip_type, #remote_port, #send_sip_msg

Methods inherited from Connection

#open?, outbound_listener?, #receive_senderror, reliable_transport_listener?, #transport

Methods included from Logger

fg_system_msg2str, load_methods, #log_id

Constructor Details

#initialize(ip, port) ⇒ TcpClient

Returns a new instance of TcpClient.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/oversip/sip/listeners/tcp_client.rb', line 12

def initialize ip, port
  # NOTE: The parent class implementing "initialize" method is Connection, and allows no arguments.
  # If we call just "super" from here it will fail since "ip" and "port" will be passed as
  # arguments. So we must use "super()" and we are done (no arguments are passed to parent).
  super()

  @remote_ip = ip
  @remote_port = port
  @connection_id = ::OverSIP::SIP::TransportManager.add_connection self, self.class, self.class.ip_type, @remote_ip, @remote_port
  @connected = false
  @pending_client_transactions = []

  ### TODO: make it configurable.
  set_pending_connect_timeout 2.0
end

Class Attribute Details

.server_classObject (readonly)

Returns the value of attribute server_class.



6
7
8
# File 'lib/oversip/sip/listeners/tcp_client.rb', line 6

def server_class
  @server_class
end

Instance Attribute Details

#connectedObject (readonly)

Returns the value of attribute connected.



9
10
11
# File 'lib/oversip/sip/listeners/tcp_client.rb', line 9

def connected
  @connected
end

#pending_client_transactionsObject (readonly)

Returns the value of attribute pending_client_transactions.



10
11
12
# File 'lib/oversip/sip/listeners/tcp_client.rb', line 10

def pending_client_transactions
  @pending_client_transactions
end

Instance Method Details

#connection_completedObject



29
30
31
32
33
34
# File 'lib/oversip/sip/listeners/tcp_client.rb', line 29

def connection_completed
  log_system_info "TCP connection opened to " << remote_desc

  @connected = true
  @pending_client_transactions.clear
end

#record_routeObject

For the case in which OverSIP receives a SIP request from a connection open by OverSIP.



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/oversip/sip/listeners/tcp_client.rb', line 75

def record_route
  @record_route and return @record_route

  server_class = self.class.server_class
  local_port, local_ip = ::Socket.unpack_sockaddr_in(get_sockname)

  case
    when server_class == ::OverSIP::SIP::IPv4TcpServer
      uri_ip = local_ip
    when server_class == ::OverSIP::SIP::IPv6TcpServer
      uri_ip = "[#{local_ip}]"
    when server_class == ::OverSIP::SIP::IPv4TlsServer
      uri_ip = local_ip
    when server_class == ::OverSIP::SIP::IPv6TlsServer
      uri_ip = "[#{local_ip}]"
    end

  @record_route = "<sip:#{uri_ip}:#{local_port};transport=#{server_class.transport.to_s};lr;ovid=#{OverSIP::SIP::Tags.value_for_route_ovid}>"
end

#remote_descObject



37
38
39
40
41
42
# File 'lib/oversip/sip/listeners/tcp_client.rb', line 37

def remote_desc
  @remote_desc ||= case self.class.ip_type
    when :ipv4  ; "#{@remote_ip}:#{@remote_port.to_s}"
    when :ipv6  ; "[#{@remote_ip}]:#{@remote_port.to_s}"
    end
end

#unbind(cause = nil) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/oversip/sip/listeners/tcp_client.rb', line 45

def unbind cause=nil
  @state = :ignore

  # Remove the connection.
  self.class.connections.delete @connection_id

  @local_closed = true  if cause == ::Errno::ETIMEDOUT

  if @connected
    log_msg = "connection to #{remote_desc} "
    log_msg << ( @local_closed ? "locally closed" : "remotely closed" )
    log_msg << " (cause: #{cause.inspect})"  if cause
    log_system_debug log_msg  if $oversip_debug

  # If the TCP client connection has failed (remote server rejected the connection) then
  # inform to all the pending client transactions.
  else
    log_system_notice "connection to #{remote_desc} failed"  if $oversip_debug

    @pending_client_transactions.each do |client_transaction|
      client_transaction.connection_failed
    end
    @pending_client_transactions.clear
  end unless $!

  @connected = false
end