module EventMachine::ApnManager::ApnServer

Public Instance Methods

post_init() click to toggle source
# File lib/em_apn_manager/apn_server.rb, line 7
def post_init
  EM::ApnManager.logger.info("Received a new connection")
  @data  = ""

  start_tls(
    :cert_chain_file  => ENV["APN_CERT"],
    :private_key_file => ENV["APN_CERT"],
    :verify_peer      => false
  )
end
process(headers, payload) click to toggle source
# File lib/em_apn_manager/apn_server.rb, line 39
def process(headers, payload)
  message = "APN RECV #{headers[4]} #{payload}"
  EM::ApnManager.logger.info(message)

  args = Yajl::Parser.parse(payload)

  # If the alert is 'DISCONNECT', then we fake a bad payload by replying
  # with an error and disconnecting.
  if args["aps"]["alert"] == "DISCONNECT"
    EM::ApnManager.logger.info("Disconnecting")
    send_data([8, 1, 0].pack("ccN"))
    close_connection_after_writing
  end
end
receive_data(data) click to toggle source
# File lib/em_apn_manager/apn_server.rb, line 22
def receive_data(data)
  @data << data

  # Try to extract the payload header
  headers = @data.unpack("cNNnH64n")
  return if headers.last.nil?

  # Try to grab the payload
  payload_size = headers.last
  payload = @data[45, payload_size]
  return if payload.length != payload_size

  @data = @data[45 + payload_size, -1] || ""

  process(headers, payload)
end
ssl_handshake_completed() click to toggle source
# File lib/em_apn_manager/apn_server.rb, line 18
def ssl_handshake_completed
  EM::ApnManager.logger.info("SSL handshake completed")
end
unbind() click to toggle source
# File lib/em_apn_manager/apn_server.rb, line 54
def unbind
  EM::ApnManager.logger.info("Connection closed")
end