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 <cstring>
19 #include <functional>
20 #include <cstdint>
21
22 #include "log.h"
23
24 namespace SysTuning {
25 namespace TraceStreamer {
ParseData(const uint8_t * data,size_t len,ResultCallBack resultCallBack)26 bool RpcServer::ParseData(const uint8_t* data, size_t len, ResultCallBack resultCallBack)
27 {
28 TS_LOGI("RPC ParseData, has parsed len %zu + %zu", lenParseData_, len);
29 do {
30 constexpr size_t blockSize = 1024 * 1024;
31 size_t parseSize = std::min(len, blockSize);
32 std::unique_ptr<uint8_t[]> buf = std::make_unique<uint8_t[]>(parseSize);
33 std::copy(data, data + parseSize, buf.get());
34
35 if (!ts_->ParseTraceDataSegment(std::move(buf), parseSize)) {
36 if (resultCallBack) {
37 resultCallBack("formaterror\r\n");
38 }
39 return false;
40 }
41 data += parseSize;
42 len -= parseSize;
43 lenParseData_ += parseSize;
44 } while (len > 0);
45 if (resultCallBack) {
46 resultCallBack("ok\r\n");
47 }
48 return true;
49 }
50
ParseDataOver(const uint8_t * data,size_t len,ResultCallBack resultCallBack)51 bool RpcServer::ParseDataOver(const uint8_t* data, size_t len, ResultCallBack resultCallBack)
52 {
53 TS_LOGI("RPC ParseDataOver, has parsed len %zu", lenParseData_);
54
55 ts_->WaitForParserEnd();
56 ts_->Clear();
57 if (resultCallBack) {
58 resultCallBack("ok\r\n");
59 }
60 lenParseData_ = 0;
61 return true;
62 }
63
SqlOperate(const uint8_t * data,size_t len,ResultCallBack resultCallBack)64 bool RpcServer::SqlOperate(const uint8_t* data, size_t len, ResultCallBack resultCallBack)
65 {
66 std::string sql(reinterpret_cast<const char*>(data), len);
67 TS_LOGI("RPC SqlOperate(%s, %zu)", sql.c_str(), len);
68
69 int ret = ts_->OperateDatabase(sql);
70 if (resultCallBack) {
71 std::string response = "ok\r\n";
72 if (ret != 0) {
73 response = "dberror\r\n";
74 }
75 resultCallBack(response);
76 }
77 return (ret == 0);
78 }
79
SqlQuery(const uint8_t * data,size_t len,ResultCallBack resultCallBack)80 bool RpcServer::SqlQuery(const uint8_t* data, size_t len, ResultCallBack resultCallBack)
81 {
82 std::string sql(reinterpret_cast<const char*>(data), len);
83 TS_LOGI("RPC SqlQuery %zu:%s", len, sql.c_str());
84
85 int ret = ts_->SearchDatabase(sql, resultCallBack);
86 if (resultCallBack && ret != 0) {
87 resultCallBack("dberror\r\n");
88 }
89 return (ret == 0);
90 }
91
Reset(const uint8_t * data,size_t len,ResultCallBack resultCallBack)92 bool RpcServer::Reset(const uint8_t* data, size_t len, ResultCallBack resultCallBack)
93 {
94 TS_LOGI("RPC reset trace_streamer");
95
96 ts_->WaitForParserEnd();
97 ts_ = std::make_unique<TraceStreamerSelector>();
98 if (resultCallBack) {
99 resultCallBack("ok\r\n");
100 }
101 return true;
102 }
103
WasmSqlQuery(const uint8_t * data,size_t len,uint8_t * out,int outLen)104 int RpcServer::WasmSqlQuery(const uint8_t* data, size_t len, uint8_t* out, int outLen)
105 {
106 std::string sql(reinterpret_cast<const char*>(data), len);
107 TS_LOGI("WASM RPC SqlQuery out(%p:%d) sql(%zu:%s)", reinterpret_cast<void*>(out), outLen,
108 len, sql.c_str());
109
110 int ret = ts_->SearchDatabase(sql, out, outLen);
111 return ret;
112 }
113 } // namespace TraceStreamer
114 } // namespace SysTuning
115