Make sync loop automatic
FossilOrigin-Name: 63d344bd1d172c6cf29ef3fad72f37d058705a7ce33deb3021c70d46a0c93a3a
This commit is contained in:
parent
c0c1dc06b6
commit
f984171b15
3 changed files with 67 additions and 47 deletions
|
@ -44,6 +44,13 @@ typedef void (^mtx_client_login_block_t)(MTXClient *_Nullable client,
|
||||||
*/
|
*/
|
||||||
typedef void (^mtx_client_response_block_t)(id _Nullable 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.
|
* @brief A block called when the room list was fetched.
|
||||||
*
|
*
|
||||||
|
@ -92,6 +99,19 @@ typedef void (^mtx_client_room_join_block_t)(OFString *_Nullable roomID,
|
||||||
*/
|
*/
|
||||||
@property (readonly, nonatomic) id <MTXStorage> storage;
|
@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
|
* @brief Creates a new client with the specified access token on the specified
|
||||||
* homeserver.
|
* homeserver.
|
||||||
|
@ -143,12 +163,17 @@ typedef void (^mtx_client_room_join_block_t)(OFString *_Nullable roomID,
|
||||||
OF_DESIGNATED_INITIALIZER;
|
OF_DESIGNATED_INITIALIZER;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Performs a sync.
|
* @brief Starts the sync loop.
|
||||||
*
|
|
||||||
* @param block A block to call when a sync was performed
|
|
||||||
*/
|
*/
|
||||||
- (void)syncWithTimeout: (of_time_interval_t)timeout
|
- (void)startSyncLoop;
|
||||||
block: (mtx_client_response_block_t)block;
|
|
||||||
|
/**
|
||||||
|
* @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.
|
* @brief Logs out the device and invalidates the access token.
|
||||||
|
|
|
@ -48,6 +48,10 @@ validateHomeserver(OFURL *homeserver)
|
||||||
}
|
}
|
||||||
|
|
||||||
@implementation MTXClient
|
@implementation MTXClient
|
||||||
|
{
|
||||||
|
bool _syncing;
|
||||||
|
}
|
||||||
|
|
||||||
+ (instancetype)clientWithUserID: (OFString *)userID
|
+ (instancetype)clientWithUserID: (OFString *)userID
|
||||||
deviceID: (OFString *)deviceID
|
deviceID: (OFString *)deviceID
|
||||||
accessToken: (OFString *)accessToken
|
accessToken: (OFString *)accessToken
|
||||||
|
@ -158,6 +162,7 @@ validateHomeserver(OFURL *homeserver)
|
||||||
_accessToken = [accessToken copy];
|
_accessToken = [accessToken copy];
|
||||||
_homeserver = [homeserver copy];
|
_homeserver = [homeserver copy];
|
||||||
_storage = [storage retain];
|
_storage = [storage retain];
|
||||||
|
_syncTimeout = 300;
|
||||||
} @catch (id e) {
|
} @catch (id e) {
|
||||||
[self release];
|
[self release];
|
||||||
@throw e;
|
@throw e;
|
||||||
|
@ -196,13 +201,17 @@ validateHomeserver(OFURL *homeserver)
|
||||||
homeserver: _homeserver];
|
homeserver: _homeserver];
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)syncWithTimeout: (of_time_interval_t)timeout
|
- (void)startSyncLoop
|
||||||
block: (mtx_client_response_block_t)block
|
|
||||||
{
|
{
|
||||||
|
if (_syncing)
|
||||||
|
return;
|
||||||
|
|
||||||
|
_syncing = true;
|
||||||
|
|
||||||
void *pool = objc_autoreleasePoolPush();
|
void *pool = objc_autoreleasePoolPush();
|
||||||
MTXRequest *request = [self
|
MTXRequest *request = [self
|
||||||
requestWithPath: @"/_matrix/client/r0/sync"];
|
requestWithPath: @"/_matrix/client/r0/sync"];
|
||||||
unsigned long long timeoutMs = timeout * 1000;
|
unsigned long long timeoutMs = _syncTimeout * 1000;
|
||||||
OFMutableDictionary<OFString *, OFString *> *query =
|
OFMutableDictionary<OFString *, OFString *> *query =
|
||||||
[OFMutableDictionary dictionaryWithObject: @(timeoutMs).stringValue
|
[OFMutableDictionary dictionaryWithObject: @(timeoutMs).stringValue
|
||||||
forKey: @"timeout"];
|
forKey: @"timeout"];
|
||||||
|
@ -211,12 +220,14 @@ validateHomeserver(OFURL *homeserver)
|
||||||
[request performWithBlock: ^ (mtx_response_t response, int statusCode,
|
[request performWithBlock: ^ (mtx_response_t response, int statusCode,
|
||||||
id exception) {
|
id exception) {
|
||||||
if (exception != nil) {
|
if (exception != nil) {
|
||||||
block(exception);
|
if (_syncExceptionHandler != NULL)
|
||||||
|
_syncExceptionHandler(exception);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (statusCode != 200) {
|
if (statusCode != 200) {
|
||||||
block([MTXSyncFailedException
|
if (_syncExceptionHandler != NULL)
|
||||||
|
_syncExceptionHandler([MTXSyncFailedException
|
||||||
exceptionWithStatusCode: statusCode
|
exceptionWithStatusCode: statusCode
|
||||||
response: response
|
response: response
|
||||||
client: self]);
|
client: self]);
|
||||||
|
@ -225,7 +236,9 @@ validateHomeserver(OFURL *homeserver)
|
||||||
|
|
||||||
OFString *nextBatch = response[@"next_batch"];
|
OFString *nextBatch = response[@"next_batch"];
|
||||||
if (![nextBatch isKindOfClass: OFString.class]) {
|
if (![nextBatch isKindOfClass: OFString.class]) {
|
||||||
block([OFInvalidServerReplyException exception]);
|
if (_syncExceptionHandler != NULL)
|
||||||
|
_syncExceptionHandler(
|
||||||
|
[OFInvalidServerReplyException exception]);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -245,16 +258,23 @@ validateHomeserver(OFURL *homeserver)
|
||||||
return true;
|
return true;
|
||||||
}];
|
}];
|
||||||
} @catch (id e) {
|
} @catch (id e) {
|
||||||
block(e);
|
if (_syncExceptionHandler != NULL)
|
||||||
|
_syncExceptionHandler(e);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
block(nil);
|
if (_syncing)
|
||||||
|
[self startSyncLoop];
|
||||||
}];
|
}];
|
||||||
|
|
||||||
objc_autoreleasePoolPop(pool);
|
objc_autoreleasePoolPop(pool);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
- (void)stopSyncLoop
|
||||||
|
{
|
||||||
|
_syncing = false;
|
||||||
|
}
|
||||||
|
|
||||||
- (void)logOutWithBlock: (mtx_client_response_block_t)block
|
- (void)logOutWithBlock: (mtx_client_response_block_t)block
|
||||||
{
|
{
|
||||||
void *pool = objc_autoreleasePoolPush();
|
void *pool = objc_autoreleasePoolPush();
|
||||||
|
|
|
@ -63,21 +63,7 @@ OF_APPLICATION_DELEGATE(Tests)
|
||||||
_client = [client retain];
|
_client = [client retain];
|
||||||
of_log(@"Logged in client: %@", _client);
|
of_log(@"Logged in client: %@", _client);
|
||||||
|
|
||||||
[self sync];
|
[_client startSyncLoop];
|
||||||
}];
|
|
||||||
}
|
|
||||||
|
|
||||||
- (void)sync
|
|
||||||
{
|
|
||||||
[_client syncWithTimeout: 5
|
|
||||||
block: ^ (id exception) {
|
|
||||||
if (exception != nil) {
|
|
||||||
of_log(@"Failed to sync: %@", exception);
|
|
||||||
[OFApplication terminateWithStatus: 1];
|
|
||||||
}
|
|
||||||
|
|
||||||
of_log(@"Synced");
|
|
||||||
|
|
||||||
[self fetchRoomList];
|
[self fetchRoomList];
|
||||||
}];
|
}];
|
||||||
}
|
}
|
||||||
|
@ -110,21 +96,6 @@ OF_APPLICATION_DELEGATE(Tests)
|
||||||
_roomID = [roomID copy];
|
_roomID = [roomID copy];
|
||||||
of_log(@"Joined room %@", _roomID);
|
of_log(@"Joined room %@", _roomID);
|
||||||
|
|
||||||
[self sync2];
|
|
||||||
}];
|
|
||||||
}
|
|
||||||
|
|
||||||
- (void)sync2
|
|
||||||
{
|
|
||||||
[_client syncWithTimeout: 5
|
|
||||||
block: ^ (id exception) {
|
|
||||||
if (exception != nil) {
|
|
||||||
of_log(@"Failed to sync: %@", exception);
|
|
||||||
[OFApplication terminateWithStatus: 1];
|
|
||||||
}
|
|
||||||
|
|
||||||
of_log(@"Synced");
|
|
||||||
|
|
||||||
[self sendMessage];
|
[self sendMessage];
|
||||||
}];
|
}];
|
||||||
}
|
}
|
||||||
|
@ -142,7 +113,11 @@ OF_APPLICATION_DELEGATE(Tests)
|
||||||
|
|
||||||
of_log(@"Message sent to %@", _roomID);
|
of_log(@"Message sent to %@", _roomID);
|
||||||
|
|
||||||
[self leaveRoom];
|
of_log(
|
||||||
|
@"Waiting 5 seconds before leaving room and logging out");
|
||||||
|
|
||||||
|
[self performSelector: @selector(leaveRoom)
|
||||||
|
afterDelay: 5];
|
||||||
}];
|
}];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue