Class: Adhearsion::VoIP::FreeSwitch::BasicConnectionManager

Inherits:
Object
  • Object
show all
Defined in:
lib/adhearsion/voip/freeswitch/basic_connection_manager.rb

Direct Known Subclasses

InboundConnectionManager

Instance Method Summary collapse

Constructor Details

#initialize(io) ⇒ BasicConnectionManager

Returns a new instance of BasicConnectionManager.



7
8
9
# File 'lib/adhearsion/voip/freeswitch/basic_connection_manager.rb', line 7

def initialize(io)
  @io = io
end

Instance Method Details

#<<(str) ⇒ Object

The send-command operator



12
13
14
# File 'lib/adhearsion/voip/freeswitch/basic_connection_manager.rb', line 12

def <<(str)
  @io.write str + "\n\n"
end

#get_headerObject



16
17
18
# File 'lib/adhearsion/voip/freeswitch/basic_connection_manager.rb', line 16

def get_header
  separate_pairs get_raw_header
end

#get_raw_headerObject



20
21
22
23
24
25
26
# File 'lib/adhearsion/voip/freeswitch/basic_connection_manager.rb', line 20

def get_raw_header
  ([].tap do |lines|
    until line = @io.gets and line.chomp.empty?
      lines << line.chomp
    end
  end) * "\n"
end

#next_eventObject



28
29
30
31
32
33
# File 'lib/adhearsion/voip/freeswitch/basic_connection_manager.rb', line 28

def next_event
  header = get_raw_header
  length = header.first[/\d+$/].to_i
  # puts "Reading an event of #{length} bytes"
  separate_pairs @io.read(length)
end

#separate_pairs(lines) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/adhearsion/voip/freeswitch/basic_connection_manager.rb', line 35

def separate_pairs(lines)
  lines.split("\n").inject({}) do |h,line|
    h.tap do |hash|
      k,v = line.split(/\s*:\s*/)
      hash[k] = URI.unescape(v).strip if k && v
    end
  end
end