1// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
2//
3// SPDX-License-Identifier: GPL-3.0-or-later
4
5#include "jsonrpccallproxy.h"
6#include <QDebug>
7
8JsonRpcCallProxy::JsonRpcCallProxy(QObject *parent)
9 : Route(parent)
10{
11 lspServOut << __FUNCTION__ << this;
12}
13
14JsonRpcCallProxy &JsonRpcCallProxy::ins()
15{
16 if (qApp->thread() != QThread::currentThread()) {
17 qCritical() << "can create from main thread";
18 qApp->quit();
19 }
20 static JsonRpcCallProxy proxy;
21 return proxy;
22}
23
24bool JsonRpcCallProxy::bindCreateProc(const std::string &language, const ProcCreatFunc &func)
25{
26 if (createFuncs.value(language.c_str()))
27 return false;
28 createFuncs.insert(language.c_str(), func);
29 return true;
30}
31
32QProcess *JsonRpcCallProxy::createLspServ(const newlsp::ProjectKey &key)
33{
34 QString langQStr(key.language.c_str());
35 QString workQStr(key.language.c_str());
36 auto createFunc = createFuncs.value(langQStr);
37 if (createFunc)
38 return createFunc(key);
39 return nullptr;
40}
41
42void JsonRpcCallProxy::methodFilter(int id, const QString &method, const QJsonObject &params)
43{
44 lspServOut << __FUNCTION__ << qApp->thread() << QThread::currentThread();
45 lspServOut << "method -->" << method.toStdString();
46 if (redMethods.keys().contains(method)) {
47 lspServOut << "Redirect filtering:" << method.toStdString();
48 QJsonObject result;
49 if (redMethods.value(method)(params, result)) {
50 QJsonObject protocol;
51 protocol[newlsp::K_JSON_RPC] = "2.0";
52 protocol[newlsp::K_ID] = id;
53 protocol[newlsp::K_RESULT] = result;
54 } else {
55 lspServErr << std::string("Redirect filtering: ") + "error called logic";
56 abort();
57 }
58 }
59 QProcess *proc = newlsp::Route::value(select);
60 std::string language = JsonRpcCallProxy::ins().select.language;
61 if (proc) {
62 QString writeData = newlsp::ServerApplication::toProtocolString(id, method, params);
63 lspServOut << "Origin" << JsonRpcCallProxy::ins().select.language
64 << "Server:"<< method.toStdString()
65 << "\nwriteData:" << writeData.toStdString();
66 proc->write(writeData.toUtf8());
67 proc->waitForBytesWritten();
68 return;
69 } else {
70 lspServErr << "can't found lsp" << language << "server to send" << method.toStdString();
71 }
72}
73
74void JsonRpcCallProxy::notificationFilter(const QString &method, const QJsonObject &params)
75{
76 lspServOut << __FUNCTION__ << qApp->thread() << QThread::currentThread();
77 lspServOut << "notification -->" << method.toStdString();
78 if (redNotifications.keys().contains(method)) {
79 lspServOut << "Redirect filtering:" << method.toStdString();
80 redNotifications.value(method)(params);
81 return;
82 }
83 QProcess *proc = newlsp::Route::value(select);
84 std::string language = JsonRpcCallProxy::ins().select.language;
85 if (proc) {
86 QString writeData = newlsp::ServerApplication::toProtocolString(method, params);
87 lspServOut << "Origin" << language
88 << "Server:" << method.toStdString()
89 << "\nwriteData:" << writeData.toStdString();
90 proc->write(writeData.toUtf8());
91 proc->waitForBytesWritten();
92 return;
93 } else {
94 lspServErr << "can't found lsp" << language << "server to send" << method.toStdString();
95 }
96}
97
98void JsonRpcCallProxy::bindFilter(const QString &methodName, const MethodFilterFunc &func)
99{
100 redMethods.insert(methodName, func);
101}
102
103void JsonRpcCallProxy::bindFilter(const QString &methodName, const NotificationFilterFunc &func)
104{
105 redNotifications.insert(methodName, func);
106}
107