Class: Adhearsion::VoIP::FreeSwitch::OesServer

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

Defined Under Namespace

Classes: OesDispatcher

Instance Method Summary collapse

Constructor Details

#initialize(port, host = nil) ⇒ OesServer

Returns a new instance of OesServer.



14
15
16
17
18
19
# File 'lib/adhearsion/voip/freeswitch/oes_server.rb', line 14

def initialize(port, host=nil)
  @port, @host = port || 4572, host || "0.0.0.0"
  @cache_lock = Mutex.new
  super @port, @host, (1.0/0.0)
  log "Starting FreeSwitch OES Server"
end

Instance Method Details

#cached_dialplan_dataObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/adhearsion/voip/freeswitch/oes_server.rb', line 72

def cached_dialplan_data
  @cache_lock.synchronize do
    log "Checking whether the contexts should be reloaded"
    if should_reload_contexts?
      log "Getting the contexts"
      @abstract_contexts = DSL::Dialplan::DialplanParser.get_contexts
      log "Creating a new OesDispatcher"
      @abstract_dispatcher = OesDispatcher.new @vars['Channel-Unique-ID']
      log "Done creating it"
      @abstract_dispatcher.def_keys! @abstract_contexts
    else  
      log "Should not reload context."
      @abstract_dispatcher.instance_variable_set :@uuid, @vars['Channel-Unique-ID']
    end
    return [@abstract_contexts.clone, @abstract_dispatcher.clone]
  end
end

#rubyize_keys_for(hash) ⇒ Object



97
98
99
100
101
# File 'lib/adhearsion/voip/freeswitch/oes_server.rb', line 97

def rubyize_keys_for(hash)
  returning({}) do |pretty|
    hash.each { |k,v| pretty[k.to_s.underscore] = v }
  end
end

#serve(io) ⇒ Object



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
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
# File 'lib/adhearsion/voip/freeswitch/oes_server.rb', line 21

def serve(io)
  
  log "Incoming call on the FreeSwitch outbound event socket..."
  Thread.me.extend DSL::Dialplan::ThreadMixin
  Thread.me.call.io = io
  conn = BasicConnectionManager.new io
  
  Thread.my.call.mgr = conn
  conn << "connect"
  @vars = conn.get_header
  answered = @vars['variable_endpoint_disposition'] == "ANSWER"
  
  conn << "myevents"
  myevents_response = conn.get_header
  answered ||= myevents_response['Event-Name'] == 'CHANNEL_ANSWER'
  
  log "Connected to Freeswitch. Waiting for answer state."
  
  until answered
    answered ||= conn.get_header['Event-Name'] == 'CHANNEL_ANSWER'
  end
  
  log "Loading cached dialplan"
  contexts, dispatcher = cached_dialplan_data
  log "Finished loading cached dialplans"
  
  first_context_name  = @vars['variable_context'] || @vars["Channel-Context"]
  first_context       = contexts[first_context_name.to_sym]
  
  log "Found context #{first_context_name} from call variables."
  
  # If the target context does not exist, warn and don't handle the call
  unless first_context
    log "No context '#{first_context_name}' found in " +
        "#{AHN_CONFIG.files_from_setting("paths", "").to_sentence(:connector => "or")}. Ignoring request!"
    return
  end
  
  # Enable events
  
  # Now that we have the code, let's dispatch it back.
  
  pretty_vars = rubyize_keys_for @vars
  dispatcher.def_keys! pretty_vars
  dispatcher.instance_eval(&first_context.block)
  
rescue => e
  p e
  puts e.backtrace.map {|x| " " * 4 + x }
end

#should_reload_contexts?Boolean

TODO. This is broken. Always returns true. Should cache the last reload time.

Returns:

  • (Boolean)


92
93
94
95
# File 'lib/adhearsion/voip/freeswitch/oes_server.rb', line 92

def should_reload_contexts?
  !@abstract_contexts || !@abstract_dispatcher ||
    AHN_CONFIG.files_from_setting("paths", "dialplan").map { |x| File.mtime(x) }.max < Time.now
end