Class: LeadZeppelin::APNS::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/lead_zeppelin/apns/client.rb

Constant Summary collapse

CLIENT_THREADS =
10
DEFAULT_POLL_FREQUENCY =
1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}, &configure) ⇒ Client

Returns a new instance of Client.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/lead_zeppelin/apns/client.rb', line 7

def initialize(opts={}, &configure)
  Logger.info "instantiating client with options: #{opts.inspect}"
  Logger.thread 'c'
  @semaphore = Mutex.new
  @opts = opts
  @applications = {}

  self.instance_eval &configure if configure

  # FIXME
  @thread_count = Queue.new
  (opts[:client_threads] || CLIENT_THREADS).times {|t| @thread_count << t}
  
  APNS.client = self
end

Instance Attribute Details

#applicationsObject

Returns the value of attribute applications.



23
24
25
# File 'lib/lead_zeppelin/apns/client.rb', line 23

def applications
  @applications
end

Instance Method Details

#add_application(name, opts = {}) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/lead_zeppelin/apns/client.rb', line 53

def add_application(name, opts={})
  Logger.info "adding application \"#{name}\""
  Logger.thread 'a'
  
  begin
    application = Application.new name, opts.merge(notification_error_block: @notification_error_block,
                                                   certificate_error_block:  @certificate_error_block)
  rescue OpenSSL::X509::CertificateError => e
    Logger.error "received a bad certificate for #{name}, not adding application"
  end

  @semaphore.synchronize do
    @applications ||= {}
    @applications[name] = application
  end
end

#hold_open_pollObject



48
49
50
51
# File 'lib/lead_zeppelin/apns/client.rb', line 48

def hold_open_poll
  Logger.info 'attaching current thread to polling thread'
  @polling_thread.join
end

#message(app_name, device_id, message, opts = {}) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/lead_zeppelin/apns/client.rb', line 81

def message(app_name, device_id, message, opts={})
  Logger.debug "message: \"#{app_name}\", \"#{device_id}\", \"#{message}\""
  Logger.thread 'm'

  if @applications[app_name]
    # FIXME
    t = @thread_count
    @applications[app_name].message device_id, message, opts
    @thread_count << t
  else
    Logger.error "not sending message for #{app_name.to_s} because it is invalid or has been removed"
  end
end

#on_certificate_error(&block) ⇒ Object



29
30
31
# File 'lib/lead_zeppelin/apns/client.rb', line 29

def on_certificate_error(&block)
  @certificate_error_block = block
end

#on_notification_error(&block) ⇒ Object



25
26
27
# File 'lib/lead_zeppelin/apns/client.rb', line 25

def on_notification_error(&block)
  @notification_error_block = block
end

#poll(frequency = DEFAULT_POLL_FREQUENCY, opts = {}, &block) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/lead_zeppelin/apns/client.rb', line 33

def poll(frequency=DEFAULT_POLL_FREQUENCY, opts={}, &block)
  Logger.info 'creating polling thread'
  Logger.thread 'p'
  @polling_thread = Thread.new {
    loop do
      self.instance_eval &block
      sleep frequency
    end

    Logger.thread 'polling thread running'
  }

  @polling_thread.join if opts[:join_parent_thread] == true
end

#remove_application(name) ⇒ Object



70
71
72
73
74
75
76
77
78
79
# File 'lib/lead_zeppelin/apns/client.rb', line 70

def remove_application(name)
  Logger.info "removing application \"#{name}\""
  Logger.thread 'r'
  
  @semaphore.synchronize do
    deleted = @applications.delete name

    Logger.warn "removing application \"#{name}\" failed! Name may be invalid." if deleted.nil?
  end
end