Class: EventMachine::Twitter::Connection

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

Constant Summary collapse

MAX_LINE_LENGTH =
1024*1024
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.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/em-twitter/connection.rb', line 27

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.



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

def client
  @client
end

#headersObject (readonly)

Returns the value of attribute headers.



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

def headers
  @headers
end

#hostObject (readonly)

Returns the value of attribute host.



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

def host
  @host
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

#portObject (readonly)

Returns the value of attribute port.



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

def port
  @port
end

#reconnectorObject

Returns the value of attribute reconnector.



25
26
27
# File 'lib/em-twitter/connection.rb', line 25

def reconnector
  @reconnector
end

Instance Method Details

#auto_reconnect?Boolean

Returns the current state of the auto_reconnect flag.

Returns:

  • (Boolean)


123
124
125
# File 'lib/em-twitter/connection.rb', line 123

def auto_reconnect?
  @auto_reconnect
end

#auto_reconnect_on_close?Boolean

Determines if the connection should reconnect if the connection closes

Returns:

  • (Boolean)


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

def auto_reconnect_on_close?
  auto_reconnect? && !gracefully_closed?
end

#connection_completedObject

Called after the connection to the server is completed. Initiates a



45
46
47
48
49
50
51
52
# File 'lib/em-twitter/connection.rb', line 45

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)


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

def gracefully_closed?
  @gracefully_closed
end

#immediate_reconnectObject

Immediately reconnects the connection



85
86
87
88
89
# File 'lib/em-twitter/connection.rb', line 85

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)


113
114
115
# File 'lib/em-twitter/connection.rb', line 113

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)


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

def network_failure?
  @response_code == 0
end

#post_initObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/em-twitter/connection.rb', line 54

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



73
74
75
# File 'lib/em-twitter/connection.rb', line 73

def receive_data(data)
  @parser << data
end

#stalled?Boolean

Returns:

  • (Boolean)


127
128
129
130
# File 'lib/em-twitter/connection.rb', line 127

def stalled?
  @last_response ||= Response.new
  @last_response.older_than?(STALL_TIMEOUT)
end

#stopObject

Close the connection gracefully, without reconnecting



78
79
80
81
82
# File 'lib/em-twitter/connection.rb', line 78

def stop
  @auto_reconnect     = false
  @gracefully_closed  = true
  close_connection
end

#unbindObject

Called when a connection is disconnected



92
93
94
95
# File 'lib/em-twitter/connection.rb', line 92

def unbind
  schedule_reconnect if auto_reconnect_on_close?
  invoke_callback(@client.close_callback)
end

#update(options = {}) ⇒ Object



132
133
134
135
# File 'lib/em-twitter/connection.rb', line 132

def update(options={})
  @options.merge!(options)
  immediate_reconnect
end