1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
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
16 #include "rpc_server.h"
17
18 #include <cstdint>
19 #include <cstring>
20 #include <functional>
21
22 #include "log.h"
23 #include "version.h"
24
25 #define UNUSED(expr) \
26 do { \
27 static_cast<void>(expr); \
28 } while (0)
29 namespace SysTuning {
30 namespace TraceStreamer {
31
SqlOperate(const uint8_t * data,size_t len,ResultCallBack resultCallBack)32 bool RpcServer::SqlOperate(const uint8_t* data, size_t len, ResultCallBack resultCallBack)
33 {
34 ts_->SetCancel(false);
35 std::string sql(reinterpret_cast<const char*>(data), len);
36 TS_LOGI("RPC SqlOperate(%s, %zu)", sql.c_str(), len);
37
38 int32_t ret = ts_->OperateDatabase(sql);
39 if (resultCallBack) {
40 std::string response = "ok\r\n";
41 if (ret != 0) {
42 response = "dberror\r\n";
43 }
44 resultCallBack(response, SEND_FINISH, 0);
45 }
46 return (ret == 0);
47 }
48
SqlQuery(const uint8_t * data,size_t len,ResultCallBack resultCallBack)49 bool RpcServer::SqlQuery(const uint8_t* data, size_t len, ResultCallBack resultCallBack)
50 {
51 ts_->SetCancel(false);
52 std::string sql(reinterpret_cast<const char*>(data), len);
53 TS_LOGI("RPC SqlQuery %zu:%s", len, sql.c_str());
54
55 int32_t ret = ts_->SearchDatabase(sql, resultCallBack);
56 if (resultCallBack && ret != 0) {
57 resultCallBack("dberror\r\n", SEND_FINISH, 0);
58 }
59 ts_->SetCancel(false);
60 return (ret == 0);
61 }
62
CancelSqlQuery()63 void RpcServer::CancelSqlQuery()
64 {
65 ts_->SetCancel(true);
66 }
67
Reset(const uint8_t * data,size_t len,ResultCallBack resultCallBack)68 bool RpcServer::Reset(const uint8_t* data, size_t len, ResultCallBack resultCallBack)
69 {
70 UNUSED(data);
71 UNUSED(len);
72 TS_LOGI("RPC reset trace_streamer");
73
74 ts_->WaitForParserEnd();
75 ts_ = std::make_unique<TraceStreamerSelector>();
76 if (resultCallBack) {
77 resultCallBack("ok\r\n", SEND_FINISH, 0);
78 }
79 return true;
80 }
81
WasmSqlQuery(const uint8_t * data,size_t len,uint8_t * out,int32_t outLen)82 int32_t RpcServer::WasmSqlQuery(const uint8_t* data, size_t len, uint8_t* out, int32_t outLen)
83 {
84 ts_->SetCancel(false);
85 std::string sql(reinterpret_cast<const char*>(data), len);
86 TS_LOGI("WASM RPC SqlQuery outlen(%d) sql(%zu:%s)", outLen, len, sql.c_str());
87 int32_t ret = ts_->SearchDatabase(sql, out, outLen);
88 return ret;
89 }
90
WasmGetPluginNameWithCallback(const uint8_t * data,size_t len) const91 int32_t RpcServer::WasmGetPluginNameWithCallback(const uint8_t* data, size_t len) const
92 {
93 std::string pluginName(reinterpret_cast<const char*>(data), len);
94 TS_LOGI("WASM pluginName(%zu:%s)", len, pluginName.c_str());
95
96 int32_t ret = ts_->sdkDataParser_->GetPluginName(pluginName);
97 return ret;
98 }
99
WasmSqlQueryWithCallback(const uint8_t * data,size_t len,ResultCallBack callback) const100 int32_t RpcServer::WasmSqlQueryWithCallback(const uint8_t* data, size_t len, ResultCallBack callback) const
101 {
102 ts_->SetCancel(false);
103 std::string sql(reinterpret_cast<const char*>(data), len);
104 TS_LOGI("WASM RPC SqlQuery sql(%zu:%s)", len, sql.c_str());
105
106 int32_t ret = ts_->SearchDatabase(sql, callback);
107 return ret;
108 }
109
110 } // namespace TraceStreamer
111 } // namespace SysTuning
112