Skip to content

Commit 465dae5

Browse files
authored
[Sdk 927] Request Module added (#94)
* namespace added on countly classes * request builder request module * request module completed one deadlock remains * request module integration completed mutext shared ptr added * code cleanup * RQ Module added Unit tests fixed * pr changes
1 parent a6740ab commit 465dae5

File tree

8 files changed

+473
-337
lines changed

8 files changed

+473
-337
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ add_library(countly
3030
${CMAKE_CURRENT_SOURCE_DIR}/src/countly.cpp
3131
${CMAKE_CURRENT_SOURCE_DIR}/src/views_module.cpp
3232
${CMAKE_CURRENT_SOURCE_DIR}/src/logger_module.cpp
33+
${CMAKE_CURRENT_SOURCE_DIR}/src/request_module.cpp
34+
${CMAKE_CURRENT_SOURCE_DIR}/src/request_builder.cpp
3335
${CMAKE_CURRENT_SOURCE_DIR}/src/event.cpp)
3436

3537
target_include_directories(countly

include/countly.hpp

Lines changed: 19 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
#include "countly/event.hpp"
2424
#include "countly/logger_module.hpp"
2525
#include "countly/views_module.hpp"
26+
#include <countly/request_builder.hpp>
27+
#include <countly/request_module.hpp>
2628

2729
namespace cly {
2830
class Countly : public cly::CountlyDelegates {
@@ -112,10 +114,6 @@ class Countly : public cly::CountlyDelegates {
112114

113115
static std::chrono::system_clock::time_point getTimestamp();
114116

115-
static std::string encodeURL(const std::string &data);
116-
117-
static std::string serializeForm(const std::map<std::string, std::string> data);
118-
119117
std::string calculateChecksum(const std::string &salt, const std::string &data);
120118

121119
#ifdef COUNTLY_USE_SQLITE
@@ -199,29 +197,26 @@ class Countly : public cly::CountlyDelegates {
199197
}
200198

201199
/**
202-
* Convert request queue into list.
203-
* Warning: This method is for debugging purposes, and it is going to be removed in the future.
204-
* You should not be using this method.
205-
* @return a vector object containing events.
200+
* This function should not be used as it will be removed in a future release.
201+
* It is currently added as a temporary workaround.
206202
*/
207-
const std::vector<std::string> debugReturnStateOfRQ() {
208-
std::vector<std::string> v(request_queue.begin(), request_queue.end());
209-
return v;
210-
}
211-
212-
/**
213-
* This function should not be used as it will be removed in a future release.
214-
* It is currently added as a temporary workaround.
215-
*/
216203
inline std::function<void(LogLevel, const std::string &)> getLogger() { return logger->getLogger(); }
217204

218205
/**
219-
* This function should not be used as it will be removed in a future release.
220-
* It is currently added as a temporary workaround.
221-
*/
222-
inline void processRQDebug() { processRequestQueue(); }
206+
* This function should not be used as it will be removed in a future release.
207+
* It is currently added as a temporary workaround.
208+
*/
209+
inline void processRQDebug() {
210+
if (is_sdk_initialized) {
211+
requestModule->processQueue(mutex);
212+
}
213+
}
223214

224-
inline void clearRequestQueue() { request_queue.clear(); }
215+
inline void clearRequestQueue() {
216+
if (is_sdk_initialized) {
217+
requestModule->clearRequestQueue();
218+
}
219+
}
225220

226221
inline const CountlyConfiguration &getConfiguration() { return *configuration.get(); }
227222

@@ -241,10 +236,6 @@ class Countly : public cly::CountlyDelegates {
241236
void _updateRemoteConfigWithSpecificValues(const std::map<std::string, std::string> &data);
242237
#pragma endregion Remote_Config_Helper_Methods
243238

244-
void processRequestQueue();
245-
void addToRequestQueue(const std::string &data);
246-
HTTPResponse sendHTTP(std::string path, std::string data);
247-
248239
void _changeDeviceIdWithMerge(const std::string &value);
249240

250241
void _changeDeviceIdWithoutMerge(const std::string &value);
@@ -254,22 +245,20 @@ class Countly : public cly::CountlyDelegates {
254245
std::chrono::system_clock::duration getSessionDuration();
255246

256247
void updateLoop();
257-
258-
bool use_https = false;
259-
260248
bool began_session = false;
261249
bool is_being_disposed = false;
262250
bool is_sdk_initialized = false;
263251

264252
std::chrono::system_clock::time_point last_sent_session_request;
265-
266253
nlohmann::json session_params;
267254

268255
std::unique_ptr<std::thread> thread;
269256
std::unique_ptr<cly::ViewsModule> views_module;
270257
std::shared_ptr<cly::CountlyConfiguration> configuration;
271258
std::shared_ptr<cly::LoggerModule> logger;
272259

260+
std::shared_ptr<cly::RequestBuilder> requestBuilder;
261+
std::unique_ptr<cly::RequestModule> requestModule;
273262
std::shared_ptr<std::mutex> mutex = std::make_shared<std::mutex>();
274263

275264
bool is_queue_being_processed = false;
@@ -279,7 +268,6 @@ class Countly : public cly::CountlyDelegates {
279268
size_t wait_milliseconds = COUNTLY_KEEPALIVE_INTERVAL;
280269

281270
size_t max_events = COUNTLY_MAX_EVENTS_DEFAULT;
282-
std::deque<std::string> request_queue;
283271
#ifndef COUNTLY_USE_SQLITE
284272
std::deque<std::string> event_queue;
285273
#else
@@ -290,5 +278,4 @@ class Countly : public cly::CountlyDelegates {
290278
nlohmann::json remote_config;
291279
};
292280
} // namespace cly
293-
294281
#endif
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#ifndef REQUEST_BUILDER_HPP_
2+
#define REQUEST_BUILDER_HPP_
3+
#include "countly/countly_configuration.hpp"
4+
#include "countly/logger_module.hpp"
5+
#include "nlohmann/json.hpp"
6+
#include <memory>
7+
#include <string>
8+
9+
namespace cly {
10+
11+
class RequestBuilder {
12+
private:
13+
std::shared_ptr<CountlyConfiguration> _configuration;
14+
std::shared_ptr<LoggerModule> _logger;
15+
16+
public:
17+
RequestBuilder(std::shared_ptr<CountlyConfiguration> config, std::shared_ptr<LoggerModule> logger);
18+
~RequestBuilder();
19+
static std::string encodeURL(const std::string &data);
20+
static std::string serializeData(const std::map<std::string, std::string> &data);
21+
std::string buildRequest(const std::map<std::string, std::string> &data);
22+
23+
};
24+
} // namespace cly
25+
#endif

include/countly/request_module.hpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#ifndef REQUEST_MODULE_HPP_
2+
#define REQUEST_MODULE_HPP_
3+
#include <map>
4+
#include <memory>
5+
#include <mutex>
6+
#include <string>
7+
8+
#include "countly/countly_configuration.hpp"
9+
#include "countly/logger_module.hpp"
10+
#include "countly/request_builder.hpp"
11+
12+
namespace cly {
13+
class RequestModule {
14+
15+
public:
16+
~RequestModule();
17+
RequestModule(std::shared_ptr<CountlyConfiguration> config, std::shared_ptr<LoggerModule> logger, std::shared_ptr<RequestBuilder> requestBuilder);
18+
19+
HTTPResponse sendHTTP(std::string path, std::string data);
20+
21+
/**
22+
* SDK central execution call for processing requests in the request queue.
23+
* Only one sender is active at a time. Requests are processed in order.
24+
*/
25+
void processQueue(std::shared_ptr<std::mutex> mutex);
26+
27+
void addRequestToQueue(const std::map<std::string, std::string> &data);
28+
29+
/**
30+
* Clear request queue.
31+
* Warning: This method is for debugging purposes, and it is going to be removed in the future.
32+
* You should not be using this method.
33+
* @return a vector object containing requests.
34+
*/
35+
void clearRequestQueue();
36+
37+
private:
38+
class RequestModuleImpl;
39+
std::unique_ptr<RequestModuleImpl> impl;
40+
};
41+
} // namespace cly
42+
#endif

0 commit comments

Comments
 (0)