Overview
Comment: | Make sync loop automatic |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA3-256: |
63d344bd1d172c6cf29ef3fad72f37d0 |
User & Date: | js on 2020-10-31 17:09:42 |
Other Links: | manifest | tags |
Context
2020-10-31
| ||
19:37 | Adjust to ObjFW changes check-in: 6b6fe6d802 user: js tags: trunk | |
17:09 | Make sync loop automatic check-in: 63d344bd1d user: js tags: trunk | |
2020-10-11
| ||
18:34 | Track joined rooms check-in: 366f90be1a user: js tags: trunk | |
Changes
Modified src/MTXClient.h from [a9c991a2fb] to [4640a34e7e].
︙ | ︙ | |||
40 41 42 43 44 45 46 47 48 49 50 51 52 53 | /** * @brief A block called when the response for an operation was received. * * @param exception `nil` on success, otherwise an exception */ typedef void (^mtx_client_response_block_t)(id _Nullable exception); /** * @brief A block called when the room list was fetched. * * @param rooms An array of joined rooms, or nil on error * @param exception An exception if fetching the room list failed */ typedef void (^mtx_client_room_list_block_t)( | > > > > > > > | 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | /** * @brief A block called when the response for an operation was received. * * @param exception `nil` on success, otherwise an exception */ typedef void (^mtx_client_response_block_t)(id _Nullable exception); /** * @brief A block called when an exception occurred during sync. * * @param exception The exception which occurred during sync */ typedef void (^mtx_sync_exception_handler_block_t)(id exception); /** * @brief A block called when the room list was fetched. * * @param rooms An array of joined rooms, or nil on error * @param exception An exception if fetching the room list failed */ typedef void (^mtx_client_room_list_block_t)( |
︙ | ︙ | |||
88 89 90 91 92 93 94 95 96 97 98 99 100 101 | @property (readonly, nonatomic) OFURL *homeserver; /** * @brief The storage used by the client. */ @property (readonly, nonatomic) id <MTXStorage> storage; /** * @brief Creates a new client with the specified access token on the specified * homeserver. * * @param userID The user ID for the client * @param deviceID The device ID for the client * @param accessToken The access token for the client | > > > > > > > > > > > > > | 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 | @property (readonly, nonatomic) OFURL *homeserver; /** * @brief The storage used by the client. */ @property (readonly, nonatomic) id <MTXStorage> storage; /** * @brief The timeout for sync requests. * * Defaults to 5 minutes. */ @property (nonatomic) of_time_interval_t syncTimeout; /** * @brief A block to handle exceptions that occurred during sync. */ @property (copy, nonatomic) mtx_sync_exception_handler_block_t syncExceptionHandler; /** * @brief Creates a new client with the specified access token on the specified * homeserver. * * @param userID The user ID for the client * @param deviceID The device ID for the client * @param accessToken The access token for the client |
︙ | ︙ | |||
139 140 141 142 143 144 145 | deviceID: (OFString *)deviceID accessToken: (OFString *)accessToken homeserver: (OFURL *)homeserver storage: (id <MTXStorage>)storage OF_DESIGNATED_INITIALIZER; /** | | | > | > > > > > | < | 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 | deviceID: (OFString *)deviceID accessToken: (OFString *)accessToken homeserver: (OFURL *)homeserver storage: (id <MTXStorage>)storage OF_DESIGNATED_INITIALIZER; /** * @brief Starts the sync loop. */ - (void)startSyncLoop; /** * @brief Stops the sync loop. * * The currently waiting sync is not aborted, but after it returns, no new sync * will be started. */ - (void)stopSyncLoop; /** * @brief Logs out the device and invalidates the access token. * * @warning The client can no longer be used after this succeeded! * * @param block A block to call when logging out succeeded or failed |
︙ | ︙ |
Modified src/MTXClient.m from [ec0c7e8ba9] to [90a5b4b816].
︙ | ︙ | |||
44 45 46 47 48 49 50 51 52 53 54 55 56 57 | if (homeserver.user != nil || homeserver.password != nil || homeserver.query != nil || homeserver.fragment != nil) @throw [OFInvalidArgumentException exception]; } @implementation MTXClient + (instancetype)clientWithUserID: (OFString *)userID deviceID: (OFString *)deviceID accessToken: (OFString *)accessToken homeserver: (OFURL *)homeserver storage: (id <MTXStorage>)storage { return [[[self alloc] initWithUserID: userID | > > > > | 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | if (homeserver.user != nil || homeserver.password != nil || homeserver.query != nil || homeserver.fragment != nil) @throw [OFInvalidArgumentException exception]; } @implementation MTXClient { bool _syncing; } + (instancetype)clientWithUserID: (OFString *)userID deviceID: (OFString *)deviceID accessToken: (OFString *)accessToken homeserver: (OFURL *)homeserver storage: (id <MTXStorage>)storage { return [[[self alloc] initWithUserID: userID |
︙ | ︙ | |||
154 155 156 157 158 159 160 161 162 163 164 165 166 167 | validateHomeserver(homeserver); _userID = [userID copy]; _deviceID = [deviceID copy]; _accessToken = [accessToken copy]; _homeserver = [homeserver copy]; _storage = [storage retain]; } @catch (id e) { [self release]; @throw e; } return self; } | > | 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 | validateHomeserver(homeserver); _userID = [userID copy]; _deviceID = [deviceID copy]; _accessToken = [accessToken copy]; _homeserver = [homeserver copy]; _storage = [storage retain]; _syncTimeout = 300; } @catch (id e) { [self release]; @throw e; } return self; } |
︙ | ︙ | |||
192 193 194 195 196 197 198 | - (MTXRequest *)requestWithPath: (OFString *)path { return [MTXRequest requestWithPath: path accessToken: _accessToken homeserver: _homeserver]; } | | > > > | > | | > | > | | | | > > | > | | > > > > > > | 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 | - (MTXRequest *)requestWithPath: (OFString *)path { return [MTXRequest requestWithPath: path accessToken: _accessToken homeserver: _homeserver]; } - (void)startSyncLoop { if (_syncing) return; _syncing = true; void *pool = objc_autoreleasePoolPush(); MTXRequest *request = [self requestWithPath: @"/_matrix/client/r0/sync"]; unsigned long long timeoutMs = _syncTimeout * 1000; OFMutableDictionary<OFString *, OFString *> *query = [OFMutableDictionary dictionaryWithObject: @(timeoutMs).stringValue forKey: @"timeout"]; query[@"since"] = [_storage nextBatchForDeviceID: _deviceID]; request.query = query; [request performWithBlock: ^ (mtx_response_t response, int statusCode, id exception) { if (exception != nil) { if (_syncExceptionHandler != NULL) _syncExceptionHandler(exception); return; } if (statusCode != 200) { if (_syncExceptionHandler != NULL) _syncExceptionHandler([MTXSyncFailedException exceptionWithStatusCode: statusCode response: response client: self]); return; } OFString *nextBatch = response[@"next_batch"]; if (![nextBatch isKindOfClass: OFString.class]) { if (_syncExceptionHandler != NULL) _syncExceptionHandler( [OFInvalidServerReplyException exception]); return; } @try { [_storage transactionWithBlock: ^ { [_storage setNextBatch: nextBatch forDeviceID: _deviceID]; [self processRoomsSync: response[@"rooms"]]; [self processPresenceSync: response[@"presence"]]; [self processAccountDataSync: response[@"account_data"]]; [self processToDeviceSync: response[@"to_device"]]; return true; }]; } @catch (id e) { if (_syncExceptionHandler != NULL) _syncExceptionHandler(e); return; } if (_syncing) [self startSyncLoop]; }]; objc_autoreleasePoolPop(pool); } - (void)stopSyncLoop { _syncing = false; } - (void)logOutWithBlock: (mtx_client_response_block_t)block { void *pool = objc_autoreleasePoolPush(); MTXRequest *request = [self requestWithPath: @"/_matrix/client/r0/logout"]; request.method = OF_HTTP_REQUEST_METHOD_POST; |
︙ | ︙ |
Modified tests/tests.m from [9eabf30e59] to [bf6cef9450].
︙ | ︙ | |||
59 60 61 62 63 64 65 | of_log(@"Error logging in: %@", exception); [OFApplication terminateWithStatus: 1]; } _client = [client retain]; of_log(@"Logged in client: %@", _client); | < < < < < < | < < < < < < < < | 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | of_log(@"Error logging in: %@", exception); [OFApplication terminateWithStatus: 1]; } _client = [client retain]; of_log(@"Logged in client: %@", _client); [_client startSyncLoop]; [self fetchRoomList]; }]; } - (void)fetchRoomList { [_client fetchRoomListWithBlock: ^ (OFArray<OFString *> *rooms, |
︙ | ︙ | |||
106 107 108 109 110 111 112 | of_log(@"Failed to join room %@: %@", room, exception); [OFApplication terminateWithStatus: 1]; } _roomID = [roomID copy]; of_log(@"Joined room %@", _roomID); | < < < < < < < < < < < < < < < > > | > > | 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | of_log(@"Failed to join room %@: %@", room, exception); [OFApplication terminateWithStatus: 1]; } _roomID = [roomID copy]; of_log(@"Joined room %@", _roomID); [self sendMessage]; }]; } - (void)sendMessage { [_client sendMessage: @"ObjMatrix test successful!" roomID: _roomID block: ^ (id exception) { if (exception != nil) { of_log(@"Failed to send message to room %@: %@", _roomID, exception); [OFApplication terminateWithStatus: 1]; } of_log(@"Message sent to %@", _roomID); of_log( @"Waiting 5 seconds before leaving room and logging out"); [self performSelector: @selector(leaveRoom) afterDelay: 5]; }]; } - (void)leaveRoom { [_client leaveRoom: _roomID block: ^ (id exception) { |
︙ | ︙ |