Class: Quaff::Call

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cxn, cid, instance_id = nil, uri = "sip:5557777888@#{Utils::local_ip}", destination = nil, target_uri = nil) ⇒ Call

Returns a new instance of Call.



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/call.rb', line 29

def initialize(cxn,
               cid,
               instance_id=nil,
               uri="sip:5557777888@#{Utils::local_ip}",
               destination=nil,
               target_uri=nil)
  @cxn = cxn
  setdest(destination, recv_from_this: true) if destination
  @retrans = nil
  @t1, @t2 = 0.5, 32
  @instance_id = instance_id
  set_default_headers cid, uri, target_uri
end

Instance Attribute Details

#cidObject (readonly)

Returns the value of attribute cid.



27
28
29
# File 'lib/call.rb', line 27

def cid
  @cid
end

Instance Method Details

#assoc_with_msg(msg) ⇒ Object



156
157
158
159
# File 'lib/call.rb', line 156

def assoc_with_msg(msg)
  @last_Via = msg.all_headers("Via")
  @last_CSeq = CSeq.new(msg.header("CSeq"))
end

#change_cid(cid) ⇒ Object



43
44
45
46
# File 'lib/call.rb', line 43

def change_cid cid
  @cid = cid
  @cxn.add_call_id @cid
end

#create_dialog(msg) ⇒ Object



59
60
61
# File 'lib/call.rb', line 59

def create_dialog msg
  set_callee msg.first_header("Contact")
end

#end_callObject



152
153
154
# File 'lib/call.rb', line 152

def end_call
  @cxn.mark_call_dead @cid
end

#get_new_via_hdrObject



55
56
57
# File 'lib/call.rb', line 55

def get_new_via_hdr
  "SIP/2.0/#{@cxn.transport} #{Quaff::Utils.local_ip}:#{@cxn.local_port};rport;branch=#{Quaff::Utils::new_branch}"
end

#get_next_hop(header) ⇒ Object



161
162
163
164
165
# File 'lib/call.rb', line 161

def get_next_hop header
  /<sip:(.+@)?(.+):(\d+);(.*)>/ =~ header
  sock = TCPSocket.new $2, $3
  return TCPSource.new sock
end

#recv_request(method, dialog_creating = true) ⇒ Object



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
104
105
106
107
108
# File 'lib/call.rb', line 78

def recv_request(method, dialog_creating=true)
  begin
    msg = recv_something
  rescue
    raise "#{ @uri } timed out waiting for #{ method }"
  end

  unless msg.type == :request \
    and Regexp.new(method) =~ msg.method
    raise((msg.to_s || "Message is nil!"))
  end

  unless @has_To_tag
    @has_To_tag = true
    tospec = ToSpec.new
    tospec.parse(msg.header("To"))
    tospec.params['tag'] = generate_random_tag
    @last_To = tospec.to_s
    @last_From = msg.header("From")
  end

  if dialog_creating
    @in_dialog = true

    set_callee msg.first_header("Contact")
    unless msg.all_headers("Record-Route").nil?
      @routeset = msg.all_headers("Record-Route")
    end
  end
  msg
end

#recv_response(code, dialog_creating = false) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/call.rb', line 110

def recv_response(code, dialog_creating=false)
  begin
    msg = recv_something
  rescue
    raise "#{ @uri } timed out waiting for #{ code }"
  end
  unless msg.type == :response \
    and Regexp.new(code) =~ msg.status_code
    raise "Expected #{ code}, got #{msg.status_code || msg}"
  end

  if dialog_creating
    @in_dialog = true
    set_callee msg.first_header("Contact")
    unless msg.all_headers("Record-Route").nil?
      @routeset = msg.all_headers("Record-Route").reverse
    end
  end

  if @in_dialog
    @has_To_tag = true
    @last_To = msg.header("To")
  end

  msg
end

#recv_response_and_create_dialog(code) ⇒ Object



137
138
139
# File 'lib/call.rb', line 137

def recv_response_and_create_dialog(code)
  recv_response code, true
end

#send_request(method, body = "", headers = {}) ⇒ Object



147
148
149
150
# File 'lib/call.rb', line 147

def send_request(method, body="", headers={})
  msg = build_message headers, body, :request, method
  send_something(msg, nil)
end

#send_response(code, phrase, body = "", retrans = nil, headers = {}) ⇒ Object



141
142
143
144
145
# File 'lib/call.rb', line 141

def send_response(code, phrase, body="", retrans=nil, headers={})
  method = nil
  msg = build_message headers, body, :response, method, code, phrase
  send_something(msg, retrans)
end

#set_callee(uri) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/call.rb', line 63

def set_callee uri
  if /<(.*?)>/ =~ uri
    uri = $1
  end

  @sip_destination = "#{uri}"
end

#setdest(source, options = {}) ⇒ Object



71
72
73
74
75
76
# File 'lib/call.rb', line 71

def setdest source, options={}
  @src = source
  if options[:recv_from_this] and source.sock
    @cxn.add_sock source.sock
  end
end

#update_branch(via_hdr = nil) ⇒ Object Also known as: new_transaction



48
49
50
51
# File 'lib/call.rb', line 48

def update_branch via_hdr=nil
  via_hdr ||= get_new_via_hdr
  @last_Via = via_hdr
end