maintainBridges static method

Future<void> maintainBridges({
  1. Directory? savedBridgesDir,
  2. String decrypter(
    1. String ciphertext
    )?,
})

This method maintains the bridges that are stored locally.

If the IP addresses where to change, then the locally saved bridges would be updated.

savedBridgesDir The directory where the bridge data will be saved. If this is not provided, the default directory will be used.

decrypter When the bridge data is read from local storage, it is decrypted. This parameter allows you to provide your own decryption method. This will be used in addition to the default decryption method. This will be performed after the default decryption method.

Implementation

static Future<void> maintainBridges({
  Directory? savedBridgesDir,
  String Function(String ciphertext)? decrypter,
}) async {
  /// The bridges already saved to this device.
  List<Bridge> savedBridges;

  if (kIsWeb) {
    // cookies instead of local storage (not yet implemented)
    savedBridges = [];
  } else {
    savedBridges = await BridgeDiscoveryRepo.fetchSavedBridges(
      decrypter: decrypter,
      directory: savedBridgesDir,
    );
  }

  // All of the ip addresses that are saved locally.
  List<String> ipAddresses = savedBridges
      .where((bridge) => bridge.ipAddress != null)
      .map((bridge) => bridge.ipAddress!)
      .toList();

  for (String ip in ipAddresses) {
    // This will go through each of the bridges who's IP address have just
    // been collected and tries to connect to them. If there is a successful
    // connection, then this isn't the first time contact has been made with
    // the bridge. In this case, the file will be overridden with the new IP
    // address.
    await BridgeDiscoveryRepo.firstContact(
      bridgeIpAddr: ip,
      savedBridgesDir: savedBridgesDir,
      controller: DiscoveryTimeoutController(timeoutSeconds: 1),
    );
  }
}