em-apns 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +23 -0
- data/.rspec +1 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +29 -0
- data/Rakefile +2 -0
- data/bin/em-apns +7 -0
- data/em-apns.gemspec +22 -0
- data/lib/em-apns.rb +67 -0
- data/lib/em-apns/connection.rb +33 -0
- data/lib/em-apns/connection_pool.rb +34 -0
- data/lib/em-apns/error.rb +34 -0
- data/lib/em-apns/notification.rb +59 -0
- data/lib/em-apns/server.rb +15 -0
- data/lib/em-apns/server_runner.rb +89 -0
- data/lib/em-apns/version.rb +5 -0
- data/spec/em-apns/connection_spec.rb +11 -0
- data/spec/em-apns/notifications_spec.rb +176 -0
- data/spec/spec_helper.rb +4 -0
- data/spec/support/certs/cert.pem +941 -0
- data/spec/support/certs/key.pem +939 -0
- data/spec/support/helper_methods.rb +17 -0
- data/spec/support/mock_server.rb +59 -0
- metadata +125 -0
data/.gitignore
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
lib/bundler/man
|
12
|
+
pkg
|
13
|
+
rdoc
|
14
|
+
spec/reports
|
15
|
+
test/tmp
|
16
|
+
test/version_tmp
|
17
|
+
tmp
|
18
|
+
Gemfile.lock
|
19
|
+
pkg/*
|
20
|
+
.rvmrc
|
21
|
+
*.sock
|
22
|
+
.project
|
23
|
+
.DS_Store
|
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Yuri Barbashov
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# Em::Apns
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'em-apns'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install em-apns
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/bin/em-apns
ADDED
data/em-apns.gemspec
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/em-apns/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Yuri Barbashov"]
|
6
|
+
gem.email = ["lolcoltd@gmail.com"]
|
7
|
+
gem.description = %q{EventMachine-driven Apple Push Notifications Sender daemon}
|
8
|
+
gem.summary = %q{EventMachine-driven Apple Push Notifications Sender daemon}
|
9
|
+
gem.homepage = ""
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "em-apns"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = EM::APNS::VERSION
|
17
|
+
|
18
|
+
gem.add_dependency "eventmachine", ">= 1.0.0.beta.3"
|
19
|
+
gem.add_dependency "json"
|
20
|
+
|
21
|
+
gem.add_development_dependency "rspec", "~> 2.6.0"
|
22
|
+
end
|
data/lib/em-apns.rb
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
require 'eventmachine'
|
2
|
+
require 'socket'
|
3
|
+
require 'logger'
|
4
|
+
require 'json'
|
5
|
+
require 'yaml'
|
6
|
+
require 'em-apns/notification'
|
7
|
+
require 'em-apns/error'
|
8
|
+
require 'em-apns/connection'
|
9
|
+
require 'em-apns/connection_pool'
|
10
|
+
require 'em-apns/server'
|
11
|
+
require "em-apns/version"
|
12
|
+
|
13
|
+
module EM::APNS
|
14
|
+
class << self
|
15
|
+
attr_writer :sock, :logger
|
16
|
+
attr_accessor :key, :cert, :pool, :env, :host
|
17
|
+
|
18
|
+
def sock
|
19
|
+
@sock ||= (defined?(Rails) && Rails.respond_to?(:root) ? File.join(Rails.root, 'tmp/sockets', "em-apns-#{Rails.env}.sock") : nil)
|
20
|
+
end
|
21
|
+
|
22
|
+
def logger
|
23
|
+
@logger ||= Logger.new(STDOUT)
|
24
|
+
end
|
25
|
+
|
26
|
+
def send_notification(*args)
|
27
|
+
send_notifications([Notification.new(*args)])
|
28
|
+
end
|
29
|
+
|
30
|
+
def send_notifications(notifications)
|
31
|
+
UNIXSocket.open(@sock) do |socket|
|
32
|
+
notifications.each do |n|
|
33
|
+
socket.puts(n.data)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
rescue => e
|
37
|
+
logger.error(e)
|
38
|
+
end
|
39
|
+
|
40
|
+
def config options
|
41
|
+
raise 'App root folder are not defined' unless options[:root]
|
42
|
+
if options[:config]
|
43
|
+
configurations = YAML.load_file options['config']
|
44
|
+
else
|
45
|
+
config_file = File.join(options[:root],"config/em-apns.yml")
|
46
|
+
configurations = YAML.load_file(config_file)
|
47
|
+
end
|
48
|
+
@env = options[:environment] || 'development'
|
49
|
+
@sock = File.join(options[:root], "tmp/sockets/em-apns-#{@env}.sock")
|
50
|
+
@pool = configurations['pool']
|
51
|
+
@key = configurations['key']
|
52
|
+
@cert = configurations['cert']
|
53
|
+
raise 'No key or certficate file' unless @key && @cert
|
54
|
+
@host = case @env
|
55
|
+
when 'development'
|
56
|
+
"gateway.sandbox.push.apple.com"
|
57
|
+
when 'production'
|
58
|
+
"gateway.push.apple.com"
|
59
|
+
else
|
60
|
+
"localhost"
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
67
|
+
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module EM::APNS
|
2
|
+
class Connection < EM::Connection
|
3
|
+
attr_accessor :num
|
4
|
+
|
5
|
+
def post_init
|
6
|
+
start_tls(
|
7
|
+
:private_key_file => EM::APNS.key,
|
8
|
+
:cert_chain_file => EM::APNS.cert,
|
9
|
+
:verify_peer => false
|
10
|
+
)
|
11
|
+
end
|
12
|
+
|
13
|
+
def connection_completed
|
14
|
+
EM::APNS.logger.info("Connection established")
|
15
|
+
end
|
16
|
+
|
17
|
+
def receive_data(data)
|
18
|
+
data_array = data.unpack("ccN")
|
19
|
+
error_response = Error.new(*data_array)
|
20
|
+
EM::APNS.logger.info("#{error_response}")
|
21
|
+
end
|
22
|
+
|
23
|
+
def unbind
|
24
|
+
EM::APNS.logger.info("Connection terminated")
|
25
|
+
@unbind.call(self) if @unbind
|
26
|
+
end
|
27
|
+
|
28
|
+
def on_unbind &block
|
29
|
+
@unbind = block
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module EM::APNS
|
2
|
+
|
3
|
+
#simple connection pool using EM queue, default size 4
|
4
|
+
class ConnectionPool
|
5
|
+
attr_reader :queue
|
6
|
+
|
7
|
+
def initialize
|
8
|
+
@pool_size = EM::APNS.pool || 4
|
9
|
+
@connections = []
|
10
|
+
@queue = EM::Queue.new
|
11
|
+
@pool_size.times { add_connection }
|
12
|
+
end
|
13
|
+
|
14
|
+
def queue_worker_loop
|
15
|
+
proc{ |connection|
|
16
|
+
@queue.pop do |notification|
|
17
|
+
connection.send_data(notification)
|
18
|
+
EM.next_tick { queue_worker_loop.call connection }
|
19
|
+
end
|
20
|
+
}
|
21
|
+
end
|
22
|
+
|
23
|
+
def add_connection
|
24
|
+
connection = EM.connect(EM::APNS.host, 2195, Connection)
|
25
|
+
connection.on_unbind do |conn|
|
26
|
+
@connections.delete(conn)
|
27
|
+
EM.next_tick{ add_connection }
|
28
|
+
end
|
29
|
+
@connections << connection
|
30
|
+
queue_worker_loop.call connection
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module EM
|
2
|
+
module APNS
|
3
|
+
class Error
|
4
|
+
DESCRIPTION = {
|
5
|
+
0 => "No errors encountered",
|
6
|
+
1 => "Processing error",
|
7
|
+
2 => "Missing device token",
|
8
|
+
3 => "Missing topic",
|
9
|
+
4 => "Missing payload",
|
10
|
+
5 => "Invalid token size",
|
11
|
+
6 => "Invalid topic size",
|
12
|
+
7 => "Invalid payload size",
|
13
|
+
8 => "Invalid token",
|
14
|
+
255 => "None (unknown)"
|
15
|
+
}
|
16
|
+
|
17
|
+
attr_reader :command, :status_code, :identifier
|
18
|
+
|
19
|
+
def initialize(command, status_code, identifier)
|
20
|
+
@command = command
|
21
|
+
@status_code = status_code
|
22
|
+
@identifier = identifier
|
23
|
+
end
|
24
|
+
|
25
|
+
def to_s
|
26
|
+
"CODE=#{@status_code} ID=#{@identifier} DESC=#{description}"
|
27
|
+
end
|
28
|
+
|
29
|
+
def description
|
30
|
+
DESCRIPTION[@status_code] || "Missing description"
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
module EventMachine
|
4
|
+
module APNS
|
5
|
+
class Notification
|
6
|
+
DATA_MAX_BYTES = 256
|
7
|
+
|
8
|
+
class PayloadTooLarge < StandardError;end
|
9
|
+
|
10
|
+
attr_reader :token
|
11
|
+
attr_accessor :identifier, :expiry
|
12
|
+
|
13
|
+
def initialize(token, aps = {}, custom = {}, options = {})
|
14
|
+
raise "Bad push token: #{token}" if token.nil? || (token.length != 64)
|
15
|
+
|
16
|
+
@token = token
|
17
|
+
@aps = aps
|
18
|
+
@custom = custom
|
19
|
+
|
20
|
+
self.identifier = options[:identifier] if options[:identifier]
|
21
|
+
self.expiry = options[:expiry] if options[:expiry]
|
22
|
+
end
|
23
|
+
|
24
|
+
def payload
|
25
|
+
@custom.merge(:aps => @aps).to_json
|
26
|
+
end
|
27
|
+
|
28
|
+
# Documentation about this format is here:
|
29
|
+
# http://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CommunicatingWIthAPS/CommunicatingWIthAPS.html
|
30
|
+
def data
|
31
|
+
identifier = @identifier || 0
|
32
|
+
expiry = @expiry || 0
|
33
|
+
size = [payload].pack("a*").size
|
34
|
+
data_array = [1, identifier, expiry, 32, token, size, payload]
|
35
|
+
data_array.pack("cNNnH*na*")
|
36
|
+
end
|
37
|
+
|
38
|
+
def validate!
|
39
|
+
if data.size > DATA_MAX_BYTES
|
40
|
+
error = "max is #{DATA_MAX_BYTES} bytes, but got #{data.size}: #{payload.inspect}"
|
41
|
+
raise PayloadTooLarge.new(error)
|
42
|
+
else
|
43
|
+
true
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def identifier=(new_identifier)
|
48
|
+
@identifier = new_identifier.to_i
|
49
|
+
end
|
50
|
+
|
51
|
+
def truncate_alert!
|
52
|
+
while data.size > DATA_MAX_BYTES && !@aps["alert"].nil? && @aps["alert"].size > 0
|
53
|
+
@aps["alert"] = @aps["alert"][0..-2]
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module EM::APNS
|
2
|
+
module Server
|
3
|
+
def post_init
|
4
|
+
@@queue ||= ConnectionPool.new.queue
|
5
|
+
@buf ||= ""
|
6
|
+
end
|
7
|
+
|
8
|
+
def receive_data(data)
|
9
|
+
@buf << data
|
10
|
+
while line = @buf.slice!(/.+\r?\n/) do
|
11
|
+
@@queue.push(line.gsub(/\r?\n/, ''))
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,89 @@
|
|
1
|
+
module EM::APNS
|
2
|
+
class ServerRunner
|
3
|
+
|
4
|
+
def initialize(args)
|
5
|
+
require 'rubygems'
|
6
|
+
require 'optparse'
|
7
|
+
require 'logger'
|
8
|
+
|
9
|
+
@options = { environment: 'development', root: File.expand_path('.') }
|
10
|
+
|
11
|
+
optparse = OptionParser.new do |opts|
|
12
|
+
opts.banner = "Usage: em-apns [options] start|stop"
|
13
|
+
|
14
|
+
opts.on('-h', '--help', 'Show this message') do
|
15
|
+
puts opts
|
16
|
+
exit 1
|
17
|
+
end
|
18
|
+
|
19
|
+
opts.on('-e', '--environment=NAME', 'Specifies the environment to run this apn sender under ([development]/production/test).') do |e|
|
20
|
+
@options[:environment] = e
|
21
|
+
end
|
22
|
+
|
23
|
+
opts.on('-p', '--pid_file=NAME', 'Specifies the pid file path.') do |path|
|
24
|
+
@pid_file = path
|
25
|
+
end
|
26
|
+
|
27
|
+
opts.on('-d', '--daemonize', 'Daemonize em-apnsender') do
|
28
|
+
@daemonize = true
|
29
|
+
end
|
30
|
+
|
31
|
+
opts.on('-c', '--config=CONFIG', 'Full path to configuration file.') do |path|
|
32
|
+
@options[:config] = path
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
|
37
|
+
# If no arguments, give help screen
|
38
|
+
@args = optparse.parse!(args.empty? ? ['-h'] : args)
|
39
|
+
|
40
|
+
case args[0]
|
41
|
+
when 'start'
|
42
|
+
start
|
43
|
+
when 'stop'
|
44
|
+
stop
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def start
|
49
|
+
if @daemonize
|
50
|
+
require 'daemons'
|
51
|
+
Daemons.run_proc("em-apns-#{@options[:environment]}", :dir => "#{@options[:root]}/tmp/pids", :dir_mode => :normal, :ARGV => @args) do |*args|
|
52
|
+
EM::APNS.logger = Logger.new(File.join(@options[:root], 'log', "em-apns-#{@options[:environment]}.log"))
|
53
|
+
run
|
54
|
+
end
|
55
|
+
else
|
56
|
+
run
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def stop
|
61
|
+
@pid_file ||= "#{@options[:root]}/tmp/pids/em-apns-#{@options[:environment]}.pid"
|
62
|
+
pid = File.open(@pid_file) {|h| h.read}.to_i
|
63
|
+
Process.kill('TERM', pid)
|
64
|
+
rescue Errno => e
|
65
|
+
require 'fileutils'
|
66
|
+
puts "#{e} #{pid}"
|
67
|
+
puts "deleting pid-file..."
|
68
|
+
FileUtils.rm( f )
|
69
|
+
end
|
70
|
+
|
71
|
+
def run
|
72
|
+
EM::APNS.config(@options)
|
73
|
+
file = EM::APNS.sock
|
74
|
+
File.unlink(file) if File.exists?(file)
|
75
|
+
EventMachine::run {
|
76
|
+
Signal.trap("INT") { EM::APNS.logger.fatal("Terminated"); EM.stop }
|
77
|
+
Signal.trap("TERM") { EM::APNS.logger.fatal("Terminated"); EM.stop }
|
78
|
+
EM::APNS.logger.info "Started"
|
79
|
+
EventMachine::start_unix_domain_server(file, EM::APNS::Server )
|
80
|
+
}
|
81
|
+
rescue => e
|
82
|
+
STDERR.puts e.message
|
83
|
+
EM::APNS.logger.fatal(e) if EM::APNS.logger.respond_to?(:fatal)
|
84
|
+
exit 1
|
85
|
+
end
|
86
|
+
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|