Class: OverSIP::SIP::UacRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/oversip/sip/uac_request.rb

Constant Summary collapse

DEFAULT_MAX_FORWARDS =
"20"
DEFAULT_FROM =
"\"OverSIP #{::OverSIP::VERSION}\" <sip:[email protected]>"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, extra_headers = [], body = nil) ⇒ UacRequest

Returns a new instance of UacRequest.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/oversip/sip/uac_request.rb', line 14

def initialize data, extra_headers=[], body=nil
  unless (@sip_method = data[:sip_method])
    raise ::OverSIP::RuntimeError, "no data[:sip_method] given"
  end
  unless (ruri = data[:ruri])
    raise ::OverSIP::RuntimeError, "no data[:ruri] given"
  end

  case ruri
  when ::OverSIP::SIP::Uri, ::OverSIP::SIP::NameAddr
    @ruri = ruri
  when ::String
    @ruri = OverSIP::SIP::Uri.parse ruri
  else
    raise ::OverSIP::RuntimeError, "invalid URI #{ruri.inspect}"
  end

  @from = data[:from] || DEFAULT_FROM
  @from_tag = data[:from_tag] || ::SecureRandom.hex(4)
  @to = data[:to] || @ruri
  @call_id = data[:call_id] || ::SecureRandom.hex(8)
  @cseq = data[:cseq] || rand(1000)
  @max_forwards = data[:max_forwards] || DEFAULT_MAX_FORWARDS

  @headers = {}
  @extra_headers = extra_headers

  @body = body

  @antiloop_id = ::OverSIP::SIP::Tags.create_antiloop_id(self)
  @via_branch_id = ::SecureRandom.hex(4)
end

Instance Attribute Details

#antiloop_idObject (readonly)

Returns the value of attribute antiloop_id.



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

def antiloop_id
  @antiloop_id
end

#bodyObject (readonly)

Returns the value of attribute body.



8
9
10
# File 'lib/oversip/sip/uac_request.rb', line 8

def body
  @body
end

#call_idObject (readonly)

Returns the value of attribute call_id.



8
9
10
# File 'lib/oversip/sip/uac_request.rb', line 8

def call_id
  @call_id
end

#cseqObject (readonly)

Returns the value of attribute cseq.



8
9
10
# File 'lib/oversip/sip/uac_request.rb', line 8

def cseq
  @cseq
end

#fromObject (readonly)

Returns the value of attribute from.



8
9
10
# File 'lib/oversip/sip/uac_request.rb', line 8

def from
  @from
end

#from_tagObject (readonly)

Returns the value of attribute from_tag.



8
9
10
# File 'lib/oversip/sip/uac_request.rb', line 8

def from_tag
  @from_tag
end

#routesObject (readonly)

Always nil (needed for OverSIP::SIP::Tags.create_antiloop_id().



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

def routes
  @routes
end

#ruriObject (readonly)

Returns the value of attribute ruri.



8
9
10
# File 'lib/oversip/sip/uac_request.rb', line 8

def ruri
  @ruri
end

#sip_methodObject (readonly)

Returns the value of attribute sip_method.



8
9
10
# File 'lib/oversip/sip/uac_request.rb', line 8

def sip_method
  @sip_method
end

#toObject (readonly)

Returns the value of attribute to.



8
9
10
# File 'lib/oversip/sip/uac_request.rb', line 8

def to
  @to
end

#tvarsObject

Transaction variables (a hash).



11
12
13
# File 'lib/oversip/sip/uac_request.rb', line 11

def tvars
  @tvars
end

#via_branch_idObject (readonly)

Returns the value of attribute via_branch_id.



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

def via_branch_id
  @via_branch_id
end

Instance Method Details

#delete_header_top(name) ⇒ Object



53
54
55
# File 'lib/oversip/sip/uac_request.rb', line 53

def delete_header_top name
  @headers.delete name
end

#insert_header(name, value) ⇒ Object



48
49
50
# File 'lib/oversip/sip/uac_request.rb', line 48

def insert_header name, value
  @headers[name] = value.to_s
end

#to_sObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/oversip/sip/uac_request.rb', line 58

def to_s
  msg = "#{@sip_method.to_s} #{@ruri.uri} SIP/2.0\r\n"

  @headers.each do |name, value|
    msg << name << ": #{value}\r\n"
  end

  msg << "From: #{@from.to_s};tag=#{@from_tag}\r\n"
  msg << "To: #{@to.to_s}\r\n"
  msg << "Call-ID: #{@call_id}\r\n"
  msg << "CSeq: #{@cseq.to_s} #{@sip_method.to_s}\r\n"
  msg << "Content-Length: #{@body ? @body.bytesize : "0"}\r\n"
  msg << "Max-Forwards: #{@max_forwards.to_s}\r\n"
  msg << HDR_USER_AGENT << CRLF

  @extra_headers.each do |header|
    msg << header << CRLF
  end

  msg << CRLF
  msg << @body  if @body
  msg
end