push-gcm 0.0.1.pre

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2012 YOURNAME
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # PushGCM
2
+
3
+ Please see [push-core](https://github.com/tompesman/push-core) for more information.
data/lib/push-gcm.rb ADDED
@@ -0,0 +1,9 @@
1
+ require 'net/http'
2
+ require 'net/https'
3
+ require 'push-gcm/version'
4
+ require 'push/message'
5
+ require 'push/message_gcm'
6
+ require 'push/feedback'
7
+ require 'push/feedback_gcm'
8
+ require 'push/daemon/gcm'
9
+ require 'push/daemon/gcm_support/connection_gcm'
@@ -0,0 +1,3 @@
1
+ module PushGcm
2
+ VERSION = "0.0.1.pre"
3
+ end
@@ -0,0 +1,24 @@
1
+ module Push
2
+ module Daemon
3
+ class Gcm
4
+ attr_accessor :configuration
5
+ def initialize(options)
6
+ self.configuration = options
7
+ end
8
+
9
+ def pushconnections
10
+ self.configuration[:connections]
11
+ end
12
+
13
+ def totalconnections
14
+ pushconnections
15
+ end
16
+
17
+ def connectiontype
18
+ GcmSupport::ConnectionGcm
19
+ end
20
+
21
+ def stop; end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,75 @@
1
+ module Push
2
+ module Daemon
3
+ module GcmSupport
4
+ class ConnectionGcm
5
+ attr_reader :response, :name
6
+ PUSH_URL = "https://android.googleapis.com/gcm/send"
7
+ IDLE_PERIOD = 5.minutes
8
+
9
+ def initialize(provider, i)
10
+ @provider = provider
11
+ @name = "ConnectionGcm #{i}"
12
+ end
13
+
14
+ def connect
15
+ @last_use = Time.now
16
+ uri = URI.parse(PUSH_URL)
17
+ @connection = open_http(uri.host, uri.port)
18
+ @connection.start
19
+ Push::Daemon.logger.info("[#{@name}] Connected to #{PUSH_URL}")
20
+ end
21
+
22
+ def write(data)
23
+ @response = notification_request(data)
24
+
25
+ # if @response.code.eql? "200"
26
+ # puts "success, but can have an exception in "
27
+ # elsif @response.code.eql? "400"
28
+ # puts "formatting exception"
29
+ # elsif @response.code.eql? "401"
30
+ # puts "authentication exception"
31
+ # elsif @response.code.eql? "500"
32
+ # puts "internal error GCM server"
33
+ # elsif response.code.eql? "503"
34
+ # puts "service un-available: exponential back-off"
35
+ #
36
+ # # do not retry for now
37
+ #
38
+ # # @response.header.each_header do |key, value|
39
+ # # if key.capitalize == "Retry-After".capitalize
40
+ # # # TODO USE DELAY
41
+ # # @delay_by = value
42
+ # # end
43
+ # # end
44
+ # # TODO or exponentional back-off
45
+ # end
46
+ end
47
+
48
+ private
49
+
50
+ def open_http(host, port)
51
+ http = Net::HTTP.new(host, port)
52
+ http.use_ssl = true
53
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
54
+ return http
55
+ end
56
+
57
+ def notification_request(data)
58
+ headers = { "Authorization" => "key=#{@provider.configuration[:key]}",
59
+ "Content-type" => "application/json",
60
+ "Content-length" => "#{data.length}" }
61
+ uri = URI.parse(PUSH_URL)
62
+
63
+ # Timeout on the http connection is 5 minutes, reconnect after 5 minutes
64
+ if @last_use + IDLE_PERIOD < Time.now
65
+ @connection.finish
66
+ @connection.start
67
+ end
68
+ @last_use = Time.now
69
+
70
+ @connection.post(uri.path, data, headers)
71
+ end
72
+ end
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,5 @@
1
+ module Push
2
+ class FeedbackGcm < Push::Feedback
3
+
4
+ end
5
+ end
@@ -0,0 +1,60 @@
1
+ module Push
2
+ class MessageGcm < Push::Message
3
+ validates :collapse_key, :presence => true
4
+ # TODO: validates max size -> The message size limit is 4096 bytes.
5
+ # The total size of the payload data that is included in a message can't exceed 4096 bytes.
6
+ # Note that this includes both the size of the keys as well as the values.
7
+
8
+ store :properties, accessors: [:collapse_key, :delay_when_idle, :time_to_live, :payload]
9
+ attr_accessible :device, :collapse_key, :delay_when_idle, :time_to_live, :payload
10
+
11
+ def to_message
12
+ hsh = Hash.new
13
+ hsh['registration_ids'] = [device]
14
+ hsh['collapse_key'] = collapse_key
15
+ hsh['delay_when_idle'] = delay_when_idle if delay_when_idle
16
+ hsh['time_to_live'] = time_to_live if time_to_live
17
+ hsh['data'] = payload
18
+ MultiJson.dump(hsh)
19
+ end
20
+
21
+ def use_connection
22
+ Push::Daemon::GcmSupport::ConnectionGcm
23
+ end
24
+
25
+ private
26
+
27
+ def check_for_error(connection)
28
+ response = connection.response
29
+
30
+ if response.code.eql? "200"
31
+ hsh = MultiJson.load(response.body)
32
+ if hsh["failure"] == 1
33
+ msg = hsh["results"][0]["error"]
34
+
35
+ # MissingRegistration, handled by validation
36
+ # MismatchSenderId, configuration error by client
37
+ # MessageTooBig, TODO: add validation
38
+
39
+ if msg == "NotRegistered" or msg == "InvalidRegistration"
40
+ with_database_reconnect_and_retry(connection.name) do
41
+ Push::FeedbackGcm.create!(:failed_at => Time.now, :device => device) # follow-up: delete device
42
+ end
43
+ end
44
+
45
+ Push::Daemon.logger.error("[#{connection.name}] Error received.")
46
+ raise Push::DeliveryError.new(response.code, id, msg, "GCM")
47
+ elsif hsh["canonical_ids"] == 1
48
+ # success, but update device token
49
+ # follow-up: delete device
50
+ # with_database_reconnect_and_retry(connection.name) do
51
+ # Push::FeedbackGcm.create!(:failed_at => Time.now, :device => device) # follow-up: update device
52
+ # end
53
+ end
54
+ else
55
+ Push::Daemon.logger.error("[#{connection.name}] Error received.")
56
+ raise Push::DeliveryError.new(response.code, id, response.body, "GCM")
57
+ end
58
+ end
59
+ end
60
+ end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: push-gcm
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1.pre
5
+ prerelease: 6
6
+ platform: ruby
7
+ authors:
8
+ - Tom Pesman
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-07-05 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: multi_json
16
+ requirement: &70315674133460 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70315674133460
25
+ - !ruby/object:Gem::Dependency
26
+ name: push-core
27
+ requirement: &70315674131040 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - =
31
+ - !ruby/object:Gem::Version
32
+ version: 0.0.1.pre2
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70315674131040
36
+ - !ruby/object:Gem::Dependency
37
+ name: sqlite3
38
+ requirement: &70315674130040 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70315674130040
47
+ description: GCM support for the modular push daemon.
48
+ email:
49
+ - tom@tnux.net
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - lib/push-gcm.rb
55
+ - lib/push-gcm/version.rb
56
+ - lib/push/daemon/gcm.rb
57
+ - lib/push/daemon/gcm_support/connection_gcm.rb
58
+ - lib/push/feedback_gcm.rb
59
+ - lib/push/message_gcm.rb
60
+ - README.md
61
+ - MIT-LICENSE
62
+ homepage: https://github.com/tompesman/push-gcm
63
+ licenses: []
64
+ post_install_message:
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ! '>='
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ! '>'
78
+ - !ruby/object:Gem::Version
79
+ version: 1.3.1
80
+ requirements: []
81
+ rubyforge_project:
82
+ rubygems_version: 1.8.5
83
+ signing_key:
84
+ specification_version: 3
85
+ summary: GCM (Android) part of the modular push daemon.
86
+ test_files: []