Class: APNS::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/apns/connection.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pem:, pass: nil, host: 'gateway.sandbox.push.apple.com', port: 2195, buffer_size: 4 * 1024) ⇒ Connection

Returns a new instance of Connection.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/apns/connection.rb', line 12

def initialize(pem: ,
               pass: nil,
               host: 'gateway.sandbox.push.apple.com',
               port: 2195,
               buffer_size: 4 * 1024)
  @notifications = InfiniteArray.new(buffer_size: buffer_size)
  @pem =  pem
  @pass = pass
  @host = host
  @port = port

  # Our current strategy is to read errors emitted by the APNS in a separate
  # thread spawned when we open a new connection (read_errors method).
  # If this thread receives an error information from the @ssl, it turns on
  # the @lock, so that no direct write operation can be performed on the same
  # Connection instance.
  #
  @lock = Mutex.new

  @sock, @ssl = open_connection
  ObjectSpace.define_finalizer(self, self.class.finalize(@sock, @ssl))
end

Instance Attribute Details

#error_handlerObject

Returns the value of attribute error_handler.



10
11
12
# File 'lib/apns/connection.rb', line 10

def error_handler
  @error_handler
end

Class Method Details

.finalize(sock, ssl) ⇒ Object



34
35
36
37
38
39
# File 'lib/apns/connection.rb', line 34

def self.finalize sock, ssl
  proc {
    ssl.close
    sock.close
  }
end

Instance Method Details

#push(ns) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/apns/connection.rb', line 41

def push ns
  # The notification identifier is set to 4 bytes in the APNS protocol.
  # Thus, upon hitting this limit, read for failures and restart the counting again.
  if @notifications.size + ns.size > MAX_32_BIT - 10
    code, failed_id = read_failure_info(timeout: 3)
    if failed_id
      ns = @notifications.items_from(failed_id+1) + ns
      reopen_connection
      @error_handler.call(code, @notifications.item_at(failed_id))
    end

    @notifications.clear
  end

  ns.each{ |n|
    n.message_identifier = [@notifications.size].pack('N')
    @notifications.push(n)
  }
  @lock.synchronize{ write ns }
end