-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsafeapi_locks.cpp
136 lines (115 loc) · 4.54 KB
/
safeapi_locks.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
83
84
85
86
87
88
89
90
91
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
128
129
130
131
132
133
134
135
136
#include "safeapi.h"
ulong SafeApi::lockObject(QString id, ulong timeout,
bool exclusive, QJsonObject userdata) {
ulong worker_id = getId();
SafeWorker *worker = createFileWorker(CALL_LOCK);
worker->addParam(PARAM_TOKEN, this->apiState.token);
worker->addParam(PARAM_OBJECT_ID, id);
worker->addParam(PARAM_LOCK_TIMEOUT, (quint32)timeout);
if(exclusive) worker->addParam(PARAM_LOCK_EXCLUSIVE, exclusive ? "1" : "0");
if(!userdata.isEmpty()) worker->addParam(PARAM_LOCK_USERDATA, userdata);
worker->setId(worker_id);
this->connect(worker, &SafeWorker::done,
[=](const SafeWorker *w, const QByteArray& data) {
freeWorker(worker_id);
processWorkersQueue();
QJsonParseError json_error;
QJsonDocument reply = QJsonDocument::fromJson(data, &json_error);
if(json_error.error) {
qWarning() << "[" << worker_id
<< "] JSON error:" << json_error.errorString();
return;
} else if(reportError(worker_id, reply)) {
return;
}
/* LOGIC */
QJsonObject response = reply.object().value("response").toObject();
emit lockObjectComplete(worker_id, response.value("lock_token").toString());
/* ----- */
});
routeWorker(worker);
return worker_id;
}
ulong SafeApi::unlockObject(QString lock_token) {
ulong worker_id = getId();
SafeWorker *worker = createFileWorker(CALL_UNLOCK);
worker->addParam(PARAM_TOKEN, this->apiState.token);
worker->addParam(PARAM_LOCK_TOKEN, lock_token);
worker->setId(worker_id);
this->connect(worker, &SafeWorker::done,
[=](const SafeWorker *w, const QByteArray& data) {
freeWorker(worker_id);
processWorkersQueue();
QJsonParseError json_error;
QJsonDocument reply = QJsonDocument::fromJson(data, &json_error);
if(json_error.error) {
qWarning() << "[" << worker_id
<< "] JSON error:" << json_error.errorString();
return;
} else if(reportError(worker_id, reply)) {
return;
}
/* LOGIC */
emit unlockObjectComplete(worker_id);
/* ----- */
});
routeWorker(worker);
return worker_id;
}
ulong SafeApi::listLocks(QString id) {
ulong worker_id = getId();
SafeWorker *worker = createFileWorker(CALL_LIST_LOCKS);
worker->addParam(PARAM_TOKEN, this->apiState.token);
worker->addParam(PARAM_OBJECT_ID, id);
worker->setId(worker_id);
this->connect(worker, &SafeWorker::done,
[=](const SafeWorker *w, const QByteArray& data) {
freeWorker(worker_id);
processWorkersQueue();
QJsonParseError json_error;
QJsonDocument reply = QJsonDocument::fromJson(data, &json_error);
if(json_error.error) {
qWarning() << "[" << worker_id
<< "] JSON error:" << json_error.errorString();
return;
} else if(reportError(worker_id, reply)) {
return;
}
/* LOGIC */
QJsonObject response = reply.object().value("response").toObject();
emit listLocksComplete(worker_id, response.value("list_locks").toArray());
/* ----- */
});
routeWorker(worker);
return worker_id;
}
ulong SafeApi::refreshLock(QString lock_token, ulong timeout) {
ulong worker_id = getId();
SafeWorker *worker = createFileWorker(CALL_REFRESH_LOCK);
worker->addParam(PARAM_TOKEN, this->apiState.token);
worker->addParam(PARAM_LOCK_TOKEN, lock_token);
worker->addParam(PARAM_LOCK_TIMEOUT, (quint32)timeout);
worker->setId(worker_id);
this->connect(worker, &SafeWorker::done,
[=](const SafeWorker *w, const QByteArray& data) {
freeWorker(worker_id);
processWorkersQueue();
QJsonParseError json_error;
QJsonDocument reply = QJsonDocument::fromJson(data, &json_error);
if(json_error.error) {
qWarning() << "[" << worker_id
<< "] JSON error:" << json_error.errorString();
return;
} else if(reportError(worker_id, reply)) {
return;
}
/* LOGIC */
QJsonObject response = reply.object().value("response").toObject();
QJsonObject userdata = response.value("userdata").toObject();
bool exclusive = (response.value("exclusive").toString() == "0") ? false : true;
emit refreshLockComplete(worker_id, userdata, exclusive);
/* ----- */
});
routeWorker(worker);
return worker_id;
}