Class: Thrift::EventMachineTransport
- Inherits:
-
BaseTransport
- Object
- BaseTransport
- Thrift::EventMachineTransport
- Defined in:
- lib/thrift_client/thrift/transport.rb
Instance Method Summary collapse
- #close ⇒ Object
-
#initialize(host, port = 9090, timeout = nil) ⇒ EventMachineTransport
constructor
A new instance of EventMachineTransport.
- #open ⇒ Object
- #open? ⇒ Boolean
- #read(sz) ⇒ Object
- #write(buf) ⇒ Object
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
#close ⇒ Object
46 47 48 |
# File 'lib/thrift_client/thrift/transport.rb', line 46 def close @connection.close if @connection && @connection.connected? end |
#open ⇒ Object
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
14 15 16 |
# File 'lib/thrift_client/thrift/transport.rb', line 14 def open? @connection && @connection.connected? end |
#read(sz) ⇒ Object
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
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 |