Class: RBZK::ZKHelper
- Inherits:
-
Object
- Object
- RBZK::ZKHelper
- Defined in:
- lib/rbzk/zk.rb
Overview
Helper class for ZK
Instance Method Summary collapse
-
#initialize(ip, port = 4370) ⇒ ZKHelper
constructor
A new instance of ZKHelper.
- #test_ping ⇒ Object
- #test_tcp ⇒ Object
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_ping ⇒ Object
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_tcp ⇒ Object
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 |