request method

Future<NodeResponse> request(
  1. String action, {
  2. Map<String, dynamic> payload = const {},
  3. Duration timeout = const Duration(seconds: 30),
})

Invokes action on the hub over the control channel and awaits its NodeResponse — the mirror of NodeGateway.request.

The hub answers with its NodeGateway.onRequest handler. Use this for the node→hub half of an application protocol (enrolment, lookups, publishing results) instead of opening a second channel back to the hub.

Throws NodeUnavailableException if the node is not registered (or drops while the call is in flight), or HubTimeoutException if the hub does not respond within timeout.

Implementation

Future<NodeResponse> request(
  String action, {
  Map<String, dynamic> payload = const {},
  Duration timeout = const Duration(seconds: 30),
}) async {
  final typed = _typed;
  if (typed == null || _state != NodeState.ready) {
    throw const NodeUnavailableException('Node is not connected to a hub');
  }
  final requestId = _ids.next('req');
  final completer = Completer<NodeResponse>();
  _pendingRequests[requestId] = completer;
  typed.send(NodeRequest(requestId, action, payload: payload));
  try {
    return await completer.future.timeout(timeout);
  } on TimeoutException {
    _pendingRequests.remove(requestId);
    throw HubTimeoutException('Hub RPC "$action" timed out');
  }
}