Class: Lowdown::Connection
- Inherits:
-
Object
- Object
- Lowdown::Connection
- Defined in:
- lib/lowdown/connection.rb
Overview
The class responsible for managing the connection to the Apple Push Notification service.
It manages both the SSL connection and processing of the HTTP/2 data sent back and forth over that connection.
Instance Attribute Summary collapse
-
#ssl_context ⇒ OpenSSL::SSL::SSLContext
readonly
A SSL context, configured with the certificate/key pair, which is used to connect to the APN service.
-
#uri ⇒ URI
readonly
The details to connect to the APN service.
Instance Method Summary collapse
-
#close ⇒ void
Flushes the connection, terminates the worker thread, and closes the socket.
-
#flush ⇒ void
Halts the calling thread until all dispatched requests have been performed.
-
#initialize(uri, ssl_context) ⇒ Connection
constructor
A new instance of Connection.
-
#open ⇒ void
Creates a new SSL connection to the service, a HTTP/2 client, and starts off a worker thread.
-
#open?(timeout = 5) ⇒ Boolean
This performs a HTTP/2 PING to determine if the connection is actually alive.
-
#post(path, headers, body) {|response| ... } ⇒ void
Sends the provided data as a
POSTrequest to the service.
Constructor Details
#initialize(uri, ssl_context) ⇒ Connection
Returns a new instance of Connection.
58 59 60 |
# File 'lib/lowdown/connection.rb', line 58 def initialize(uri, ssl_context) @uri, @ssl_context = URI(uri), ssl_context end |
Instance Attribute Details
#ssl_context ⇒ OpenSSL::SSL::SSLContext (readonly)
Returns a SSL context, configured with the certificate/key pair, which is used to connect to the APN service.
70 71 72 |
# File 'lib/lowdown/connection.rb', line 70 def ssl_context @ssl_context end |
#uri ⇒ URI (readonly)
Returns the details to connect to the APN service.
65 66 67 |
# File 'lib/lowdown/connection.rb', line 65 def uri @uri end |
Instance Method Details
#close ⇒ void
This method returns an undefined value.
Flushes the connection, terminates the worker thread, and closes the socket. Finally it peforms one more check for pending jobs dispatched onto the main thread.
87 88 89 90 91 92 |
# File 'lib/lowdown/connection.rb', line 87 def close return unless @worker flush @worker.stop @worker = @requests = nil end |
#flush ⇒ void
This method returns an undefined value.
Halts the calling thread until all dispatched requests have been performed.
121 122 123 124 |
# File 'lib/lowdown/connection.rb', line 121 def flush return unless @worker sleep 0.1 until !@worker.working? && @requests.zero? end |
#open ⇒ void
This method returns an undefined value.
Creates a new SSL connection to the service, a HTTP/2 client, and starts off a worker thread.
76 77 78 79 80 |
# File 'lib/lowdown/connection.rb', line 76 def open raise "Connection already open." if @worker @requests = Threading::Counter.new @worker = Worker.new(@uri, @ssl_context) end |
#open?(timeout = 5) ⇒ Boolean
This performs a HTTP/2 PING to determine if the connection is actually alive.
102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/lowdown/connection.rb', line 102 def open?(timeout = 5) return false unless @worker Timeout.timeout(timeout) do caller_thread = Thread.current @worker.enqueue do |http| http.ping('whatever') { caller_thread.run } end Thread.stop end # If the thread was woken-up before the timeout was reached, that means we got a PONG. true rescue Timeout::Error false end |
#post(path, headers, body) {|response| ... } ⇒ void
The callback is performed on a different thread, dedicated to perfoming these callbacks.
This method returns an undefined value.
Sends the provided data as a POST request to the service.
147 148 149 |
# File 'lib/lowdown/connection.rb', line 147 def post(path, headers, body, &callback) request('POST', path, headers, body, &callback) end |