Class: Nl::Genl::Client

Inherits:
Object
  • Object
show all
Defined in:
nl/lib/nl/genl/client.rb

Overview

Owns one generic Netlink connection shared by compatible families.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resolver:, executor: nil, notification_capacity: Nl::Connection::DEFAULT_NOTIFICATION_CAPACITY) ⇒ Client

Returns a new instance of Client.

Parameters:

  • resolver (#call) —

    a callable that resolves a family name to FamilyInfo

  • executor (:thread, :fiber, nil) (defaults to: nil) —

    the asynchronous executor, or nil for blocking operation

  • notification_capacity (Integer) (defaults to: Nl::Connection::DEFAULT_NOTIFICATION_CAPACITY) —

    the maximum number of queued notifications



54
55
56
57
58
59
60
61
62
63
# File 'nl/lib/nl/genl/client.rb', line 54

def initialize(resolver:, executor: nil, notification_capacity: Nl::Connection::DEFAULT_NOTIFICATION_CAPACITY)
  @resolver = resolver
  @family_cache = {}
  @family_cache_mutex = Mutex.new
  @connection = Nl::Connection.new(
    protocol: Protocol.new,
    executor:,
    notification_capacity:,
  )
end

Class Method Details

.open(resolver:, executor: nil, notification_capacity: Nl::Connection::DEFAULT_NOTIFICATION_CAPACITY) ⇒ Client .open(resolver:, executor: nil, notification_capacity: Nl::Connection::DEFAULT_NOTIFICATION_CAPACITY) {|client| ... } ⇒ Object

Opens a generic Netlink client.

Overloads:

  • .open(resolver:, executor: nil, notification_capacity: Nl::Connection::DEFAULT_NOTIFICATION_CAPACITY) ⇒ Client

    The caller is responsible for closing the client.

    Parameters:

    • resolver (#call) —

      a callable that resolves a family name to FamilyInfo

    • executor (:thread, :fiber, nil) (defaults to: nil) —

      the asynchronous executor, or nil for blocking operation

    • notification_capacity (Integer) (defaults to: Nl::Connection::DEFAULT_NOTIFICATION_CAPACITY) —

      the maximum number of queued notifications

    Returns:

    • (Client) —

      the opened client

  • .open(resolver:, executor: nil, notification_capacity: Nl::Connection::DEFAULT_NOTIFICATION_CAPACITY) {|client| ... } ⇒ Object

    The client is automatically closed after the block returns.

    Parameters:

    • resolver (#call) —

      a callable that resolves a family name to FamilyInfo

    • executor (:thread, :fiber, nil) (defaults to: nil) —

      the asynchronous executor, or nil for blocking operation

    • notification_capacity (Integer) (defaults to: Nl::Connection::DEFAULT_NOTIFICATION_CAPACITY) —

      the maximum number of queued notifications

    Yield Parameters:

    • client (Client) —

      the opened client

    Returns:

    • (Object) —

      the value returned from the block



39
40
41
42
43
44
45
46
47
48
# File 'nl/lib/nl/genl/client.rb', line 39

def self.open(resolver:, executor: nil, notification_capacity: Nl::Connection::DEFAULT_NOTIFICATION_CAPACITY)
  client = new(resolver:, executor:, notification_capacity:)
  return client unless block_given?

  begin
    yield client
  ensure
    client.close
  end
end

Instance Method Details

#close ⇒ void

This method returns an undefined value.

Closes the underlying Netlink connection.



87
88
89
# File 'nl/lib/nl/genl/client.rb', line 87

def close
  @connection.close
end

#family(family_class) ⇒ Family

Builds a family backed by this client's connection.

Parameters:

  • family_class (Class<Family>) —

    a generic Netlink family class

Returns:

  • (Family) —

    an instance of family_class

Raises:

  • (TypeError) —

    if family_class does not inherit from Family



71
72
73
74
75
76
77
78
79
80
81
# File 'nl/lib/nl/genl/client.rb', line 71

def family(family_class)
  unless family_class <= Family
    raise TypeError, "family class must inherit from #{Family}"
  end

  info = family_info(family_class::NAME)
  family_class.new(
    @connection,
    endpoint: Endpoint.new(family_class, info),
  )
end