Class: RR::ProxyRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/rubyrep/proxy_runner.rb

Overview

This class implements the functionality of the rrproxy.rb command.

Constant Summary collapse

DEFAULT_OPTIONS =

Default options to start a DatabaseProxy server

{
  :port => DatabaseProxy::DEFAULT_PORT,
  :host => ''
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.run(args) ⇒ Object

Runs the ProxyRunner (processing of command line & starting of server) args: the array of command line options with which to start the server



75
76
77
78
79
80
81
82
83
84
# File 'lib/rubyrep/proxy_runner.rb', line 75

def self.run(args)
  runner = ProxyRunner.new
  
  options, status = runner.get_options(args)
  if options
    url = runner.build_url(options)
    runner.start_server(url)
  end
  status
end

Instance Method Details

#build_url(options) ⇒ Object

Builds the druby URL from the given options and returns it



61
62
63
# File 'lib/rubyrep/proxy_runner.rb', line 61

def build_url(options)
  "druby://#{options[:host]}:#{options[:port]}"
end

#get_options(args) ⇒ Object

Parses the given command line parameter array. Returns

* the options hash or nil if command line parsing failed
* status (as per UNIX conventions: 1 if parameters were invalid, 0 otherwise)


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/rubyrep/proxy_runner.rb', line 25

def get_options(args)
  options = DEFAULT_OPTIONS
  status = 0

  parser = OptionParser.new do |opts|
    opts.banner = "Usage: #{$0} proxy [options]"
    opts.separator ""
    opts.separator "Specific options:"

    opts.on("-h","--host", "=IP_ADDRESS", "IP address to listen on. Default: binds to all IP addresses of the computer") do |arg|
      options[:host] = arg
    end

    opts.on("-p","--port", "=PORT_NUMBER", Integer, "TCP port to listen on. Default port: #{DatabaseProxy::DEFAULT_PORT}") do |arg|
      options[:port] = arg
    end

    opts.on_tail("--help", "Show this message") do
      $stderr.puts opts
      options = nil
    end
  end

  begin
    parser.parse!(args)
  rescue Exception => e
    $stderr.puts "Command line parsing failed: #{e}"
    $stderr.puts parser.help
    options = nil
    status = 1
  end
  
  return options, status
end

#start_server(url) ⇒ Object

Starts a proxy server under the given druby URL



66
67
68
69
70
71
# File 'lib/rubyrep/proxy_runner.rb', line 66

def start_server(url)
  proxy = DatabaseProxy.new

  DRb.start_service(url, proxy)
  DRb.thread.join
end