Class: Rapns::Daemon::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/rapns/daemon/connection.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, host, port) ⇒ Connection

Returns a new instance of Connection.



12
13
14
15
16
17
# File 'lib/rapns/daemon/connection.rb', line 12

def initialize(name, host, port)
  @name = name
  @host = host
  @port = port
  written
end

Instance Attribute Details

#last_writeObject

Returns the value of attribute last_write.



6
7
8
# File 'lib/rapns/daemon/connection.rb', line 6

def last_write
  @last_write
end

Class Method Details

.idle_periodObject



8
9
10
# File 'lib/rapns/daemon/connection.rb', line 8

def self.idle_period
  30.minutes
end

Instance Method Details

#closeObject



24
25
26
27
28
29
30
# File 'lib/rapns/daemon/connection.rb', line 24

def close
  begin
    @ssl_socket.close if @ssl_socket
    @tcp_socket.close if @tcp_socket
  rescue IOError
  end
end

#connectObject



19
20
21
22
# File 'lib/rapns/daemon/connection.rb', line 19

def connect
  @ssl_context = setup_ssl_context
  @tcp_socket, @ssl_socket = connect_socket
end

#read(num_bytes) ⇒ Object



32
33
34
# File 'lib/rapns/daemon/connection.rb', line 32

def read(num_bytes)
  @ssl_socket.read(num_bytes)
end

#reconnectObject



64
65
66
67
# File 'lib/rapns/daemon/connection.rb', line 64

def reconnect
  close
  @tcp_socket, @ssl_socket = connect_socket
end

#select(timeout) ⇒ Object



36
37
38
# File 'lib/rapns/daemon/connection.rb', line 36

def select(timeout)
  IO.select([@ssl_socket], nil, nil, timeout)
end

#write(data) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/rapns/daemon/connection.rb', line 40

def write(data)
  reconnect_idle if idle_period_exceeded?

  retry_count = 0

  begin
    write_data(data)
  rescue Errno::EPIPE, Errno::ETIMEDOUT, OpenSSL::SSL::SSLError => e
    retry_count += 1;

    if retry_count == 1
      Rapns::Daemon.logger.error("[#{@name}] Lost connection to #{@host}:#{@port} (#{e.class.name}), reconnecting...")
    end

    if retry_count <= 3
      reconnect
      sleep 1
      retry
    else
      raise ConnectionError, "#{@name} tried #{retry_count-1} times to reconnect but failed (#{e.class.name})."
    end
  end
end