Class: Lowdown::Connection

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

Instance Method Summary collapse

Constructor Details

#initialize(uri, ssl_context) ⇒ 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.



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_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.



70
71
72
# File 'lib/lowdown/connection.rb', line 70

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.



65
66
67
# File 'lib/lowdown/connection.rb', line 65

def uri
  @uri
end

Instance Method Details

#closevoid

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

#flushvoid

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

#openvoid

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.

Parameters:

  • timeout (Numeric) (defaults to: 5)

    the maximum amount of time to wait for the service to reply to the PING.

Returns:

  • (Boolean)

    whether or not the Connection is open.



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

Note:

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.

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.

Yields:

  • (response)

    called when the request is finished and a response is available.

Yield Parameters:

  • response (Response)

    the Response that holds the status data that came back from 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