Class: Stomper::Sockets::SSL

Inherits:
OpenSSL::SSL::SSLSocket
  • Object
show all
Defined in:
lib/stomper/sockets.rb

Overview

A wrapper for an SSL Socket that tidies up the SSL specifics so our Connection library isn’t troubled by them.

Constant Summary collapse

DEFAULT_SSL_OPTIONS =

Default SSL options to use with new Stomper::Sockets::SSL connections.

Returns:

  • (Symbol => Object)
{
  :verify_mode => ::OpenSSL::SSL::VERIFY_PEER |
    ::OpenSSL::SSL::VERIFY_FAIL_IF_NO_PEER_CERT,
  :ca_file => nil,
  :ca_path => nil,
  :cert => nil,
  :key => nil,
  :post_connection_check => true
}

Instance Method Summary collapse

Constructor Details

#initialize(host, port, ssl_opts = {}) ⇒ SSL

Create a new Stomper::Sockets::SSL connection to host on port.

Parameters:

  • host (String)

    hostname or IP address to connect to

  • port (Fixnum)

    port number to establish the connection on

  • opts (Hash)

    a customizable set of options



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/stomper/sockets.rb', line 36

def initialize(host, port, ssl_opts={})
  ssl_opts = DEFAULT_SSL_OPTIONS.merge(ssl_opts)

  @context = ::OpenSSL::SSL::SSLContext.new
  post_check = ssl_opts.delete(:post_connection_check)
  post_check_host = (post_check == true) ? host : post_check
  
  DEFAULT_SSL_OPTIONS.keys.each do |k|
    @context.__send__(:"#{k}=", ssl_opts[k]) if ssl_opts.key?(k)
  end

  tcp_sock = ::TCPSocket.new(host, port)
  @socket = ::OpenSSL::SSL::SSLSocket.new(tcp_sock, @context)
  @socket.sync_close = true
  @socket.connect
  if post_check_host
    @socket.post_connection_check(post_check_host)
  end
  super(@socket)
end

Instance Method Details

#ready?Boolean

Passes the :ready? message on to the socket’s underlying io object.

Returns:

  • (Boolean)


58
# File 'lib/stomper/sockets.rb', line 58

def ready?; @socket.io.ready?; end

#shutdown(mode = 2) ⇒ Object

Passes the :shutdown message on to the socket’s underlying io object.



61
# File 'lib/stomper/sockets.rb', line 61

def shutdown(mode=2); @socket.io.shutdown(mode); end