Class: EventMachine::Twitter::Connection
- Inherits:
-
EM::Connection
- Object
- EM::Connection
- EventMachine::Twitter::Connection
- Defined in:
- lib/em-twitter/connection.rb
Constant Summary collapse
- STALL_TIMEOUT =
90
- STALL_TIMER =
10
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
-
#reconnector ⇒ Object
Returns the value of attribute reconnector.
Instance Method Summary collapse
-
#auto_reconnect? ⇒ Boolean
Returns the current state of the auto_reconnect flag.
-
#auto_reconnect_on_close? ⇒ Boolean
Determines if the connection should reconnect if the connection closes.
-
#connection_completed ⇒ Object
Called after the connection to the server is completed.
-
#gracefully_closed? ⇒ Boolean
Returns the current state of the gracefully_closed flag gracefully_closed is set to true when the connection is explicitly stopped using the stop method.
-
#immediate_reconnect ⇒ Object
Immediately reconnects the connection.
-
#immediate_reconnect? ⇒ Boolean
Returns the current state of the immediate_reconnect flag immediate_reconnect is true when the immediate_reconnect method is invoked on the connection.
-
#initialize(client, host, port) ⇒ Connection
constructor
A new instance of Connection.
-
#network_failure? ⇒ Boolean
Returns a status of the connection, if no response was ever received from the server, then we assume a network failure.
- #post_init ⇒ Object
-
#receive_data(data) ⇒ Object
Receives responses from the server and passes them on to the HttpParser.
- #stalled? ⇒ Boolean
-
#stop ⇒ Object
Close the connection gracefully, without reconnecting.
-
#unbind ⇒ Object
Called when a connection is disconnected.
- #update(options = {}) ⇒ Object
Constructor Details
#initialize(client, host, port) ⇒ Connection
Returns a new instance of Connection.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/em-twitter/connection.rb', line 26 def initialize(client, host, port) @client = client @host = host @port = port @options = @client. @on_inited_callback = @options.delete(:on_inited) if verify_peer? @certificate_store = OpenSSL::X509::Store.new @certificate_store.add_file(@options[:ssl][:cert_chain_file]) end @network_reconnector = EM::Twitter::Reconnectors::NetworkFailure.new @application_reconnector = EM::Twitter::Reconnectors::ApplicationFailure.new end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
23 24 25 |
# File 'lib/em-twitter/connection.rb', line 23 def client @client end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
23 24 25 |
# File 'lib/em-twitter/connection.rb', line 23 def headers @headers end |
#host ⇒ Object (readonly)
Returns the value of attribute host.
23 24 25 |
# File 'lib/em-twitter/connection.rb', line 23 def host @host end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
23 24 25 |
# File 'lib/em-twitter/connection.rb', line 23 def @options end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
23 24 25 |
# File 'lib/em-twitter/connection.rb', line 23 def port @port end |
#reconnector ⇒ Object
Returns the value of attribute reconnector.
24 25 26 |
# File 'lib/em-twitter/connection.rb', line 24 def reconnector @reconnector end |
Instance Method Details
#auto_reconnect? ⇒ Boolean
Returns the current state of the auto_reconnect flag.
122 123 124 |
# File 'lib/em-twitter/connection.rb', line 122 def auto_reconnect? @auto_reconnect end |
#auto_reconnect_on_close? ⇒ Boolean
Determines if the connection should reconnect if the connection closes
117 118 119 |
# File 'lib/em-twitter/connection.rb', line 117 def auto_reconnect_on_close? auto_reconnect? && !gracefully_closed? end |
#connection_completed ⇒ Object
Called after the connection to the server is completed. Initiates a
44 45 46 47 48 49 50 51 |
# File 'lib/em-twitter/connection.rb', line 44 def connection_completed start_tls(@options[:ssl]) if ssl? reset_connection @request = Request.new(@options) send_data(@request) end |
#gracefully_closed? ⇒ Boolean
Returns the current state of the gracefully_closed flag gracefully_closed is set to true when the connection is explicitly stopped using the stop method
105 106 107 |
# File 'lib/em-twitter/connection.rb', line 105 def gracefully_closed? @gracefully_closed end |
#immediate_reconnect ⇒ Object
Immediately reconnects the connection
84 85 86 87 88 |
# File 'lib/em-twitter/connection.rb', line 84 def immediate_reconnect @immediate_reconnect = true @gracefully_closed = false close_connection end |
#immediate_reconnect? ⇒ Boolean
Returns the current state of the immediate_reconnect flag immediate_reconnect is true when the immediate_reconnect method is invoked on the connection
112 113 114 |
# File 'lib/em-twitter/connection.rb', line 112 def immediate_reconnect? @immediate_reconnect end |
#network_failure? ⇒ Boolean
Returns a status of the connection, if no response was ever received from the server, then we assume a network failure.
98 99 100 |
# File 'lib/em-twitter/connection.rb', line 98 def network_failure? @response_code == 0 end |
#post_init ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/em-twitter/connection.rb', line 53 def post_init @headers = {} @reconnector = @network_reconnector @stall_timer = EM::PeriodicTimer.new(STALL_TIMER) do if gracefully_closed? @stall_timer.cancel elsif stalled? close_connection invoke_callback(@client.no_data_callback) end end invoke_callback(@on_inited_callback) set_comm_inactivity_timeout(@options[:timeout]) if @options[:timeout] > 0 end |
#receive_data(data) ⇒ Object
Receives responses from the server and passes them on to the HttpParser
72 73 74 |
# File 'lib/em-twitter/connection.rb', line 72 def receive_data(data) @parser << data end |
#stalled? ⇒ Boolean
126 127 128 129 |
# File 'lib/em-twitter/connection.rb', line 126 def stalled? @last_response ||= Response.new @last_response.older_than?(STALL_TIMEOUT) end |
#stop ⇒ Object
Close the connection gracefully, without reconnecting
77 78 79 80 81 |
# File 'lib/em-twitter/connection.rb', line 77 def stop @auto_reconnect = false @gracefully_closed = true close_connection end |
#unbind ⇒ Object
Called when a connection is disconnected
91 92 93 94 |
# File 'lib/em-twitter/connection.rb', line 91 def unbind schedule_reconnect if auto_reconnect_on_close? invoke_callback(@client.close_callback) end |
#update(options = {}) ⇒ Object
131 132 133 134 |
# File 'lib/em-twitter/connection.rb', line 131 def update(={}) @options.merge!() immediate_reconnect end |