Class: Ddslbg::Client
- Inherits:
-
Object
- Object
- Ddslbg::Client
- Defined in:
- lib/ddslbg/client.rb
Constant Summary collapse
- BUNDLED_JAR_PATH =
File.( '../../../vendor/bin/ddsl-cmdline-tool_2.10-0.3.5-SNAPSHOT-one-jar.jar', __FILE__)
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #available_services ⇒ Object
- #best_service_location(service_request) ⇒ Object
- #connect! ⇒ Object
- #connected? ⇒ Boolean
- #disconnect! ⇒ Object
-
#do_send(cmd) ⇒ Object
Sends a raw command to DDSL and returns the response as a unprocessed string.
- #down(service) ⇒ Object
- #fallback_urls=(fallback_urls) ⇒ Object
-
#initialize(options = {}) ⇒ Client
constructor
A new instance of Client.
-
#send(msg, data = {}, options = {}) ⇒ Object
‘data` can be a Ruby array or hash.
- #service_locations(service_request) ⇒ Object
- #up(service) ⇒ Object
- #zookeeper_hosts=(hosts = []) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Client
Returns a new instance of Client.
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/ddslbg/client.rb', line 14 def initialize(={}) default_opts = { java_cmd: 'java -jar', jar_path: BUNDLED_JAR_PATH, auto_connect: false } @options = default_opts.merge() connect! if @options[:auto_connect] end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
12 13 14 |
# File 'lib/ddslbg/client.rb', line 12 def @options end |
Instance Method Details
#available_services ⇒ Object
25 26 27 |
# File 'lib/ddslbg/client.rb', line 25 def available_services send('getAllAvailableServices') end |
#best_service_location(service_request) ⇒ Object
37 38 39 |
# File 'lib/ddslbg/client.rb', line 37 def best_service_location(service_request) send('getBestServiceLocation', service_request) end |
#connect! ⇒ Object
71 72 73 74 75 76 |
# File 'lib/ddslbg/client.rb', line 71 def connect! cmd = [[:java_cmd], [:jar_path]].join(' ') @stdin, @stdout, @wait_thr = Open3.popen2(cmd) process_line(@stdout.gets).include?('ddsl-cmdline-tool started') end |
#connected? ⇒ Boolean
86 87 88 |
# File 'lib/ddslbg/client.rb', line 86 def connected? !!@wait_thr && @wait_thr.alive? end |
#disconnect! ⇒ Object
78 79 80 81 82 83 84 |
# File 'lib/ddslbg/client.rb', line 78 def disconnect! return false unless connected? @stdin.close unless @stdin.closed? @stdout.close unless @stdout.closed? Process.kill(:INT, @wait_thr.pid) true end |
#do_send(cmd) ⇒ Object
Sends a raw command to DDSL and returns the response as a unprocessed string.
65 66 67 68 69 |
# File 'lib/ddslbg/client.rb', line 65 def do_send(cmd) connect! unless connected? @stdin.puts(cmd) @stdout.gets end |
#down(service) ⇒ Object
33 34 35 |
# File 'lib/ddslbg/client.rb', line 33 def down(service) send('serviceDown', service) end |
#fallback_urls=(fallback_urls) ⇒ Object
45 46 47 |
# File 'lib/ddslbg/client.rb', line 45 def fallback_urls=(fallback_urls) send('setFallbackUrlsMap', fallback_urls, parse: false) end |
#send(msg, data = {}, options = {}) ⇒ Object
‘data` can be a Ruby array or hash. It is converted to JSON before it is sent to DDSL.
55 56 57 58 59 60 61 62 |
# File 'lib/ddslbg/client.rb', line 55 def send(msg, data={}, ={}) default_opts = { parse: true } = default_opts.merge() msg += " #{data.to_json}" unless !data || data.empty? res = process_line(do_send(msg)) res = parse(res) if [:parse] res end |
#service_locations(service_request) ⇒ Object
41 42 43 |
# File 'lib/ddslbg/client.rb', line 41 def service_locations(service_request) send('getServiceLocations', service_request) end |
#up(service) ⇒ Object
29 30 31 |
# File 'lib/ddslbg/client.rb', line 29 def up(service) send('serviceUp', service) end |
#zookeeper_hosts=(hosts = []) ⇒ Object
49 50 51 |
# File 'lib/ddslbg/client.rb', line 49 def zookeeper_hosts=(hosts=[]) send('setZookeeperHosts', hosts, parse: false) end |