Class: TCPClient::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/tcp-client/configuration.rb,
lib/tcp-client/default_configuration.rb

Overview

A Configuration is used to configure the behavior of a TCPClient instance.

It allows to specify the monitor timeout, how to handle exceptions, if SSL should be used and to setup the underlying Socket.

Instance Attributes Socket Level collapse

Instance Attributes Timeout Monitoring collapse

Other Instance Attributes collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = nil) ⇒ Configuration

Initializes and optionally configures the instance with given options.

See Also:



44
45
46
47
48
49
50
51
52
# File 'lib/tcp-client/configuration.rb', line 44

def initialize(options = nil)
  @buffered = @keep_alive = @reverse_lookup = true
  self.timeout = @ssl_params = nil
  @connect_timeout_error = ConnectTimeoutError
  @read_timeout_error = ReadTimeoutError
  @write_timeout_error = WriteTimeoutError
  @normalize_network_errors = false
  configure(options) if options
end

Instance Attribute Details

#:default(: default) ⇒ Configuration (readonly)

Returns used by default if no dedicated configuration was specified.

Returns:

  • (Configuration)

    used by default if no dedicated configuration was specified

See Also:



49
# File 'lib/tcp-client/default_configuration.rb', line 49

def self.default = TCPClient.default_configuration

#bufferedBoolean

Enables/disables use of Socket-level buffering

Returns:

  • (Boolean)

    whether the connection is allowed to use internal buffers (default) or not



62
63
64
# File 'lib/tcp-client/configuration.rb', line 62

def buffered
  @buffered
end

#connect_timeoutNumeric?

The maximum time in seconds to establish a connection.

Returns:

  • (Numeric)

    maximum time in seconds

  • (nil)

    if the connect time should not be monitored (default)

See Also:



131
132
133
# File 'lib/tcp-client/configuration.rb', line 131

def connect_timeout
  @connect_timeout
end

#connect_timeout_errorClass<Exception>

The exception class which will be raised if TCPClient#connect can not be finished in time.

Returns:

  • (Class<Exception>)

    exception class raised

Raises:



144
145
146
# File 'lib/tcp-client/configuration.rb', line 144

def connect_timeout_error
  @connect_timeout_error
end

#keep_aliveBoolean

Enables/disables use of Socket-level keep alive handling.

Returns:

  • (Boolean)

    whether the connection is allowed to use keep alive signals (default) or not



74
75
76
# File 'lib/tcp-client/configuration.rb', line 74

def keep_alive
  @keep_alive
end

#normalize_network_errorsBoolean

Enables/disables if network exceptions should be raised as NetworkError.

This allows to handle all network/socket related exceptions like SocketError, OpenSSL::SSL::SSLError, IOError, etc. in a uniform manner. If this option is set to true all these error cases are raised as NetworkError and can be easily captured.

Returns:

  • (Boolean)

    whether all network exceptions should be raised as NetworkError, or not (default)



252
253
254
# File 'lib/tcp-client/configuration.rb', line 252

def normalize_network_errors
  @normalize_network_errors
end

#read_timeoutNumeric?

The maximum time in seconds to read from a connection.

Returns:

  • (Numeric)

    maximum time in seconds

  • (nil)

    if the read time should not be monitored (default)

See Also:



158
159
160
# File 'lib/tcp-client/configuration.rb', line 158

def read_timeout
  @read_timeout
end

#read_timeout_errorClass<Exception>

The exception class which will be raised if TCPClient#read can not be finished in time.

Returns:

  • (Class<Exception>)

    exception class raised

Raises:



171
172
173
# File 'lib/tcp-client/configuration.rb', line 171

def read_timeout_error
  @read_timeout_error
end

#reverse_lookupBoolean

Enables/disables address lookup.

Returns:

  • (Boolean)

    whether the connection is allowed to lookup the address (default) or not



86
87
88
# File 'lib/tcp-client/configuration.rb', line 86

def reverse_lookup
  @reverse_lookup
end

#ssl?Boolean (readonly)

Returns whether SSL is configured, see #ssl_params.

Returns:

  • (Boolean)

    whether SSL is configured, see #ssl_params



96
# File 'lib/tcp-client/configuration.rb', line 96

def ssl? = @ssl_params ? true : false

#ssl_params{Symbol => Object}?

Parameters used to initialize a SSL context. SSL/TLS will only be used if this attribute is not nil.

Returns:

  • ({Symbol => Object})

    SSL parameters for the SSL context

  • (nil)

    if no SSL should be used (default)



105
106
107
# File 'lib/tcp-client/configuration.rb', line 105

def ssl_params
  @ssl_params
end

#timeout=(value) ⇒ Numeric? (writeonly)

Shorthand to set maximum time in seconds for all timeout monitoring.

Returns:

  • (Numeric)

    maximum time in seconds for any action

  • (nil)

    if all timeout monitoring should be disabled (default)

See Also:



215
216
217
# File 'lib/tcp-client/configuration.rb', line 215

def timeout=(value)
  @connect_timeout = @write_timeout = @read_timeout = as_seconds(value)
end

#timeout_error=(value) ⇒ Class<Exception> (writeonly)

Shorthand to set the exception class which will by raised by any reached timeout.

Returns:

  • (Class<Exception>)

    exception class raised

Raises:

See Also:



232
233
234
235
# File 'lib/tcp-client/configuration.rb', line 232

def timeout_error=(value)
  @connect_timeout_error =
    @read_timeout_error = @write_timeout_error = as_exception(value)
end

#write_timeoutNumeric?

The maximum time in seconds to write to a connection.

Returns:

  • (Numeric)

    maximum time in seconds

  • (nil)

    if the write time should not be monitored (default)

See Also:



185
186
187
# File 'lib/tcp-client/configuration.rb', line 185

def write_timeout
  @write_timeout
end

#write_timeout_errorClass<Exception>

The exception class which will be raised if TCPClient#write can not be finished in time.

Returns:

  • (Class<Exception>)

    exception class raised

Raises:



198
199
200
# File 'lib/tcp-client/configuration.rb', line 198

def write_timeout_error
  @write_timeout_error
end

Class Method Details

.create {|configuration| ... } ⇒ Configuration .create(options) ⇒ Configuration

Shorthand to create a new configuration.

Overloads:

Yields:

Returns:



33
34
35
36
37
# File 'lib/tcp-client/configuration.rb', line 33

def self.create(options = nil)
  configuration = new(options)
  yield(configuration) if block_given?
  configuration
end

.defaultConfiguration

Returns used by default if no dedicated configuration was specified.

Returns:

  • (Configuration)

    used by default if no dedicated configuration was specified

See Also:



49
# File 'lib/tcp-client/default_configuration.rb', line 49

def self.default = TCPClient.default_configuration

Instance Method Details

#configure(options) ⇒ Configuration

Configures the instance with given options Hash.

Parameters:

  • options ({Symbol => Object})

Options Hash (options):

Returns:



312
313
314
315
# File 'lib/tcp-client/configuration.rb', line 312

def configure(options)
  options.each_pair { set(*_1) }
  self
end

#to_h{Symbol => Object} #to_h(&block) ⇒ {Symbol => Object}

Returns Hash containing all attributes.

Returns:

  • ({Symbol => Object})

    Hash containing all attributes

See Also:



288
# File 'lib/tcp-client/configuration.rb', line 288

def to_h(&block) = block ? to_hash.to_h(&block) : to_hash

#to_hash{Symbol => Object}

Returns Hash containing all attributes.

Returns:

  • ({Symbol => Object})

    Hash containing all attributes

See Also:



265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/tcp-client/configuration.rb', line 265

def to_hash
  {
    buffered: @buffered,
    keep_alive: @keep_alive,
    reverse_lookup: @reverse_lookup,
    ssl_params: @ssl_params,
    connect_timeout: @connect_timeout,
    connect_timeout_error: @connect_timeout_error,
    read_timeout: @read_timeout,
    read_timeout_error: @read_timeout_error,
    write_timeout: @write_timeout,
    write_timeout_error: @write_timeout_error,
    normalize_network_errors: @normalize_network_errors
  }
end