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

.close_all_connectionsObject



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/lvs/json_service/connection_manager.rb', line 27

def self.close_all_connections
  LVS::JsonService::Logger.debug "Requesting to close all (#{@@connections.size}) connections"
  @@connections.each do |key, connection|
    begin
      LVS::JsonService::Logger.debug "Disconnecting from #{host}:#{port}"
      connection.finish
    rescue IOError
      # Do nothing
    end
  end
  reset_all_connections
end

.create_connection(host, port, options) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/lvs/json_service/connection_manager.rb', line 44

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

  LVS::JsonService::Logger.debug "Connecting to #{host}:#{port}"
  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)
  connection = @@connections[key]
  if connection.nil? || !connection.started?
    connection = create_connection(host, port, options)
    @@connections[key] = connection
  end
  connection
end

.reset_all_connectionsObject



40
41
42
# File 'lib/lvs/json_service/connection_manager.rb', line 40

def self.reset_all_connections
  @@connections = {}
end

.reset_connection(host, port, options) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/lvs/json_service/connection_manager.rb', line 15

def self.reset_connection(host, port, options)
  @@connections ||= {}
  key = create_key(host, port)
  begin
    LVS::JsonService::Logger.debug "Disconnecting from #{host}:#{port}"
    @@connections[key].finish
  rescue IOError
    # Do nothing
  end
  @@connections.delete(key)
end