Class: Rapnd::Daemon
- Inherits:
-
Object
- Object
- Rapnd::Daemon
- Defined in:
- lib/rapnd/daemon.rb
Instance Attribute Summary collapse
-
#apple ⇒ Object
Returns the value of attribute apple.
-
#cert ⇒ Object
Returns the value of attribute cert.
-
#connected ⇒ Object
Returns the value of attribute connected.
-
#host ⇒ Object
Returns the value of attribute host.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#queue ⇒ Object
Returns the value of attribute queue.
-
#redis ⇒ Object
Returns the value of attribute redis.
Instance Method Summary collapse
- #connect! ⇒ Object
-
#initialize(options = {}) ⇒ Daemon
constructor
A new instance of Daemon.
- #run! ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Daemon
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/rapnd/daemon.rb', line 13 def initialize( = {}) [:redis_host] ||= 'localhost' [:redis_port] ||= '6379' [:host] ||= 'gateway.sandbox.push.apple.com' [:queue] ||= 'rapnd_queue' [:password] ||= '' raise 'No cert provided!' unless [:cert] = { :host => [:redis_host], :port => [:redis_port] } [:password] = [:redis_password] if .has_key?(:redis_password) @redis = Redis.new() @queue = [:queue] @cert = [:cert] @host = [:host] @logger = Logger.new("#{[:dir]}/log/#{[:queue]}.log") @logger.info "Listening on queue: #{self.queue}" end |
Instance Attribute Details
#apple ⇒ Object
Returns the value of attribute apple.
11 12 13 |
# File 'lib/rapnd/daemon.rb', line 11 def apple @apple end |
#cert ⇒ Object
Returns the value of attribute cert.
11 12 13 |
# File 'lib/rapnd/daemon.rb', line 11 def cert @cert end |
#connected ⇒ Object
Returns the value of attribute connected.
11 12 13 |
# File 'lib/rapnd/daemon.rb', line 11 def connected @connected end |
#host ⇒ Object
Returns the value of attribute host.
11 12 13 |
# File 'lib/rapnd/daemon.rb', line 11 def host @host end |
#logger ⇒ Object
Returns the value of attribute logger.
11 12 13 |
# File 'lib/rapnd/daemon.rb', line 11 def logger @logger end |
#queue ⇒ Object
Returns the value of attribute queue.
11 12 13 |
# File 'lib/rapnd/daemon.rb', line 11 def queue @queue end |
#redis ⇒ Object
Returns the value of attribute redis.
11 12 13 |
# File 'lib/rapnd/daemon.rb', line 11 def redis @redis end |
Instance Method Details
#connect! ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/rapnd/daemon.rb', line 32 def connect! @logger.info 'Connecting...' @context = OpenSSL::SSL::SSLContext.new @context.cert = OpenSSL::X509::Certificate.new(File.read(@cert)) @context.key = OpenSSL::PKey::RSA.new(File.read(@cert), @password) @sock = TCPSocket.new(@host, 2195) self.apple = OpenSSL::SSL::SSLSocket.new(@sock, @context) self.apple.sync = true self.apple.connect self.connected = true @logger.info 'Connected!' return @sock, @ssl end |
#run! ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/rapnd/daemon.rb', line 49 def run! loop do begin = @redis.blpop(self.queue, 1) if notification = Rapnd::Notification.new(Marshal.load(.last)) self.connect! unless self.connected @logger.info "Sending #{notification.device_token}: #{notification.json_payload}" self.apple.write(notification.to_bytes) end rescue Exception => e if e.class == Interrupt || e.class == SystemExit @logger.info "Shutting down..." exit(0) end self.connect! if notification @logger.info "Trying again for: #{notification.json_payload}" self.apple.write(notification.to_bytes) end @logger.error "Encountered error: #{e}" end end end |