discoverPeers method

Future<List<NodeDescriptor>> discoverPeers({
  1. String? capability,
  2. Map<String, String> labels = const {},
  3. Map<String, dynamic> filter = const {},
  4. Duration timeout = const Duration(seconds: 10),
})

Discovers peer nodes via the hub (must be isReady).

capability and labels use the hub's built-in flat matching. filter is an application-defined query, interpreted hub-side by a NodeMatcher — use it for semantics the hub's flat filters cannot express; it is ignored if the hub has no matcher configured.

Implementation

Future<List<NodeDescriptor>> discoverPeers({
  String? capability,
  Map<String, String> labels = const {},
  Map<String, dynamic> filter = const {},
  Duration timeout = const Duration(seconds: 10),
}) async {
  final typed = _typed;
  if (typed == null || _state != NodeState.ready) {
    throw const NodeUnavailableException('Node is not connected to a hub');
  }
  final requestId = _ids.next('q');
  final completer = Completer<List<NodeDescriptor>>();
  _pendingQueries[requestId] = completer;
  typed.send(
    NodeQuery(
      requestId,
      capability: capability,
      labels: labels,
      filter: filter,
    ),
  );
  try {
    return await completer.future.timeout(timeout);
  } on TimeoutException {
    _pendingQueries.remove(requestId);
    throw const HubTimeoutException('Peer discovery timed out');
  }
}