<commit_before>require 'gserver'
module Adhearsion
  module VoIP
    module Asterisk
      module AGI
        class Server
          
          class RubyServer < GServer
            
            def initialize(port, host)
              super(port, host, (1.0/0.0)) # (1.0/0.0) == Infinity
            end
            
            def serve(io)
              Hooks::BeforeCall.trigger_hooks
          	  call = Adhearsion.receive_call_from(io)
          	  ahn_log.agi "Handling call with variables #{call.variables.inspect}"
          	  
          	  return DialPlan::ConfirmationManager.handle(call) if DialPlan::ConfirmationManager.confirmation_call?(call)
          	  
      	      # This is what happens 99.9% of the time.
      	      
      	      DialPlan::Manager.handle call
            rescue DialPlan::Manager::NoContextError => e
              ahn_log.agi e.message
              call.hangup!
            rescue FailedExtensionCallException => failed_call
              begin
                ahn_log.agi "Received \"failed\" meta-call with :failed_reason => #{failed_call.call.failed_reason.inspect}. Executing OnFailedCall hooks."
                Hooks::OnFailedCall.trigger_hooks(failed_call.call)
                call.hangup!
              rescue => e
                ahn_log.agi.error e
              end
            rescue HungupExtensionCallException => hungup_call
              begin
                ahn_log.agi "Received \"h\" meta-call. Executing OnHungupCall hooks."
                Hooks::OnHungupCall.trigger_hooks(hungup_call.call)
                call.hangup!
              rescue => e
                ahn_log.agi.error e
              end
            rescue UselessCallException
              ahn_log.agi "Ignoring meta-AGI request"
              call.hangup!
        	  # TBD: (may have more hooks than what Jay has defined in hooks.rb)
            rescue => e
              ahn_log.agi.error e.inspect
              ahn_log.agi.error e.backtrace.map { |s| " " * 5 + s }.join("\n")
            end
          end
         
          DEFAULT_OPTIONS = { :server_class => RubyServer, :port => 4573, :host => "0.0.0.0" } unless defined? DEFAULT_OPTIONS
          attr_reader :host, :port, :server_class, :server

          def initialize(options = {})
            options                     = DEFAULT_OPTIONS.merge options
            @host, @port, @server_class = options.values_at(:host, :port, :server_class)
            @server                     = server_class.new(port, host)
          end

          def start
            server.start
          end

          def shutdown
            server.stop
          end
          
          def join
            server.join
          end
          
        end
      end
    end
  end<commit_msg>Fixing a bug in which Adhearsion.active_calls contained inactive calls<commit_after>require 'gserver'
module Adhearsion
  module VoIP
    module Asterisk
      module AGI
        class Server
          
          class RubyServer < GServer
            
            def initialize(port, host)
              super(port, host, (1.0/0.0)) # (1.0/0.0) == Infinity
            end
            
            def serve(io)
              Hooks::BeforeCall.trigger_hooks
          	  call = Adhearsion.receive_call_from(io)
          	  ahn_log.agi "Handling call with variables #{call.variables.inspect}"
          	  
          	  return DialPlan::ConfirmationManager.handle(call) if DialPlan::ConfirmationManager.confirmation_call?(call)
          	  
      	      # This is what happens 99.9% of the time.
      	      
      	      DialPlan::Manager.handle call
            rescue DialPlan::Manager::NoContextError => e
              ahn_log.agi e.message
              call.hangup!
            rescue FailedExtensionCallException => failed_call
              begin
                ahn_log.agi "Received \"failed\" meta-call with :failed_reason => #{failed_call.call.failed_reason.inspect}. Executing OnFailedCall hooks."
                Hooks::OnFailedCall.trigger_hooks(failed_call.call)
                call.hangup!
              rescue => e
                ahn_log.agi.error e
              end
            rescue HungupExtensionCallException => hungup_call
              begin
                ahn_log.agi "Received \"h\" meta-call. Executing OnHungupCall hooks."
                Hooks::OnHungupCall.trigger_hooks(hungup_call.call)
                call.hangup!
              rescue => e
                ahn_log.agi.error e
              end
            rescue UselessCallException
              ahn_log.agi "Ignoring meta-AGI request"
              call.hangup!
        	  # TBD: (may have more hooks than what Jay has defined in hooks.rb)
            rescue => e
              ahn_log.agi.error e.inspect
              ahn_log.agi.error e.backtrace.map { |s| " " * 5 + s }.join("\n")
            ensure
              Adhearsion.remove_inactive_call call
            end
            
          end
         
          DEFAULT_OPTIONS = { :server_class => RubyServer, :port => 4573, :host => "0.0.0.0" } unless defined? DEFAULT_OPTIONS
          attr_reader :host, :port, :server_class, :server

          def initialize(options = {})
            options                     = DEFAULT_OPTIONS.merge options
            @host, @port, @server_class = options.values_at(:host, :port, :server_class)
            @server                     = server_class.new(port, host)
          end

          def start
            server.start
          end

          def shutdown
            server.stop
          end
          
          def join
            server.join
          end
          
        end
      end
    end
  end
{'fraction_non_alphanumeric': 0.07735583684950774, 'fraction_numerical': 0.007032348804500703, 'mean_word_length': 1.3994095318431041, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6876890', 'n_tokens_mistral': 1577, 'n_tokens_neox': 1532, 'n_words': 352}