sendResponse method

  1. @visibleForTesting
Future<void> sendResponse(
  1. AtNotification notification,
  2. AtRpcReq request,
  3. AtRpcResp response
)

Not part of API, but visibleForTesting. Sends a response. Note that this is marked as @visibleForTesting as it is only called by handleRequestNotification and is not intended to be used directly by AtRpc users.

Implementation

@visibleForTesting
Future<void> sendResponse(
    AtNotification notification, AtRpcReq request, AtRpcResp response) async {
  bool sent = false;
  int delayMillis = 200;
  for (int attemptNumber = 1;
      attemptNumber <= maxSendAttempts && !sent;
      attemptNumber++) {
    try {
      String responseAtID =
          '${response.respType.name}.${request.reqId}.$domainNameSpace.$rpcsNameSpace';
      var responseAtKey = AtKey()
        ..key = responseAtID
        ..sharedBy = atClient.getCurrentAtSign()
        ..sharedWith = notification.from
        ..namespace = baseNameSpace
        ..metadata = _defaultMetaData;

      logger.info(
          "Sending notification $responseAtKey with payload ${response.toJson()}");
      await atClient.notificationService.notify(
          NotificationParams.forUpdate(responseAtKey,
              value: jsonEncode(response.toJson()),
              notificationExpiry: defaultNotificationExpiry),
          checkForFinalDeliveryStatus: false,
          waitForFinalDeliveryStatus: false);
      sent = true;
    } catch (e) {
      if (attemptNumber < maxSendAttempts) {
        logger.warning(
            'Exception $e sending response $response on attempt $attemptNumber - will retry in $delayMillis ms');
      } else {
        logger.severe(
            'Exception $e sending response $response on attempt $attemptNumber - giving up');
      }
      await Future.delayed(Duration(milliseconds: delayMillis));
      if (delayMillis < 5000) {
        delayMillis *= 5;
      }
    }
  }
}