Class: Rubarb::Connection
- Inherits:
-
Object
- Object
- Rubarb::Connection
show all
- Defined in:
- lib/rubarb/connection.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(host, port, api, insecure_methods = Default::INSECURE_METHODS, keep_alive_time = 0) ⇒ Connection
Returns a new instance of Connection.
112
113
114
115
116
117
118
119
120
121
|
# File 'lib/rubarb/connection.rb', line 112
def initialize(host, port, api, insecure_methods=Default::INSECURE_METHODS, keep_alive_time = 0)
@host = host
@port = port
@api = api
@msg_id_generator = Id.new
@errbacks = []
@insecure_methods = insecure_methods
@connections = []
@keep_alive_time = keep_alive_time
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
184
185
186
187
188
189
190
191
192
|
# File 'lib/rubarb/connection.rb', line 184
def method_missing(method, * args, & block)
EventMachine::schedule do
begin
@outgoing_connection.remote_call(method, args, & block)
rescue Exception => e
call_errbacks(e)
end
end
end
|
Instance Attribute Details
#msg_id_generator ⇒ Object
Returns the value of attribute msg_id_generator.
106
107
108
|
# File 'lib/rubarb/connection.rb', line 106
def msg_id_generator
@msg_id_generator
end
|
Instance Method Details
#call_errbacks(message) ⇒ Object
152
153
154
155
156
|
# File 'lib/rubarb/connection.rb', line 152
def call_errbacks(message)
@errbacks.each do |e|
e.call(message)
end
end
|
#close_connections ⇒ Object
133
134
135
136
137
|
# File 'lib/rubarb/connection.rb', line 133
def close_connections
EM.next_tick do
@connections.each { |conn| conn.close_connection_after_writing }
end
end
|
#connection_closed(connection) ⇒ Object
139
140
141
142
143
144
145
146
|
# File 'lib/rubarb/connection.rb', line 139
def connection_closed(connection)
@connections.delete(connection)
if !@connections.empty?
close_connections
else
call_errbacks(ConnectionError.new)
end
end
|
#connection_completed(connection) ⇒ Object
148
149
150
|
# File 'lib/rubarb/connection.rb', line 148
def connection_completed(connection)
@connections << connection
end
|
#errback(&block) ⇒ Object
158
159
160
|
# File 'lib/rubarb/connection.rb', line 158
def errback & block
@errbacks << block if block
end
|
#keep_alive_time=(keep_alive_time_seconds) ⇒ Object
123
124
125
126
127
128
129
130
131
|
# File 'lib/rubarb/connection.rb', line 123
def keep_alive_time=(keep_alive_time_seconds)
@keep_alive_time = keep_alive_time_seconds
if @outgoing_connection
EventMachine::schedule do
@outgoing_connection.keep_alive_time = @keep_alive_time
@outgoing_connection.reset_keep_alive
end
end
end
|
#remote_connection ⇒ Object
108
109
110
|
# File 'lib/rubarb/connection.rb', line 108
def remote_connection
@outgoing_connection
end
|
#start(&block) ⇒ Object
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
# File 'lib/rubarb/connection.rb', line 162
def start & block
EventMachine::schedule do
begin
EventMachine::connect(@host, @port, OutgoingHandler) do |connection|
connection.host = @host
connection.port = @port
connection.on_connection = block
connection.api = @api
connection.msg_id_generator = @msg_id_generator
connection.insecure_methods = @insecure_methods
connection.parent = self
connection.keep_alive_time = @keep_alive_time
@outgoing_connection = connection
end
rescue Exception => e
@errbacks.each do |errback|
errback.call(e)
end
end
end
end
|
#stop(&callback) ⇒ Object
194
195
196
197
198
199
200
201
202
203
204
205
|
# File 'lib/rubarb/connection.rb', line 194
def stop(& callback)
EventMachine::schedule do
if @outgoing_connection
EventMachine::next_tick do
@outgoing_connection.close_connection_after_writing
callback.call(true) if callback
end
else
callback.call(false) if callback
end
end
end
|