Class: Push::Daemon::C2dmSupport::ConnectionC2dm

Inherits:
Object
  • Object
show all
Defined in:
lib/push/daemon/c2dm_support/connection_c2dm.rb

Constant Summary collapse

AUTH_URL =
"https://www.google.com/accounts/ClientLogin"
PUSH_URL =
"https://android.apis.google.com/c2dm/send"
IDLE_PERIOD =
5.minutes

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(provider, i) ⇒ ConnectionC2dm

Returns a new instance of ConnectionC2dm.



12
13
14
15
16
17
18
# File 'lib/push/daemon/c2dm_support/connection_c2dm.rb', line 12

def initialize(provider, i)
  @provider = provider
  @name = "#{@provider.configuration[:name]}: ConnectionC2dm #{i}"

  @email = @provider.configuration[:email]
  @password = @provider.configuration[:password]
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/push/daemon/c2dm_support/connection_c2dm.rb', line 7

def name
  @name
end

#providerObject (readonly)

Returns the value of attribute provider.



7
8
9
# File 'lib/push/daemon/c2dm_support/connection_c2dm.rb', line 7

def provider
  @provider
end

#responseObject (readonly)

Returns the value of attribute response.



7
8
9
# File 'lib/push/daemon/c2dm_support/connection_c2dm.rb', line 7

def response
  @response
end

Instance Method Details

#connectObject



20
21
22
23
24
25
26
27
# File 'lib/push/daemon/c2dm_support/connection_c2dm.rb', line 20

def connect
  @auth_token = fetch_auth_token
  @last_use = Time.now
  uri = URI.parse(PUSH_URL)
  @connection = open_http(uri.host, uri.port)
  @connection.start
  Push::Daemon.logger.info("[#{@name}] Connected to #{PUSH_URL}")
end

#write(data) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/push/daemon/c2dm_support/connection_c2dm.rb', line 29

def write(data)
  @response = notification_request(data)

  # the response can be one of three codes:
  #   200 (success)
  #   401 (auth failed)
  #   503 (retry later with exponential backoff)
  #   see more documentation here:  http://code.google.com/android/c2dm/#testing
  if @response.code.eql? "200"
    # look for the header 'Update-Client-Auth' in the response you get after sending
    # a message. It indicates that this is the token to be used for the next message to send.
    @response.header.each_header do |key, value|
      if key.capitalize == "Update-Client-Auth".capitalize
        Push::Daemon.logger.info("[#{@name}] Received new authentication token")
        @auth_token = value
      end
    end

  elsif @response.code.eql? "401"
    # auth failed.  Refresh auth key and requeue
    @auth_token = fetch_auth_token
    @response = notification_request(data)

  elsif response.code.eql? "503"
    # service un-available.
  end
end