Class: MultiClientServer

Inherits:
AbstractServer show all
Defined in:
lib/thrift_client/multi_client_server.rb

Overview

multi client connection thrift server encapsulation

Instance Attribute Summary

Attributes inherited from AbstractServer

#client, #host, #port

Instance Method Summary collapse

Methods inherited from AbstractServer

#disconnect, #to_s

Constructor Details

#initialize(connect_string, options = {}) ⇒ MultiClientServer

Returns a new instance of MultiClientServer.



8
9
10
11
12
13
14
# File 'lib/thrift_client/multi_client_server.rb', line 8

def initialize(connect_string, options = {})
  super
  @pool_timeout = @options[:pool_timeout]
  @pool = Array.new(@options[:size]) { client = create_client }
  @mutex = Mutex.new
  @resource = ConditionVariable.new
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


16
17
18
19
20
# File 'lib/thrift_client/multi_client_server.rb', line 16

def active?
  active = false
  @pool.each {|client| active = client.open?; break if active}
  return active
end

#destroyObject



22
23
24
# File 'lib/thrift_client/multi_client_server.rb', line 22

def destroy
  @pool.each {|client| client.close if client}
end

#get_clientObject



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/thrift_client/multi_client_server.rb', line 26

def get_client
  client = pop
  if @test_on_borrow
    if check_client(client)
      return client
    else
      return nil
    end
  else
    client
  end
end

#return_client(client) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/thrift_client/multi_client_server.rb', line 39

def return_client(client)
  client = create_client unless client.open?
  @mutex.synchronize do
    @pool.push client
    @resource.broadcast
  end
end