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.



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

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.



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

def bind_address
  @bind_address
end

#clientObject

Returns the value of attribute client.



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

def client
  @client
end

#portObject

Returns the value of attribute port.



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

def port
  @port
end

Instance Method Details

#start!Object



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
# File 'lib/apnserver/server.rb', line 13

def start!
  EventMachine::run do
    Config.logger.info "#{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, OpenSSL::SSL::SSLError, Errno::ECONNRESET
              Config.logger.error "Caught Error, closing connecting and adding notification back to queue"
              @client.disconnect!
              @queue.push(notification)
            rescue RuntimeError => e
              Config.logger.error "Unable to handle: #{e}"
            end
          end
        end
      end
    end
  end
end