Class: Lowdown::Connection

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(uri, ssl_context, connect = true) ⇒ Connection

Returns a new instance of Connection.

Parameters:

  • uri (URI, String)

    the details to connect to the APN service.

  • ssl_context (OpenSSL::SSL::SSLContext)

    a SSL context, configured with the certificate/key pair, which is used to connect to the APN service.

  • connect (Boolean) (defaults to: true)

    whether or not to immediately connect on initialization.



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_contextOpenSSL::SSL::SSLContext (readonly)

Returns a SSL context, configured with the certificate/key pair, which is used to connect to the APN service.

Returns:

  • (OpenSSL::SSL::SSLContext)

    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

#uriURI (readonly)

Returns the details to connect to the APN service.

Returns:

  • (URI)

    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

#connectvoid

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.

Returns:

  • (Boolean)

    whether or not the Connection is open.



131
132
133
# File 'lib/lowdown/connection.rb', line 131

def connected?
  !@connection.nil? && !@connection.closed?
end

#disconnectvoid

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

#pingBoolean

Note:

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.

Returns:

  • (Boolean)

    whether or not a reply was received.



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

Note:

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.

Parameters:

  • path (String)

    the request path, which should be /3/device/<device-token>.

  • headers (Hash)

    the additional headers for the request. By default it sends :method, :path, and content-length.

  • body (String)

    the (JSON) encoded payload data to send to the service.

  • delegate (DelegateProtocol)

    an object that implements the delegate protocol.

  • context (Object, nil) (defaults to: nil)

    any object that you want to be passed to the delegate once the response is back.



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