2 * Copyright 2017 Google
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #import <Foundation/Foundation.h>
19 @class GtalkDataMessageStanza;
22 @class FIRMessagingPersistentSyncMessage;
25 * Called on each raw message.
27 typedef void(^FIRMessagingRmqMessageHandler)(int64_t rmqId, int8_t tag, NSData *data);
30 * Called on each DataMessageStanza.
32 typedef void(^FIRMessagingDataMessageHandler)(int64_t rmqId, GtalkDataMessageStanza *stanza);
35 * Used to scan through the rmq and perform actions on messages as required.
37 @protocol FIRMessagingRmqScanner <NSObject>
40 * Scan the RMQ for outgoing messages and process them as required.
42 - (void)scanWithRmqMessageHandler:(FIRMessagingRmqMessageHandler)rmqMessageHandler
43 dataMessageHandler:(FIRMessagingDataMessageHandler)dataMessageHandler;
48 * This manages the RMQ persistent store.
50 * The store is used to store all the S2D id's that were received by the client and were ACK'ed
51 * by us but the server hasn't confirmed the ACK. We don't delete these id's until the server
52 * ACK's us that they have received them.
54 * We also store the upstream messages(d2s) that were sent by the client.
56 * Also store the lastRMQId that was sent by us so that for a new connection being setup we don't
57 * duplicate RMQ Id's for the new messages.
59 @interface FIRMessagingRmqManager : NSObject <FIRMessagingRmqScanner>
61 // designated initializer
62 - (instancetype)initWithDatabaseName:(NSString *)databaseName;
67 * Save an upstream message to RMQ. If the message send fails for some reason we would not
68 * lose the message since it would be saved in the RMQ.
70 * @param message The upstream message to be saved.
71 * @param error The error if any while saving the message else nil.
73 * @return YES if the message was successfully saved to RMQ else NO.
75 - (BOOL)saveRmqMessage:(GPBMessage *)message error:(NSError **)error;
78 * Save Server to device message with the given RMQ-ID.
80 * @param rmqID The rmqID of the s2d message to save.
82 * @return YES if the save was successfull else NO.
84 - (BOOL)saveS2dMessageWithRmqId:(NSString *)rmqID;
87 * A list of all unacked Server to device RMQ IDs.
89 * @return A list of unacked Server to Device RMQ ID's. All values are Strings.
91 - (NSArray *)unackedS2dRmqIds;
94 * Removes the outgoing message from RMQ store.
96 * @param rmqId The rmqID to remove from the store.
98 * @return The number of messages deleted successfully.
100 - (int)removeRmqMessagesWithRmqId:(NSString *)rmqId;
103 * Removes the messages with the given rmqIDs from RMQ store.
105 * @param rmqIds The lsit of rmqID's to remove from the store.
107 * @return The number of messages deleted successfully.
109 - (int)removeRmqMessagesWithRmqIds:(NSArray *)rmqIds;
112 * Removes a list of downstream messages from the RMQ.
114 * @param s2dIds The list of messages ACK'ed by the server that we should remove
115 * from the RMQ store.
117 - (void)removeS2dIds:(NSArray *)s2dIds;
119 #pragma mark - Sync Messages
122 * Get persisted sync message with rmqID.
124 * @param rmqID The rmqID of the persisted sync message.
126 * @return A valid persistent sync message with the given rmqID if found in the RMQ else nil.
128 - (FIRMessagingPersistentSyncMessage *)querySyncMessageWithRmqID:(NSString *)rmqID;
131 * Delete sync message with rmqID.
133 * @param rmqID The rmqID of the persisted sync message.
135 * @return YES if the message was successfully deleted else NO.
137 - (BOOL)deleteSyncMessageWithRmqID:(NSString *)rmqID;
140 * Delete the expired sync messages from persisten store. Also deletes messages that have been
141 * delivered both via APNS and MCS.
143 * @param error The error if any while deleting the messages.
145 * @return The total number of messages that were deleted from the persistent store.
147 - (int)deleteExpiredOrFinishedSyncMessages:(NSError **)error;
150 * Save sync message received by the device.
152 * @param rmqID The rmqID of the message received.
153 * @param expirationTime The expiration time of the sync message received.
154 * @param apnsReceived YES if the message was received via APNS else NO.
155 * @param mcsReceived YES if the message was received via MCS else NO.
156 * @param error The error if any while saving the sync message to persistent store.
158 * @return YES if the message save was successful else NO.
160 - (BOOL)saveSyncMessageWithRmqID:(NSString *)rmqID
161 expirationTime:(int64_t)expirationTime
162 apnsReceived:(BOOL)apnsReceived
163 mcsReceived:(BOOL)mcsReceived
164 error:(NSError **)error;
167 * Update sync message received via APNS.
169 * @param rmqID The rmqID of the received message.
170 * @param error The error if any while updating the sync message.
172 * @return YES if the persistent sync message was successfully updated else NO.
174 - (BOOL)updateSyncMessageViaAPNSWithRmqID:(NSString *)rmqID error:(NSError **)error;
177 * Update sync message received via MCS.
179 * @param rmqID The rmqID of the received message.
180 * @param error The error if any while updating the sync message.
182 * @return YES if the persistent sync message was successfully updated else NO.
184 - (BOOL)updateSyncMessageViaMCSWithRmqID:(NSString *)rmqID error:(NSError **)error;
186 #pragma mark - Testing
188 + (void)removeDatabaseWithName:(NSString *)dbName;