forked from meganz/sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest.cpp
More file actions
297 lines (261 loc) · 6.81 KB
/
request.cpp
File metadata and controls
297 lines (261 loc) · 6.81 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
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
184
185
186
187
188
189
190
191
192
193
194
195
196
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/**
* @file request.cpp
* @brief Generic request interface
*
* (c) 2013-2014 by Mega Limited, Auckland, New Zealand
*
* This file is part of the MEGA SDK - Client Access Engine.
*
* Applications using the MEGA API must present a valid application key
* and comply with the the rules set forth in the Terms of Service.
*
* The MEGA SDK is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* @copyright Simplified (2-clause) BSD License.
*
* You should have received a copy of the license along with this
* program.
*/
#include "mega/request.h"
#include "mega/command.h"
#include "mega/logging.h"
#include "mega/megaclient.h"
namespace mega {
bool Request::isFetchNodes() const
{
return cmds.size() == 1 && dynamic_cast<CommandFetchNodes*>(cmds.back());
}
void Request::add(Command* c)
{
cmds.push_back(c);
}
size_t Request::size() const
{
return cmds.size();
}
void Request::get(string* req, bool& suppressSID) const
{
// concatenate all command objects, resulting in an API request
*req = "[";
for (int i = 0; i < (int)cmds.size(); i++)
{
req->append(i ? ",{" : "{");
req->append(cmds[i]->getstring());
req->append("}");
suppressSID = suppressSID && cmds[i]->suppressSID;
}
req->append("]");
}
void Request::process(MegaClient* client)
{
DBTableTransactionCommitter committer(client->tctable);
client->mTctableRequestCommitter = &committer;
client->json = json;
for (; processindex < cmds.size() && !stopProcessing; processindex++)
{
Command* cmd = cmds[processindex];
// in future we may exit this loop early here, when processing actionpackets associated with the command
client->restag = cmd->tag;
cmd->client = client;
if (client->json.enterobject())
{
cmd->procresult();
if (!client->json.leaveobject())
{
LOG_err << "Invalid object";
}
}
else if (client->json.enterarray())
{
cmd->procresult();
if (!client->json.leavearray())
{
LOG_err << "Invalid array";
}
}
else
{
cmd->procresult();
}
}
json = client->json;
if (processindex == cmds.size() || stopProcessing)
{
clear();
}
client->mTctableRequestCommitter = nullptr;
}
void Request::serverresponse(std::string&& movestring, MegaClient* client)
{
assert(processindex == 0);
jsonresponse = std::move(movestring);
json.begin(jsonresponse.c_str());
if (!json.enterarray())
{
LOG_err << "Invalid response from server";
}
}
void Request::servererror(const std::string& e, MegaClient* client)
{
ostringstream s;
s << "[";
for (size_t i = cmds.size(); i--; )
{
s << e << (i ? "," : "");
}
s << "]";
serverresponse(s.str(), client);
}
void Request::clear()
{
for (int i = (int)cmds.size(); i--; )
{
if (!cmds[i]->persistent)
{
delete cmds[i];
}
}
cmds.clear();
jsonresponse.clear();
json.pos = NULL;
processindex = 0;
stopProcessing = false;
}
bool Request::empty() const
{
return cmds.empty();
}
void Request::swap(Request& r)
{
// we use swap to move between queues, but process only after it gets into the completedreqs
cmds.swap(r.cmds);
assert(jsonresponse.empty() && r.jsonresponse.empty());
assert(json.pos == NULL && r.json.pos == NULL);
assert(processindex == 0 && r.processindex == 0);
}
RequestDispatcher::RequestDispatcher()
{
nextreqs.push_back(Request());
}
#ifdef MEGA_MEASURE_CODE
void RequestDispatcher::sendDeferred()
{
if (!nextreqs.back().empty())
{
nextreqs.push_back(Request());
}
nextreqs.back().swap(deferredRequests);
}
#endif
void RequestDispatcher::add(Command *c)
{
#ifdef MEGA_MEASURE_CODE
if (deferRequests && deferRequests(c))
{
deferredRequests.add(c);
return;
}
#endif
if (nextreqs.back().size() >= MAX_COMMANDS)
{
LOG_debug << "Starting an additional Request due to MAX_COMMANDS";
nextreqs.push_back(Request());
}
if (c->batchSeparately && !nextreqs.back().empty())
{
LOG_debug << "Starting an additional Request for a batch-separately command";
nextreqs.push_back(Request());
}
nextreqs.back().add(c);
if (c->batchSeparately)
{
nextreqs.push_back(Request());
}
}
bool RequestDispatcher::cmdspending() const
{
return !nextreqs.front().empty();
}
void RequestDispatcher::serverrequest(string *out, bool& suppressSID, bool &includesFetchingNodes)
{
assert(inflightreq.empty());
inflightreq.swap(nextreqs.front());
nextreqs.pop_front();
if (nextreqs.empty())
{
nextreqs.push_back(Request());
}
inflightreq.get(out, suppressSID);
includesFetchingNodes = inflightreq.isFetchNodes();
#ifdef MEGA_MEASURE_CODE
csRequestsSent += inflightreq.size();
csBatchesSent += 1;
#endif
}
void RequestDispatcher::requeuerequest()
{
#ifdef MEGA_MEASURE_CODE
csBatchesReceived += 1;
#endif
assert(!inflightreq.empty());
if (!nextreqs.front().empty())
{
nextreqs.push_front(Request());
}
nextreqs.front().swap(inflightreq);
}
void RequestDispatcher::serverresponse(std::string&& movestring, MegaClient *client)
{
CodeCounter::ScopeTimer ccst(client->performanceStats.csResponseProcessingTime);
#ifdef MEGA_MEASURE_CODE
csBatchesReceived += 1;
csRequestsCompleted += inflightreq.size();
#endif
processing = true;
inflightreq.serverresponse(std::move(movestring), client);
inflightreq.process(client);
assert(inflightreq.empty());
processing = false;
if (clearWhenSafe)
{
clear();
}
}
void RequestDispatcher::servererror(const std::string& e, MegaClient *client)
{
// notify all the commands in the batch of the failure
// so that they can deallocate memory, take corrective action etc.
processing = true;
inflightreq.servererror(e, client);
inflightreq.process(client);
assert(inflightreq.empty());
processing = false;
if (clearWhenSafe)
{
clear();
}
}
void RequestDispatcher::clear()
{
if (processing)
{
// we are being called from a command that is in progress (eg. logout) - delay wiping the data structure until that call ends.
clearWhenSafe = true;
inflightreq.stopProcessing = true;
}
else
{
inflightreq.clear();
for (auto& r : nextreqs)
{
r.clear();
}
nextreqs.clear();
nextreqs.push_back(Request());
processing = false;
clearWhenSafe = false;
}
}
} // namespace