<commit_before>require "base64"
require "thread"
require File.join File.dirname(File.expand_path(__FILE__)), "apn_connection.rb"
require File.join File.dirname(File.expand_path(__FILE__)), "gcm_connection.rb"

module Suj
  module Pusher
    class ConnectionPool
      include Suj::Pusher::Logger

      APN_SANDBOX = "gateway.sandbox.push.apple.com"
      APN_GATEWAY = "gateway.push.apple.com"
      FEEDBACK_SANDBOX = "feedback.sandbox.push.apple.com"
      FEEDBACK_GATEWAY = "feedback.push.apple.com"
      APN_PORT = 2195
      FEEDBACK_PORT = 2196

      class UnknownConnection < StandardError; end

      def initialize(daemon)
        @pool = {}
        @daemon = daemon
        @mutex = Mutex.new
      end

      def get_connection(options = {})
        if options.has_key?(:cert)
          if options.has_key?(:development) && options[:development]
            return apn_sandbox_connection(options)
          else
            return apn_connection(options)
          end
        elsif options.has_key?(:api_key)
          return gcm_connection(options)
        elsif options.has_key?(:secret) && options.has_key?(:sid)
          return wns_connection(options)
        elsif  options.has_key?(:wpnotificationclass)
          return wpns_connection(options)
        end
        raise UnknownConnection
      end

      def remove_connection(key)
        info "Removing connection #{key}"
        info "Connection not found" unless @pool.delete(key)
      end

      private

      def apn_connection(options = {})
        cert = Digest::SHA1.hexdigest options[:cert]
        info "APN connection #{cert}"
        @mutex.synchronize do
          @pool[cert] ||= EM.connect(APN_GATEWAY, APN_PORT, APNConnection, self, options)
        end
      end

      def apn_sandbox_connection(options = {})
        cert = Digest::SHA1.hexdigest options[:cert]
        info "APN connection #{cert}"<commit_msg>Fix possible race condition when deleting connections<commit_after>require "base64"
require "thread"
require File.join File.dirname(File.expand_path(__FILE__)), "apn_connection.rb"
require File.join File.dirname(File.expand_path(__FILE__)), "gcm_connection.rb"

module Suj
  module Pusher
    class ConnectionPool
      include Suj::Pusher::Logger

      APN_SANDBOX = "gateway.sandbox.push.apple.com"
      APN_GATEWAY = "gateway.push.apple.com"
      FEEDBACK_SANDBOX = "feedback.sandbox.push.apple.com"
      FEEDBACK_GATEWAY = "feedback.push.apple.com"
      APN_PORT = 2195
      FEEDBACK_PORT = 2196

      class UnknownConnection < StandardError; end

      def initialize(daemon)
        @pool = {}
        @daemon = daemon
        @mutex = Mutex.new
      end

      def get_connection(options = {})
        if options.has_key?(:cert)
          if options.has_key?(:development) && options[:development]
            return apn_sandbox_connection(options)
          else
            return apn_connection(options)
          end
        elsif options.has_key?(:api_key)
          return gcm_connection(options)
        elsif options.has_key?(:secret) && options.has_key?(:sid)
          return wns_connection(options)
        elsif  options.has_key?(:wpnotificationclass)
          return wpns_connection(options)
        end
        raise UnknownConnection
      end

      def remove_connection(key)
        @mutex.synchronize {
          info "Removing connection #{key}"
          info "Connection not found" unless @pool.delete(key)
        }
      end

      private

      def apn_connection(options = {})
        cert = Digest::SHA1.hexdigest options[:cert]
        info "APN connection #{cert}"
        @mutex.synchronize do
          @pool[cert] ||= EM.connect(APN_GATEWAY, APN_PORT, APNConnection, self, options)
        end
      end

      def apn_sandbox_connection(options = {})
        cert = Digest::SHA1.hexdigest options[:cert]
        info "APN connection #{cert}"
{'fraction_non_alphanumeric': 0.09746088740702745, 'fraction_numerical': 0.00615542446781226, 'mean_word_length': 2.6077705827937097, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '226264', 'n_tokens_mistral': 1220, 'n_tokens_neox': 1172, 'n_words': 238}