<commit_before>module AmqpManager
  class << self

    def rails_channel
      Thread.current[:rails_channel] ||= connection.create_channel
    end

    def rails_xchange
      Thread.current[:rails_xchange] ||= rails_channel.topic('voice.rails', auto_delete: false)
    end

    def rails_queue
      Thread.current[:rails_queue] ||= rails_channel.queue('voice.rails', auto_delete: false)
    end


    def push_channel
      Thread.current[:push_channel] ||= connection.create_channel
    end

    def push_xchange
      Thread.current[:push_xchange] ||= push_channel.topic('voice.push', auto_delete: false)
    end

    def push_publish(payload)
      push_xchange.publish(payload.to_json, routing_key: 'voice.push')
      true
    end


    def ahn_channel
      Thread.current[:ahn_channel] ||= connection.create_channel
    end

    def ahn_xchange
      Thread.current[:ahn_xchange] ||= ahn_channel.topic('voice.ahn', auto_delete: false)
    end

    def ahn_queue
      Thread.current[:ahn_queue] ||= ahn_channel.queue('voice.ahn', auto_delete: false)
    end

    def ahn_publish(payload)
      ahn_xchange.publish(payload.to_json, routing_key: 'voice.ahn')
      true
    end


    def shutdown
      connection.close
    end


    def connection
      establish_connection unless @connection
      @connection
    end


    def establish_connection
      @connection = Bunny.new(
        host:     WimConfig.rabbit_host,
        user:     WimConfig.rabbit_user,
        password: <PASSWORD>
      ).tap { |c| c.start }
    rescue Bunny::TCPConnectionFailed
      sleep 1
      retry
    end


    # VA is responsible for changing the agent callstates.
    # We listen to AHNs signals to re-send the user object
    # after the external update here:
    #
    def handle_status_message(data)
      if data['name'] == 'PeerStatus'
        peer = data['headers']['Peer'][/^SIP.(\d+)/, 1]
      elsif data['name'] == 'Newstate' && data['headers']['ChannelState'] == '6' # 6 => Up
        peer = data['headers']['Channel'][/^SIP.(\d+)/, 1]
      elsif data['name'] == 'Hangup'
        peer = data['headers']['Channel'][/^SIP.(\d+)/, 1]
      else
        return
      end

      user = User.where(name: peer).first
      user.send_update_notification_to_clients if user
    end


    def start<commit_msg>name the channel regex<commit_after>module AmqpManager

  ChannelRegex = /^SIP.(\d+)/

  class << self

    def rails_channel
      Thread.current[:rails_channel] ||= connection.create_channel
    end

    def rails_xchange
      Thread.current[:rails_xchange] ||= rails_channel.topic('voice.rails', auto_delete: false)
    end

    def rails_queue
      Thread.current[:rails_queue] ||= rails_channel.queue('voice.rails', auto_delete: false)
    end


    def push_channel
      Thread.current[:push_channel] ||= connection.create_channel
    end

    def push_xchange
      Thread.current[:push_xchange] ||= push_channel.topic('voice.push', auto_delete: false)
    end

    def push_publish(payload)
      push_xchange.publish(payload.to_json, routing_key: 'voice.push')
      true
    end


    def ahn_channel
      Thread.current[:ahn_channel] ||= connection.create_channel
    end

    def ahn_xchange
      Thread.current[:ahn_xchange] ||= ahn_channel.topic('voice.ahn', auto_delete: false)
    end

    def ahn_queue
      Thread.current[:ahn_queue] ||= ahn_channel.queue('voice.ahn', auto_delete: false)
    end

    def ahn_publish(payload)
      ahn_xchange.publish(payload.to_json, routing_key: 'voice.ahn')
      true
    end


    def shutdown
      connection.close
    end


    def connection
      establish_connection unless @connection
      @connection
    end


    def establish_connection
      @connection = Bunny.new(
        host:     WimConfig.rabbit_host,
        user:     WimConfig.rabbit_user,
        password: <PASSWORD>
      ).tap { |c| c.start }
    rescue Bunny::TCPConnectionFailed
      sleep 1
      retry
    end


    # VA is responsible for changing the agent callstates.
    # We listen to AHNs signals to re-send the user object
    # after the external update here:
    #
    def handle_status_message(data)
      if data['name'] == 'PeerStatus'
        peer = data['headers']['Peer'][ChannelRegex, 1]
      elsif data['name'] == 'Newstate' && data['headers']['ChannelState'] == '6' # 6 => Up
        peer = data['headers']['Channel'][ChannelRegex, 1]
      elsif data['name'] == 'Hangup'
        peer = data['headers']['Channel'][ChannelRegex, 1]
      else
        return
      end

      user = User.where(name: peer).first
      user.send_update_notification_to_clients if user
    end


    def start
{'fraction_non_alphanumeric': 0.11173064550718421, 'fraction_numerical': 0.0025734505683036673, 'mean_word_length': 2.7887896019496345, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '2819857', 'n_tokens_mistral': 1491, 'n_tokens_neox': 1401, 'n_words': 318}