feat: 使用QJson替换nlohmann
使用qt自带json库,替换掉nlohmann库 Log: Task: https://pms.uniontech.com/task-view-111975.html Influence: json解析 Change-Id: Ifef186afc84f7ebd92f9f1591df4b96eba0774c7
This commit is contained in:
parent
0b22bb3adf
commit
dd7d4737bf
1
debian/control
vendored
1
debian/control
vendored
@ -6,7 +6,6 @@ Build-Depends:
|
|||||||
cmake,
|
cmake,
|
||||||
debhelper-compat (= 11),
|
debhelper-compat (= 11),
|
||||||
pkg-config,
|
pkg-config,
|
||||||
nlohmann-json3-dev,
|
|
||||||
libcap-dev,
|
libcap-dev,
|
||||||
qt5-qmake,
|
qt5-qmake,
|
||||||
qtbase5-dev,
|
qtbase5-dev,
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
include(FindPkgConfig)
|
include(FindPkgConfig)
|
||||||
find_package(PkgConfig REQUIRED)
|
find_package(PkgConfig REQUIRED)
|
||||||
|
find_package(Qt5 REQUIRED COMPONENTS Core DBus Concurrent)
|
||||||
|
|
||||||
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
|
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
|
||||||
add_definitions(-DDEFINE_LOADER_PATH)
|
add_definitions(-DDEFINE_LOADER_PATH)
|
||||||
|
@ -35,5 +35,6 @@ target_link_libraries(deepin-application-loader
|
|||||||
pthread
|
pthread
|
||||||
PkgConfig::LIBCAP
|
PkgConfig::LIBCAP
|
||||||
stdc++fs
|
stdc++fs
|
||||||
|
Qt5::Core
|
||||||
)
|
)
|
||||||
install(TARGETS deepin-application-loader DESTINATION bin)
|
install(TARGETS deepin-application-loader DESTINATION bin)
|
||||||
|
@ -88,7 +88,7 @@ int child(void* _arg)
|
|||||||
prctl(PR_SET_PDEATHSIG, SIGTERM);
|
prctl(PR_SET_PDEATHSIG, SIGTERM);
|
||||||
prctl(PR_SET_PDEATHSIG, SIGHUP);
|
prctl(PR_SET_PDEATHSIG, SIGHUP);
|
||||||
|
|
||||||
App app = parseApp(task->runId);
|
App app = parseApp(task->runId.toStdString());
|
||||||
std::string path{ "/usr/share/applications/" + app.id + ".desktop" };
|
std::string path{ "/usr/share/applications/" + app.id + ".desktop" };
|
||||||
if (app.type == "user") {
|
if (app.type == "user") {
|
||||||
struct passwd* user = getpwuid(getuid());
|
struct passwd* user = getpwuid(getuid());
|
||||||
@ -111,15 +111,17 @@ int child(void* _arg)
|
|||||||
mount.data = { "ro" };
|
mount.data = { "ro" };
|
||||||
runtime.hostname = "hostname";
|
runtime.hostname = "hostname";
|
||||||
runtime.process.cwd = "/";
|
runtime.process.cwd = "/";
|
||||||
std::filesystem::path container_root_path(annotations.container_root_path);
|
std::filesystem::path container_root_path(annotations.container_root_path.toStdString());
|
||||||
if (!std::filesystem::exists(container_root_path)) {
|
if (!std::filesystem::exists(container_root_path)) {
|
||||||
if (!std::filesystem::create_directories(container_root_path)) {
|
if (!std::filesystem::create_directories(container_root_path)) {
|
||||||
std::cout << "[Loader] [Warning] cannot create container root path." << std::endl;
|
std::cout << "[Loader] [Warning] cannot create container root path." << std::endl;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
std::transform(task->environments.begin(), task->environments.end(), std::back_inserter(runtime.process.env),
|
|
||||||
[](const std::pair<std::string, std::string>& pair) -> std::string { return pair.first + "=" + pair.second; });
|
for (auto it = task->environments.begin(); it != task->environments.end(); ++it) {
|
||||||
|
runtime.process.env.append(it.key() + "=" + it.value());
|
||||||
|
}
|
||||||
|
|
||||||
std::istringstream stream(dd.value<std::string>("Exec"));
|
std::istringstream stream(dd.value<std::string>("Exec"));
|
||||||
std::string s;
|
std::string s;
|
||||||
@ -132,10 +134,12 @@ int child(void* _arg)
|
|||||||
if (s.length() == 2 && s[0] == '%') {
|
if (s.length() == 2 && s[0] == '%') {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
runtime.process.args.push_back(s);
|
runtime.process.args.push_back(QString::fromStdString(s));
|
||||||
}
|
}
|
||||||
|
|
||||||
std::cout << nlohmann::json(runtime).dump() << std::endl;
|
QByteArray runtimeArray;
|
||||||
|
toJson(runtimeArray, runtime);
|
||||||
|
qWarning() << "runtimeArray: " << runtimeArray;
|
||||||
|
|
||||||
int pipeEnds[2];
|
int pipeEnds[2];
|
||||||
if (pipe(pipeEnds) != 0) {
|
if (pipe(pipeEnds) != 0) {
|
||||||
@ -161,8 +165,9 @@ int child(void* _arg)
|
|||||||
exit(ret);
|
exit(ret);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
nlohmann::json json = runtime;
|
QByteArray runtimeArray;
|
||||||
const std::string data = std::move(json.dump());
|
linglong::toJson(runtimeArray, runtime);
|
||||||
|
const std::string data = runtimeArray.data();
|
||||||
close(pipeEnds[0]);
|
close(pipeEnds[0]);
|
||||||
write(pipeEnds[1], data.c_str(), data.size());
|
write(pipeEnds[1], data.c_str(), data.size());
|
||||||
close(pipeEnds[1]);
|
close(pipeEnds[1]);
|
||||||
@ -192,15 +197,17 @@ int main(int argc, char* argv[])
|
|||||||
Socket::Client client;
|
Socket::Client client;
|
||||||
client.connect(socketPath);
|
client.connect(socketPath);
|
||||||
|
|
||||||
|
QByteArray registerArray;
|
||||||
Methods::Registe registe;
|
Methods::Registe registe;
|
||||||
registe.id = dam_task_type;
|
registe.id = dam_task_type;
|
||||||
registe.hash = dam_task_hash;
|
registe.hash = dam_task_hash;
|
||||||
|
Methods::toJson(registerArray, registe);
|
||||||
|
|
||||||
Methods::Registe registe_result;
|
Methods::Registe registe_result;
|
||||||
registe_result.state = false;
|
registe_result.state = false;
|
||||||
auto result = client.get(registe);
|
auto result = client.get(registerArray);
|
||||||
if (!result.is_null()) {
|
if (!result.isEmpty()) {
|
||||||
registe_result = result;
|
Methods::fromJson(result, registe_result);
|
||||||
}
|
}
|
||||||
if (!registe_result.state) {
|
if (!registe_result.state) {
|
||||||
return -3;
|
return -3;
|
||||||
@ -209,9 +216,12 @@ int main(int argc, char* argv[])
|
|||||||
Methods::Instance instance;
|
Methods::Instance instance;
|
||||||
instance.hash = registe_result.hash;
|
instance.hash = registe_result.hash;
|
||||||
std::cout << "get task" << std::endl;
|
std::cout << "get task" << std::endl;
|
||||||
result = client.get(instance);
|
QByteArray instanceArray;
|
||||||
Methods::Task task = result;
|
Methods::toJson(instanceArray, instance);
|
||||||
std::cout << "[result] " << result << std::endl;
|
result = client.get(instanceArray);
|
||||||
|
Methods::Task task;
|
||||||
|
Methods::toJson(result, task);
|
||||||
|
qWarning() << "[result] " << result;
|
||||||
|
|
||||||
pthread_attr_t attr;
|
pthread_attr_t attr;
|
||||||
size_t stack_size;
|
size_t stack_size;
|
||||||
@ -246,7 +256,9 @@ int main(int argc, char* argv[])
|
|||||||
Methods::Quit quit;
|
Methods::Quit quit;
|
||||||
quit.code = exitCode;
|
quit.code = exitCode;
|
||||||
quit.id = task.id;
|
quit.id = task.id;
|
||||||
client.send(quit);
|
QByteArray quitArray;
|
||||||
|
Methods::toJson(quitArray, quit);
|
||||||
|
client.send(quitArray);
|
||||||
|
|
||||||
return exitCode;
|
return exitCode;
|
||||||
}
|
}
|
||||||
|
@ -1,16 +1,31 @@
|
|||||||
#ifndef BASIC_H_
|
#ifndef BASIC_H_
|
||||||
#define BASIC_H_
|
#define BASIC_H_
|
||||||
|
#include <QDebug>
|
||||||
|
#include <QJsonObject>
|
||||||
|
#include <QJsonDocument>
|
||||||
|
|
||||||
#include <nlohmann/json.hpp>
|
namespace Methods
|
||||||
|
{
|
||||||
namespace Methods {
|
struct Basic
|
||||||
struct Basic {
|
{
|
||||||
std::string type;
|
QString type;
|
||||||
};
|
};
|
||||||
|
|
||||||
using json = nlohmann::json;
|
inline void fromJson(const QByteArray &array, Basic &basic)
|
||||||
inline void from_json(const json &j, Basic &basic) {
|
{
|
||||||
j.at("type").get_to(basic.type);
|
QJsonDocument doc = QJsonDocument::fromJson(array);
|
||||||
|
if (!doc.isObject()) {
|
||||||
|
qWarning() << "fromJson basic failed";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
QJsonObject obj = doc.object();
|
||||||
|
if (!obj.contains("type")) {
|
||||||
|
qWarning() << "type not exist in basic array";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
basic.type = obj.value("type").toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace Methods
|
} // namespace Methods
|
||||||
|
@ -1,23 +1,39 @@
|
|||||||
#ifndef C664E26D_6517_412B_950F_07E20963349E
|
#ifndef C664E26D_6517_412B_950F_07E20963349E
|
||||||
#define C664E26D_6517_412B_950F_07E20963349E
|
#define C664E26D_6517_412B_950F_07E20963349E
|
||||||
|
|
||||||
#include <nlohmann/json.hpp>
|
#include <QDebug>
|
||||||
|
#include <QJsonObject>
|
||||||
|
#include <QJsonDocument>
|
||||||
|
|
||||||
namespace Methods {
|
namespace Methods {
|
||||||
struct Instance {
|
struct Instance {
|
||||||
std::string hash;
|
QString hash;
|
||||||
std::string type{ "instance" };
|
QString type{ "instance" };
|
||||||
};
|
};
|
||||||
|
|
||||||
using json = nlohmann::json;
|
inline void toJson(QByteArray &array, const Instance &instance) {
|
||||||
inline void to_json(json &j, const Instance &instance)
|
QJsonObject obj {
|
||||||
{
|
{ "type", instance.type },
|
||||||
j = json{ { "type", instance.type }, { "hash", instance.hash } };
|
{ "hash", instance.hash }
|
||||||
|
};
|
||||||
|
|
||||||
|
array = QJsonDocument(obj).toJson();
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void from_json(const json &j, Instance &instance)
|
inline void fromJson(const QByteArray &array, Instance &instance) {
|
||||||
{
|
QJsonDocument doc = QJsonDocument::fromJson(array);
|
||||||
j.at("hash").get_to(instance.hash);
|
if (!doc.isObject()) {
|
||||||
|
qWarning() << "fromJson instance failed";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
QJsonObject obj = doc.object();
|
||||||
|
if (!obj.contains("hash")) {
|
||||||
|
qWarning() << "hash not exist in instance array";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
instance.hash = obj.value("hash").toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace Methods
|
} // namespace Methods
|
||||||
|
@ -1,27 +1,46 @@
|
|||||||
#ifndef QUIT_H_
|
#ifndef QUIT_H_
|
||||||
#define QUIT_H_
|
#define QUIT_H_
|
||||||
|
#include <QDebug>
|
||||||
#include <nlohmann/json.hpp>
|
#include <QJsonObject>
|
||||||
|
#include <QJsonDocument>
|
||||||
|
|
||||||
namespace Methods {
|
namespace Methods {
|
||||||
struct Quit {
|
struct Quit {
|
||||||
std::string date;
|
QString date;
|
||||||
std::string type{ "quit" };
|
QString type{ "quit" };
|
||||||
std::string id;
|
QString id;
|
||||||
int code;
|
int code;
|
||||||
};
|
};
|
||||||
using json = nlohmann::json;
|
|
||||||
inline void to_json(json &j, const Quit &quit)
|
inline void toJson(QByteArray &array, const Quit &quit) {
|
||||||
{
|
QJsonObject obj {
|
||||||
j = json{ { "type", quit.type }, { "date", quit.date }, { "id", quit.id }, { "code", quit.code } };
|
{ "type", quit.type },
|
||||||
|
{ "date", quit.date },
|
||||||
|
{ "id", quit.id },
|
||||||
|
{ "code", quit.code }
|
||||||
|
};
|
||||||
|
|
||||||
|
array = QJsonDocument(obj).toJson();
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void from_json(const json &j, Quit &quit)
|
inline void fromJson(const QByteArray &array, Quit &quit) {
|
||||||
{
|
QJsonDocument doc = QJsonDocument::fromJson(array);
|
||||||
j.at("id").get_to(quit.id);
|
if (!doc.isObject()) {
|
||||||
j.at("date").get_to(quit.date);
|
qWarning() << "fromJson quit failed";
|
||||||
j.at("code").get_to(quit.code);
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QJsonObject obj = doc.object();
|
||||||
|
if (!obj.contains("id") || !obj.contains("date") || !obj.contains("code")) {
|
||||||
|
qWarning() << "id date code not exist in quit array";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
quit.id = obj.value("id").toString();
|
||||||
|
quit.date = obj.value("date").toString();
|
||||||
|
quit.code = obj.value("code").toInt();
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace Methods
|
} // namespace Methods
|
||||||
|
|
||||||
#endif // QUIT_H_
|
#endif // QUIT_H_
|
||||||
|
@ -1,29 +1,50 @@
|
|||||||
#ifndef REGISTER_H_
|
#ifndef REGISTER_H_
|
||||||
#define REGISTER_H_
|
#define REGISTER_H_
|
||||||
|
#include <QJsonObject>
|
||||||
#include <nlohmann/json.hpp>
|
#include <QJsonDocument>
|
||||||
|
#include <QDebug>
|
||||||
|
|
||||||
namespace Methods {
|
namespace Methods {
|
||||||
struct Registe {
|
struct Registe {
|
||||||
std::string date;
|
QString date;
|
||||||
std::string id;
|
QString id;
|
||||||
std::string type{ "registe" };
|
QString type{ "registe" };
|
||||||
std::string hash;
|
QString hash;
|
||||||
bool state;
|
bool state;
|
||||||
};
|
};
|
||||||
|
|
||||||
using json = nlohmann::json;
|
inline void toJson(QByteArray &array, const Registe ®iste) {
|
||||||
inline void to_json(json &j, const Registe ®iste)
|
QJsonObject obj = {
|
||||||
{
|
{ "type", registe.type },
|
||||||
j = json{ { "type", registe.type }, { "id", registe.id }, { "hash", registe.hash }, { "state", registe.state }, { "date", registe.date } };
|
{ "id", registe.id },
|
||||||
|
{ "hash", registe.hash },
|
||||||
|
{ "state", registe.state },
|
||||||
|
{ "date", registe.date }
|
||||||
|
};
|
||||||
|
|
||||||
|
array = QJsonDocument(obj).toJson();
|
||||||
}
|
}
|
||||||
inline void from_json(const json &j, Registe ®iste)
|
|
||||||
{
|
inline void fromJson(const QByteArray &array, Registe ®iste) {
|
||||||
j.at("id").get_to(registe.id);
|
QJsonDocument doc = QJsonDocument::fromJson(array);
|
||||||
j.at("date").get_to(registe.date);
|
if (!doc.isObject()) {
|
||||||
j.at("hash").get_to(registe.hash);
|
qWarning() << "fromJson registe failed";
|
||||||
j.at("state").get_to(registe.state);
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QJsonObject obj = doc.object();
|
||||||
|
if (!obj.contains("id") || !obj.contains("date") || !obj.contains("hash")\
|
||||||
|
|| !obj.contains("state")) {
|
||||||
|
qWarning() << "id date code state not exist in registe array";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
registe.id = obj.value("id").toString();
|
||||||
|
registe.date = obj.value("date").toString();
|
||||||
|
registe.hash = obj.value("hash").toString();
|
||||||
|
registe.state = obj.value("state").toBool();
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace Methods
|
} // namespace Methods
|
||||||
|
|
||||||
#endif // REGISTER_H_
|
#endif // REGISTER_H_
|
||||||
|
@ -1,36 +1,76 @@
|
|||||||
#ifndef B0B88BD6_CF1E_4E87_926A_E6DBE6B9B19C
|
#ifndef B0B88BD6_CF1E_4E87_926A_E6DBE6B9B19C
|
||||||
#define B0B88BD6_CF1E_4E87_926A_E6DBE6B9B19C
|
#define B0B88BD6_CF1E_4E87_926A_E6DBE6B9B19C
|
||||||
|
|
||||||
#include <map>
|
|
||||||
#include <nlohmann/json.hpp>
|
|
||||||
#include <utility>
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
namespace Methods {
|
#include <utility>
|
||||||
struct Task {
|
#include <QList>
|
||||||
std::string id;
|
#include <QMap>
|
||||||
std::string runId;
|
#include <QJsonObject>
|
||||||
std::string type{ "task" };
|
#include <QJsonDocument>
|
||||||
std::string date;
|
#include <QJsonArray>
|
||||||
std::vector<std::string> arguments;
|
#include <QDebug>
|
||||||
std::multimap<std::string, std::string> environments;
|
|
||||||
|
namespace Methods
|
||||||
|
{
|
||||||
|
struct Task
|
||||||
|
{
|
||||||
|
QString id;
|
||||||
|
QString runId;
|
||||||
|
QString type{"task"};
|
||||||
|
QString date;
|
||||||
|
QList<QString> arguments;
|
||||||
|
QMap<QString, QString> environments;
|
||||||
};
|
};
|
||||||
|
|
||||||
using json = nlohmann::json;
|
inline void toJson(QByteArray &array, const Task &task) {
|
||||||
inline void to_json(json &j, const Task &task)
|
QJsonArray argArray;
|
||||||
{
|
for (auto arg : task.arguments) {
|
||||||
j = json{ { "type", task.type }, { "id", task.id }, { "run_id", task.runId }, { "date", task.date }, { "arguments", task.arguments }, { "environments", task.environments } };
|
argArray.append(arg);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void from_json(const json &j, Task &task)
|
QVariantMap envsMap;
|
||||||
{
|
for (auto it = task.environments.constBegin(); it != task.environments.constEnd(); ++it) {
|
||||||
j.at("id").get_to(task.id);
|
envsMap.insert(it.key(), it.value());
|
||||||
j.at("run_id").get_to(task.runId);
|
|
||||||
j.at("date").get_to(task.date);
|
|
||||||
j.at("arguments").get_to(task.arguments);
|
|
||||||
j.at("environments").get_to(task.environments);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QJsonObject obj = {
|
||||||
|
{"type", task.type},
|
||||||
|
{"id", task.id},
|
||||||
|
{"run_id", task.runId},
|
||||||
|
{"date", task.date},
|
||||||
|
{"arguments", argArray},
|
||||||
|
{"environments", QJsonObject::fromVariantMap(envsMap)}
|
||||||
|
};
|
||||||
|
|
||||||
|
array = QJsonDocument(obj).toJson();
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void fromJson(const QByteArray &array, Task &task) {
|
||||||
|
QJsonDocument doc = QJsonDocument::fromJson(array);
|
||||||
|
if (!doc.isObject()) {
|
||||||
|
qWarning() << "fromJson task failed";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
QJsonObject obj = doc.object();
|
||||||
|
if (!obj.contains("id") || !obj.contains("runId") || !obj.contains("date") \
|
||||||
|
|| !obj.contains("arguments") || !obj.contains("environments")) {
|
||||||
|
qWarning() << "id runId date arguments environments not exist in task array";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
task.id = obj.value("id").toString();
|
||||||
|
task.runId = obj.value("runId").toString();
|
||||||
|
task.date = obj.value("date").toString();
|
||||||
|
for (auto arg : obj.value("arguments").toArray()) {
|
||||||
|
task.arguments.append(arg.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
QVariantMap envsMap = obj.value("environments").toObject().toVariantMap();
|
||||||
|
for (auto it = envsMap.constBegin(); it != envsMap.constEnd(); ++it) {
|
||||||
|
task.environments.insert(it.key(), it.value().toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
} // namespace Methods
|
} // namespace Methods
|
||||||
|
|
||||||
#endif /* B0B88BD6_CF1E_4E87_926A_E6DBE6B9B19C */
|
#endif /* B0B88BD6_CF1E_4E87_926A_E6DBE6B9B19C */
|
||||||
|
@ -67,27 +67,29 @@ struct ClientPrivate {
|
|||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
nlohmann::json get(const nlohmann::json& call)
|
|
||||||
{
|
QByteArray get(const QByteArray &call) {
|
||||||
send(call);
|
send(call);
|
||||||
|
|
||||||
char buf[512];
|
char buf[512];
|
||||||
std::string result;
|
std::string result;
|
||||||
int bytesRead;
|
int bytesRead;
|
||||||
while ((bytesRead = recv(socket_fd, buf, 512, 0)) > 0) {
|
while ((bytesRead = recv(socket_fd, buf, 512, 0)) > 0) {
|
||||||
for (int i = 0; i < bytesRead; i++) {
|
for (int i = 0; i < bytesRead; ++i) {
|
||||||
result += buf[i];
|
result += buf[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
if (buf[bytesRead - 1] == '\0') {
|
if (buf[bytesRead - 1] == '\0') {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
};
|
|
||||||
return nlohmann::json::parse(result);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t send(const nlohmann::json& call)
|
QJsonDocument doc = QJsonDocument::fromRawData(result.data(), result.size());
|
||||||
{
|
return doc.toJson();
|
||||||
std::string data = call.dump();
|
}
|
||||||
|
|
||||||
|
size_t send(const QByteArray &call) {
|
||||||
|
std::string data = call.data();
|
||||||
data += '\0';
|
data += '\0';
|
||||||
return write(socket_fd, data.c_str(), data.length());
|
return write(socket_fd, data.c_str(), data.length());
|
||||||
}
|
}
|
||||||
@ -102,12 +104,12 @@ bool Client::connect(const std::string& host)
|
|||||||
return d_ptr->connect(host);
|
return d_ptr->connect(host);
|
||||||
}
|
}
|
||||||
|
|
||||||
nlohmann::json Client::get(const nlohmann::json& call)
|
QByteArray Client::get(const QByteArray &call)
|
||||||
{
|
{
|
||||||
return d_ptr->get(call);
|
return d_ptr->get(call);
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t Client::send(const nlohmann::json& call)
|
size_t Client::send(const QByteArray &call)
|
||||||
{
|
{
|
||||||
return d_ptr->send(call);
|
return d_ptr->send(call);
|
||||||
}
|
}
|
||||||
|
@ -3,20 +3,23 @@
|
|||||||
|
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <nlohmann/json.hpp>
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
namespace Socket {
|
#include <QJsonDocument>
|
||||||
|
|
||||||
|
namespace Socket
|
||||||
|
{
|
||||||
class ClientPrivate;
|
class ClientPrivate;
|
||||||
class Client {
|
class Client
|
||||||
|
{
|
||||||
std::unique_ptr<ClientPrivate> d_ptr;
|
std::unique_ptr<ClientPrivate> d_ptr;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Client();
|
Client();
|
||||||
~Client();
|
~Client();
|
||||||
bool connect(const std::string &host);
|
bool connect(const std::string &host);
|
||||||
nlohmann::json get(const nlohmann::json& call);
|
QByteArray get(const QByteArray &call);
|
||||||
size_t send(const nlohmann::json& call);
|
size_t send(const QByteArray &call);
|
||||||
void onReadyRead(std::function<void(const std::vector<char> &)> func);
|
void onReadyRead(std::function<void(const std::vector<char> &)> func);
|
||||||
void waitForFinished();
|
void waitForFinished();
|
||||||
};
|
};
|
||||||
|
@ -13,46 +13,56 @@
|
|||||||
|
|
||||||
#define JSON_USE_IMPLICIT_CONVERSIONS 0
|
#define JSON_USE_IMPLICIT_CONVERSIONS 0
|
||||||
|
|
||||||
#include <nlohmann/json.hpp>
|
|
||||||
#include <optional>
|
#include <optional>
|
||||||
|
|
||||||
#define tl std
|
#define tl std
|
||||||
|
|
||||||
namespace nlohmann {
|
// namespace nlohmann {
|
||||||
|
|
||||||
template<class J, class T>
|
// template<class J, class T>
|
||||||
inline void from_json(const J &j, tl::optional<T> &v)
|
// inline void from_json(const J &j, tl::optional<T> &v)
|
||||||
{
|
// {
|
||||||
if (j.is_null()) {
|
// if (j.is_null()) {
|
||||||
v = tl::nullopt;
|
// v = tl::nullopt;
|
||||||
} else {
|
// } else {
|
||||||
v = j.template get<T>();
|
// v = j.template get<T>();
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
template<class J, class T>
|
// template<class J, class T>
|
||||||
inline void to_json(J &j, const tl::optional<T> &o)
|
// inline void to_json(J &j, const tl::optional<T> &o)
|
||||||
{
|
// {
|
||||||
if (o.has_value()) {
|
// if (o.has_value()) {
|
||||||
j = o.value();
|
// j = o.value();
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
} // namespace nlohmann
|
// } // namespace nlohmann
|
||||||
|
|
||||||
namespace linglong {
|
// namespace linglong {
|
||||||
|
|
||||||
template<class T>
|
// template<class T>
|
||||||
tl::optional<T> optional(const nlohmann::json &j, const char *key)
|
// tl::optional<T> optional(const QJsonObject &j, const char *key)
|
||||||
{
|
// {
|
||||||
tl::optional<T> o;
|
// tl::optional<T> o;
|
||||||
auto iter = j.template find(key);
|
// auto iter = j.template find(key);
|
||||||
if (iter != j.end()) {
|
// if (iter != j.end()) {
|
||||||
o = iter->template get<tl::optional<T>>();
|
// o = iter->template get<tl::optional<T>>();
|
||||||
}
|
// }
|
||||||
return o;
|
// return o;
|
||||||
}
|
// }
|
||||||
|
|
||||||
} // namespace linglong
|
// template<class T>
|
||||||
|
// tl::optional<T> optional(const nlohmann::json &j, const char *key)
|
||||||
|
// {
|
||||||
|
// tl::optional<T> o;
|
||||||
|
// auto iter = j.template find(key);
|
||||||
|
// if (iter != j.end()) {
|
||||||
|
// o = iter->template get<tl::optional<T>>();
|
||||||
|
// }
|
||||||
|
// return o;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// } // namespace linglong
|
||||||
|
|
||||||
#endif /* LINGLONG_BOX_SRC_UTIL_JSON_H_ */
|
#endif /* LINGLONG_BOX_SRC_UTIL_JSON_H_ */
|
||||||
|
File diff suppressed because it is too large
Load Diff
@ -20,25 +20,25 @@
|
|||||||
|
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
|
||||||
namespace linglong {
|
// namespace linglong {
|
||||||
namespace util {
|
// namespace util {
|
||||||
namespace json {
|
// namespace json {
|
||||||
|
|
||||||
inline nlohmann::json fromByteArray(const std::string &content)
|
// inline nlohmann::json fromByteArray(const std::string &content)
|
||||||
{
|
// {
|
||||||
return nlohmann::json::parse(content);
|
// return nlohmann::json::parse(content);
|
||||||
}
|
// }
|
||||||
|
|
||||||
inline nlohmann::json fromFile(const std::string &filepath)
|
// inline nlohmann::json fromFile(const std::string &filepath)
|
||||||
{
|
// {
|
||||||
std::ifstream f(filepath);
|
// std::ifstream f(filepath);
|
||||||
std::string str((std::istreambuf_iterator<char>(f)), std::istreambuf_iterator<char>());
|
// std::string str((std::istreambuf_iterator<char>(f)), std::istreambuf_iterator<char>());
|
||||||
auto j = fromByteArray(str);
|
// auto j = fromByteArray(str);
|
||||||
return j;
|
// return j;
|
||||||
}
|
// }
|
||||||
|
|
||||||
} // namespace json
|
// } // namespace json
|
||||||
} // namespace util
|
// } // namespace util
|
||||||
} // namespace linglong
|
// } // namespace linglong
|
||||||
|
|
||||||
#endif /* LINGLONG_BOX_SRC_UTIL_UTIL_H_ */
|
#endif /* LINGLONG_BOX_SRC_UTIL_UTIL_H_ */
|
||||||
|
@ -2,8 +2,6 @@ set(CMAKE_INCLUDE_CURRENT_DIR ON)
|
|||||||
set(CMAKE_AUTOMOC ON)
|
set(CMAKE_AUTOMOC ON)
|
||||||
set(CMAKE_AUTORCC ON)
|
set(CMAKE_AUTORCC ON)
|
||||||
|
|
||||||
find_package(Qt5 REQUIRED COMPONENTS Core DBus Concurrent)
|
|
||||||
|
|
||||||
qt5_add_dbus_adaptor(ADAPTER_SOURCES
|
qt5_add_dbus_adaptor(ADAPTER_SOURCES
|
||||||
../../DBus/org.desktopspec.ApplicationManager.xml
|
../../DBus/org.desktopspec.ApplicationManager.xml
|
||||||
impl/application_manager.h
|
impl/application_manager.h
|
||||||
|
@ -140,15 +140,15 @@ Methods::Task ApplicationInstance::taskInfo() const
|
|||||||
Q_D(const ApplicationInstance);
|
Q_D(const ApplicationInstance);
|
||||||
|
|
||||||
Methods::Task task;
|
Methods::Task task;
|
||||||
task.id = d->m_id.toStdString();
|
task.id = d->m_id;
|
||||||
task.runId = d->application->id().toStdString();
|
task.runId = d->application->id();
|
||||||
task.date = QString::number(startuptime()).toStdString();
|
task.date = QString::number(startuptime());
|
||||||
|
|
||||||
// TODO: debug to display environment
|
// TODO: debug to display environment
|
||||||
task.environments.insert({ "DISPLAY", ":0" });
|
task.environments.insert( "DISPLAY", ":0" );
|
||||||
auto sysEnv = QProcessEnvironment::systemEnvironment();
|
auto sysEnv = QProcessEnvironment::systemEnvironment();
|
||||||
for (const auto& key : sysEnv.keys()) {
|
for (const auto& key : sysEnv.keys()) {
|
||||||
task.environments.insert({ key.toStdString(), sysEnv.value(key).toStdString() });
|
task.environments.insert( key, sysEnv.value(key) );
|
||||||
}
|
}
|
||||||
|
|
||||||
return task;
|
return task;
|
||||||
|
@ -5,7 +5,6 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <nlohmann/json.hpp>
|
|
||||||
#include <thread>
|
#include <thread>
|
||||||
|
|
||||||
#include "../../modules/methods/basic.h"
|
#include "../../modules/methods/basic.h"
|
||||||
@ -18,7 +17,8 @@
|
|||||||
#include "application_instance.h"
|
#include "application_instance.h"
|
||||||
#include "applicationinstanceadaptor.h"
|
#include "applicationinstanceadaptor.h"
|
||||||
|
|
||||||
class ApplicationManagerPrivate : public QObject {
|
class ApplicationManagerPrivate : public QObject
|
||||||
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
ApplicationManager *q_ptr = nullptr;
|
ApplicationManager *q_ptr = nullptr;
|
||||||
Q_DECLARE_PUBLIC(ApplicationManager);
|
Q_DECLARE_PUBLIC(ApplicationManager);
|
||||||
@ -43,43 +43,47 @@ private:
|
|||||||
for (char c : data) {
|
for (char c : data) {
|
||||||
tmp += c;
|
tmp += c;
|
||||||
}
|
}
|
||||||
using namespace nlohmann;
|
|
||||||
if (json::parse(tmp).is_null()) {
|
QByteArray jsonArray = data.data();
|
||||||
server.close(socket);
|
Methods::Basic basic;
|
||||||
return;
|
Methods::fromJson(jsonArray, basic);
|
||||||
}
|
QByteArray tmpArray;
|
||||||
Methods::Basic basic = json::parse(tmp);
|
|
||||||
do {
|
do {
|
||||||
if (basic.type == "instance") {
|
if (basic.type == "instance") {
|
||||||
Methods::Instance instance = nlohmann::json::parse(tmp);
|
Methods::Instance instance;
|
||||||
auto find = tasks.find(instance.hash);
|
Methods::fromJson(jsonArray, instance);
|
||||||
if (find != tasks.end()) {
|
auto find = tasks.find(instance.hash.toStdString());
|
||||||
nlohmann::json result = find->second->taskInfo();
|
if (find != tasks.end())
|
||||||
write(socket, result.dump());
|
{
|
||||||
|
Methods::Task task = find->second->taskInfo();
|
||||||
|
Methods::toJson(tmpArray, task);
|
||||||
|
write(socket, tmpArray.toStdString());
|
||||||
tasks.erase(find);
|
tasks.erase(find);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (basic.type == "quit") {
|
if (basic.type == "quit") {
|
||||||
Methods::Quit quit = json::parse(tmp);
|
Methods::Quit quit;
|
||||||
|
Methods::fromJson(jsonArray, quit);
|
||||||
server.close(socket);
|
server.close(socket);
|
||||||
std::cout << "client quit" << std::endl;
|
std::cout << "client quit" << std::endl;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (basic.type == "registe") {
|
if (basic.type == "registe") {
|
||||||
Methods::Registe registe = nlohmann::json::parse(tmp);
|
Methods::Registe registe;
|
||||||
|
Methods::fromJson(jsonArray, registe);
|
||||||
Methods::Registe result;
|
Methods::Registe result;
|
||||||
result.state = false;
|
result.state = false;
|
||||||
// std::lock_guard<std::mutex> lock(task_mutex);
|
// std::lock_guard<std::mutex> lock(task_mutex);
|
||||||
for (auto it = tasks.begin(); it != tasks.end(); ++it) {
|
for (auto it = tasks.begin(); it != tasks.end(); ++it) {
|
||||||
result.state = true;
|
result.state = true;
|
||||||
result.hash = it->first;
|
result.hash = QString::fromStdString(it->first);
|
||||||
}
|
}
|
||||||
write(socket, json(result).dump());
|
Methods::toJson(tmpArray, result);
|
||||||
std::cout << "registe a new client" << std::endl;
|
write(socket, tmpArray.toStdString());
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
write(socket, json().dump());
|
write(socket, jsonArray.toStdString());
|
||||||
} while (false);
|
} while (false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user