• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "demo_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 
DemoSqlOperate(const uint8_t * data,size_t len,ResultCallBack retCallBack)32 bool DemoRpcServer::DemoSqlOperate(const uint8_t* data, size_t len, ResultCallBack retCallBack)
33 {
34     demoTs_->SetCancel(false);
35     std::string demoSql(reinterpret_cast<const char*>(data), len);
36     TS_LOGI("RPC DemoSqlOperate(%s, %zu)", demoSql.c_str(), len);
37 
38     int32_t ret = demoTs_->OperateDatabase(demoSql);
39     if (retCallBack) {
40         std::string response = "ok\r\n";
41         if (ret != 0) {
42             response = "dberror\r\n";
43         }
44         retCallBack(response, SEND_FINISH, 0);
45     }
46     return (ret == 0);
47 }
48 
DemoSqlQuery(const uint8_t * data,size_t len,ResultCallBack retCallBack)49 bool DemoRpcServer::DemoSqlQuery(const uint8_t* data, size_t len, ResultCallBack retCallBack)
50 {
51     demoTs_->SetCancel(false);
52     std::string demoSql(reinterpret_cast<const char*>(data), len);
53     TS_LOGI("RPC DemoSqlQuery %zu:%s", len, demoSql.c_str());
54 
55     int32_t ret = demoTs_->SearchDatabase(demoSql, retCallBack);
56     if (retCallBack && ret != 0) {
57         retCallBack("dberror\r\n", SEND_FINISH, 0);
58     }
59     demoTs_->SetCancel(false);
60     return (ret == 0);
61 }
62 
DemoCancelSqlQuery()63 void DemoRpcServer::DemoCancelSqlQuery()
64 {
65     demoTs_->SetCancel(true);
66 }
67 
DemoReset(const uint8_t * data,size_t len,ResultCallBack retCallBack)68 bool DemoRpcServer::DemoReset(const uint8_t* data, size_t len, ResultCallBack retCallBack)
69 {
70     UNUSED(data);
71     UNUSED(len);
72     TS_LOGI("RPC DemoReset trace_streamer");
73     demoTs_->WaitForParserEnd();
74     demoTs_ = std::make_unique<TraceStreamerSelector>();
75     if (retCallBack) {
76         retCallBack("ok\r\n", SEND_FINISH, 0);
77     }
78     return true;
79 }
80 
DemoWasmSqlQuery(const uint8_t * data,size_t len,uint8_t * out,int32_t outLen)81 int32_t DemoRpcServer::DemoWasmSqlQuery(const uint8_t* data, size_t len, uint8_t* out, int32_t outLen)
82 {
83     demoTs_->SetCancel(false);
84     std::string demoSql(reinterpret_cast<const char*>(data), len);
85     TS_LOGI("WASM RPC DemoSqlQuery outlen(%d) demoSql(%zu:%s)", outLen, len, demoSql.c_str());
86     int32_t ret = demoTs_->SearchDatabase(demoSql, out, outLen);
87     return ret;
88 }
89 
DemoWasmGetPluginNameWithCallback(const uint8_t * data,size_t len) const90 int32_t DemoRpcServer::DemoWasmGetPluginNameWithCallback(const uint8_t* data, size_t len) const
91 {
92     std::string pluginName(reinterpret_cast<const char*>(data), len);
93     TS_LOGI("WASM pluginName(%zu:%s)", len, pluginName.c_str());
94 
95     int32_t ret = demoTs_->sdkDataParser_->GetPluginName(pluginName);
96     return ret;
97 }
98 
DemoWasmSqlQueryWithCallback(const uint8_t * data,size_t len,ResultCallBack callback) const99 int32_t DemoRpcServer::DemoWasmSqlQueryWithCallback(const uint8_t* data, size_t len, ResultCallBack callback) const
100 {
101     demoTs_->SetCancel(false);
102     std::string demoSql(reinterpret_cast<const char*>(data), len);
103     TS_LOGI("WASM RPC DemoSqlQuery demoSql(%zu:%s)", len, demoSql.c_str());
104 
105     int32_t ret = demoTs_->SearchDatabase(demoSql, callback);
106     return ret;
107 }
108 
109 } // namespace TraceStreamer
110 } // namespace SysTuning
111