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
23using PtJson = panda::ecmascript::tooling::PtJson;
24using Result = panda::ecmascript::tooling::Result;
25namespace OHOS::ArkCompiler::Toolchain {
26DomainManager::DomainManager(uint32_t sessionId)
27    : sessionId_(sessionId), heapProfilerClient_(sessionId), profilerClient_(sessionId),
28      debuggerClient_(sessionId), runtimeClient_(sessionId), testClient_(sessionId)
29{
30}
31
32void 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        if (length == 0) {
61            return;
62        }
63        std::string::size_type indexPoint = 0;
64        indexPoint = wholeMethod.find_first_of('.', 0);
65        if (indexPoint == std::string::npos || indexPoint == 0 || indexPoint == length - 1) {
66            return;
67        }
68        domain = wholeMethod.substr(0, indexPoint);
69    }
70
71    if (domain == "HeapProfiler") {
72        heapProfilerClient_.RecvReply(std::move(json));
73    } else if (domain == "Profiler") {
74        profilerClient_.RecvProfilerResult(std::move(json));
75    } else if (domain == "Runtime") {
76        runtimeClient_.RecvReply(std::move(json));
77    } else if (domain == "Debugger") {
78        debuggerClient_.RecvReply(std::move(json));
79    }
80}
81}