| 1 | // SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd. |
| 2 | // |
| 3 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 4 | |
| 5 | #include "serverhandler.h" |
| 6 | |
| 7 | #include <QMetaObject> |
| 8 | #include <QMutex> |
| 9 | #include <QReadLocker> |
| 10 | #include <QWriteLocker> |
| 11 | |
| 12 | namespace { |
| 13 | static QReadWriteLock rwLock; |
| 14 | } |
| 15 | |
| 16 | ServerHandler::ServerHandler(TcpSocketServer &server, Tools *tools) |
| 17 | : AbstractServer<ServerHandler>(server) |
| 18 | , tools(tools) |
| 19 | { |
| 20 | qRegisterMetaType<Json::Value>("Json::Value" ); |
| 21 | qRegisterMetaType<Json::Value>("Json::Value&" ); |
| 22 | this->bindAndAddMethod(Procedure("initialization" , PARAMS_BY_NAME, JSON_STRING, "params" , JSON_OBJECT, NULL), |
| 23 | &ServerHandler::initialization); |
| 24 | this->bindAndAddMethod(Procedure("pullData" , PARAMS_BY_NAME, JSON_STRING, "params" , JSON_OBJECT, NULL), |
| 25 | &ServerHandler::pullData); |
| 26 | this->bindAndAddMethod(Procedure("shutdown" , PARAMS_BY_NAME, JSON_STRING, "params" , JSON_OBJECT, NULL), |
| 27 | &ServerHandler::shutdown); |
| 28 | this->bindAndAddNotification(Procedure("exit" , PARAMS_BY_NAME, NULL), &ServerHandler::exit); |
| 29 | |
| 30 | QObject::connect(tools, &Tools::attachData, this, [=](const Json::Value &value){ |
| 31 | std::cout << "update counts from attach process data" << std::endl; |
| 32 | QWriteLocker lock(&rwLock); |
| 33 | toolsData = value; |
| 34 | }); |
| 35 | } |
| 36 | |
| 37 | void ServerHandler::initialization(const Json::Value &request, Json::Value &response) |
| 38 | { |
| 39 | std::cout << __FUNCTION__ << std::endl; |
| 40 | std::cout << request.toStyledString() << std::endl; |
| 41 | Json::Value params = request["params" ]; |
| 42 | int pid = params["processId" ].asInt(); |
| 43 | |
| 44 | std::cout << "attach process id:" << pid << std::endl; |
| 45 | if (!tools) { |
| 46 | // tools->setAttachPID(pid); |
| 47 | QObject::metaObject()->invokeMethod(tools, "setAttachPID" , Qt::QueuedConnection, Q_ARG(int, pid)); |
| 48 | QObject::metaObject()->invokeMethod(tools, "startAll" , Qt::QueuedConnection); |
| 49 | } else { |
| 50 | QObject::metaObject()->invokeMethod(tools, "stopAll" , Qt::QueuedConnection); |
| 51 | // tools->setAttachPID(pid); |
| 52 | QObject::metaObject()->invokeMethod(tools, "setAttachPID" , Qt::QueuedConnection, Q_ARG(int, pid)); |
| 53 | QObject::metaObject()->invokeMethod(tools, "startAll" , Qt::QueuedConnection); |
| 54 | } |
| 55 | response["result" ] = {}; |
| 56 | } |
| 57 | |
| 58 | void ServerHandler::pullData(const Json::Value &request, Json::Value &response) |
| 59 | { |
| 60 | std::cout << __FUNCTION__ << std::endl; |
| 61 | Q_UNUSED(request) |
| 62 | QReadLocker lock(&rwLock); |
| 63 | response["result" ] = toolsData; |
| 64 | } |
| 65 | |
| 66 | void ServerHandler::shutdown(const Json::Value &request, Json::Value &response) |
| 67 | { |
| 68 | std::cout << __FUNCTION__ << std::endl; |
| 69 | Q_UNUSED(request); |
| 70 | int pid = tools->attachPID(); |
| 71 | QObject::metaObject()->invokeMethod(tools, "stopAll" , Qt::QueuedConnection); |
| 72 | Json::Value result; |
| 73 | toolsData.clear(); |
| 74 | result["processId" ] = pid; |
| 75 | response["result" ] = result; |
| 76 | } |
| 77 | |
| 78 | void ServerHandler::exit(const Json::Value &request) |
| 79 | { |
| 80 | std::cout << __FUNCTION__ << std::endl; |
| 81 | Q_UNUSED(request); |
| 82 | QObject::metaObject()->invokeMethod(tools, "stopAll" , Qt::QueuedConnection); |
| 83 | StopListening(); |
| 84 | toolsData.clear(); |
| 85 | std::cout << "server exit..." << std::endl; |
| 86 | qApp->exit(0); |
| 87 | } |
| 88 | |