Class: Lowdown::Connection
- Inherits:
-
Object
- Object
- Lowdown::Connection
- Includes:
- Celluloid::IO, Celluloid::Internals::Logger
- Defined in:
- lib/lowdown/connection.rb,
lib/lowdown/connection/monitor.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.
Defined Under Namespace
Modules: DelegateProtocol, Monitor Classes: Request, TimedOut
Constant Summary collapse
- CONNECT_RETRIES =
5- CONNECT_RETRY_BACKOFF =
5- CONNECT_TIMEOUT =
10- HEARTBEAT_INTERVAL =
10- HEARTBEAT_TIMEOUT =
CONNECT_TIMEOUT
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
-
#connect ⇒ void
Creates a new SSL connection to the service, a HTTP/2 client, and starts off the main runloop.
-
#connected? ⇒ Boolean
Whether or not the Connection is open.
-
#disconnect ⇒ void
Closes the connection and resets the internal state.
-
#initialize(uri, ssl_context, connect = true) ⇒ Connection
constructor
A new instance of Connection.
-
#ping ⇒ Boolean
This performs a HTTP/2 PING to determine if the connection is actually alive.
-
#post(path:, headers:, body:, delegate:, context: nil) ⇒ void
Sends the provided data as a
POSTrequest to the service.
Constructor Details
#initialize(uri, ssl_context, connect = true) ⇒ Connection
Returns a new instance of Connection.
86 87 88 89 90 91 92 93 94 95 |
# File 'lib/lowdown/connection.rb', line 86 def initialize(uri, ssl_context, connect = true) @uri, @ssl_context = URI(uri), ssl_context reset_state! if connect # This ensures that calls to the public #connect method are ignored while already connecting. @connecting = true async.connect! end 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.
105 106 107 |
# File 'lib/lowdown/connection.rb', line 105 def ssl_context @ssl_context end |
#uri ⇒ URI (readonly)
Returns the details to connect to the APN service.
100 101 102 |
# File 'lib/lowdown/connection.rb', line 100 def uri @uri end |
Instance Method Details
#connect ⇒ void
This method returns an undefined value.
Creates a new SSL connection to the service, a HTTP/2 client, and starts off the main runloop.
111 112 113 |
# File 'lib/lowdown/connection.rb', line 111 def connect connect! unless @connecting end |
#connected? ⇒ Boolean
Returns whether or not the Connection is open.
131 132 133 |
# File 'lib/lowdown/connection.rb', line 131 def connected? !@connection.nil? && !@connection.closed? end |
#disconnect ⇒ void
This method returns an undefined value.
Closes the connection and resets the internal state
119 120 121 122 123 124 125 126 |
# File 'lib/lowdown/connection.rb', line 119 def disconnect if @connection info "Closing..." @connection.close end @heartbeat.cancel if @heartbeat reset_state! end |
#ping ⇒ Boolean
This halts the caller thread until a reply is received. You should call this on a future and possibly set a timeout.
This performs a HTTP/2 PING to determine if the connection is actually alive. Be sure to not call this on a sleeping connection, or it will be guaranteed to fail.
144 145 146 147 148 149 150 151 152 |
# File 'lib/lowdown/connection.rb', line 144 def ping if connected? condition = Celluloid::Condition.new @http.ping("whatever") { condition.signal(true) } condition.wait else false end end |
#post(path:, headers:, body:, delegate:, context: nil) ⇒ void
It is strongly advised that the delegate object is a Celluloid actor and that you pass in an async proxy of that object, but that is not required. If you do not pass in an actor, then be advised that the callback will run on this connection’s private thread and thus you should not perform long blocking operations.
This method returns an undefined value.
Sends the provided data as a POST request to the service.
311 312 313 |
# File 'lib/lowdown/connection.rb', line 311 def post(path:, headers:, body:, delegate:, context: nil) request("POST", path, headers, body, delegate, context) end |