Class: LVS::JsonService::ConnectionManager

Inherits:
Object
  • Object
show all
Defined in:
lib/lvs/json_service/connection_manager.rb

Class Method Summary collapse

Class Method Details

.create_connection(host, port, options) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/lvs/json_service/connection_manager.rb', line 25

def self.create_connection(host, port, options)
  http = Net::HTTP.new(host, port)
  if options[:encrypted]
    http.use_ssl = true
    # Self-signed certs give streams of "warning: peer certificate won't be verified in this SSL session"
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE 
    LVS::JsonService::Logger.debug "Using SSL"
    if options[:auth_cert]
      LVS::JsonService::Logger.debug "Using Auth"
      http.cert = OpenSSL::X509::Certificate.new(File.read(options[:auth_cert]))
      http.key = OpenSSL::PKey::RSA.new(File.read(options[:auth_key]), options[:auth_key_password])
    end
  end

  http.open_timeout = options[:timeout] || 1
  http.read_timeout = options[:timeout] || 1
  LVS::JsonService::Logger.debug "Connecting"
  http.start
  http
end

.get_connection(host, port, options) ⇒ Object



4
5
6
7
8
9
10
11
12
13
# File 'lib/lvs/json_service/connection_manager.rb', line 4

def self.get_connection(host, port, options)
  @@connections ||= {}
  key = create_key(host, port, options)
  connection = @@connections[key]
  if connection.nil? || !connection.started?
    connection = create_connection(host, port, options)
    @@connections[key] = connection
  end
  connection
end

.reset_all_connectionsObject



21
22
23
# File 'lib/lvs/json_service/connection_manager.rb', line 21

def self.reset_all_connections
  @@connections = {}
end

.reset_connection(host, port, options) ⇒ Object



15
16
17
18
19
# File 'lib/lvs/json_service/connection_manager.rb', line 15

def self.reset_connection(host, port, options)
  @@connections ||= {}
  key = create_key(host, port, options)
  @@connections.delete(key)
end