Class: MaxCube::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/maxcube/runner.rb

Overview

Module that provides running of Cube clients: Network::TCP::Client and Network::UDP::Client

Constant Summary collapse

HELP_KEYS =

These will display help message.

%w[h -h help -help --help ? -?].freeze

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ Runner

Assigns command line arguments to internal variable.



12
13
14
# File 'lib/maxcube/runner.rb', line 12

def initialize(argv)
  @argv = argv
end

Instance Method Details

#runObject

Runs either TCP or UDP client.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/maxcube/runner.rb', line 17

def run
  help = @argv.size == 1 && HELP_KEYS.include?(@argv.first)
  wrong_args = @argv.size > 2

  if help || wrong_args
    if wrong_args
      puts "Wrong number of arguments: #{@argv.size} (expected: 0..2)"
    end
    puts "USAGE: ruby #{__FILE__} [<help>|<host>] [<port>]\n" \
         "  <help> - on of these: #{HELP_KEYS}\n\n" \
         "If no arguments are given, UDP discovery is performed.\n" \
         'Otherwise, TCP client is launched (unless help command entered).'
    exit
  end

  if @argv.empty?
    puts "No arguments given - performing UDP discovery ...\n" \
         "(For usage message, type one of these: #{HELP_KEYS})\n\n"
    client = MaxCube::Network::UDP::Client.new
    client.discovery
    client.close
    exit
  end

  client = MaxCube::Network::TCP::Client.new
  client.connect(*@argv)
end