1 /*
2 * Copyright (c) Huawei Technologies Co., Ltd. 2023. All rights reserved.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include "demo_rpc_server.h"
16
17 #include <cstdint>
18 #include <cstring>
19 #include <functional>
20
21 #include "log.h"
22 #include "version.h"
23
24 #define UNUSED(expr) \
25 do { \
26 static_cast<void>(expr); \
27 } while (0)
28 namespace SysTuning {
29 namespace TraceStreamer {
30
DemoSqlOperate(const uint8_t * data,size_t len,ResultCallBack resultCallBack)31 bool DemoRpcServer::DemoSqlOperate(const uint8_t *data, size_t len, ResultCallBack resultCallBack)
32 {
33 demoTs_->SetCancel(false);
34 std::string demoSql(reinterpret_cast<const char *>(data), len);
35 TS_LOGI("RPC DemoSqlOperate(%s, %zu)", demoSql.c_str(), len);
36
37 int32_t ret = demoTs_->OperateDatabase(demoSql);
38 if (resultCallBack) {
39 std::string response = "ok\r\n";
40 if (ret != 0) {
41 response = "dberror\r\n";
42 }
43 resultCallBack(response, SEND_FINISH, 0);
44 }
45 return (ret == 0);
46 }
47
DemoSqlQuery(const uint8_t * data,size_t len,ResultCallBack resultCallBack)48 bool DemoRpcServer::DemoSqlQuery(const uint8_t *data, size_t len, ResultCallBack resultCallBack)
49 {
50 demoTs_->SetCancel(false);
51 std::string demoSql(reinterpret_cast<const char *>(data), len);
52 TS_LOGI("RPC DemoSqlQuery %zu:%s", len, demoSql.c_str());
53
54 int32_t ret = demoTs_->SearchDatabase(demoSql, resultCallBack);
55 if (resultCallBack && ret != 0) {
56 resultCallBack("dberror\r\n", SEND_FINISH, 0);
57 }
58 demoTs_->SetCancel(false);
59 return (ret == 0);
60 }
61
DemoCancelSqlQuery()62 void DemoRpcServer::DemoCancelSqlQuery()
63 {
64 demoTs_->SetCancel(true);
65 }
66
DemoReset(const uint8_t * data,size_t len,ResultCallBack resultCallBack)67 bool DemoRpcServer::DemoReset(const uint8_t *data, size_t len, ResultCallBack resultCallBack)
68 {
69 UNUSED(data);
70 UNUSED(len);
71 TS_LOGI("RPC DemoReset trace_streamer");
72 demoTs_->WaitForParserEnd();
73 demoTs_ = std::make_unique<TraceStreamerSelector>();
74 if (resultCallBack) {
75 resultCallBack("ok\r\n", SEND_FINISH, 0);
76 }
77 return true;
78 }
79
DemoWasmSqlQuery(const uint8_t * data,size_t len,uint8_t * out,int32_t outLen)80 int32_t DemoRpcServer::DemoWasmSqlQuery(const uint8_t *data, size_t len, uint8_t *out, int32_t outLen)
81 {
82 demoTs_->SetCancel(false);
83 std::string demoSql(reinterpret_cast<const char *>(data), len);
84 TS_LOGI("WASM RPC DemoSqlQuery outlen(%d) demoSql(%zu:%s)", outLen, len, demoSql.c_str());
85 int32_t ret = demoTs_->SearchDatabase(demoSql, out, outLen);
86 return ret;
87 }
88
DemoWasmGetPluginNameWithCallback(const uint8_t * data,size_t len) const89 int32_t DemoRpcServer::DemoWasmGetPluginNameWithCallback(const uint8_t *data, size_t len) const
90 {
91 std::string pluginName(reinterpret_cast<const char *>(data), len);
92 TS_LOGI("WASM pluginName(%zu:%s)", len, pluginName.c_str());
93
94 int32_t ret = demoTs_->sdkDataParser_->GetPluginName(pluginName);
95 return ret;
96 }
97
DemoWasmSqlQueryWithCallback(const uint8_t * data,size_t len,ResultCallBack callback) const98 int32_t DemoRpcServer::DemoWasmSqlQueryWithCallback(const uint8_t *data, size_t len, ResultCallBack callback) const
99 {
100 demoTs_->SetCancel(false);
101 std::string demoSql(reinterpret_cast<const char *>(data), len);
102 TS_LOGI("WASM RPC DemoSqlQuery demoSql(%zu:%s)", len, demoSql.c_str());
103
104 int32_t ret = demoTs_->SearchDatabase(demoSql, callback);
105 return ret;
106 }
107
108 } // namespace TraceStreamer
109 } // namespace SysTuning
110