Class: Suj::Pusher::ConnectionPool

Inherits:
Object
  • Object
show all
Includes:
Logger
Defined in:
lib/suj/pusher/connection_pool.rb

Defined Under Namespace

Classes: UnknownConnection

Constant Summary collapse

APN_SANDBOX =
"gateway.sandbox.push.apple.com"
APN_GATEWAY =
"gateway.push.apple.com"
FEEDBACK_SANDBOX =
"feedback.sandbox.push.apple.com"
FEEDBACK_GATEWAY =
"feedback.push.apple.com"
CONNECTION_EXPIRY =

Approx 1 month

60*60*24*30
TOKEN_EXPIRY =

Approx 1 week

60*60*24*7
APN_PORT =
2195
FEEDBACK_PORT =
2196

Instance Method Summary collapse

Constructor Details

#initialize(daemon) ⇒ ConnectionPool

Returns a new instance of ConnectionPool.



22
23
24
25
26
27
28
29
# File 'lib/suj/pusher/connection_pool.rb', line 22

def initialize(daemon)
  @pool = {}
  @feedback_pool = {}
  @daemon = daemon
  @mutex = Mutex.new
  @feedback_mutex = Mutex.new
  @invalid_tokens = Vash.new
end

Instance Method Details

#closeObject



95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/suj/pusher/connection_pool.rb', line 95

def close
  @mutex.synchronize {
    @pool.each do |k, conn|
      conn.close_connection_after_writing
    end
  }
  @feedback_mutex.synchronize {
    @feedback_pool.each do |k, conn|
      conn.close_connection
    end
  }
end

#feedbackObject

Method that creates APN feedback connections



85
86
87
88
89
90
91
92
93
# File 'lib/suj/pusher/connection_pool.rb', line 85

def feedback
  @invalid_tokens.cleanup!
  @invalid_tokens.each { |k,v| v.cleanup! if v }

  @pool.each do |k, conn|
    next if ! conn.is_a?(Suj::Pusher::APNConnection)
    conn = get_feedback_connection(conn.options)
  end
end

#get_connection(options = {}) ⇒ Object

Raises:



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/suj/pusher/connection_pool.rb', line 31

def get_connection(options = {})
  if options.has_key?(:cert)
    if options.has_key?(:development) && options[:development]
      return apn_sandbox_connection(options)
    else
      return apn_connection(options)
    end
  elsif options.has_key?(:api_key)
    return gcm_connection(options)
  elsif options.has_key?(:secret) && options.has_key?(:sid)
    return wns_connection(options)
  elsif  options.has_key?(:wpnotificationclass)
    return wpns_connection(options)
  end
  raise UnknownConnection
end

#get_feedback_connection(options = {}) ⇒ Object



48
49
50
51
52
53
54
55
56
57
# File 'lib/suj/pusher/connection_pool.rb', line 48

def get_feedback_connection(options = {})
  if options.has_key?(:cert)
    if options.has_key?(:development) && options[:development]
      return feedback_sandbox_connection(options)
    else
      return feedback_connection(options)
    end
  end
  return nil
end

#invalidate_token(conn, token) ⇒ Object



73
74
75
76
# File 'lib/suj/pusher/connection_pool.rb', line 73

def invalidate_token(conn, token)
  @invalid_tokens[conn, CONNECTION_EXPIRY] = Vash.new if @invalid_tokens[conn].nil?
  @invalid_tokens[conn][token, TOKEN_EXPIRY] = Time.now.to_s
end

#pending_connections?Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/suj/pusher/connection_pool.rb', line 108

def pending_connections?
  @pool.size + @feedback_pool.size > 0
end

#remove_connection(key) ⇒ Object



66
67
68
69
70
71
# File 'lib/suj/pusher/connection_pool.rb', line 66

def remove_connection(key)
  @mutex.synchronize {
    info "Removing connection #{key}"
    info "Connection not found" unless @pool.delete(key)
  }
end

#remove_feedback_connection(key) ⇒ Object



59
60
61
62
63
64
# File 'lib/suj/pusher/connection_pool.rb', line 59

def remove_feedback_connection(key)
  @feedback_mutex.synchronize {
    info "Removing feedback connection #{key}"
    @feedback_pool.delete(key)
  }
end

#valid_token?(conn, token) ⇒ Boolean

Returns:

  • (Boolean)


78
79
80
81
82
# File 'lib/suj/pusher/connection_pool.rb', line 78

def valid_token?(conn, token)
  return true if ! @invalid_tokens[conn]
  return false if @invalid_tokens[conn].has_key?(token)
  return true
end