Sha256: d5cf58f6f6d1d6ec0f0d153367b2372c277a6531fc80d30dc90e83cea2f3daba

Contents?: true

Size: 1.85 KB

Versions: 5

Compression:

Stored size: 1.85 KB

Contents

#!/usr/bin/env ruby
# -*- encoding: binary -*-
lib = File.expand_path('../../lib', __FILE__)
$LOAD_PATH.unshift lib unless $LOAD_PATH.include?(lib)

require 'netsoul/client'

$stderr.sync = true
require 'optparse'
require 'yaml'

process_name_string = "#{__FILE__} #{ARGV.join(' ')}".freeze
Process.respond_to?(:setproctitle) ? Process.setproctitle(process_name_string) : $PROGRAM_NAME = process_name_string

options = {}
OptionParser.new do |opts|
  opts.banner = 'Usage: netsoul-ruby [options]'.freeze
  opts.separator ''.freeze
  opts.separator 'Netsoul-Ruby options:'.freeze

  opts.on('-c'.freeze, '--config FILE'.freeze, 'Configuration file in YAML'.freeze) do |file|
    options[:user_opts] = YAML.load_file(file) if File.file?(file)
  end

  opts.on('-h'.freeze, '--help', 'Display this screen'.freeze) do
    puts opts
    exit 42
  end
end.parse!

if options.empty? || options[:user_opts].size == 0
  unless ENV.to_a.count { |k, _v| %w(NETSOUL_LOGIN NETSOUL_SOCKS_PASSWORD NETSOUL_LOGIN NETSOUL_UNIX_PASSWORD NETSOUL_AUTH_METHOD).include?(k) } >= 2
    puts '[ERROR] You have to specify a configuration file or environment variables'.freeze
    exit 42
  end
end

retry_count = 10
retry_wait_time = 1.0
RETRY_WAIT_FACTOR = 2.0 # Each time retry is called in Exception block, current 'retry_wait_time' is increased with this factor
begin
  c = Netsoul::Client.new options[:user_opts]
  c.connect
  if c.started
    retry_count = 10
    retry_wait_time = 1.0
    loop do
      res = c.get
      c.send res if res.to_s.match(/^ping.*/)
      sleep 1
    end
  end
rescue Interrupt
  begin c.disconnect; end
  puts '!!! [SIGINT] !!!'.freeze
  exit 42
rescue => e
  puts "[ERROR]: #{e}"
  puts "[RETRY_COUNT]: #{retry_count}"
  begin c.disconnect; end
  if retry_count > 0
    retry_count -= 1
    retry_wait_time *= RETRY_WAIT_FACTOR
    sleep(retry_wait_time)
    retry
  end
  exit 42
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
netsoul-2.3.0 bin/netsoul-ruby
netsoul-2.2.1 bin/netsoul-ruby
netsoul-2.2.0 bin/netsoul-ruby
netsoul-2.1.0 bin/netsoul-ruby
netsoul-2.0.0 bin/netsoul-ruby