Class: Suj::Pusher::APNFeedbackConnection

Inherits:
EM::Connection
  • Object
show all
Includes:
Logger
Defined in:
lib/suj/pusher/apn_feedback_connection.rb

Instance Method Summary collapse

Constructor Details

#initialize(pool, options = {}) ⇒ APNFeedbackConnection

Returns a new instance of APNFeedbackConnection.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/suj/pusher/apn_feedback_connection.rb', line 10

def initialize(pool, options = {})
  super
  @disconnected = true
  @options = options
  @pool = pool
  @cert_key = Digest::SHA1.hexdigest(@options[:cert])
  @cert_file = File.join(Suj::Pusher.config.certs_path, @cert_key)
  @buffer = IO::Buffer.new
  self.comm_inactivity_timeout = 10          # Close after 10 sec of inactivity
  File.open(@cert_file, "w") do |f|
    f.write @options[:cert]
  end
  @ssl_options = {
    private_key_file: @cert_file,
    cert_chain_file: @cert_file,
    verify_peer: false
  }
  info "APN feedback #{@cert_key}: Creating"
end

Instance Method Details

#connection_completedObject



57
58
59
60
# File 'lib/suj/pusher/apn_feedback_connection.rb', line 57

def connection_completed
  info "APN feedback #{@cert_key}: Connection established..."
  @disconnected = false
end

#disconnected?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/suj/pusher/apn_feedback_connection.rb', line 30

def disconnected?
  @disconnected
end

#post_initObject



34
35
36
37
# File 'lib/suj/pusher/apn_feedback_connection.rb', line 34

def post_init
  info "APN feedback #{@cert_key}: Connection init "
  start_tls(@ssl_options)
end

#receive_data(data) ⇒ Object

Receive feedback data from APN servers.

The format is:

timestamp -> 4 byte bigendian
len       -> 2 byte token length
token     -> 32 bytes token


46
47
48
49
50
51
52
53
54
55
# File 'lib/suj/pusher/apn_feedback_connection.rb', line 46

def receive_data(data)
  @buffer << data

  while @buffer.size >= 38
    timestamp, size = @buffer.read(6).unpack("Nn")
    token = @buffer.read(size)
    info "APN feedback #{@cert_key}: TIMESTAMP: #{timestamp} SIZE: #{size} TOKEN: #{token}"
    @pool.invalidate_token(@cert_key, token)
  end
end

#unbindObject



62
63
64
65
66
# File 'lib/suj/pusher/apn_feedback_connection.rb', line 62

def unbind
  info "APN feedback #{@cert_key}: Connection closed..."
  @disconnected = true
  @pool.remove_feedback_connection(@cert_key)
end