1 /*
2 * Copyright (c) 2023 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 "domain_manager.h"
17
18 #include "common/log_wrapper.h"
19 #include "tooling/client/manager/breakpoint_manager.h"
20 #include "tooling/base/pt_json.h"
21 #include "tooling/client/session/session.h"
22
23 using PtJson = panda::ecmascript::tooling::PtJson;
24 using Result = panda::ecmascript::tooling::Result;
25 namespace OHOS::ArkCompiler::Toolchain {
DomainManager(uint32_t sessionId)26 DomainManager::DomainManager(uint32_t sessionId)
27 : sessionId_(sessionId), heapProfilerClient_(sessionId), profilerClient_(sessionId),
28 debuggerClient_(sessionId), runtimeClient_(sessionId), testClient_(sessionId)
29 {
30 }
31
DispatcherReply(char * msg)32 void DomainManager::DispatcherReply(char* msg)
33 {
34 std::string decMessage = std::string(msg);
35 std::unique_ptr<PtJson> json = PtJson::Parse(decMessage);
36 if (json == nullptr) {
37 LOGE("json parse error");
38 return;
39 }
40
41 if (!json->IsObject()) {
42 LOGE("json parse format error");
43 json->ReleaseRoot();
44 return;
45 }
46
47 std::string domain;
48 Result ret;
49 int32_t id;
50 ret = json->GetInt("id", &id);
51 if (ret == Result::SUCCESS) {
52 domain = GetDomainById(id);
53 RemoveDomainById(id);
54 }
55
56 std::string wholeMethod;
57 ret = json->GetString("method", &wholeMethod);
58 if (ret == Result::SUCCESS) {
59 std::string::size_type length = wholeMethod.length();
60 std::string::size_type indexPoint = 0;
61 indexPoint = wholeMethod.find_first_of('.', 0);
62 if (indexPoint == std::string::npos || indexPoint == 0 || indexPoint == length - 1) {
63 return;
64 }
65 domain = wholeMethod.substr(0, indexPoint);
66 }
67
68 if (domain == "HeapProfiler") {
69 heapProfilerClient_.RecvReply(std::move(json));
70 } else if (domain == "Profiler") {
71 profilerClient_.RecvProfilerResult(std::move(json));
72 } else if (domain == "Runtime") {
73 runtimeClient_.RecvReply(std::move(json));
74 } else if (domain == "Debugger") {
75 debuggerClient_.RecvReply(std::move(json));
76 }
77 }
78 }