fetchFromServer static method

Future<void> fetchFromServer(
  1. SchemaMetaData smd,
  2. SchemaMetaData smdSys,
  3. ConfigurationNameDefaults defaults, {
  4. String dbName = 'default_db',
})

Implementation

static Future<void> fetchFromServer(SchemaMetaData smd, SchemaMetaData smdSys,
    ConfigurationNameDefaults defaults,
    {String dbName: 'default_db'}) async {
  DbTransaction transaction;
  ConfigurationDao configurationDao;
  UserTools userTools;
  AbstractWarden warden;

  int attempts = 0;
  bool complete = false;
  UserDto? currentUserDto;
  int start_ts;
  transaction = await DataBaseHelper.getDbTransaction(dbName);
  configurationDao = ConfigurationDao(smd, transaction, defaults);
  await configurationDao.init(initTable: true);
  userTools = UserTools();
  try {
    currentUserDto = await userTools.getCurrentUserDto(smd, transaction);
  } on SqlException catch (e) {
    if (e.sqlExceptionEnum == SqlExceptionEnum.ENTRY_NOT_FOUND)
      print("This server is not defined in the user table");
    else
      rethrow;
  }
  if (currentUserDto != null) {
    do {
      start_ts = DateTime.now().millisecondsSinceEpoch;
      // Initialize configuration table
      try {
        AuthenticationDto? authenticationDto;
        try {
          authenticationDto = await authenticateUser(
              currentUserDto.warden,
              C_REMOTE_WARDEN,
              currentUserDto.pass_key,
              WaterState.SERVER_APPROVED,
              AbstractEntries.C_MINIMUM_VERSION,
              smd,
              smdSys,
              transaction,
              userTools,
              defaults);
        } on TransmitStatusException catch (e) {
          print("WS $e");
        }
        if (authenticationDto != null) {
          warden = WardenFactory.getAbstractWarden(
              currentUserDto.warden!, authenticationDto.warden!);

          FetchLatestRows fetchRows = FetchLatestRows(
              C_LOCAL_WARDEN,
              C_REMOTE_WARDEN,
              smd,
              smdSys,
              transaction,
              userTools,
              warden,
              defaults);
          await fetchRows.downloadRows(WaterState.SERVER_APPROVED);
        }
        complete = true;
      } on Exception catch (e) {
        if (e.runtimeType == TransmitStatusException ||
            (e.runtimeType == SqlException)) {
          if (e.runtimeType == SqlExceptionEnum) {
            SqlException ex = e as SqlException;
            if (ex.sqlExceptionEnum != SqlExceptionEnum.FAILED_SELECT)
              continue;
          }
          // If we had a successful run for 20s, reset attempts back to 0
          if ((start_ts + C_RETRY_WAIT_MS + 20000) >
              DateTime.now().millisecondsSinceEpoch) attempts = 0;
          attempts++;
          print(StackTrace.current);
          if (attempts < C_RETRY_ATTEMPTS) {
            complete = false;
            print(
                "Connection error, retrying $attempts of $C_RETRY_ATTEMPTS");
            try {
              sleep(Duration(milliseconds: C_RETRY_WAIT_MS));
            } on Exception catch (e1) {
              print("WS $e1");
            }
          } else {
            complete = true;
            print("Giving up connecting after $C_RETRY_ATTEMPTS attempts");
          }
        }
      }
    } while (!complete);
  }
  await transaction.connection.close();
  await transaction.endTransaction();
  await transaction.closePool();
}