Class: Ddslbg::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/ddslbg/client.rb

Constant Summary collapse

BUNDLED_JAR_PATH =
File.expand_path(
'../../../vendor/bin/ddsl-cmdline-tool_2.10-0.3.5-SNAPSHOT-one-jar.jar',
__FILE__)

Instance Attribute Summary collapse

Instance Method Summary collapse

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(options={})
  default_opts = {
    java_cmd: 'java -jar',
    jar_path: BUNDLED_JAR_PATH,
    auto_connect: false
  }
  @options = default_opts.merge(options)

  connect! if @options[:auto_connect]
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



12
13
14
# File 'lib/ddslbg/client.rb', line 12

def options
  @options
end

Instance Method Details

#available_servicesObject



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 = [options[:java_cmd], options[:jar_path]].join(' ')
  @stdin, @stdout, @wait_thr = Open3.popen2(cmd)

  process_line(@stdout.gets).include?('ddsl-cmdline-tool started')
end

#connected?Boolean

Returns:

  • (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={}, options={})
  default_opts = { parse: true }
  options = default_opts.merge(options)
  msg += " #{data.to_json}" unless !data || data.empty?
  res = process_line(do_send(msg))
  res = parse(res) if options[: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