This repository has been archived by the owner on Dec 11, 2018. It is now read-only.
forked from Aseman-Land/TelegramQML
-
Notifications
You must be signed in to change notification settings - Fork 1
/
syncmanager.cpp
82 lines (73 loc) · 2.07 KB
/
syncmanager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include "syncmanager.h"
SyncManager::SyncManager(TelegramQml *tQ)
{
this->tQ = tQ;
this->_globalState = UpdatesState();
this->_channelStates = QHash<qint32, UpdatesState>();
this->syncTask = new SyncManagerSyncTask(&serverSyncMutex, &serverSyncRequested, tQ);
syncTask->start();
}
UpdatesState SyncManager::getState(qint32 channelId)
{
UpdatesState result = UpdatesState();
updateMutex.lock();
if (channelId == 0)
result = this->_globalState;
else if (this->_channelStates.contains(channelId))
{
result = this->_channelStates.value(channelId);
}
updateMutex.unlock();
return result;
}
void SyncManager::requestSync()
{
serverSyncRequested.wakeAll();
}
void SyncManager::setState(const UpdatesState &state, qint32 channelId)
{
updateMutex.lock();
if (channelId == 0)
{
this->_globalState = state;
}
else
this->_channelStates[channelId] = UpdatesState(state);
updateMutex.unlock();
}
bool SyncManager::isSynced(const UpdatesState &state, qint32 channelId)
{
bool result = false;
updateMutex.lock();
if (channelId == 0)
{
result = (this->_globalState.seq() == state.seq() );
}
else
{
if (this->_channelStates.contains(channelId))
{
UpdatesState currentState = this->_channelStates[channelId];
result = (currentState.seq() == state.seq() - 1);
}
}
updateMutex.unlock();
return result;
}
SyncManagerSyncTask::SyncManagerSyncTask(QMutex *serverSyncMutex, QWaitCondition *serverSyncRequested, TelegramQml *tQ, QObject *parent): QThread(parent)
{
this->tQ = tQ;
this->serverSyncMutex = serverSyncMutex;
this->serverSyncRequested = serverSyncRequested;
}
void SyncManagerSyncTask::run()
{
serverSyncMutex->lock();
while(true)
{
serverSyncRequested->wait(this->serverSyncMutex);
QThread::msleep(1500);
qWarning() << "Deferred execution of server synchronization requested";
QMetaObject::invokeMethod(tQ, "updatesGetDifference", Qt::QueuedConnection);
}
}