Class: ApnServer::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/apnserver/server.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pem, bind_address = '0.0.0.0', port = 22195) ⇒ Server

Returns a new instance of Server.

[View source]

6
7
8
9
10
# File 'lib/apnserver/server.rb', line 6

def initialize(pem, bind_address = '0.0.0.0', port = 22195)
  @queue = EM::Queue.new
  @client = ApnServer::Client.new(pem)
  @bind_address, @port = bind_address, port
end

Instance Attribute Details

#bind_addressObject

Returns the value of attribute bind_address.


4
5
6
# File 'lib/apnserver/server.rb', line 4

def bind_address
  @bind_address
end

#clientObject

Returns the value of attribute client.


4
5
6
# File 'lib/apnserver/server.rb', line 4

def client
  @client
end

#portObject

Returns the value of attribute port.


4
5
6
# File 'lib/apnserver/server.rb', line 4

def port
  @port
end

Instance Method Details

#start!Object

[View source]

12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/apnserver/server.rb', line 12

def start!
  EventMachine::run do
    puts "#{Time.now} Starting APN Server on #{bind_address}:#{port}"
    
    EM.start_server(bind_address, port, ApnServer::ServerConnection) do |s|
      s.queue = @queue
    end 
     
    EventMachine::PeriodicTimer.new(1) do
      unless @queue.empty?
        size = @queue.size
        size.times do 
          @queue.pop do |notification|
            begin
              @client.connect! unless @client.connected?
              @client.write(notification)
            rescue Errno::EPIPE
              puts "Caught Errno::EPIPE adding notification back to queue"
              @queue.push(notification)
            rescue OpenSSL::SSL::SSLError
              puts "Caught OpenSSL Error, closing connecting and adding notification back to queue"
              @client.disconnect!
              @queue.push(notification)
            rescue RuntimeError => e
              puts "Unable to handle: #{e}"
            end
          end
        end
      end
    end
  end   
end