Class: EventMachine::Twitter::Connection

Inherits:
EM::Connection
  • Object
show all
Defined in:
lib/em-twitter/connection.rb

Constant Summary collapse

STALL_TIMEOUT =
90
STALL_TIMER =
10

Instance Attribute Summary collapse

Instance Method Summary collapse

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.options
  @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

#clientObject (readonly)

Returns the value of attribute client.



23
24
25
# File 'lib/em-twitter/connection.rb', line 23

def client
  @client
end

#headersObject (readonly)

Returns the value of attribute headers.



23
24
25
# File 'lib/em-twitter/connection.rb', line 23

def headers
  @headers
end

#hostObject (readonly)

Returns the value of attribute host.



23
24
25
# File 'lib/em-twitter/connection.rb', line 23

def host
  @host
end

#optionsObject (readonly)

Returns the value of attribute options.



23
24
25
# File 'lib/em-twitter/connection.rb', line 23

def options
  @options
end

#portObject (readonly)

Returns the value of attribute port.



23
24
25
# File 'lib/em-twitter/connection.rb', line 23

def port
  @port
end

#reconnectorObject

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.

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


117
118
119
# File 'lib/em-twitter/connection.rb', line 117

def auto_reconnect_on_close?
  auto_reconnect? && !gracefully_closed?
end

#connection_completedObject

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

Returns:

  • (Boolean)


105
106
107
# File 'lib/em-twitter/connection.rb', line 105

def gracefully_closed?
  @gracefully_closed
end

#immediate_reconnectObject

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

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


98
99
100
# File 'lib/em-twitter/connection.rb', line 98

def network_failure?
  @response_code == 0
end

#post_initObject



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

Returns:

  • (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

#stopObject

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

#unbindObject

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={})
  @options.merge!(options)
  immediate_reconnect
end