Class: Mameapns::Connection
- Inherits:
-
Object
- Object
- Mameapns::Connection
- Defined in:
- lib/mameapns/connection.rb
Constant Summary collapse
- IDLE_PERIOD =
1800
Instance Attribute Summary collapse
-
#host ⇒ Object
Returns the value of attribute host.
-
#last_write ⇒ Object
Returns the value of attribute last_write.
-
#port ⇒ Object
Returns the value of attribute port.
-
#ssl_cert ⇒ Object
Returns the value of attribute ssl_cert.
-
#ssl_cert_key ⇒ Object
Returns the value of attribute ssl_cert_key.
-
#ssl_cert_pass ⇒ Object
Returns the value of attribute ssl_cert_pass.
Instance Method Summary collapse
- #close ⇒ Object
- #connect ⇒ Object
- #connect_socket ⇒ Object
- #idle_period_exceeded? ⇒ Boolean
-
#initialize(host, port, options = {}) ⇒ Connection
constructor
A new instance of Connection.
- #read(bytesize) ⇒ Object
- #reconnect ⇒ Object
- #select(timeout) ⇒ Object
- #setup_ssl_context ⇒ Object
- #write(data) ⇒ Object
- #write_data(data) ⇒ Object
- #written ⇒ Object
Constructor Details
#initialize(host, port, options = {}) ⇒ Connection
Returns a new instance of Connection.
11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/mameapns/connection.rb', line 11 def initialize(host, port, ={}) self.host = host self.port = port .each do |k, v| setter_name = "#{k}=" self.__send__(setter_name, v) if respond_to?(setter_name) end written end |
Instance Attribute Details
#host ⇒ Object
Returns the value of attribute host.
7 8 9 |
# File 'lib/mameapns/connection.rb', line 7 def host @host end |
#last_write ⇒ Object
Returns the value of attribute last_write.
7 8 9 |
# File 'lib/mameapns/connection.rb', line 7 def last_write @last_write end |
#port ⇒ Object
Returns the value of attribute port.
7 8 9 |
# File 'lib/mameapns/connection.rb', line 7 def port @port end |
#ssl_cert ⇒ Object
Returns the value of attribute ssl_cert.
7 8 9 |
# File 'lib/mameapns/connection.rb', line 7 def ssl_cert @ssl_cert end |
#ssl_cert_key ⇒ Object
Returns the value of attribute ssl_cert_key.
7 8 9 |
# File 'lib/mameapns/connection.rb', line 7 def ssl_cert_key @ssl_cert_key end |
#ssl_cert_pass ⇒ Object
Returns the value of attribute ssl_cert_pass.
7 8 9 |
# File 'lib/mameapns/connection.rb', line 7 def ssl_cert_pass @ssl_cert_pass end |
Instance Method Details
#close ⇒ Object
28 29 30 31 32 33 34 |
# File 'lib/mameapns/connection.rb', line 28 def close begin @tcp_socket.close @ssl_socket.close rescue IOError end end |
#connect ⇒ Object
23 24 25 26 |
# File 'lib/mameapns/connection.rb', line 23 def connect setup_ssl_context @tcp_socket, @ssl_socket = connect_socket end |
#connect_socket ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/mameapns/connection.rb', line 94 def connect_socket begin tcp_socket = TCPSocket.new(host, port) tcp_socket.setsockopt(Socket::SOL_SOCKET, Socket::SO_KEEPALIVE, 1) tcp_socket.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1) ssl_socket = OpenSSL::SSL::SSLSocket.new(tcp_socket, @ssl_context) ssl_socket.sync = true ssl_socket.connect [tcp_socket, ssl_socket] rescue OpenSSL::SSL::SSLError => e raise SSLError, e rescue SocketError => e raise ConnectionError, e end end |
#idle_period_exceeded? ⇒ Boolean
75 76 77 |
# File 'lib/mameapns/connection.rb', line 75 def idle_period_exceeded? Time.now - last_write > IDLE_PERIOD end |
#read(bytesize) ⇒ Object
36 37 38 |
# File 'lib/mameapns/connection.rb', line 36 def read(bytesize) @ssl_socket.read(bytesize) end |
#reconnect ⇒ Object
44 45 46 47 |
# File 'lib/mameapns/connection.rb', line 44 def reconnect close @tcp_socket, @ssl_socket = connect_socket end |
#select(timeout) ⇒ Object
40 41 42 |
# File 'lib/mameapns/connection.rb', line 40 def select(timeout) IO.select([@ssl_socket], nil, nil, timeout) end |
#setup_ssl_context ⇒ Object
83 84 85 86 87 88 89 90 91 92 |
# File 'lib/mameapns/connection.rb', line 83 def setup_ssl_context begin @ssl_context = OpenSSL::SSL::SSLContext.new @ssl_context.key = OpenSSL::PKey::RSA.new(ssl_cert_key, ssl_cert_pass) @ssl_context.cert = OpenSSL::X509::Certificate.new(ssl_cert) @ssl_context rescue OpenSSL::PKey::RSAError => e raise SSLError, "Neither PUB key nor PRIV key:: nested asn1 error" end end |
#write(data) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/mameapns/connection.rb', line 49 def write(data) reconnect if idle_period_exceeded? retry_count = 0 begin write_data(data) rescue Errno::EPIPE, Errno::ETIMEOUT, OpenSSL::SSL::SSLError => e retry_count += 1 if retry_count <= 3 reconnect sleep 1 retry else raise ConnectionError, "Attempt to reconnect but failed" end end end |
#write_data(data) ⇒ Object
69 70 71 72 73 |
# File 'lib/mameapns/connection.rb', line 69 def write_data(data) @ssl_socket.write(data) @ssl_socket.flush written end |
#written ⇒ Object
79 80 81 |
# File 'lib/mameapns/connection.rb', line 79 def written self.last_write = Time.now end |