Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ add_library(countly
${CMAKE_CURRENT_SOURCE_DIR}/src/countly.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/views_module.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/logger_module.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/request_module.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/request_builder.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/event.cpp)

target_include_directories(countly
Expand Down
51 changes: 19 additions & 32 deletions include/countly.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#include "countly/event.hpp"
#include "countly/logger_module.hpp"
#include "countly/views_module.hpp"
#include <countly/request_builder.hpp>
#include <countly/request_module.hpp>

namespace cly {
class Countly : public cly::CountlyDelegates {
Expand Down Expand Up @@ -112,10 +114,6 @@ class Countly : public cly::CountlyDelegates {

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

static std::string encodeURL(const std::string &data);

static std::string serializeForm(const std::map<std::string, std::string> data);

std::string calculateChecksum(const std::string &salt, const std::string &data);

#ifdef COUNTLY_USE_SQLITE
Expand Down Expand Up @@ -199,29 +197,26 @@ class Countly : public cly::CountlyDelegates {
}

/**
* Convert request queue into list.
* Warning: This method is for debugging purposes, and it is going to be removed in the future.
* You should not be using this method.
* @return a vector object containing events.
* This function should not be used as it will be removed in a future release.
* It is currently added as a temporary workaround.
*/
const std::vector<std::string> debugReturnStateOfRQ() {
std::vector<std::string> v(request_queue.begin(), request_queue.end());
return v;
}

/**
* This function should not be used as it will be removed in a future release.
* It is currently added as a temporary workaround.
*/
inline std::function<void(LogLevel, const std::string &)> getLogger() { return logger->getLogger(); }

/**
* This function should not be used as it will be removed in a future release.
* It is currently added as a temporary workaround.
*/
inline void processRQDebug() { processRequestQueue(); }
* This function should not be used as it will be removed in a future release.
* It is currently added as a temporary workaround.
*/
inline void processRQDebug() {
if (is_sdk_initialized) {
requestModule->processQueue(mutex);
}
}

inline void clearRequestQueue() { request_queue.clear(); }
inline void clearRequestQueue() {
if (is_sdk_initialized) {
requestModule->clearRequestQueue();
}
}

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

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

void processRequestQueue();
void addToRequestQueue(const std::string &data);
HTTPResponse sendHTTP(std::string path, std::string data);

void _changeDeviceIdWithMerge(const std::string &value);

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

void updateLoop();

bool use_https = false;

bool began_session = false;
bool is_being_disposed = false;
bool is_sdk_initialized = false;

std::chrono::system_clock::time_point last_sent_session_request;

nlohmann::json session_params;

std::unique_ptr<std::thread> thread;
std::unique_ptr<cly::ViewsModule> views_module;
std::shared_ptr<cly::CountlyConfiguration> configuration;
std::shared_ptr<cly::LoggerModule> logger;

std::shared_ptr<cly::RequestBuilder> requestBuilder;
std::unique_ptr<cly::RequestModule> requestModule;
std::shared_ptr<std::mutex> mutex = std::make_shared<std::mutex>();

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

size_t max_events = COUNTLY_MAX_EVENTS_DEFAULT;
std::deque<std::string> request_queue;
#ifndef COUNTLY_USE_SQLITE
std::deque<std::string> event_queue;
#else
Expand All @@ -290,5 +278,4 @@ class Countly : public cly::CountlyDelegates {
nlohmann::json remote_config;
};
} // namespace cly

#endif
25 changes: 25 additions & 0 deletions include/countly/request_builder.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#ifndef REQUEST_BUILDER_HPP_
#define REQUEST_BUILDER_HPP_
#include "countly/countly_configuration.hpp"
#include "countly/logger_module.hpp"
#include "nlohmann/json.hpp"
#include <memory>
#include <string>

namespace cly {

class RequestBuilder {
private:
std::shared_ptr<CountlyConfiguration> _configuration;
std::shared_ptr<LoggerModule> _logger;

public:
RequestBuilder(std::shared_ptr<CountlyConfiguration> config, std::shared_ptr<LoggerModule> logger);
~RequestBuilder();
static std::string encodeURL(const std::string &data);
static std::string serializeData(const std::map<std::string, std::string> &data);
std::string buildRequest(const std::map<std::string, std::string> &data);

};
} // namespace cly
#endif
42 changes: 42 additions & 0 deletions include/countly/request_module.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#ifndef REQUEST_MODULE_HPP_
#define REQUEST_MODULE_HPP_
#include <map>
#include <memory>
#include <mutex>
#include <string>

#include "countly/countly_configuration.hpp"
#include "countly/logger_module.hpp"
#include "countly/request_builder.hpp"

namespace cly {
class RequestModule {

public:
~RequestModule();
RequestModule(std::shared_ptr<CountlyConfiguration> config, std::shared_ptr<LoggerModule> logger, std::shared_ptr<RequestBuilder> requestBuilder);

HTTPResponse sendHTTP(std::string path, std::string data);

/**
* SDK central execution call for processing requests in the request queue.
* Only one sender is active at a time. Requests are processed in order.
*/
void processQueue(std::shared_ptr<std::mutex> mutex);

void addRequestToQueue(const std::map<std::string, std::string> &data);

/**
* Clear request queue.
* Warning: This method is for debugging purposes, and it is going to be removed in the future.
* You should not be using this method.
* @return a vector object containing requests.
*/
void clearRequestQueue();

private:
class RequestModuleImpl;
std::unique_ptr<RequestModuleImpl> impl;
};
} // namespace cly
#endif
Loading