Class: Rpush::Daemon::Apns::FeedbackReceiver

Inherits:
Object
  • Object
show all
Includes:
Loggable, Reflectable
Defined in:
lib/rpush/daemon/apns/feedback_receiver.rb

Constant Summary collapse

TUPLE_BYTES =
38
HOSTS =
{
  production: ['feedback.push.apple.com', 2196],
  development: ['feedback.sandbox.push.apple.com', 2196], # deprecated
  sandbox: ['feedback.sandbox.push.apple.com', 2196]
}

Instance Method Summary collapse

Methods included from Loggable

#log_debug, #log_error, #log_info, #log_warn

Methods included from Reflectable

#reflect

Constructor Details

#initialize(app) ⇒ FeedbackReceiver

Returns a new instance of FeedbackReceiver.



17
18
19
20
21
22
23
# File 'lib/rpush/daemon/apns/feedback_receiver.rb', line 17

def initialize(app)
  @app = app
  @host, @port = HOSTS[@app.environment.to_sym]
  @certificate = app.certificate
  @password = app.password
  @interruptible_sleep = InterruptibleSleep.new
end

Instance Method Details

#check_for_feedbackObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/rpush/daemon/apns/feedback_receiver.rb', line 53

def check_for_feedback
  connection = nil
  begin
    connection = Rpush::Daemon::TcpConnection.new(@app, @host, @port)
    connection.connect
    tuple = connection.read(TUPLE_BYTES)

    while tuple
      timestamp, device_token = parse_tuple(tuple)
      create_feedback(timestamp, device_token)
      tuple = connection.read(TUPLE_BYTES)
    end
  rescue StandardError => e
    log_error(e)
    reflect(:error, e)
  ensure
    connection.close if connection
  end
end

#startObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rpush/daemon/apns/feedback_receiver.rb', line 25

def start
  return if Rpush.config.push
  Rpush.logger.info("[#{@app.name}] Starting feedback receiver... ", true)

  @thread = Thread.new do
    loop do
      break if @stop
      check_for_feedback
      @interruptible_sleep.sleep(Rpush.config.apns.feedback_receiver.frequency)
    end

    Rpush::Daemon.store.release_connection
  end

  puts ANSI.green { '' } if Rpush.config.foreground
end

#stopObject



42
43
44
45
46
47
48
49
50
51
# File 'lib/rpush/daemon/apns/feedback_receiver.rb', line 42

def stop
  @stop = true
  @interruptible_sleep.stop
  @thread.join if @thread
rescue StandardError => e
  log_error(e)
  reflect(:error, e)
ensure
  @thread = nil
end