Class: Nl::Raw::Client

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

Overview

Owns one raw Netlink connection shared by compatible families.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Client.

Parameters:

  • protonum (Integer) —

    the Netlink protocol number

  • 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



43
44
45
46
47
48
49
50
# File 'nl/lib/nl/raw/client.rb', line 43

def initialize(protonum:, executor: nil, notification_capacity: Nl::Connection::DEFAULT_NOTIFICATION_CAPACITY)
  @protonum = protonum
  @connection = Nl::Connection.new(
    protocol: Protocol.new(protonum),
    executor:,
    notification_capacity:,
  )
end

Class Method Details

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

Opens a client for a raw Netlink protocol.

Overloads:

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

    The caller is responsible for closing the client.

    Parameters:

    • protonum (Integer) —

      the Netlink protocol number

    • 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(protonum:, executor: nil, notification_capacity: Nl::Connection::DEFAULT_NOTIFICATION_CAPACITY) {|client| ... } ⇒ Object

    The client is automatically closed after the block returns.

    Parameters:

    • protonum (Integer) —

      the Netlink protocol number

    • 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



28
29
30
31
32
33
34
35
36
37
# File 'nl/lib/nl/raw/client.rb', line 28

def self.open(protonum:, executor: nil, notification_capacity: Nl::Connection::DEFAULT_NOTIFICATION_CAPACITY)
  client = new(protonum:, 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.



78
79
80
# File 'nl/lib/nl/raw/client.rb', line 78

def close
  @connection.close
end

#family(family_class) ⇒ Family

Builds a family backed by this client's connection.

Parameters:

  • family_class (Class<Family>) —

    a raw Netlink family class

Returns:

  • (Family) —

    an instance of family_class

Raises:

  • (TypeError) —

    if family_class does not inherit from Family

  • (ArgumentError) —

    if the family's protocol number differs from the client's protocol number



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'nl/lib/nl/raw/client.rb', line 59

def family(family_class)
  unless family_class <= Family
    raise TypeError, "family class must inherit from #{Family}"
  end
  unless family_class::PROTONUM == @protonum
    raise ArgumentError,
      "family protonum #{family_class::PROTONUM} does not match client protonum #{@protonum}"
  end

  family_class.new(
    @connection,
    endpoint: Endpoint.new(family_class),
  )
end