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.

190 lines
5.9 KiB

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 <Foundation/Foundation.h>
  17. @class GtalkDataMessageStanza;
  18. @class GPBMessage;
  19. @class FIRMessagingPersistentSyncMessage;
  20. /**
  21. * Called on each raw message.
  22. */
  23. typedef void(^FIRMessagingRmqMessageHandler)(int64_t rmqId, int8_t tag, NSData *data);
  24. /**
  25. * Called on each DataMessageStanza.
  26. */
  27. typedef void(^FIRMessagingDataMessageHandler)(int64_t rmqId, GtalkDataMessageStanza *stanza);
  28. /**
  29. * Used to scan through the rmq and perform actions on messages as required.
  30. */
  31. @protocol FIRMessagingRmqScanner <NSObject>
  32. /**
  33. * Scan the RMQ for outgoing messages and process them as required.
  34. */
  35. - (void)scanWithRmqMessageHandler:(FIRMessagingRmqMessageHandler)rmqMessageHandler
  36. dataMessageHandler:(FIRMessagingDataMessageHandler)dataMessageHandler;
  37. @end
  38. /**
  39. * This manages the RMQ persistent store.
  40. *
  41. * The store is used to store all the S2D id's that were received by the client and were ACK'ed
  42. * by us but the server hasn't confirmed the ACK. We don't delete these id's until the server
  43. * ACK's us that they have received them.
  44. *
  45. * We also store the upstream messages(d2s) that were sent by the client.
  46. *
  47. * Also store the lastRMQId that was sent by us so that for a new connection being setup we don't
  48. * duplicate RMQ Id's for the new messages.
  49. */
  50. @interface FIRMessagingRmqManager : NSObject <FIRMessagingRmqScanner>
  51. // designated initializer
  52. - (instancetype)initWithDatabaseName:(NSString *)databaseName;
  53. - (void)loadRmqId;
  54. /**
  55. * Save an upstream message to RMQ. If the message send fails for some reason we would not
  56. * lose the message since it would be saved in the RMQ.
  57. *
  58. * @param message The upstream message to be saved.
  59. * @param error The error if any while saving the message else nil.
  60. *
  61. * @return YES if the message was successfully saved to RMQ else NO.
  62. */
  63. - (BOOL)saveRmqMessage:(GPBMessage *)message error:(NSError **)error;
  64. /**
  65. * Save Server to device message with the given RMQ-ID.
  66. *
  67. * @param rmqID The rmqID of the s2d message to save.
  68. *
  69. * @return YES if the save was successfull else NO.
  70. */
  71. - (BOOL)saveS2dMessageWithRmqId:(NSString *)rmqID;
  72. /**
  73. * A list of all unacked Server to device RMQ IDs.
  74. *
  75. * @return A list of unacked Server to Device RMQ ID's. All values are Strings.
  76. */
  77. - (NSArray *)unackedS2dRmqIds;
  78. /**
  79. * Removes the outgoing message from RMQ store.
  80. *
  81. * @param rmqId The rmqID to remove from the store.
  82. *
  83. * @return The number of messages deleted successfully.
  84. */
  85. - (int)removeRmqMessagesWithRmqId:(NSString *)rmqId;
  86. /**
  87. * Removes the messages with the given rmqIDs from RMQ store.
  88. *
  89. * @param rmqIds The lsit of rmqID's to remove from the store.
  90. *
  91. * @return The number of messages deleted successfully.
  92. */
  93. - (int)removeRmqMessagesWithRmqIds:(NSArray *)rmqIds;
  94. /**
  95. * Removes a list of downstream messages from the RMQ.
  96. *
  97. * @param s2dIds The list of messages ACK'ed by the server that we should remove
  98. * from the RMQ store.
  99. */
  100. - (void)removeS2dIds:(NSArray *)s2dIds;
  101. #pragma mark - Sync Messages
  102. /**
  103. * Get persisted sync message with rmqID.
  104. *
  105. * @param rmqID The rmqID of the persisted sync message.
  106. *
  107. * @return A valid persistent sync message with the given rmqID if found in the RMQ else nil.
  108. */
  109. - (FIRMessagingPersistentSyncMessage *)querySyncMessageWithRmqID:(NSString *)rmqID;
  110. /**
  111. * Delete sync message with rmqID.
  112. *
  113. * @param rmqID The rmqID of the persisted sync message.
  114. *
  115. * @return YES if the message was successfully deleted else NO.
  116. */
  117. - (BOOL)deleteSyncMessageWithRmqID:(NSString *)rmqID;
  118. /**
  119. * Delete the expired sync messages from persisten store. Also deletes messages that have been
  120. * delivered both via APNS and MCS.
  121. *
  122. * @param error The error if any while deleting the messages.
  123. *
  124. * @return The total number of messages that were deleted from the persistent store.
  125. */
  126. - (int)deleteExpiredOrFinishedSyncMessages:(NSError **)error;
  127. /**
  128. * Save sync message received by the device.
  129. *
  130. * @param rmqID The rmqID of the message received.
  131. * @param expirationTime The expiration time of the sync message received.
  132. * @param apnsReceived YES if the message was received via APNS else NO.
  133. * @param mcsReceived YES if the message was received via MCS else NO.
  134. * @param error The error if any while saving the sync message to persistent store.
  135. *
  136. * @return YES if the message save was successful else NO.
  137. */
  138. - (BOOL)saveSyncMessageWithRmqID:(NSString *)rmqID
  139. expirationTime:(int64_t)expirationTime
  140. apnsReceived:(BOOL)apnsReceived
  141. mcsReceived:(BOOL)mcsReceived
  142. error:(NSError **)error;
  143. /**
  144. * Update sync message received via APNS.
  145. *
  146. * @param rmqID The rmqID of the received message.
  147. * @param error The error if any while updating the sync message.
  148. *
  149. * @return YES if the persistent sync message was successfully updated else NO.
  150. */
  151. - (BOOL)updateSyncMessageViaAPNSWithRmqID:(NSString *)rmqID error:(NSError **)error;
  152. /**
  153. * Update sync message received via MCS.
  154. *
  155. * @param rmqID The rmqID of the received message.
  156. * @param error The error if any while updating the sync message.
  157. *
  158. * @return YES if the persistent sync message was successfully updated else NO.
  159. */
  160. - (BOOL)updateSyncMessageViaMCSWithRmqID:(NSString *)rmqID error:(NSError **)error;
  161. #pragma mark - Testing
  162. + (void)removeDatabaseWithName:(NSString *)dbName;
  163. @end