Sha256: 57c1349c32fe14a17ef8df8a9aeeb30acc10c8de5b39a362a700b32850dd0304

Contents?: true

Size: 1.6 KB

Versions: 1

Compression:

Stored size: 1.6 KB

Contents

# Racoon - A distributed APNs provider
# Copyright (c) 2011, Jeremy Tregunna, All Rights Reserved.
#
# This module contains the firehose which is responsible for maintaining all the open
# connections to Apple, and sending data over the right ones.

require 'digest/sha1'
require 'zmqmachine'
require 'yaml'

module Racoon
  class Firehose
    attr_accessor :connections, :feedback_callback

    def initialize(reactor, address = ZM::Address.new('*', 11555, :tcp), &feedback_callback)
      @connections = {}
      @reactor = reactor
      @address = address
      @feedback_callback = feedback_callback
    end

    def on_attach(socket)
      socket.bind(@address)
    end

    def on_readable(socket, messages)
      messages.each do |message|
        packet = YAML::load(message.copy_out_string)
        apns(packet[:project], packet[:bytes])
      end
    end

    private

    def apns(project, bytes, retries=2)
      uri = "gateway.#{project[:sandbox] ? 'sandbox.' : ''}push.apple.com"
      hash = Digest::SHA1.hexdigest("#{project[:name]}-#{project[:certificate]}")

      begin
        @connections[hash] ||= { :connection => Racoon::APNS::Connection.new(project[:certificate], uri),
                                 :certificate => project[:certificate],
                                 :sandbox => project[:sandbox] }
        connection = @connections[hash][:connection]

        connection.connect! unless connection.connected?
        connection.write(bytes)
      rescue Errno::EPIPE, OpenSSL::SSL::SSLError, Errno::ECONNRESET
        connection.disconnect!
        retry if (retries -= 1) > 0
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
racoon-0.6.0 lib/racoon/firehose.rb