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,
|
||||
debhelper-compat (= 11),
|
||||
pkg-config,
|
||||
nlohmann-json3-dev,
|
||||
libcap-dev,
|
||||
qt5-qmake,
|
||||
qtbase5-dev,
|
||||
|
@ -1,5 +1,6 @@
|
||||
include(FindPkgConfig)
|
||||
find_package(PkgConfig REQUIRED)
|
||||
find_package(Qt5 REQUIRED COMPONENTS Core DBus Concurrent)
|
||||
|
||||
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
|
||||
add_definitions(-DDEFINE_LOADER_PATH)
|
||||
|
@ -35,5 +35,6 @@ target_link_libraries(deepin-application-loader
|
||||
pthread
|
||||
PkgConfig::LIBCAP
|
||||
stdc++fs
|
||||
Qt5::Core
|
||||
)
|
||||
install(TARGETS deepin-application-loader DESTINATION bin)
|
||||
|
@ -88,7 +88,7 @@ int child(void* _arg)
|
||||
prctl(PR_SET_PDEATHSIG, SIGTERM);
|
||||
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" };
|
||||
if (app.type == "user") {
|
||||
struct passwd* user = getpwuid(getuid());
|
||||
@ -111,15 +111,17 @@ int child(void* _arg)
|
||||
mount.data = { "ro" };
|
||||
runtime.hostname = "hostname";
|
||||
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::create_directories(container_root_path)) {
|
||||
std::cout << "[Loader] [Warning] cannot create container root path." << std::endl;
|
||||
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::string s;
|
||||
@ -132,10 +134,12 @@ int child(void* _arg)
|
||||
if (s.length() == 2 && s[0] == '%') {
|
||||
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];
|
||||
if (pipe(pipeEnds) != 0) {
|
||||
@ -161,8 +165,9 @@ int child(void* _arg)
|
||||
exit(ret);
|
||||
}
|
||||
else {
|
||||
nlohmann::json json = runtime;
|
||||
const std::string data = std::move(json.dump());
|
||||
QByteArray runtimeArray;
|
||||
linglong::toJson(runtimeArray, runtime);
|
||||
const std::string data = runtimeArray.data();
|
||||
close(pipeEnds[0]);
|
||||
write(pipeEnds[1], data.c_str(), data.size());
|
||||
close(pipeEnds[1]);
|
||||
@ -192,15 +197,17 @@ int main(int argc, char* argv[])
|
||||
Socket::Client client;
|
||||
client.connect(socketPath);
|
||||
|
||||
QByteArray registerArray;
|
||||
Methods::Registe registe;
|
||||
registe.id = dam_task_type;
|
||||
registe.hash = dam_task_hash;
|
||||
Methods::toJson(registerArray, registe);
|
||||
|
||||
Methods::Registe registe_result;
|
||||
registe_result.state = false;
|
||||
auto result = client.get(registe);
|
||||
if (!result.is_null()) {
|
||||
registe_result = result;
|
||||
auto result = client.get(registerArray);
|
||||
if (!result.isEmpty()) {
|
||||
Methods::fromJson(result, registe_result);
|
||||
}
|
||||
if (!registe_result.state) {
|
||||
return -3;
|
||||
@ -209,9 +216,12 @@ int main(int argc, char* argv[])
|
||||
Methods::Instance instance;
|
||||
instance.hash = registe_result.hash;
|
||||
std::cout << "get task" << std::endl;
|
||||
result = client.get(instance);
|
||||
Methods::Task task = result;
|
||||
std::cout << "[result] " << result << std::endl;
|
||||
QByteArray instanceArray;
|
||||
Methods::toJson(instanceArray, instance);
|
||||
result = client.get(instanceArray);
|
||||
Methods::Task task;
|
||||
Methods::toJson(result, task);
|
||||
qWarning() << "[result] " << result;
|
||||
|
||||
pthread_attr_t attr;
|
||||
size_t stack_size;
|
||||
@ -246,7 +256,9 @@ int main(int argc, char* argv[])
|
||||
Methods::Quit quit;
|
||||
quit.code = exitCode;
|
||||
quit.id = task.id;
|
||||
client.send(quit);
|
||||
QByteArray quitArray;
|
||||
Methods::toJson(quitArray, quit);
|
||||
client.send(quitArray);
|
||||
|
||||
return exitCode;
|
||||
}
|
||||
|
@ -1,17 +1,32 @@
|
||||
#ifndef BASIC_H_
|
||||
#define BASIC_H_
|
||||
#include <QDebug>
|
||||
#include <QJsonObject>
|
||||
#include <QJsonDocument>
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
namespace Methods
|
||||
{
|
||||
struct Basic
|
||||
{
|
||||
QString type;
|
||||
};
|
||||
|
||||
namespace Methods {
|
||||
struct Basic {
|
||||
std::string type;
|
||||
};
|
||||
inline void fromJson(const QByteArray &array, Basic &basic)
|
||||
{
|
||||
QJsonDocument doc = QJsonDocument::fromJson(array);
|
||||
if (!doc.isObject()) {
|
||||
qWarning() << "fromJson basic failed";
|
||||
return;
|
||||
}
|
||||
|
||||
using json = nlohmann::json;
|
||||
inline void from_json(const json &j, Basic &basic) {
|
||||
j.at("type").get_to(basic.type);
|
||||
}
|
||||
QJsonObject obj = doc.object();
|
||||
if (!obj.contains("type")) {
|
||||
qWarning() << "type not exist in basic array";
|
||||
return;
|
||||
}
|
||||
|
||||
basic.type = obj.value("type").toString();
|
||||
}
|
||||
|
||||
} // namespace Methods
|
||||
|
||||
|
@ -1,23 +1,39 @@
|
||||
#ifndef C664E26D_6517_412B_950F_07E20963349E
|
||||
#define C664E26D_6517_412B_950F_07E20963349E
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <QDebug>
|
||||
#include <QJsonObject>
|
||||
#include <QJsonDocument>
|
||||
|
||||
namespace Methods {
|
||||
struct Instance {
|
||||
std::string hash;
|
||||
std::string type{ "instance" };
|
||||
QString hash;
|
||||
QString type{ "instance" };
|
||||
};
|
||||
|
||||
using json = nlohmann::json;
|
||||
inline void to_json(json &j, const Instance &instance)
|
||||
{
|
||||
j = json{ { "type", instance.type }, { "hash", instance.hash } };
|
||||
inline void toJson(QByteArray &array, const Instance &instance) {
|
||||
QJsonObject obj {
|
||||
{ "type", instance.type },
|
||||
{ "hash", instance.hash }
|
||||
};
|
||||
|
||||
array = QJsonDocument(obj).toJson();
|
||||
}
|
||||
|
||||
inline void from_json(const json &j, Instance &instance)
|
||||
{
|
||||
j.at("hash").get_to(instance.hash);
|
||||
inline void fromJson(const QByteArray &array, Instance &instance) {
|
||||
QJsonDocument doc = QJsonDocument::fromJson(array);
|
||||
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
|
||||
|
@ -1,27 +1,46 @@
|
||||
#ifndef QUIT_H_
|
||||
#define QUIT_H_
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <QDebug>
|
||||
#include <QJsonObject>
|
||||
#include <QJsonDocument>
|
||||
|
||||
namespace Methods {
|
||||
struct Quit {
|
||||
std::string date;
|
||||
std::string type{ "quit" };
|
||||
std::string id;
|
||||
int code;
|
||||
QString date;
|
||||
QString type{ "quit" };
|
||||
QString id;
|
||||
int code;
|
||||
};
|
||||
using json = nlohmann::json;
|
||||
inline void to_json(json &j, const Quit &quit)
|
||||
{
|
||||
j = json{ { "type", quit.type }, { "date", quit.date }, { "id", quit.id }, { "code", quit.code } };
|
||||
|
||||
inline void toJson(QByteArray &array, const Quit &quit) {
|
||||
QJsonObject obj {
|
||||
{ "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)
|
||||
{
|
||||
j.at("id").get_to(quit.id);
|
||||
j.at("date").get_to(quit.date);
|
||||
j.at("code").get_to(quit.code);
|
||||
inline void fromJson(const QByteArray &array, Quit &quit) {
|
||||
QJsonDocument doc = QJsonDocument::fromJson(array);
|
||||
if (!doc.isObject()) {
|
||||
qWarning() << "fromJson quit failed";
|
||||
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
|
||||
|
||||
#endif // QUIT_H_
|
||||
|
@ -1,29 +1,50 @@
|
||||
#ifndef REGISTER_H_
|
||||
#define REGISTER_H_
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <QJsonObject>
|
||||
#include <QJsonDocument>
|
||||
#include <QDebug>
|
||||
|
||||
namespace Methods {
|
||||
struct Registe {
|
||||
std::string date;
|
||||
std::string id;
|
||||
std::string type{ "registe" };
|
||||
std::string hash;
|
||||
QString date;
|
||||
QString id;
|
||||
QString type{ "registe" };
|
||||
QString hash;
|
||||
bool state;
|
||||
};
|
||||
|
||||
using json = nlohmann::json;
|
||||
inline void to_json(json &j, const Registe ®iste)
|
||||
{
|
||||
j = json{ { "type", registe.type }, { "id", registe.id }, { "hash", registe.hash }, { "state", registe.state }, { "date", registe.date } };
|
||||
inline void toJson(QByteArray &array, const Registe ®iste) {
|
||||
QJsonObject obj = {
|
||||
{ "type", registe.type },
|
||||
{ "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)
|
||||
{
|
||||
j.at("id").get_to(registe.id);
|
||||
j.at("date").get_to(registe.date);
|
||||
j.at("hash").get_to(registe.hash);
|
||||
j.at("state").get_to(registe.state);
|
||||
|
||||
inline void fromJson(const QByteArray &array, Registe ®iste) {
|
||||
QJsonDocument doc = QJsonDocument::fromJson(array);
|
||||
if (!doc.isObject()) {
|
||||
qWarning() << "fromJson registe failed";
|
||||
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
|
||||
|
||||
#endif // REGISTER_H_
|
||||
|
@ -1,36 +1,76 @@
|
||||
#ifndef B0B88BD6_CF1E_4E87_926A_E6DBE6B9B19C
|
||||
#define B0B88BD6_CF1E_4E87_926A_E6DBE6B9B19C
|
||||
|
||||
#include <map>
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
#include <QList>
|
||||
#include <QMap>
|
||||
#include <QJsonObject>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonArray>
|
||||
#include <QDebug>
|
||||
|
||||
namespace Methods {
|
||||
struct Task {
|
||||
std::string id;
|
||||
std::string runId;
|
||||
std::string type{ "task" };
|
||||
std::string date;
|
||||
std::vector<std::string> arguments;
|
||||
std::multimap<std::string, std::string> environments;
|
||||
};
|
||||
|
||||
using json = nlohmann::json;
|
||||
inline void to_json(json &j, const Task &task)
|
||||
namespace Methods
|
||||
{
|
||||
j = json{ { "type", task.type }, { "id", task.id }, { "run_id", task.runId }, { "date", task.date }, { "arguments", task.arguments }, { "environments", task.environments } };
|
||||
}
|
||||
struct Task
|
||||
{
|
||||
QString id;
|
||||
QString runId;
|
||||
QString type{"task"};
|
||||
QString date;
|
||||
QList<QString> arguments;
|
||||
QMap<QString, QString> environments;
|
||||
};
|
||||
|
||||
inline void from_json(const json &j, Task &task)
|
||||
{
|
||||
j.at("id").get_to(task.id);
|
||||
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);
|
||||
}
|
||||
inline void toJson(QByteArray &array, const Task &task) {
|
||||
QJsonArray argArray;
|
||||
for (auto arg : task.arguments) {
|
||||
argArray.append(arg);
|
||||
}
|
||||
|
||||
} // namespace Methods
|
||||
QVariantMap envsMap;
|
||||
for (auto it = task.environments.constBegin(); it != task.environments.constEnd(); ++it) {
|
||||
envsMap.insert(it.key(), it.value());
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
#endif /* B0B88BD6_CF1E_4E87_926A_E6DBE6B9B19C */
|
||||
|
@ -67,27 +67,29 @@ struct ClientPrivate {
|
||||
|
||||
return true;
|
||||
}
|
||||
nlohmann::json get(const nlohmann::json& call)
|
||||
{
|
||||
|
||||
QByteArray get(const QByteArray &call) {
|
||||
send(call);
|
||||
|
||||
char buf[512];
|
||||
char buf[512];
|
||||
std::string result;
|
||||
int bytesRead;
|
||||
int bytesRead;
|
||||
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];
|
||||
}
|
||||
|
||||
if (buf[bytesRead - 1] == '\0') {
|
||||
break;
|
||||
}
|
||||
};
|
||||
return nlohmann::json::parse(result);
|
||||
}
|
||||
|
||||
QJsonDocument doc = QJsonDocument::fromRawData(result.data(), result.size());
|
||||
return doc.toJson();
|
||||
}
|
||||
|
||||
size_t send(const nlohmann::json& call)
|
||||
{
|
||||
std::string data = call.dump();
|
||||
size_t send(const QByteArray &call) {
|
||||
std::string data = call.data();
|
||||
data += '\0';
|
||||
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);
|
||||
}
|
||||
|
||||
nlohmann::json Client::get(const nlohmann::json& call)
|
||||
QByteArray Client::get(const QByteArray &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);
|
||||
}
|
||||
|
@ -3,23 +3,26 @@
|
||||
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <string>
|
||||
|
||||
namespace Socket {
|
||||
class ClientPrivate;
|
||||
class Client {
|
||||
std::unique_ptr<ClientPrivate> d_ptr;
|
||||
#include <QJsonDocument>
|
||||
|
||||
public:
|
||||
Client();
|
||||
~Client();
|
||||
bool connect(const std::string& host);
|
||||
nlohmann::json get(const nlohmann::json& call);
|
||||
size_t send(const nlohmann::json& call);
|
||||
void onReadyRead(std::function<void(const std::vector<char>&)> func);
|
||||
void waitForFinished();
|
||||
};
|
||||
} // namespace Socket
|
||||
namespace Socket
|
||||
{
|
||||
class ClientPrivate;
|
||||
class Client
|
||||
{
|
||||
std::unique_ptr<ClientPrivate> d_ptr;
|
||||
|
||||
public:
|
||||
Client();
|
||||
~Client();
|
||||
bool connect(const std::string &host);
|
||||
QByteArray get(const QByteArray &call);
|
||||
size_t send(const QByteArray &call);
|
||||
void onReadyRead(std::function<void(const std::vector<char> &)> func);
|
||||
void waitForFinished();
|
||||
};
|
||||
} // namespace Socket
|
||||
|
||||
#endif /* B1D5EB4F_7645_4BDA_87D6_6B80A4910014 */
|
||||
|
@ -13,46 +13,56 @@
|
||||
|
||||
#define JSON_USE_IMPLICIT_CONVERSIONS 0
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <optional>
|
||||
|
||||
#define tl std
|
||||
|
||||
namespace nlohmann {
|
||||
// namespace nlohmann {
|
||||
|
||||
template<class J, class T>
|
||||
inline void from_json(const J &j, tl::optional<T> &v)
|
||||
{
|
||||
if (j.is_null()) {
|
||||
v = tl::nullopt;
|
||||
} else {
|
||||
v = j.template get<T>();
|
||||
}
|
||||
}
|
||||
// template<class J, class T>
|
||||
// inline void from_json(const J &j, tl::optional<T> &v)
|
||||
// {
|
||||
// if (j.is_null()) {
|
||||
// v = tl::nullopt;
|
||||
// } else {
|
||||
// v = j.template get<T>();
|
||||
// }
|
||||
// }
|
||||
|
||||
template<class J, class T>
|
||||
inline void to_json(J &j, const tl::optional<T> &o)
|
||||
{
|
||||
if (o.has_value()) {
|
||||
j = o.value();
|
||||
}
|
||||
}
|
||||
// template<class J, class T>
|
||||
// inline void to_json(J &j, const tl::optional<T> &o)
|
||||
// {
|
||||
// if (o.has_value()) {
|
||||
// j = o.value();
|
||||
// }
|
||||
// }
|
||||
|
||||
} // namespace nlohmann
|
||||
// } // namespace nlohmann
|
||||
|
||||
namespace linglong {
|
||||
// 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;
|
||||
}
|
||||
// template<class T>
|
||||
// tl::optional<T> optional(const QJsonObject &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
|
||||
// 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_ */
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -20,25 +20,25 @@
|
||||
|
||||
#include <fstream>
|
||||
|
||||
namespace linglong {
|
||||
namespace util {
|
||||
namespace json {
|
||||
// namespace linglong {
|
||||
// namespace util {
|
||||
// namespace json {
|
||||
|
||||
inline nlohmann::json fromByteArray(const std::string &content)
|
||||
{
|
||||
return nlohmann::json::parse(content);
|
||||
}
|
||||
// inline nlohmann::json fromByteArray(const std::string &content)
|
||||
// {
|
||||
// return nlohmann::json::parse(content);
|
||||
// }
|
||||
|
||||
inline nlohmann::json fromFile(const std::string &filepath)
|
||||
{
|
||||
std::ifstream f(filepath);
|
||||
std::string str((std::istreambuf_iterator<char>(f)), std::istreambuf_iterator<char>());
|
||||
auto j = fromByteArray(str);
|
||||
return j;
|
||||
}
|
||||
// inline nlohmann::json fromFile(const std::string &filepath)
|
||||
// {
|
||||
// std::ifstream f(filepath);
|
||||
// std::string str((std::istreambuf_iterator<char>(f)), std::istreambuf_iterator<char>());
|
||||
// auto j = fromByteArray(str);
|
||||
// return j;
|
||||
// }
|
||||
|
||||
} // namespace json
|
||||
} // namespace util
|
||||
} // namespace linglong
|
||||
// } // namespace json
|
||||
// } // namespace util
|
||||
// } // namespace linglong
|
||||
|
||||
#endif /* LINGLONG_BOX_SRC_UTIL_UTIL_H_ */
|
||||
|
@ -2,8 +2,6 @@ set(CMAKE_INCLUDE_CURRENT_DIR ON)
|
||||
set(CMAKE_AUTOMOC ON)
|
||||
set(CMAKE_AUTORCC ON)
|
||||
|
||||
find_package(Qt5 REQUIRED COMPONENTS Core DBus Concurrent)
|
||||
|
||||
qt5_add_dbus_adaptor(ADAPTER_SOURCES
|
||||
../../DBus/org.desktopspec.ApplicationManager.xml
|
||||
impl/application_manager.h
|
||||
|
@ -140,15 +140,15 @@ Methods::Task ApplicationInstance::taskInfo() const
|
||||
Q_D(const ApplicationInstance);
|
||||
|
||||
Methods::Task task;
|
||||
task.id = d->m_id.toStdString();
|
||||
task.runId = d->application->id().toStdString();
|
||||
task.date = QString::number(startuptime()).toStdString();
|
||||
task.id = d->m_id;
|
||||
task.runId = d->application->id();
|
||||
task.date = QString::number(startuptime());
|
||||
|
||||
// TODO: debug to display environment
|
||||
task.environments.insert({ "DISPLAY", ":0" });
|
||||
task.environments.insert( "DISPLAY", ":0" );
|
||||
auto sysEnv = QProcessEnvironment::systemEnvironment();
|
||||
for (const auto& key : sysEnv.keys()) {
|
||||
task.environments.insert({ key.toStdString(), sysEnv.value(key).toStdString() });
|
||||
task.environments.insert( key, sysEnv.value(key) );
|
||||
}
|
||||
|
||||
return task;
|
||||
|
@ -5,7 +5,6 @@
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <mutex>
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <thread>
|
||||
|
||||
#include "../../modules/methods/basic.h"
|
||||
@ -18,19 +17,20 @@
|
||||
#include "application_instance.h"
|
||||
#include "applicationinstanceadaptor.h"
|
||||
|
||||
class ApplicationManagerPrivate : public QObject {
|
||||
class ApplicationManagerPrivate : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
ApplicationManager *q_ptr = nullptr;
|
||||
Q_DECLARE_PUBLIC(ApplicationManager);
|
||||
|
||||
QList<QSharedPointer<Application>> applications;
|
||||
Socket::Server server;
|
||||
QList<QSharedPointer<Application>> applications;
|
||||
Socket::Server server;
|
||||
std::multimap<std::string, QSharedPointer<ApplicationInstance>> tasks;
|
||||
|
||||
public:
|
||||
ApplicationManagerPrivate(ApplicationManager *parent) : QObject(parent), q_ptr(parent)
|
||||
{
|
||||
const QString socketPath{ QString("/run/user/%1/deepin-application-manager.socket").arg(getuid()) };
|
||||
const QString socketPath{QString("/run/user/%1/deepin-application-manager.socket").arg(getuid())};
|
||||
connect(&server, &Socket::Server::onReadyRead, this, &ApplicationManagerPrivate::recvClientData, Qt::QueuedConnection);
|
||||
server.listen(socketPath.toStdString());
|
||||
}
|
||||
@ -43,43 +43,47 @@ private:
|
||||
for (char c : data) {
|
||||
tmp += c;
|
||||
}
|
||||
using namespace nlohmann;
|
||||
if (json::parse(tmp).is_null()) {
|
||||
server.close(socket);
|
||||
return;
|
||||
}
|
||||
Methods::Basic basic = json::parse(tmp);
|
||||
|
||||
QByteArray jsonArray = data.data();
|
||||
Methods::Basic basic;
|
||||
Methods::fromJson(jsonArray, basic);
|
||||
QByteArray tmpArray;
|
||||
do {
|
||||
if (basic.type == "instance") {
|
||||
Methods::Instance instance = nlohmann::json::parse(tmp);
|
||||
auto find = tasks.find(instance.hash);
|
||||
if (find != tasks.end()) {
|
||||
nlohmann::json result = find->second->taskInfo();
|
||||
write(socket, result.dump());
|
||||
Methods::Instance instance;
|
||||
Methods::fromJson(jsonArray, instance);
|
||||
auto find = tasks.find(instance.hash.toStdString());
|
||||
if (find != tasks.end())
|
||||
{
|
||||
Methods::Task task = find->second->taskInfo();
|
||||
Methods::toJson(tmpArray, task);
|
||||
write(socket, tmpArray.toStdString());
|
||||
tasks.erase(find);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (basic.type == "quit") {
|
||||
Methods::Quit quit = json::parse(tmp);
|
||||
Methods::Quit quit;
|
||||
Methods::fromJson(jsonArray, quit);
|
||||
server.close(socket);
|
||||
std::cout << "client quit" << std::endl;
|
||||
break;
|
||||
}
|
||||
if (basic.type == "registe") {
|
||||
Methods::Registe registe = nlohmann::json::parse(tmp);
|
||||
Methods::Registe registe;
|
||||
Methods::fromJson(jsonArray, registe);
|
||||
Methods::Registe result;
|
||||
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) {
|
||||
result.state = true;
|
||||
result.hash = it->first;
|
||||
result.hash = QString::fromStdString(it->first);
|
||||
}
|
||||
write(socket, json(result).dump());
|
||||
std::cout << "registe a new client" << std::endl;
|
||||
Methods::toJson(tmpArray, result);
|
||||
write(socket, tmpArray.toStdString());
|
||||
break;
|
||||
}
|
||||
write(socket, json().dump());
|
||||
write(socket, jsonArray.toStdString());
|
||||
} while (false);
|
||||
}
|
||||
|
||||
@ -150,8 +154,8 @@ QDBusObjectPath ApplicationManager::Run(const QString &id)
|
||||
for (const QSharedPointer<Application> &app : d->applications) {
|
||||
if (app->id() == id) {
|
||||
// 创建任务所需的数据,并记录到任务队列,等待 loader 消耗
|
||||
QSharedPointer<ApplicationInstance> instance{ app->createInstance() };
|
||||
const std::string hash{ instance->hash().toStdString() };
|
||||
QSharedPointer<ApplicationInstance> instance{app->createInstance()};
|
||||
const std::string hash{instance->hash().toStdString()};
|
||||
connect(instance.get(), &ApplicationInstance::taskFinished, this, [=] {
|
||||
for (auto it = d->tasks.begin(); it != d->tasks.end(); ++it) {
|
||||
if (it->first == hash) {
|
||||
|
Loading…
Reference in New Issue
Block a user