FIRMessagingDelayedMessageQueue.m 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "FirebaseMessaging/Sources/FIRMessagingDelayedMessageQueue.h"
  17. #import "FirebaseMessaging/Sources/Protos/GtalkCore.pbobjc.h"
  18. #import "FirebaseMessaging/Sources/FIRMessagingDefines.h"
  19. #import "FirebaseMessaging/Sources/FIRMessagingRmqManager.h"
  20. #import "FirebaseMessaging/Sources/FIRMessagingUtilities.h"
  21. static const int kMaxQueuedMessageCount = 10;
  22. @interface FIRMessagingDelayedMessageQueue ()
  23. @property(nonatomic, readonly, weak) id<FIRMessagingRmqScanner> rmqScanner;
  24. @property(nonatomic, readonly, copy)
  25. FIRMessagingSendDelayedMessagesHandler sendDelayedMessagesHandler;
  26. @property(nonatomic, readwrite, assign) int persistedMessageCount;
  27. // the scheduled timeout or -1 if not set
  28. @property(nonatomic, readwrite, assign) int64_t scheduledTimeoutMilliseconds;
  29. // The time of the last scan of the message DB,
  30. // used to avoid retrieving messages more than once.
  31. @property(nonatomic, readwrite, assign) int64_t lastDBScanTimestampSeconds;
  32. @property(nonatomic, readwrite, strong) NSMutableArray *messages;
  33. @property(nonatomic, readwrite, strong) NSTimer *sendTimer;
  34. @end
  35. @implementation FIRMessagingDelayedMessageQueue
  36. - (instancetype)init {
  37. FIRMessagingInvalidateInitializer();
  38. }
  39. - (instancetype)initWithRmqScanner:(id<FIRMessagingRmqScanner>)rmqScanner
  40. sendDelayedMessagesHandler:
  41. (FIRMessagingSendDelayedMessagesHandler)sendDelayedMessagesHandler {
  42. self = [super init];
  43. if (self) {
  44. _rmqScanner = rmqScanner;
  45. _sendDelayedMessagesHandler = sendDelayedMessagesHandler;
  46. _messages = [NSMutableArray arrayWithCapacity:10];
  47. _scheduledTimeoutMilliseconds = -1;
  48. }
  49. return self;
  50. }
  51. - (BOOL)queueMessage:(GtalkDataMessageStanza *)message {
  52. if (self.messages.count >= kMaxQueuedMessageCount) {
  53. return NO;
  54. }
  55. if (message.ttl == 0) {
  56. // ttl=0 messages aren't persisted, add it to memory
  57. [self.messages addObject:message];
  58. } else {
  59. self.persistedMessageCount++;
  60. }
  61. int64_t timeoutMillis = [self calculateTimeoutInMillisWithDelayInSeconds:message.maxDelay];
  62. if (![self isTimeoutScheduled] || timeoutMillis < self.scheduledTimeoutMilliseconds) {
  63. [self scheduleTimeoutInMillis:timeoutMillis];
  64. }
  65. return YES;
  66. }
  67. - (NSArray *)removeDelayedMessages {
  68. [self cancelTimeout];
  69. if ([self messageCount] == 0) {
  70. return @[];
  71. }
  72. NSMutableArray *delayedMessages = [NSMutableArray array];
  73. // add the ttl=0 messages
  74. if (self.messages.count) {
  75. [delayedMessages addObjectsFromArray:delayedMessages];
  76. [self.messages removeAllObjects];
  77. }
  78. // add persistent messages
  79. if (self.persistedMessageCount > 0) {
  80. FIRMessaging_WEAKIFY(self);
  81. [self.rmqScanner scanWithRmqMessageHandler:^(NSDictionary *messages) {
  82. FIRMessaging_STRONGIFY(self);
  83. for (NSString *rmqID in messages) {
  84. GPBMessage *proto = messages[rmqID];
  85. GtalkDataMessageStanza *stanza = (GtalkDataMessageStanza *)proto;
  86. if ([stanza hasMaxDelay] && [stanza sent] >= self.lastDBScanTimestampSeconds) {
  87. [delayedMessages addObject:stanza];
  88. }
  89. }
  90. }];
  91. self.lastDBScanTimestampSeconds = FIRMessagingCurrentTimestampInSeconds();
  92. self.persistedMessageCount = 0;
  93. }
  94. return delayedMessages;
  95. }
  96. - (void)sendMessages {
  97. if (self.sendDelayedMessagesHandler) {
  98. self.sendDelayedMessagesHandler([self removeDelayedMessages]);
  99. }
  100. }
  101. #pragma mark - Private
  102. - (NSInteger)messageCount {
  103. return self.messages.count + self.persistedMessageCount;
  104. }
  105. - (BOOL)isTimeoutScheduled {
  106. return self.scheduledTimeoutMilliseconds > 0;
  107. }
  108. - (int64_t)calculateTimeoutInMillisWithDelayInSeconds:(int)delay {
  109. return FIRMessagingCurrentTimestampInMilliseconds() + delay * 1000.0;
  110. }
  111. - (void)scheduleTimeoutInMillis:(int64_t)time {
  112. [self cancelTimeout];
  113. self.scheduledTimeoutMilliseconds = time;
  114. double delay = (time - FIRMessagingCurrentTimestampInMilliseconds()) / 1000.0;
  115. [self performSelector:@selector(sendMessages) withObject:self afterDelay:delay];
  116. }
  117. - (void)cancelTimeout {
  118. if ([self isTimeoutScheduled]) {
  119. [NSObject cancelPreviousPerformRequestsWithTarget:self
  120. selector:@selector(sendMessages)
  121. object:nil];
  122. self.scheduledTimeoutMilliseconds = -1;
  123. }
  124. }
  125. @end