Class: Dalli::Elasticache::AutoDiscovery::BaseCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/dalli/elasticache/auto_discovery/base_command.rb

Overview

Base command class for configuration endpoint command. Contains the network logic.

Direct Known Subclasses

ConfigCommand, StatsCommand

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, port, timeout = nil) ⇒ BaseCommand

Returns a new instance of BaseCommand.



13
14
15
16
17
# File 'lib/dalli/elasticache/auto_discovery/base_command.rb', line 13

def initialize(host, port, timeout = nil)
  @host = host
  @port = port
  @timeout = timeout
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



11
12
13
# File 'lib/dalli/elasticache/auto_discovery/base_command.rb', line 11

def host
  @host
end

#portObject (readonly)

Returns the value of attribute port.



11
12
13
# File 'lib/dalli/elasticache/auto_discovery/base_command.rb', line 11

def port
  @port
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



11
12
13
# File 'lib/dalli/elasticache/auto_discovery/base_command.rb', line 11

def timeout
  @timeout
end

Instance Method Details

#response_from_socket(socket) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/dalli/elasticache/auto_discovery/base_command.rb', line 32

def response_from_socket(socket)
  data = +''
  loop do
    wait_for_data(socket)
    line = socket.readline
    break if line.include?('END')

    data << line
  end

  data
end

#send_commandObject

Send an ASCII command to the endpoint

Returns the raw response as a String



22
23
24
25
26
27
28
29
30
# File 'lib/dalli/elasticache/auto_discovery/base_command.rb', line 22

def send_command
  socket = ::Socket.tcp(@host, @port, connect_timeout: timeout)
  begin
    socket.puts command
    response_from_socket(socket)
  ensure
    socket.close
  end
end