Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
AstroAir committed Dec 24, 2023
1 parent 77d50d1 commit 4689ecb
Show file tree
Hide file tree
Showing 154 changed files with 9,841 additions and 22,116 deletions.
2 changes: 0 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,6 @@ set(server_module
${lithium_src_dir}/websocket/WsServer.cpp
${lithium_src_dir}/websocket/WsHub.cpp
${lithium_src_dir}/websocket/WsInstance.cpp
${lithium_src_dir}/websocket/components/WsDeviceComponent.cpp
${lithium_src_dir}/websocket/components/WsTaskComponent.cpp
)

set(script_module
Expand Down
34 changes: 19 additions & 15 deletions locale/lithium.pot
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: \n"
"Report-Msgid-Bugs-To: [email protected]\n"
"POT-Creation-Date: 2023-12-17 21:01+0800\n"
"POT-Creation-Date: 2023-12-23 14:40+0800\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
Expand All @@ -17,7 +17,7 @@ msgstr ""
"Content-Type: text/plain; charset=CHARSET\n"
"Content-Transfer-Encoding: 8bit\n"

#: E:/msys64/home/Qrm/Lithium/src/device/device_manager.cpp:167
#: E:/msys64/home/Qrm/Lithium/src/device/device_manager.cpp:209
#, c++-format
msgid "A device with name {} already exists, please choose a different name"
msgstr ""
Expand Down Expand Up @@ -74,67 +74,71 @@ msgstr ""
msgid "Set server port to %d"
msgstr ""

#: E:/msys64/home/Qrm/Lithium/src/LithiumApp.cpp:84
#: E:/msys64/home/Qrm/Lithium/src/LithiumApp.cpp:93
#, c++-format
msgid "Failed to load Lithium App , error : {}"
msgstr ""

#: E:/msys64/home/Qrm/Lithium/src/LithiumApp.cpp:120
#: E:/msys64/home/Qrm/Lithium/src/LithiumApp.cpp:136
#, c++-format
msgid "Get config value: {}"
msgstr ""

#: E:/msys64/home/Qrm/Lithium/src/LithiumApp.cpp:126
#: E:/msys64/home/Qrm/Lithium/src/LithiumApp.cpp:142
#, c++-format
msgid "Set {} to {}"
msgstr ""

#: E:/msys64/home/Qrm/Lithium/src/LithiumApp.cpp:344
#: E:/msys64/home/Qrm/Lithium/src/LithiumApp.cpp:150
msgid "Get config value failed: Missing key_path"
msgstr ""

#: E:/msys64/home/Qrm/Lithium/src/LithiumApp.cpp:374
#, c++-format
msgid "Failed to run chai command : {}"
msgstr ""

#: E:/msys64/home/Qrm/Lithium/src/LithiumApp.cpp:362
#: E:/msys64/home/Qrm/Lithium/src/LithiumApp.cpp:392
#, c++-format
msgid "Failed to run chai multi command {}"
msgstr ""

#: E:/msys64/home/Qrm/Lithium/src/LithiumApp.cpp:375
#: E:/msys64/home/Qrm/Lithium/src/LithiumApp.cpp:405
#, c++-format
msgid "Failed to load chaiscript file {}"
msgstr ""

#: E:/msys64/home/Qrm/Lithium/src/LithiumApp.cpp:388
#: E:/msys64/home/Qrm/Lithium/src/LithiumApp.cpp:418
#, c++-format
msgid "Failed to unload chaiscript file {}"
msgstr ""

#: E:/msys64/home/Qrm/Lithium/src/LithiumApp.cpp:401
#: E:/msys64/home/Qrm/Lithium/src/LithiumApp.cpp:431
#, c++-format
msgid "Failed to run chai script {}"
msgstr ""

#: E:/msys64/home/Qrm/Lithium/src/LithiumApp.cpp:423
#: E:/msys64/home/Qrm/Lithium/src/LithiumApp.cpp:453
#, c++-format
msgid "Failed to load module {} in {}"
msgstr ""

#: E:/msys64/home/Qrm/Lithium/src/LithiumApp.cpp:442
#: E:/msys64/home/Qrm/Lithium/src/LithiumApp.cpp:472
#, c++-format
msgid "Failed to unload module {}"
msgstr ""

#: E:/msys64/home/Qrm/Lithium/src/LithiumApp.cpp:462
#: E:/msys64/home/Qrm/Lithium/src/LithiumApp.cpp:492
#, c++-format
msgid "Failed to reload module {}"
msgstr ""

#: E:/msys64/home/Qrm/Lithium/src/LithiumApp.cpp:499
#: E:/msys64/home/Qrm/Lithium/src/LithiumApp.cpp:529
#, c++-format
msgid "Failed to enable module {}"
msgstr ""

#: E:/msys64/home/Qrm/Lithium/src/LithiumApp.cpp:518
#: E:/msys64/home/Qrm/Lithium/src/LithiumApp.cpp:548
#, c++-format
msgid "Failed to disable module {}"
msgstr ""
1 change: 0 additions & 1 deletion src/atom/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ list(APPEND ${PROJECT_NAME}_HEADERS
utils/huffman.hpp
utils/math.hpp
utils/refl_clang.hpp
utils/reflection.hpp
utils/refl.hpp
utils/string.hpp
utils/static_switch.hpp
Expand Down
4 changes: 4 additions & 0 deletions src/atom/log/loguru.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1143,6 +1143,8 @@ LOGURU_ANONYMOUS_NAMESPACE_END
#define DVLOG_IF_F(verbosity, ...) VLOG_IF_F(verbosity, __VA_ARGS__)
#define DRAW_LOG_F(verbosity_name, ...) RAW_LOG_F(verbosity_name, __VA_ARGS__)
#define DRAW_VLOG_F(verbosity, ...) RAW_VLOG_F(verbosity, __VA_ARGS__)
#define DLOG_SCOPE_F(verbosity_name,...) LOG_SCOPE_F(verbosity_name, __VA_ARGS__)
#define DLOG_SCOPE_FUNCTION(...) LOG_SCOPE_FUNCTION(__VA_ARGS__)
#else
// Debug logging disabled:
#define DLOG_F(verbosity_name, ...)
Expand All @@ -1151,6 +1153,8 @@ LOGURU_ANONYMOUS_NAMESPACE_END
#define DVLOG_IF_F(verbosity, ...)
#define DRAW_LOG_F(verbosity_name, ...)
#define DRAW_VLOG_F(verbosity, ...)
#define DLOG_SCOPE_F(verbosity_name,...)
#define DLOG_SCOPE_FUNCTION(...)
#endif

#define CHECK_EQ_F(a, b, ...) CHECK_OP_F(a, b, ==, ##__VA_ARGS__)
Expand Down
76 changes: 58 additions & 18 deletions src/atom/server/deserialize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,34 +31,74 @@ Description: This file contains the declaration of the DeserializationEngine cla

#include "deserialize.hpp"

#include "atom/type/iparams.hpp"
#include "atom/type/json.hpp"
#include "atom/log/loguru.hpp"

using json = nlohmann::json;

std::optional<std::any> JsonDeserializer::deserialize(const std::string &data) const
namespace Atom::Server
{
try
std::optional<std::any> JsonDeserializer::deserialize(const std::string &data) const
{
nlohmann::json jsonData = nlohmann::json::parse(data);
return jsonData.get<std::map<std::string, std::string>>();
DLOG_F(INFO, "JsonDeserializer::deserialize called with {}", data);
try
{
json jsonData = json::parse(data);
DLOG_F(INFO, "JsonDeserializer::deserialize: Successfully deserialized json data.");
return jsonData.get<std::map<std::string, std::string>>();
}
catch (const json::parse_error &)
{
LOG_F(ERROR, "JsonDeserializer::deserialize: Failed to deserialize json data.");
return std::nullopt;
}
}
catch (const nlohmann::json::parse_error &)

std::optional<std::any> JsonParamsDeserializer::deserialize(const std::string &data) const
{
return std::nullopt;
DLOG_F(INFO, "JsonParamsDeserializer::deserialize called with {}", data);
try
{
std::shared_ptr<IParams> params;
if (!params->fromJson(data))
{
LOG_F(ERROR, "JsonParamsDeserializer::deserialize: Failed to deserialize json data.");
return std::nullopt;
}
DLOG_F(INFO, "JsonParamsDeserializer::deserialize: Successfully deserialized json data.");
return params;
}
catch (const std::exception &)
{
LOG_F(ERROR, "JsonParamsDeserializer::deserialize: Failed to deserialize json data.");
return std::nullopt;
}
}
}

void DeserializationEngine::addDeserializeEngine(const std::string &name, const std::shared_ptr<DeserializeEngine> &DeserializeEngine)
{
deserializationEngines_[name] = DeserializeEngine;
}
void DeserializationEngine::addDeserializeEngine(const std::string &name, const std::shared_ptr<DeserializeEngine> &DeserializeEngine)
{
DLOG_F(INFO, "DeserializationEngine::addDeserializeEngine called with {}", name);
if (deserializationEngines_.find(name)!= deserializationEngines_.end())
{
LOG_F(ERROR, "DeserializationEngine::addDeserializeEngine: Deserialize engine already exists: {}", name);
return;
}
deserializationEngines_[name] = DeserializeEngine;
LOG_F(INFO, "DeserializationEngine::addDeserializeEngine: Add deserialize engine: {}", name);
}

bool DeserializationEngine::setCurrentDeserializeEngine(const std::string &name)
{
auto it = deserializationEngines_.find(name);
if (it != deserializationEngines_.end())
bool DeserializationEngine::setCurrentDeserializeEngine(const std::string &name)
{
currentDeserializationEngine_ = name;
return true;
DLOG_F(INFO, "DeserializationEngine::setCurrentDeserializeEngine called with {}", name);
auto it = deserializationEngines_.find(name);
if (it != deserializationEngines_.end())
{
currentDeserializationEngine_ = name;
LOG_F(INFO, "DeserializationEngine::setCurrentDeserializeEngine: Set current deserialize engine to: {}", name);
return true;
}
LOG_F(ERROR, "DeserializationEngine::setCurrentDeserializeEngine: No such deserialize engine: {}", name);
return false;
}
return false;
}
71 changes: 57 additions & 14 deletions src/atom/server/deserialize.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,60 @@ class DeserializeEngine
virtual std::optional<std::any> deserialize(const std::string &data) const = 0;
};

class DeserializationEngine
namespace Atom::Server
{
public:
DeserializationEngine() = default;
~DeserializationEngine() = default;

void addDeserializeEngine(const std::string &name, const std::shared_ptr<DeserializeEngine> &DeserializeEngine);

bool setCurrentDeserializeEngine(const std::string &name);
/**
* @class DeserializationEngine
* @brief A class responsible for deserializing data using different deserialization engines.
*/
class DeserializationEngine
{
public:
/**
* @brief Default constructor.
*/
DeserializationEngine() = default;

/**
* @brief Default destructor.
*/
~DeserializationEngine() = default;

/**
* @brief Adds a deserialization engine to the engine map.
* @param name The name of the deserialization engine.
* @param engine A shared pointer to the deserialization engine.
*/
void addDeserializeEngine(const std::string &name, const std::shared_ptr<DeserializeEngine> &engine);

/**
* @brief Sets the current deserialization engine.
* @param name The name of the deserialization engine to set as current.
* @return True if the deserialization engine was set successfully, false otherwise.
*/
bool setCurrentDeserializeEngine(const std::string &name);

/**
* @brief Deserializes the given data using the current deserialization engine.
* @tparam T The type to deserialize the data into.
* @param data The serialized data to deserialize.
* @return An optional containing the deserialized object if successful, or an empty optional otherwise.
*/
template <typename T>
std::optional<T> deserialize(const std::string &data) const;

private:
#ifdef ENABLE_FASTHASH
emhash8::HashMap<std::string, std::shared_ptr<DeserializeEngine>> deserializationEngines_; /**< Map of deserialization engines. */
#else
std::unordered_map<std::string, std::shared_ptr<DeserializeEngine>> deserializationEngines_; /**< Map of deserialization engines. */
#endif
std::string currentDeserializationEngine_; /**< Name of the current deserialization engine. */
mutable std::mutex mutex_; /**< Mutex to ensure thread safety. */
};

template <typename T>
std::optional<T> deserialize(const std::string &data) const
std::optional<T> DeserializationEngine::deserialize(const std::string &data) const
{
std::lock_guard<std::mutex> lock(mutex_);

Expand All @@ -83,14 +125,15 @@ class DeserializationEngine

return std::nullopt;
}
}

private:
std::unordered_map<std::string, std::shared_ptr<DeserializeEngine>> deserializationEngines_;
std::string currentDeserializationEngine_;
mutable std::mutex mutex_;
class JsonDeserializer : public DeserializeEngine
{
public:
std::optional<std::any> deserialize(const std::string &data) const override;
};

class JsonDeserializer : public DeserializeEngine
class JsonParamsDeserializer : public DeserializeEngine
{
public:
std::optional<std::any> deserialize(const std::string &data) const override;
Expand Down
Loading

0 comments on commit 4689ecb

Please sign in to comment.