Class: AppleShove::APNS::NotifyConnection

Inherits:
Connection
  • Object
show all
Includes:
Celluloid
Defined in:
lib/apple_shove/apns/notify_connection.rb

Instance Attribute Summary collapse

Attributes inherited from Connection

#last_used

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Connection

#disconnect, #reconnect, #safe_last_used, #socket

Constructor Details

#initialize(p12, sandbox) ⇒ NotifyConnection

Returns a new instance of NotifyConnection.



11
12
13
14
15
16
17
18
19
# File 'lib/apple_shove/apns/notify_connection.rb', line 11

def initialize(p12, sandbox)
  @name = self.class.generate_name(p12, sandbox)
  @last_message           = nil
  @pending_notifications  = 0

  host = "gateway.#{sandbox ? 'sandbox.' : ''}push.apple.com"

  super host, 2195, p12
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/apple_shove/apns/notify_connection.rb', line 9

def name
  @name
end

#pending_notificationsObject

Returns the value of attribute pending_notifications.



8
9
10
# File 'lib/apple_shove/apns/notify_connection.rb', line 8

def pending_notifications
  @pending_notifications
end

Class Method Details

.generate_name(p12, sandbox) ⇒ Object



21
22
23
# File 'lib/apple_shove/apns/notify_connection.rb', line 21

def self.generate_name(p12, sandbox)
  Digest::SHA1.hexdigest("#{p12}#{sandbox}")
end

Instance Method Details

#connectObject



27
28
29
30
# File 'lib/apple_shove/apns/notify_connection.rb', line 27

def connect
  super
  @last_used = Time.now
end

#send(notification) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/apple_shove/apns/notify_connection.rb', line 32

def send(notification)
  message = notification.binary_message

  begin
    if @last_used && Time.now - safe_last_used > CONFIG[:reconnect_timer] * 60
      Logger.info("refreshing connection", self, notification)
      reconnect
    end

    socket.write message
  rescue Exception => e
    handler = WriteExceptionHandler.new(e)
    Logger.warn(handler.message, self, notification)

    begin
      reconnect                   if handler.reconnect?
      socket.write @last_message  if handler.rewrite? && @last_message
    rescue Exception => e
      Logger.warn("failed while trying to recover from write error", self, notification)
    end

    retry                       if handler.retry?
  else
    Logger.info("delivered notification", self, notification)

    @last_message = message
    @last_used    = Time.now
  end

  @pending_notifications -= 1
end

#shutdownObject



64
65
66
67
68
69
70
71
72
# File 'lib/apple_shove/apns/notify_connection.rb', line 64

def shutdown
  while @pending_notifications > 0
    Logger.info("waiting to shut down. #{@pending_notifications} job(s) remaining.", self)
    sleep 1
  end

  self.disconnect
  self.terminate
end