1e509ee18Sopenharmony_ci/*
2e509ee18Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
3e509ee18Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4e509ee18Sopenharmony_ci * you may not use this file except in compliance with the License.
5e509ee18Sopenharmony_ci * You may obtain a copy of the License at
6e509ee18Sopenharmony_ci *
7e509ee18Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8e509ee18Sopenharmony_ci *
9e509ee18Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10e509ee18Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11e509ee18Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12e509ee18Sopenharmony_ci * See the License for the specific language governing permissions and
13e509ee18Sopenharmony_ci * limitations under the License.
14e509ee18Sopenharmony_ci */
15e509ee18Sopenharmony_ci
16e509ee18Sopenharmony_ci#include "domain_manager.h"
17e509ee18Sopenharmony_ci
18e509ee18Sopenharmony_ci#include "common/log_wrapper.h"
19e509ee18Sopenharmony_ci#include "tooling/client/manager/breakpoint_manager.h"
20e509ee18Sopenharmony_ci#include "tooling/base/pt_json.h"
21e509ee18Sopenharmony_ci#include "tooling/client/session/session.h"
22e509ee18Sopenharmony_ci
23e509ee18Sopenharmony_ciusing PtJson = panda::ecmascript::tooling::PtJson;
24e509ee18Sopenharmony_ciusing Result = panda::ecmascript::tooling::Result;
25e509ee18Sopenharmony_cinamespace OHOS::ArkCompiler::Toolchain {
26e509ee18Sopenharmony_ciDomainManager::DomainManager(uint32_t sessionId)
27e509ee18Sopenharmony_ci    : sessionId_(sessionId), heapProfilerClient_(sessionId), profilerClient_(sessionId),
28e509ee18Sopenharmony_ci      debuggerClient_(sessionId), runtimeClient_(sessionId), testClient_(sessionId)
29e509ee18Sopenharmony_ci{
30e509ee18Sopenharmony_ci}
31e509ee18Sopenharmony_ci
32e509ee18Sopenharmony_civoid DomainManager::DispatcherReply(char* msg)
33e509ee18Sopenharmony_ci{
34e509ee18Sopenharmony_ci    std::string decMessage = std::string(msg);
35e509ee18Sopenharmony_ci    std::unique_ptr<PtJson> json = PtJson::Parse(decMessage);
36e509ee18Sopenharmony_ci    if (json == nullptr) {
37e509ee18Sopenharmony_ci        LOGE("json parse error");
38e509ee18Sopenharmony_ci        return;
39e509ee18Sopenharmony_ci    }
40e509ee18Sopenharmony_ci
41e509ee18Sopenharmony_ci    if (!json->IsObject()) {
42e509ee18Sopenharmony_ci        LOGE("json parse format error");
43e509ee18Sopenharmony_ci        json->ReleaseRoot();
44e509ee18Sopenharmony_ci        return;
45e509ee18Sopenharmony_ci    }
46e509ee18Sopenharmony_ci
47e509ee18Sopenharmony_ci    std::string domain;
48e509ee18Sopenharmony_ci    Result ret;
49e509ee18Sopenharmony_ci    int32_t id;
50e509ee18Sopenharmony_ci    ret = json->GetInt("id", &id);
51e509ee18Sopenharmony_ci    if (ret == Result::SUCCESS) {
52e509ee18Sopenharmony_ci        domain = GetDomainById(id);
53e509ee18Sopenharmony_ci        RemoveDomainById(id);
54e509ee18Sopenharmony_ci    }
55e509ee18Sopenharmony_ci
56e509ee18Sopenharmony_ci    std::string wholeMethod;
57e509ee18Sopenharmony_ci    ret = json->GetString("method", &wholeMethod);
58e509ee18Sopenharmony_ci    if (ret == Result::SUCCESS) {
59e509ee18Sopenharmony_ci        std::string::size_type length = wholeMethod.length();
60e509ee18Sopenharmony_ci        if (length == 0) {
61e509ee18Sopenharmony_ci            return;
62e509ee18Sopenharmony_ci        }
63e509ee18Sopenharmony_ci        std::string::size_type indexPoint = 0;
64e509ee18Sopenharmony_ci        indexPoint = wholeMethod.find_first_of('.', 0);
65e509ee18Sopenharmony_ci        if (indexPoint == std::string::npos || indexPoint == 0 || indexPoint == length - 1) {
66e509ee18Sopenharmony_ci            return;
67e509ee18Sopenharmony_ci        }
68e509ee18Sopenharmony_ci        domain = wholeMethod.substr(0, indexPoint);
69e509ee18Sopenharmony_ci    }
70e509ee18Sopenharmony_ci
71e509ee18Sopenharmony_ci    if (domain == "HeapProfiler") {
72e509ee18Sopenharmony_ci        heapProfilerClient_.RecvReply(std::move(json));
73e509ee18Sopenharmony_ci    } else if (domain == "Profiler") {
74e509ee18Sopenharmony_ci        profilerClient_.RecvProfilerResult(std::move(json));
75e509ee18Sopenharmony_ci    } else if (domain == "Runtime") {
76e509ee18Sopenharmony_ci        runtimeClient_.RecvReply(std::move(json));
77e509ee18Sopenharmony_ci    } else if (domain == "Debugger") {
78e509ee18Sopenharmony_ci        debuggerClient_.RecvReply(std::move(json));
79e509ee18Sopenharmony_ci    }
80e509ee18Sopenharmony_ci}
81e509ee18Sopenharmony_ci}