setService method

  1. @override
Future<void> setService(
  1. String serviceName
)
override

Implementation

@override
Future<void> setService(String serviceName) async {
  if (_serviceScopeImpl != null) {
    throw Exception('$name: stream scope already attached to a service: ${_serviceScopeImpl!.name}');
  }
  if (_protocolScopeImpl == null || _peerProtoScope == null) {
    throw StateError('$name: stream scope not attached to a protocol before setting service');
  }
  _logger.fine('$name: Setting service to $serviceName for peer ${_peerScopeImpl.peer}, protocol ${_protocolScopeImpl!.protocol}');

  // 1. Get necessary scopes
  final newServiceScope = _rcmgr.getServiceScopeInternal(serviceName);
  final systemScope = _rcmgr.systemScope;
  final limiter = _rcmgr.limiter;

  // Explicitly cast to the concrete PeerId type
  final newPeerSvcScope = newServiceScope.getPeerSubScope(
    _peerScopeImpl.peer,
    limiter,
    systemScope
  );

  // 2. Add the stream to the service and peer-service scopes. Nothing is
  // released: a stream counts against its service on top of its protocol.
  // reparent() counts it once in each scope it gains.
  try {
    reparent([_peerScopeImpl, _peerProtoScope!, newPeerSvcScope]);
  } on network_errors.ResourceLimitExceededException catch (e) {
    _logger.fine('$name: Failed to reserve resources for service $serviceName: $e.');
    rethrow;
  }
  _serviceScopeImpl = newServiceScope;
  _peerSvcScope = newPeerSvcScope;
  _logger.fine('$name: Successfully set service to $serviceName. Edges updated.');
}