Class: ZooKeeper::EventMachine::Binding

Inherits:
Object
  • Object
show all
Includes:
Slf4r::Logger
Defined in:
lib/zkruby/eventmachine.rb

Overview

The EventMachine binding is very simple because there is only one thread! and we have good stuff like timers provided for us

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



95
96
97
# File 'lib/zkruby/eventmachine.rb', line 95

def client
  @client
end

#sessionObject (readonly)

Returns the value of attribute session.



95
96
97
# File 'lib/zkruby/eventmachine.rb', line 95

def session
  @session
end

Class Method Details

.available?Boolean

We can use this binding if we are running in the reactor thread

Returns:

  • (Boolean)


84
85
86
# File 'lib/zkruby/eventmachine.rb', line 84

def self.available?()
    EM.reactor_running? && EM.reactor_thread?
end

.context(&context_block) ⇒ Object



88
89
90
91
92
93
# File 'lib/zkruby/eventmachine.rb', line 88

def self.context(&context_block)
    s = Strand.new() do
        context_block.call(Strand)
    end
    s.join
end

Instance Method Details

#close(&callback) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/zkruby/eventmachine.rb', line 126

def close(&callback)

    op = AsyncOp.new(self,&callback)

    begin
        @session.close() do |error,response|
            op.resume(error,response) 
        end
    rescue ZooKeeper::Error => ex
        op.resume(ex,nil)
    end

    op
end

#connect(host, port, delay, timeout) ⇒ Object



102
103
104
105
106
# File 'lib/zkruby/eventmachine.rb', line 102

def connect(host,port,delay,timeout)
    EM.add_timer(delay) do
        EM.connect(host,port,ZooKeeper::EventMachine::ClientConn,@session,timeout)
    end
end

#invoke(callback, *args) ⇒ Object

You are working in event machine it is up to you to ensure your callbacks do not block



109
110
111
# File 'lib/zkruby/eventmachine.rb', line 109

def invoke(callback,*args)
    callback.call(*args)
end

#queue_request(*args, &callback) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/zkruby/eventmachine.rb', line 113

def queue_request(*args,&callback)
    op = AsyncOp.new(self,&callback)
    begin
        @session.queue_request(*args) do |error,response|
            op.resume(error,response)
        end
    rescue ZooKeeper::Error => ex
        op.resume(ex,nil)
    end

    op
end

#start(client, session) ⇒ Object



96
97
98
99
100
# File 'lib/zkruby/eventmachine.rb', line 96

def start(client,session)
    @client = client
    @session = session
    @session.start()
end