| Module | Thrift::EventMachineConnection |
| In: |
lib/thrift_client/event_machine.rb
lib/thrift_client/event_machine.rb |
| GARBAGE_BUFFER_SIZE | = | 4096 |
| GARBAGE_BUFFER_SIZE | = | 4096 |
# File lib/thrift_client/event_machine.rb, line 52
52: def self.connect(host='localhost', port=9090, timeout=5, &block)
53: EM.connect(host, port, self, host, port) do |conn|
54: conn.pending_connect_timeout = timeout
55: end
56: end
# File lib/thrift_client/event_machine.rb, line 52
52: def self.connect(host='localhost', port=9090, timeout=5, &block)
53: EM.connect(host, port, self, host, port) do |conn|
54: conn.pending_connect_timeout = timeout
55: end
56: end
# File lib/thrift_client/event_machine.rb, line 67
67: def initialize(host, port=9090)
68: @host, @port = host, port
69: @index = 0
70: @disconnected = 'not connected'
71: @buf = ''
72: end
# File lib/thrift_client/event_machine.rb, line 67
67: def initialize(host, port=9090)
68: @host, @port = host, port
69: @index = 0
70: @disconnected = 'not connected'
71: @buf = ''
72: end
# File lib/thrift_client/event_machine.rb, line 81
81: def blocking_read(size)
82: raise IOError, "lost connection to #{@host}:#{@port}: #{@disconnected}" if @disconnected
83: if can_read?(size)
84: yank(size)
85: else
86: raise ArgumentError, "Unexpected state" if @size or @callback
87:
88: fiber = Fiber.current
89: @size = size
90: @callback = proc { |data|
91: fiber.resume(data)
92: }
93: Fiber.yield
94: end
95: end
# File lib/thrift_client/event_machine.rb, line 81
81: def blocking_read(size)
82: raise IOError, "lost connection to #{@host}:#{@port}: #{@disconnected}" if @disconnected
83: if can_read?(size)
84: yank(size)
85: else
86: raise ArgumentError, "Unexpected state" if @size or @callback
87:
88: fiber = Fiber.current
89: @size = size
90: @callback = proc { |data|
91: fiber.resume(data)
92: }
93: Fiber.yield
94: end
95: end
# File lib/thrift_client/event_machine.rb, line 127
127: def can_read?(size)
128: @buf.size >= @index + size
129: end
# File lib/thrift_client/event_machine.rb, line 127
127: def can_read?(size)
128: @buf.size >= @index + size
129: end
# File lib/thrift_client/event_machine.rb, line 74
74: def close
75: trap do
76: @disconnected = 'closed'
77: close_connection(true)
78: end
79: end
# File lib/thrift_client/event_machine.rb, line 74
74: def close
75: trap do
76: @disconnected = 'closed'
77: close_connection(true)
78: end
79: end
# File lib/thrift_client/event_machine.rb, line 110
110: def connected?
111: !@disconnected
112: end
# File lib/thrift_client/event_machine.rb, line 110
110: def connected?
111: !@disconnected
112: end
# File lib/thrift_client/event_machine.rb, line 114
114: def connection_completed
115: @disconnected = nil
116: succeed
117: end
# File lib/thrift_client/event_machine.rb, line 114
114: def connection_completed
115: @disconnected = nil
116: succeed
117: end
# File lib/thrift_client/event_machine.rb, line 97
97: def receive_data(data)
98: trap do
99: (@buf) << data
100:
101: if @callback and can_read?(@size)
102: callback = @callback
103: data = yank(@size)
104: @callback = @size = nil
105: callback.call(data)
106: end
107: end
108: end
# File lib/thrift_client/event_machine.rb, line 97
97: def receive_data(data)
98: trap do
99: (@buf) << data
100:
101: if @callback and can_read?(@size)
102: callback = @callback
103: data = yank(@size)
104: @callback = @size = nil
105: callback.call(data)
106: end
107: end
108: end
# File lib/thrift_client/event_machine.rb, line 58
58: def trap
59: begin
60: yield
61: rescue Exception => ex
62: puts ex.message
63: puts ex.backtrace.join("\n")
64: end
65: end
# File lib/thrift_client/event_machine.rb, line 58
58: def trap
59: begin
60: yield
61: rescue Exception => ex
62: puts ex.message
63: puts ex.backtrace.join("\n")
64: end
65: end