Module: ZK::Client::StateMixin

Included in:
Threaded
Defined in:
lib/zk/client/state_mixin.rb

Overview

Provides client-state related methods. Included in ZK::Client::Base. (refactored out to this class to ease documentation overload)

Instance Method Summary collapse

Instance Method Details

#associating?bool

is the underlying connection is in the +associating+ state?

Returns:

  • (bool)


13
14
15
# File 'lib/zk/client/state_mixin.rb', line 13

def associating?
  wrap_state_closed_error { cnx and cnx.associating? }
end

#connected?Boolean

Returns true if the underlying connection is in the +connected+ state.

Returns:

  • (Boolean)


7
8
9
# File 'lib/zk/client/state_mixin.rb', line 7

def connected?
  wrap_state_closed_error { cnx and cnx.connected? }
end

#connecting?bool

is the underlying connection is in the +connecting+ state?

Returns:

  • (bool)


19
20
21
# File 'lib/zk/client/state_mixin.rb', line 19

def connecting?
  wrap_state_closed_error { cnx and cnx.connecting? }
end

#expired_session?bool

is the underlying connection is in the +expired_session+ state?

Returns:

  • (bool)


25
26
27
28
29
30
31
32
33
# File 'lib/zk/client/state_mixin.rb', line 25

def expired_session?
  return nil unless @cnx

  if defined?(::JRUBY_VERSION)
    cnx.state == Java::OrgApacheZookeeper::ZooKeeper::States::EXPIRED_SESSION
  else
    wrap_state_closed_error { cnx.state == Zookeeper::ZOO_EXPIRED_SESSION_STATE }
  end
end

#on_connected(&block) ⇒ Object

Register a block to be called on connection, when the client has connected.

the block will be called with no arguments

returns an EventHandlerSubscription object that can be used to unregister this block from further updates



67
68
69
# File 'lib/zk/client/state_mixin.rb', line 67

def on_connected(&block)
  watcher.register_state_handler(:connected, &block)
end

#on_connecting(&block) ⇒ Object

register a block to be called when the client is attempting to reconnect to the zookeeper server. the documentation says that this state should be taken to mean that the application should enter into "safe mode" and operate conservatively, as it won't be getting updates until it has reconnected



76
77
78
# File 'lib/zk/client/state_mixin.rb', line 76

def on_connecting(&block)
  watcher.register_state_handler(:connecting, &block)
end

#on_expired_session(&block) ⇒ Object

TODO:

need to come up with a way to test this

register a block to be called when our session has expired. This usually happens due to a network partitioning event, and means that all callbacks and watches must be re-registered with the server



85
86
87
# File 'lib/zk/client/state_mixin.rb', line 85

def on_expired_session(&block)
  watcher.register_state_handler(:expired_session, &block)
end

#on_state_change {|event| ... } ⇒ Object

Register a block to be called when any connection event occurs

Yields:

  • (event)

    yields the connection event to the block

Yield Parameters:

  • event (ZK::Event)

    the event that occurred



55
56
57
# File 'lib/zk/client/state_mixin.rb', line 55

def on_state_change(&block)
  watcher.register_state_handler(:all, &block)
end

#stateObject

returns the current state of the connection as reported by the underlying driver as a symbol. The possible values are [:closed, :expired_session, :auth_failed :connecting, :connected, :associating].

See the Zookeeper session documentation[http://hadoop.apache.org/zookeeper/docs/current/zookeeperProgrammers.html#ch_zkSessions] for more information



43
44
45
46
47
48
49
# File 'lib/zk/client/state_mixin.rb', line 43

def state
  if defined?(::JRUBY_VERSION) 
    cnx.state.to_string.downcase.to_sym
  else
    STATE_SYM_MAP.fetch(cnx.state) { |k| raise IndexError, "unrecognized state: #{k}" }
  end
end