<commit_before>require 'eventmachine'

module RemoteSyslog
  class TlsEndpoint
    class Handler < EventMachine::Connection
      def initialize(endpoint)
        @endpoint = endpoint
        super()
      end

      def connection_completed
        start_tls(:verify_peer => @endpoint.server_cert != nil,
          :cert_chain_file => @endpoint.client_cert_chain,
          :private_key_file => @endpoint.client_private_key)
      end

      def ssl_verify_peer(peer_cert)
        peer_cert = OpenSSL::X509::Certificate.new(peer_cert)
        peer_cert.verify(@endpoint.server_cert.public_key)
      end

      def ssl_handshake_completed
        @endpoint.connection = self
      end

      def unbind
        @endpoint.unbind
      end
    end

    attr_accessor :connection
    attr_reader :server_cert, :client_cert_chain, :client_private_key

    attr_reader :logger

    def initialize(address, port, options = {})
      @address            = address
      @port               = port.to_i
      @client_cert_chain  = options[:client_cert_chain]
      @client_private_key = options[:client_private_key]
      @queue_limit        = options[:queue_limit] || 10_000
      @logger             = options[:logger] || Logger.new(STDERR)

      if options[:server_cert]
        @server_cert = OpenSSL::X509::Certificate.new(File.read(options[:server_cert]))
      end

      # Try to resolve the address
      resolve_address

      # Every 60 seconds we'll see if the address has changed
      EventMachine.add_periodic_timer(60) do
        resolve_address
      end

      connect
    end

    def connection=(conn)
      port, ip = Socket.unpack_sockaddr_in(conn.get_peername)
      logger.debug "Connected to #{ip}:#{port}"
      @connection = conn
    end

    def resolve_address
      request = EventMachine::DnsResolver.resolve(@address)
      request.callback do |addrs|
        @cached_ip = addrs.first
      end
    end

    def address
      @cached_ip || @address
    end

    def connect
      logger.debug "Connecting to #{address}:#{@port}"
      EventMachine.connect(address, @port, TlsEndpoint::Handler, self)
    end

    def unbind
      @connection = nil
      connect<commit_msg>Wait a second before attempting to reconnect<commit_after>require 'eventmachine'

module RemoteSyslog
  class TlsEndpoint
    class Handler < EventMachine::Connection
      def initialize(endpoint)
        @endpoint = endpoint
        super()
      end

      def connection_completed
        start_tls(:verify_peer => @endpoint.server_cert != nil,
          :cert_chain_file => @endpoint.client_cert_chain,
          :private_key_file => @endpoint.client_private_key)
      end

      def ssl_verify_peer(peer_cert)
        peer_cert = OpenSSL::X509::Certificate.new(peer_cert)
        peer_cert.verify(@endpoint.server_cert.public_key)
      end

      def ssl_handshake_completed
        @endpoint.connection = self
      end

      def unbind
        @endpoint.unbind
      end
    end

    attr_accessor :connection
    attr_reader :server_cert, :client_cert_chain, :client_private_key

    attr_reader :logger

    def initialize(address, port, options = {})
      @address            = address
      @port               = port.to_i
      @client_cert_chain  = options[:client_cert_chain]
      @client_private_key = options[:client_private_key]
      @queue_limit        = options[:queue_limit] || 10_000
      @logger             = options[:logger] || Logger.new(STDERR)

      if options[:server_cert]
        @server_cert = OpenSSL::X509::Certificate.new(File.read(options[:server_cert]))
      end

      # Try to resolve the address
      resolve_address

      # Every 60 seconds we'll see if the address has changed
      EventMachine.add_periodic_timer(60) do
        resolve_address
      end

      connect
    end

    def connection=(conn)
      port, ip = Socket.unpack_sockaddr_in(conn.get_peername)
      logger.debug "Connected to #{ip}:#{port}"
      @connection = conn
    end

    def resolve_address
      request = EventMachine::DnsResolver.resolve(@address)
      request.callback do |addrs|
        @cached_ip = addrs.first
      end
    end

    def address
      @cached_ip || @address
    end

    def connect
      logger.debug "Connecting to #{address}:#{@port}"
      EventMachine.connect(address, @port, TlsEndpoint::Handler, self)
    end

    def unbind
      @connection = nil

      EventMachine.add_timer(1) do
        connect
      end
{'fraction_non_alphanumeric': 0.08376144739781104, 'fraction_numerical': 0.006924279651552379, 'mean_word_length': 2.5149136577708004, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, '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': '7200271', 'n_tokens_mistral': 1332, 'n_tokens_neox': 1270, 'n_words': 284}