apnd 0.1.5 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -79,10 +79,12 @@ push notifications to APND.
79
79
 
80
80
  # Set the host/port APND is running on
81
81
  # (not needed if you're using localhost:22195)
82
-
82
+ # Put this in config/initializers/apnd.rb for Rails
83
83
  APND::Notification.upstream_host = 'localhost'
84
84
  APND::Notification.upstream_port = 22195
85
85
 
86
+
87
+ # Initialize some notifications
86
88
  notification1 = APND::Notification.new(
87
89
  :alert => 'Alert!',
88
90
  :token => 'fe15a27d5df3c34778defb1f4f3880265cc52c0c047682223be59fb68500a9a2',
@@ -95,6 +97,7 @@ push notifications to APND.
95
97
  :badge => 99
96
98
  )
97
99
 
100
+
98
101
  # Send multiple notifications at once to avoid overhead in
99
102
  # opening/closing the upstream socket connection each time
100
103
  APND::Notification.open_upstream_socket do |sock|
@@ -102,6 +105,7 @@ push notifications to APND.
102
105
  sock.puts notification2
103
106
  end
104
107
 
108
+
105
109
  # Send a notification to the upstream socket immediately
106
110
  notification3 = APND::Notification.create(
107
111
  :alert => 'Alert!',
data/Rakefile CHANGED
@@ -1,7 +1,5 @@
1
1
  $:.unshift 'lib'
2
2
 
3
- require 'rubygems'
4
-
5
3
  task :default => :test
6
4
 
7
5
  require 'rake/testtask'
@@ -29,5 +27,5 @@ task :publish do
29
27
  sh "git push origin v#{ver}"
30
28
  sh "git push origin master"
31
29
  sh "git clean -fd"
32
- exec "rake pages"
30
+ sh "rake pages"
33
31
  end
@@ -7,8 +7,9 @@ module APND
7
7
  # writen to the AppleConnection
8
8
  #
9
9
  class Daemon
10
- autoload :Protocol, 'apnd/daemon/protocol'
11
- autoload :AppleConnection, 'apnd/daemon/apple_connection'
10
+ autoload :Protocol, 'apnd/daemon/protocol'
11
+ autoload :AppleConnection, 'apnd/daemon/apple_connection'
12
+ autoload :ServerConnection, 'apnd/daemon/server_connection'
12
13
 
13
14
  #
14
15
  # Create a new Daemon and run it
@@ -35,7 +36,7 @@ module APND
35
36
  def run!
36
37
  EventMachine::run do
37
38
  ohai "Starting APND Daemon v#{APND::Version} on #{@bind}:#{@port}"
38
- EventMachine::start_server(@bind, @port, APND::Daemon::Protocol) do |server|
39
+ EventMachine::start_server(@bind, @port, APND::Daemon::ServerConnection) do |server|
39
40
  server.queue = @queue
40
41
  end
41
42
 
@@ -63,7 +63,7 @@ module APND
63
63
  #
64
64
  # Establishes a connection if needed and yields it
65
65
  #
66
- # Ex: open { |conn| conn.write('write to socket) }
66
+ # Ex: open { |conn| conn.write('write to socket') }
67
67
  #
68
68
  def open(&block)
69
69
  unless connected?
@@ -1,19 +1,16 @@
1
+ require 'socket'
2
+
1
3
  module APND
2
4
  #
3
5
  # Daemon::Protocol handles incoming APNs
4
6
  #
5
- class Daemon::Protocol < ::EventMachine::Connection
6
-
7
- #
8
- # Queue should be the EventMachine queue, see APND::Daemon
9
- #
10
- attr_accessor :queue
7
+ module Daemon::Protocol
11
8
 
12
9
  #
13
10
  # Called when a client connection is opened
14
11
  #
15
12
  def post_init
16
- @address = Socket.unpack_sockaddr_in(self.get_peername)
13
+ @address = ::Socket.unpack_sockaddr_in(self.get_peername)
17
14
  ohai "#{@address.last}:#{@address.first} opened connection"
18
15
  end
19
16
 
@@ -0,0 +1,15 @@
1
+ module APND
2
+ #
3
+ # Daemon::ServerConnection links APND::Daemon::Protocol to EM
4
+ #
5
+ class Daemon::ServerConnection < ::EventMachine::Connection
6
+
7
+ include APND::Daemon::Protocol
8
+
9
+ #
10
+ # Queue should be the EventMachine queue, see APND::Daemon
11
+ #
12
+ attr_accessor :queue
13
+
14
+ end
15
+ end
@@ -2,7 +2,7 @@ module APND
2
2
  class Version #:nodoc:
3
3
  MAJOR = 0
4
4
  MINOR = 1
5
- TINY = 5
5
+ TINY = 6
6
6
 
7
7
  def self.to_s
8
8
  [MAJOR, MINOR, TINY].join('.')
@@ -14,24 +14,13 @@ class APNDTest < Test::Unit::TestCase
14
14
  })
15
15
  end
16
16
 
17
- context "instances" do
18
- should "be initialized with a hash of options" do
19
- [:token, :alert, :sound, :badge, :custom].each do |key|
20
- assert_not_nil @notification.send(key)
21
- end
22
- end
23
-
24
- should "return a valid hex_token" do
25
- expected = %|\376\025\242}]\363\303Gx\336\373\037O8\200&\\\305,\f\004v\202";\345\237\266\205\000\251\242|
26
- assert_equal @notification.hex_token, expected
27
- end
28
-
29
- should "return a valid byte string" do
30
- assert_equal @notification.to_bytes, @@bytes
17
+ should "allow initialization with options hash" do
18
+ [:token, :alert, :sound, :badge, :custom].each do |key|
19
+ assert_not_nil @notification.send(key)
31
20
  end
32
21
  end
33
22
 
34
- should "parse a valid packet" do
23
+ should "parse a raw packet" do
35
24
  notification = APND::Notification.parse(@@bytes)
36
25
 
37
26
  assert notification
@@ -41,12 +30,6 @@ class APNDTest < Test::Unit::TestCase
41
30
  end
42
31
  end
43
32
 
44
- should "raise InvalidNotificationHeader parsing a bad packet" do
45
- assert_raise APND::Errors::InvalidNotificationHeader do
46
- APND::Notification.parse("I'm not a packet!")
47
- end
48
- end
49
-
50
33
  should "raise InvalidPayload if custom hash is too large" do
51
34
  assert_raise APND::Errors::InvalidPayload do
52
35
  notification = @notification.dup
@@ -56,13 +39,41 @@ class APNDTest < Test::Unit::TestCase
56
39
  APND::Notification.parse(notification.to_bytes)
57
40
  end
58
41
  end
42
+
43
+ context "instances" do
44
+ should "return a valid hex_token" do
45
+ expected = %|\376\025\242}]\363\303Gx\336\373\037O8\200&\\\305,\f\004v\202";\345\237\266\205\000\251\242|
46
+ assert_equal @notification.hex_token, expected
47
+ end
48
+
49
+ should "return a valid byte string" do
50
+ assert_equal @notification.to_bytes, @@bytes
51
+ end
52
+ end
53
+
59
54
  end
60
55
 
61
56
  context "APND Daemon" do
62
- should "receive multiple Notifications in a single packet" do
63
- notifications = [@@bytes, @@bytes, @@bytes].join("\n")
64
- notifications.each_line do |line|
65
- assert APND::Notification.valid?(line)
57
+ context "Protocol" do
58
+ setup do
59
+ @daemon = TestDaemon.new
60
+ end
61
+
62
+ should "add valid notification to queue" do
63
+ @daemon.receive_data(@@bytes)
64
+ assert_equal 1, @daemon.queue.size
65
+ end
66
+
67
+ should "receive multiple Notifications in a single packet" do
68
+ @daemon.receive_data([@@bytes, @@bytes, @@bytes].join("\n"))
69
+ assert 3, @daemon.queue.size
70
+ end
71
+
72
+ should "raise InvalidNotificationHeader parsing a bad packet" do
73
+ assert_raise APND::Errors::InvalidNotificationHeader do
74
+ APND::Notification.parse("I'm not a packet!")
75
+ end
76
+ assert 0, @daemon.queue.size
66
77
  end
67
78
  end
68
79
  end
@@ -8,3 +8,20 @@ rescue LoadError
8
8
  end
9
9
 
10
10
  require 'apnd'
11
+
12
+ class TestDaemon
13
+ include APND::Daemon::Protocol
14
+
15
+ def initialize
16
+ @queue = []
17
+ @address = [123, '10.10.10.1']
18
+ end
19
+
20
+ def queue
21
+ @queue
22
+ end
23
+
24
+ end
25
+
26
+ # Silence ohai in testing
27
+ def ohai(*args); end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 5
9
- version: 0.1.5
8
+ - 6
9
+ version: 0.1.6
10
10
  platform: ruby
11
11
  authors:
12
12
  - Joshua Priddle
@@ -87,6 +87,7 @@ files:
87
87
  - bin/apnd-push
88
88
  - lib/apnd/daemon/apple_connection.rb
89
89
  - lib/apnd/daemon/protocol.rb
90
+ - lib/apnd/daemon/server_connection.rb
90
91
  - lib/apnd/daemon.rb
91
92
  - lib/apnd/errors.rb
92
93
  - lib/apnd/notification.rb