Class: Rapns::Daemon::Apns::Connection
- Inherits:
-
Object
- Object
- Rapns::Daemon::Apns::Connection
- Includes:
- Reflectable
- Defined in:
- lib/rapns/daemon/apns/connection.rb
Instance Attribute Summary collapse
-
#last_write ⇒ Object
Returns the value of attribute last_write.
Class Method Summary collapse
Instance Method Summary collapse
- #close ⇒ Object
- #connect ⇒ Object
-
#initialize(app, host, port) ⇒ Connection
constructor
A new instance of Connection.
- #read(num_bytes) ⇒ Object
- #reconnect ⇒ Object
- #select(timeout) ⇒ Object
- #write(data) ⇒ Object
Methods included from Reflectable
Constructor Details
#initialize(app, host, port) ⇒ Connection
Returns a new instance of Connection.
15 16 17 18 19 20 21 22 |
# File 'lib/rapns/daemon/apns/connection.rb', line 15 def initialize(app, host, port) @app = app @host = host @port = port @certificate = app.certificate @password = app.password written end |
Instance Attribute Details
#last_write ⇒ Object
Returns the value of attribute last_write.
9 10 11 |
# File 'lib/rapns/daemon/apns/connection.rb', line 9 def last_write @last_write end |
Class Method Details
.idle_period ⇒ Object
11 12 13 |
# File 'lib/rapns/daemon/apns/connection.rb', line 11 def self.idle_period 30.minutes end |
Instance Method Details
#close ⇒ Object
29 30 31 32 33 34 35 |
# File 'lib/rapns/daemon/apns/connection.rb', line 29 def close begin @ssl_socket.close if @ssl_socket @tcp_socket.close if @tcp_socket rescue IOError end end |
#connect ⇒ Object
24 25 26 27 |
# File 'lib/rapns/daemon/apns/connection.rb', line 24 def connect @ssl_context = setup_ssl_context @tcp_socket, @ssl_socket = connect_socket end |
#read(num_bytes) ⇒ Object
37 38 39 |
# File 'lib/rapns/daemon/apns/connection.rb', line 37 def read(num_bytes) @ssl_socket.read(num_bytes) end |
#reconnect ⇒ Object
70 71 72 73 |
# File 'lib/rapns/daemon/apns/connection.rb', line 70 def reconnect close @tcp_socket, @ssl_socket = connect_socket end |
#select(timeout) ⇒ Object
41 42 43 |
# File 'lib/rapns/daemon/apns/connection.rb', line 41 def select(timeout) IO.select([@ssl_socket], nil, nil, timeout) end |
#write(data) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/rapns/daemon/apns/connection.rb', line 45 def write(data) reconnect_idle if idle_period_exceeded? retry_count = 0 begin write_data(data) rescue Errno::EPIPE, Errno::ETIMEDOUT, OpenSSL::SSL::SSLError, IOError => e retry_count += 1; if retry_count == 1 Rapns.logger.error("[#{@app.name}] Lost connection to #{@host}:#{@port} (#{e.class.name}), reconnecting...") reflect(:apns_connection_lost, @app, e) end if retry_count <= 3 reconnect sleep 1 retry else raise ConnectionError, "#{@app.name} tried #{retry_count-1} times to reconnect but failed (#{e.class.name})." end end end |