run static method

Future<void> run(
  1. GroupIdentity identity
)

Say hello to the primary, get its ports, then start the providers and the server like isolateHandler. Call it from startServer(), after the providers of the config are added.

Implementation

static Future<void> run(GroupIdentity identity) async {
  ReceivePort inbox = ReceivePort();
  try {
    postSendPort(identity.primaryPort, inbox.sendPort);
    Object? message = await inbox.first.timeout(
      handshakeTimeout,
      onTimeout: () => throw StateError(
        'isolate ${identity.isolateId} got no answer from the primary '
        'isolate in ${handshakeTimeout.inSeconds} seconds',
      ),
    );
    List<Object?> ports = message as List<Object?>;
    RateLimitCounter.instance.connect(ports[1] as SendPort?);
    SessionMemoryStore.instance.connect(ports[2] as SendPort?);
    IsolateBus.instance.connect(ports[3] as SendPort?, identity.isolateId);
    SendPort control = ports[0] as SendPort;

    AppConfig config = Dox().config;
    await Dox().startProviders();
    DoxServer().setResponseHandler(config.responseHandler);
    await DoxServer().listen(
      DoxServer.portFor(config.serverPort),
      address: DoxServer.hostFor(config.host),
    );

    /// the primary sends a reply port here to stop this isolate
    ReceivePort commands = ReceivePort();
    commands.listen((dynamic reply) async {
      commands.close();
      await Dox().stopIsolate();
      (reply as SendPort).send(true);
    });
    control.send(commands.sendPort);
  } finally {
    inbox.close();
  }
}