Module: APND::Daemon::Protocol

Included in:
ServerConnection
Defined in:
lib/apnd/daemon/protocol.rb

Overview

Daemon::Protocol handles incoming APNs

Instance Method Summary collapse

Instance Method Details

#post_initObject

Called when a client connection is opened



12
13
14
15
# File 'lib/apnd/daemon/protocol.rb', line 12

def post_init
  @address = ::Socket.unpack_sockaddr_in(self.get_peername)
  APND.logger "#{@address.last}:#{@address.first} opened connection"
end

#receive_data(data) ⇒ Object

Add incoming notification(s) to @buffer



49
50
51
52
# File 'lib/apnd/daemon/protocol.rb', line 49

def receive_data(data)
  APND.logger "#{@address.last}:#{@address.first} buffering data"
  (@buffer ||= "") << data
end

#unbindObject

Called when a client connection is closed

Checks @buffer for any pending notifications to be queued



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/apnd/daemon/protocol.rb', line 23

def unbind
  # totally broken.
  @buffer.chomp!
  while(@buffer.length > 0) do
    # 3 bytes for header
    # 32 bytes for token
    # 2 bytes for json length
    
    # taking the last is acceptable because we know it's never
    # longer than 256 bytes from the apple documentation.
    json_length = @buffer.slice(35,37).unpack('CC').last
    chunk = @buffer.slice!(0,json_length + 3 + 32 + 2)
    if notification = APND::Notification.valid?(chunk)
      APND.logger "#{@address.last}:#{@address.first} added new Notification to queue"
      queue.push(notification)
    else
      APND.logger "#{@address.last}:#{@address.first} submitted invalid Notification"
    end
    @buffer.strip!
  end
  APND.logger "#{@address.last}:#{@address.first} closed connection"
end