Class: RBZK::ZKHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/rbzk/zk.rb

Overview

Helper class for ZK

Instance Method Summary collapse

Constructor Details

#initialize(ip, port = 4370) ⇒ ZKHelper

Returns a new instance of ZKHelper.



10
11
12
13
14
# File 'lib/rbzk/zk.rb', line 10

def initialize(ip, port = 4370)
  @ip = ip
  @port = port
  @address = [ ip, port ]
end

Instance Method Details

#test_pingObject



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/rbzk/zk.rb', line 16

def test_ping
  Timeout.timeout(5) do
    s = TCPSocket.new(@ip, @port)
    s.close
    return true
  end
rescue Timeout::Error, Errno::ECONNREFUSED, Errno::EHOSTUNREACH, SocketError
  false
rescue StandardError
  false
end

#test_tcpObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rbzk/zk.rb', line 28

def test_tcp
  client = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM)
  client.setsockopt(Socket::SOL_SOCKET, Socket::SO_RCVTIMEO, [ 10, 0 ].pack('l_*'))

  sockaddr = Socket.pack_sockaddr_in(@port, @ip)
  begin
    client.connect(sockaddr)
    result = 0 # Success code
  rescue Errno::EISCONN
    result = 0 # Already connected
  rescue StandardError => e
    # Some exceptions (e.g., Socket::ResolutionError) don't provide errno
    result = e.respond_to?(:errno) ? e.errno : 1 # Connection failed
  end

  client.close
  result
rescue StandardError => e
  # Some exceptions (e.g., Socket::ResolutionError) don't provide errno
  e.respond_to?(:errno) ? e.errno : 1
end