Class: Thrift::EventMachineTransport

Inherits:
BaseTransport
  • Object
show all
Defined in:
lib/thrift_client/thrift/transport.rb

Instance Method Summary collapse

Constructor Details

#initialize(host, port = 9090, timeout = nil) ⇒ EventMachineTransport

Returns a new instance of EventMachineTransport.



9
10
11
12
# File 'lib/thrift_client/thrift/transport.rb', line 9

def initialize(host, port=9090, timeout=nil)
  @host, @port, @timeout = host, port, timeout
  @connection = nil
end

Instance Method Details

#closeObject



46
47
48
# File 'lib/thrift_client/thrift/transport.rb', line 46

def close
  @connection.close if @connection && @connection.connected?
end

#openObject

Raises:



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/thrift_client/thrift/transport.rb', line 18

def open
  @connection = EventMachine.connect(@host, @port, EventMachineConnection) do |conn|
    conn.pending_connect_timeout = @timeout
  end

  fiber = Fiber.current

  @connection.callback {  |arg|
    if fiber == Fiber.current
      return arg
    else
      fiber.resume(arg)
    end
  }
  @connection.errback {  |arg|
    if fiber == Fiber.current
      return arg
    else
      fiber.resume(arg)
    end
  }

  Fiber.yield

  raise TransportException, TransportException::NOT_OPEN, "Unable to connect to #{@host}:#{@port}" unless @connection.connected?
  @connection
end

#open?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/thrift_client/thrift/transport.rb', line 14

def open?
  @connection && @connection.connected?
end

#read(sz) ⇒ Object

Raises:

  • (IOError)


50
51
52
53
# File 'lib/thrift_client/thrift/transport.rb', line 50

def read(sz)
  raise IOError, "read failed, closed stream." unless open?
  @connection.read(sz,@timeout)
end

#write(buf) ⇒ Object

Raises:

  • (IOError)


55
56
57
58
# File 'lib/thrift_client/thrift/transport.rb', line 55

def write(buf)
  raise IOError, "write failed, closed stream." unless open?
  @connection.write(buf,@timeout)
end