Class: ClamAV::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/clamav/connection.rb

Defined Under Namespace

Classes: ReadTimeoutError, WriteTimeoutError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ Connection

Returns a new instance of Connection.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/clamav/connection.rb', line 28

def initialize(attrs={})
  attrs.each do |attr, value|
    send("#{attr}=", value)
  end

  begin
    validate!
  rescue => e
    @client = nil
    @socket = nil
    @wrapper = nil

    raise e
  end
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



24
25
26
# File 'lib/clamav/connection.rb', line 24

def client
  @client
end

#socketObject

Returns the value of attribute socket.



25
26
27
# File 'lib/clamav/connection.rb', line 25

def socket
  @socket
end

#wrapperObject

Returns the value of attribute wrapper.



26
27
28
# File 'lib/clamav/connection.rb', line 26

def wrapper
  @wrapper
end

Instance Method Details

#disconnect!Object



105
106
107
108
109
110
111
112
113
# File 'lib/clamav/connection.rb', line 105

def disconnect!
  return true if @socket.nil?

  @socket.close

  @socket.closed?.tap do
    @socket = nil
  end
end

#establish_connectionObject



62
63
64
# File 'lib/clamav/connection.rb', line 62

def establish_connection
  write_request("IDSESSION")
end

#raw_write(str) ⇒ Object



120
121
122
# File 'lib/clamav/connection.rb', line 120

def raw_write(str)
  @socket.write str
end

#read_responseObject



87
88
89
90
91
# File 'lib/clamav/connection.rb', line 87

def read_response
  return read_response_with_timeout if read_timeout

  read_response_without_timeout
end

#read_response_with_timeoutObject



97
98
99
100
101
102
103
# File 'lib/clamav/connection.rb', line 97

def read_response_with_timeout
  Timeout::timeout(read_timeout) do
    read_response_without_timeout
  end
rescue Timeout::Error => e
  raise ReadTimeoutError.new(e.to_s)
end

#read_response_without_timeoutObject



93
94
95
# File 'lib/clamav/connection.rb', line 93

def read_response_without_timeout
  @wrapper.read_response(@socket)
end

#send_request(str) ⇒ Object



115
116
117
118
# File 'lib/clamav/connection.rb', line 115

def send_request(str)
  write_request(str)
  read_response
end

#validate!Object



44
45
46
47
48
# File 'lib/clamav/connection.rb', line 44

def validate!
  missing_required_argument(:client) if !client
  missing_required_argument(:socket) if !socket
  missing_required_argument(:wrapper) if !wrapper
end

#write_request(str) ⇒ Object



66
67
68
69
70
# File 'lib/clamav/connection.rb', line 66

def write_request(str)
  return write_request_with_timeout(str) if write_timeout

  write_request_without_timeout(str)
end

#write_request_with_timeout(str) ⇒ Object



78
79
80
81
82
83
84
85
# File 'lib/clamav/connection.rb', line 78

def write_request_with_timeout(str)
  Timeout::timeout(connect_timeout) do
    write_request_without_timeout(str)
  end
rescue Timeout::Error => e

  raise WriteTimeoutError.new(e.to_s)
end

#write_request_without_timeout(str) ⇒ Object



72
73
74
75
76
# File 'lib/clamav/connection.rb', line 72

def write_request_without_timeout(str)
  wrapped_request = @wrapper.wrap_request(str)

  @socket.write wrapped_request
end