1 /**
2  * @file mega/request.h
3  * @brief Generic request interface
4  *
5  * (c) 2013-2014 by Mega Limited, Auckland, New Zealand
6  *
7  * This file is part of the MEGA SDK - Client Access Engine.
8  *
9  * Applications using the MEGA API must present a valid application key
10  * and comply with the the rules set forth in the Terms of Service.
11  *
12  * The MEGA SDK is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15  *
16  * @copyright Simplified (2-clause) BSD License.
17  *
18  * You should have received a copy of the license along with this
19  * program.
20  */
21 
22 #ifndef MEGA_REQUEST_H
23 #define MEGA_REQUEST_H 1
24 
25 #include "types.h"
26 #include "json.h"
27 
28 namespace mega {
29 
30 
31 // API request
32 class MEGA_API Request
33 {
34 private:
35     vector<Command*> cmds;
36     string jsonresponse;
37     JSON json;
38     size_t processindex = 0;
39 
40 
41 public:
42     void add(Command*);
43 
44     size_t size() const;
45 
46     void get(string*, bool& suppressSID) const;
47 
48     void serverresponse(string&& movestring, MegaClient*);
49     void servererror(const std::string &e, MegaClient* client);
50 
51     void process(MegaClient* client);
52 
53     void clear();
54     bool empty() const;
55     void swap(Request&);
56 
57     bool stopProcessing = false;
58 
59     // if contains only one command and that command is FetchNodes
60     bool isFetchNodes() const;
61 };
62 
63 
64 class MEGA_API RequestDispatcher
65 {
66     // these ones have been sent to the server, but we haven't received the response yet
67     Request inflightreq;
68 
69     // client-server request double-buffering, in batches of up to MAX_COMMANDS
70     deque<Request> nextreqs;
71 
72     // flags for dealing with resetting everything from a command in progress
73     bool processing = false;
74     bool clearWhenSafe = false;
75 
76     static const int MAX_COMMANDS = 10000;
77 
78 public:
79     RequestDispatcher();
80 
81     // Queue a command to be send to MEGA. Some commands must go in their own batch (in case other commands fail the whole batch), determined by the Command's `batchSeparately` field.
82     void add(Command*);
83 
84     bool cmdspending() const;
85 
86     /**
87      * @brief get the set of commands to be sent to the server (could be a retry)
88      * @param suppressSID
89      * @param includesFetchingNodes set to whether the commands include fetch nodes
90      */
91     void serverrequest(string*, bool& suppressSID, bool &includesFetchingNodes);
92 
93     // once the server response is determined, call one of these to specify the results
94     void requeuerequest();
95     void serverresponse(string&& movestring, MegaClient*);
96     void servererror(const std::string &e, MegaClient*);
97 
98     void clear();
99 
100 #ifdef MEGA_MEASURE_CODE
101     Request deferredRequests;
102     std::function<bool(Command*)> deferRequests;
103     void sendDeferred();
104     uint64_t csRequestsSent = 0, csRequestsCompleted = 0;
105     uint64_t csBatchesSent = 0, csBatchesReceived = 0;
106 #endif
107 
108 };
109 
110 } // namespace
111 
112 #endif
113