Class: MobileNotify::Apns::Connection
- Inherits:
-
Object
- Object
- MobileNotify::Apns::Connection
- Defined in:
- lib/mobile_notify/apns/connection.rb
Defined Under Namespace
Classes: AlreadyConnectedError, ConnectionTimeoutError, NotConnectedError, TransmissionTimeoutError
Constant Summary collapse
- DEFAULT_CONNECTION_TIMEOUT =
30
- DEFAULT_CONNECTION_RETRY_DELAY =
2
- DEFAULT_TRANSMISSION_TIMEOUT =
10
- DEFAULT_TRANSMISSION_RETRY_DELAY =
2
Instance Attribute Summary collapse
-
#connection_retry_delay ⇒ Object
Returns the value of attribute connection_retry_delay.
-
#connection_timeout ⇒ Object
Returns the value of attribute connection_timeout.
-
#transmission_retry_delay ⇒ Object
Returns the value of attribute transmission_retry_delay.
-
#transmission_timeout ⇒ Object
Returns the value of attribute transmission_timeout.
Class Method Summary collapse
Instance Method Summary collapse
- #close ⇒ Object
-
#initialize(uri, certificate_file, key_file = certificate_file) ⇒ Connection
constructor
A new instance of Connection.
- #open ⇒ Object
- #send(notification) ⇒ Object
Constructor Details
#initialize(uri, certificate_file, key_file = certificate_file) ⇒ Connection
Returns a new instance of Connection.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/mobile_notify/apns/connection.rb', line 50 def initialize(uri, certificate_file, key_file = certificate_file) @uri = uri @certificate_file = certificate_file @key_file = key_file.nil? ? certificate_file : key_file @connection_timeout = DEFAULT_CONNECTION_TIMEOUT @connection_retry_delay = DEFAULT_CONNECTION_RETRY_DELAY @transmission_timeout = DEFAULT_TRANSMISSION_TIMEOUT @transmission_retry_delay = DEFAULT_TRANSMISSION_RETRY_DELAY @ssl_context = OpenSSL::SSL::SSLContext.new() @ssl_context.cert = OpenSSL::X509::Certificate.new(File::read(@certificate_file)) @ssl_context.key = OpenSSL::PKey::RSA.new(File::read(@key_file)) @tcp_socket = TCPSocket.new(@uri.host, @uri.port) @ssl_socket = nil end |
Instance Attribute Details
#connection_retry_delay ⇒ Object
Returns the value of attribute connection_retry_delay.
39 40 41 |
# File 'lib/mobile_notify/apns/connection.rb', line 39 def connection_retry_delay @connection_retry_delay end |
#connection_timeout ⇒ Object
Returns the value of attribute connection_timeout.
39 40 41 |
# File 'lib/mobile_notify/apns/connection.rb', line 39 def connection_timeout @connection_timeout end |
#transmission_retry_delay ⇒ Object
Returns the value of attribute transmission_retry_delay.
40 41 42 |
# File 'lib/mobile_notify/apns/connection.rb', line 40 def transmission_retry_delay @transmission_retry_delay end |
#transmission_timeout ⇒ Object
Returns the value of attribute transmission_timeout.
40 41 42 |
# File 'lib/mobile_notify/apns/connection.rb', line 40 def transmission_timeout @transmission_timeout end |
Class Method Details
.open(uri, certificate_file, key_file = certificate_file) ⇒ Object
42 43 44 45 46 47 48 |
# File 'lib/mobile_notify/apns/connection.rb', line 42 def self.open(uri, certificate_file, key_file = certificate_file) connection = new(uri, certificate_file, key_file = certificate_file) connection.open yield connection ensure connection.close if connection end |
Instance Method Details
#close ⇒ Object
88 89 90 91 92 93 |
# File 'lib/mobile_notify/apns/connection.rb', line 88 def close @ssl_socket.close if @ssl_socket @ssl_socket = nil self end |
#open ⇒ Object
67 68 69 70 71 72 |
# File 'lib/mobile_notify/apns/connection.rb', line 67 def open raise AlreadyConnectedError.new(@uri) if @ssl_socket @ssl_socket = establish_connection self end |
#send(notification) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/mobile_notify/apns/connection.rb', line 74 def send(notification) raise NotConnectedError.new(@uri) unless @ssl_socket retry_timer ||= 0 @ssl_socket.write(notification.to_data) self rescue OpenSSL::SSL::SSLError, Errno::EPIPE sleep(self.transmission_retry_delay) retry_timer += self.transmission_retry_delay retry if retry_timer < self.transmission_timeout raise TransmissionTimeoutError.new(@uri, self.transmission_timeout, $!) if retry_timer >= self.transmission_timeout end |