push-gcm 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,3 @@
1
1
  module PushGcm
2
- VERSION = "1.0.0"
2
+ VERSION = "1.0.1"
3
3
  end
@@ -1,6 +1,8 @@
1
1
  module Push
2
2
  module Daemon
3
3
  module GcmSupport
4
+ class ConnectionError < StandardError; end
5
+
4
6
  class ConnectionGcm
5
7
  attr_reader :response, :name, :provider
6
8
  PUSH_URL = "https://android.googleapis.com/gcm/send"
@@ -50,7 +52,6 @@ module Push
50
52
  def open_http(host, port)
51
53
  http = Net::HTTP.new(host, port)
52
54
  http.use_ssl = true
53
- http.verify_mode = OpenSSL::SSL::VERIFY_NONE
54
55
  return http
55
56
  end
56
57
 
@@ -59,15 +60,48 @@ module Push
59
60
  "Content-type" => "application/json",
60
61
  "Content-length" => "#{data.length}" }
61
62
  uri = URI.parse(PUSH_URL)
63
+ post(uri, data, headers)
64
+ end
62
65
 
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
66
+ def post(uri, data, headers)
67
+ reconnect_idle if idle_period_exceeded?
68
+
69
+ retry_count = 0
70
+
71
+ begin
72
+ response = @connection.post(uri.path, data, headers)
73
+ @last_use = Time.now
74
+ rescue EOFError, Errno::ECONNRESET, Timeout::Error => e
75
+ retry_count += 1
76
+
77
+ Push::Daemon.logger.error("[#{@name}] Lost connection to #{PUSH_URL} (#{e.class.name}), reconnecting ##{retry_count}...")
78
+
79
+ if retry_count <= 3
80
+ reconnect
81
+ sleep 1
82
+ retry
83
+ else
84
+ raise ConnectionError, "#{@name} tried #{retry_count-1} times to reconnect but failed (#{e.class.name})."
85
+ end
67
86
  end
68
- @last_use = Time.now
69
87
 
70
- @connection.post(uri.path, data, headers)
88
+ response
89
+ end
90
+
91
+ def idle_period_exceeded?
92
+ # Timeout on the http connection is 5 minutes, reconnect after 5 minutes
93
+ @last_use + IDLE_PERIOD < Time.now
94
+ end
95
+
96
+ def reconnect_idle
97
+ Push::Daemon.logger.info("[#{@name}] Idle period exceeded, reconnecting...")
98
+ reconnect
99
+ end
100
+
101
+ def reconnect
102
+ @connection.finish
103
+ @last_use = Time.now
104
+ @connection.start
71
105
  end
72
106
  end
73
107
  end
@@ -26,7 +26,6 @@ module Push
26
26
 
27
27
  def check_for_error(connection)
28
28
  response = connection.response
29
-
30
29
  if response.code.eql? "200"
31
30
  hsh = MultiJson.load(response.body)
32
31
  if hsh["failure"] == 1
@@ -41,10 +40,10 @@ module Push
41
40
  Push::FeedbackGcm.create!(:app => connection.provider.configuration[:name], :failed_at => Time.now,
42
41
  :device => device, :follow_up => 'delete')
43
42
  end
44
- else
45
- Push::Daemon.logger.error("[#{connection.name}] Error received.")
46
- raise Push::DeliveryError.new(response.code, id, msg, "GCM")
47
43
  end
44
+
45
+ Push::Daemon.logger.error("[#{connection.name}] Error received.")
46
+ raise Push::DeliveryError.new(response.code, id, msg, "GCM")
48
47
  elsif hsh["canonical_ids"] == 1
49
48
  # success, but update device token
50
49
  update_to = hsh["results"][0]["registration_id"]
@@ -55,7 +54,7 @@ module Push
55
54
  end
56
55
  else
57
56
  Push::Daemon.logger.error("[#{connection.name}] Error received.")
58
- raise Push::DeliveryError.new(response.code, id, response.body, "GCM")
57
+ raise Push::DeliveryError.new(response.code, id, response.message, "GCM")
59
58
  end
60
59
  end
61
60
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: push-gcm
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-09-19 00:00:00.000000000 Z
12
+ date: 2012-09-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: multi_json
16
- requirement: &70179723946700 !ruby/object:Gem::Requirement
16
+ requirement: &70221520088900 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '1.0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70179723946700
24
+ version_requirements: *70221520088900
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: push-core
27
- requirement: &70179723945680 !ruby/object:Gem::Requirement
27
+ requirement: &70221520087640 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 1.0.0
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70179723945680
35
+ version_requirements: *70221520087640
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: sqlite3
38
- requirement: &70179723945080 !ruby/object:Gem::Requirement
38
+ requirement: &70221520086880 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70179723945080
46
+ version_requirements: *70221520086880
47
47
  description: GCM support for the modular push daemon.
48
48
  email:
49
49
  - tom@tnux.net