<commit_before>require 'net/ssh'
require 'devproxy/net-ssh-patch'

module Devproxy
  class Connection
    class Error                 < StandardError; end
    class Error::Authentication < Error        ; end

    attr_reader :options, :ssh

    MAX_DOTS = 60

    def initialize options, ssh
      @options , @ssh   = options , ssh
      @halt    , @dotno = false   , 0
    end

    def loop!
      @ssh.loop { !halt? }
    end
    def stop!
      @halt = true
    end
    def halt?
      @halt
    end

    def on_stdout data
      if (@dotno += 1) % MAX_DOTS == 0
        $stdout.puts "\n"
      end
      $stdout.write data
    end

    def on_stderr data
      @dotno = 0
      $stderr.puts "\nError: #{data}"
    end

    def on_close
      stop!
    end

    def self.loop!(options)
      create(options).loop!
    end

    def self.create(options)
      ssh        = open_ssh(options)
      connection = new(options,ssh)
      ssh.forward.remote(options.port,"localhost",0,'0.0.0.0')
      channel    = ssh.exec(options.proxy) do |ch,stream,data|
        if stream == :stdout
          connection.on_stdout(data)
        else
          connection.on_stderr(data)
        end<commit_msg>handle ECONNREFUSED<commit_after>require 'net/ssh'
require 'devproxy/net-ssh-patch'

module Devproxy
  class Connection
    class Error                 < StandardError; end
    class Error::Authentication < Error        ; end

    attr_reader :options, :ssh

    MAX_DOTS = 60

    def initialize options, ssh
      @options, @ssh, @halt = options, ssh, false
      reset_dots!
    end

    def loop!
      @ssh.loop { !halt? }
    rescue Errno::ECONNREFUSED
      on_stderr "CONNECTION REFUSED.  Is there anything listening on port #{options.port}?"
      retry
    end
    def stop!
      @halt = true
    end
    def halt?
      @halt
    end

    def on_stdout data
      $stdout.write data
      if (@dotno += 1) % MAX_DOTS == 0
        $stdout.write "\n"
      end
    end

    def on_stderr data
      $stderr.puts "\nError: #{data}"
      reset_dots!
    end

    def on_close
      stop!
    end

    protected
    def reset_dots!
      @dotno = 0
    end

    def self.loop!(options)
      create(options).loop!
    end

    def self.create(options)
      ssh        = open_ssh(options)
      connection = new(options,ssh)
      ssh.forward.remote(options.port,"localhost",0,'0.0.0.0')
      channel    = ssh.exec(options.proxy) do |ch,stream,data|
        if stream == :stdout
          connection.on_stdout(data)
        else
          connection.on_stderr(data)
        end
{'fraction_non_alphanumeric': 0.09887553315238465, 'fraction_numerical': 0.0081426909654905, 'mean_word_length': 1.889137737961926, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, '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': '2102758', 'n_tokens_mistral': 859, 'n_tokens_neox': 810, 'n_words': 211}