You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

148 lines
4.7 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  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 "Firebase/Messaging/FIRMessagingDelayedMessageQueue.h"
  17. #import "Firebase/Messaging/Protos/GtalkCore.pbobjc.h"
  18. #import "Firebase/Messaging/FIRMessagingDefines.h"
  19. #import "Firebase/Messaging/FIRMessagingRmqManager.h"
  20. #import "Firebase/Messaging/FIRMessagingUtilities.h"
  21. static const int kMaxQueuedMessageCount = 10;
  22. @interface FIRMessagingDelayedMessageQueue ()
  23. @property(nonatomic, readonly, weak) id<FIRMessagingRmqScanner> rmqScanner;
  24. @property(nonatomic, readonly, copy) FIRMessagingSendDelayedMessagesHandler sendDelayedMessagesHandler;
  25. @property(nonatomic, readwrite, assign) int persistedMessageCount;
  26. // the scheduled timeout or -1 if not set
  27. @property(nonatomic, readwrite, assign) int64_t scheduledTimeoutMilliseconds;
  28. // The time of the last scan of the message DB,
  29. // used to avoid retrieving messages more than once.
  30. @property(nonatomic, readwrite, assign) int64_t lastDBScanTimestampSeconds;
  31. @property(nonatomic, readwrite, strong) NSMutableArray *messages;
  32. @property(nonatomic, readwrite, strong) NSTimer *sendTimer;
  33. @end
  34. @implementation FIRMessagingDelayedMessageQueue
  35. - (instancetype)init {
  36. FIRMessagingInvalidateInitializer();
  37. }
  38. - (instancetype)initWithRmqScanner:(id<FIRMessagingRmqScanner>)rmqScanner
  39. sendDelayedMessagesHandler:(FIRMessagingSendDelayedMessagesHandler)sendDelayedMessagesHandler {
  40. self = [super init];
  41. if (self) {
  42. _rmqScanner = rmqScanner;
  43. _sendDelayedMessagesHandler = sendDelayedMessagesHandler;
  44. _messages = [NSMutableArray arrayWithCapacity:10];
  45. _scheduledTimeoutMilliseconds = -1;
  46. }
  47. return self;
  48. }
  49. - (BOOL)queueMessage:(GtalkDataMessageStanza *)message {
  50. if (self.messages.count >= kMaxQueuedMessageCount) {
  51. return NO;
  52. }
  53. if (message.ttl == 0) {
  54. // ttl=0 messages aren't persisted, add it to memory
  55. [self.messages addObject:message];
  56. } else {
  57. self.persistedMessageCount++;
  58. }
  59. int64_t timeoutMillis = [self calculateTimeoutInMillisWithDelayInSeconds:message.maxDelay];
  60. if (![self isTimeoutScheduled] || timeoutMillis < self.scheduledTimeoutMilliseconds) {
  61. [self scheduleTimeoutInMillis:timeoutMillis];
  62. }
  63. return YES;
  64. }
  65. - (NSArray *)removeDelayedMessages {
  66. [self cancelTimeout];
  67. if ([self messageCount] == 0) {
  68. return @[];
  69. }
  70. NSMutableArray *delayedMessages = [NSMutableArray array];
  71. // add the ttl=0 messages
  72. if (self.messages.count) {
  73. [delayedMessages addObjectsFromArray:delayedMessages];
  74. [self.messages removeAllObjects];
  75. }
  76. // add persistent messages
  77. if (self.persistedMessageCount > 0) {
  78. FIRMessaging_WEAKIFY(self);
  79. [self.rmqScanner scanWithRmqMessageHandler:^(NSDictionary *messages) {
  80. FIRMessaging_STRONGIFY(self);
  81. for (NSString *rmqID in messages) {
  82. GPBMessage *proto = messages[rmqID];
  83. GtalkDataMessageStanza *stanza = (GtalkDataMessageStanza *)proto;
  84. if ([stanza hasMaxDelay] &&
  85. [stanza sent] >= self.lastDBScanTimestampSeconds) {
  86. [delayedMessages addObject:stanza];
  87. }
  88. }
  89. }];
  90. self.lastDBScanTimestampSeconds = FIRMessagingCurrentTimestampInSeconds();
  91. self.persistedMessageCount = 0;
  92. }
  93. return delayedMessages;
  94. }
  95. - (void)sendMessages {
  96. if (self.sendDelayedMessagesHandler) {
  97. self.sendDelayedMessagesHandler([self removeDelayedMessages]);
  98. }
  99. }
  100. #pragma mark - Private
  101. - (NSInteger)messageCount {
  102. return self.messages.count + self.persistedMessageCount;
  103. }
  104. - (BOOL)isTimeoutScheduled {
  105. return self.scheduledTimeoutMilliseconds > 0;
  106. }
  107. - (int64_t)calculateTimeoutInMillisWithDelayInSeconds:(int)delay {
  108. return FIRMessagingCurrentTimestampInMilliseconds() + delay * 1000.0;
  109. }
  110. - (void)scheduleTimeoutInMillis:(int64_t)time {
  111. [self cancelTimeout];
  112. self.scheduledTimeoutMilliseconds = time;
  113. double delay = (time - FIRMessagingCurrentTimestampInMilliseconds()) / 1000.0;
  114. [self performSelector:@selector(sendMessages) withObject:self afterDelay:delay];
  115. }
  116. - (void)cancelTimeout {
  117. if ([self isTimeoutScheduled]) {
  118. [NSObject cancelPreviousPerformRequestsWithTarget:self
  119. selector:@selector(sendMessages)
  120. object:nil];
  121. self.scheduledTimeoutMilliseconds = -1;
  122. }
  123. }
  124. @end