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 "tooling/client/domain/test_client.h"
17
18#include "common/log_wrapper.h"
19#include "tooling/client/manager/variable_manager.h"
20#include "tooling/base/pt_json.h"
21#include "tooling/client/session/session.h"
22
23using PtJson = panda::ecmascript::tooling::PtJson;
24namespace OHOS::ArkCompiler::Toolchain {
25bool TestClient::DispatcherCmd(const std::string &cmd)
26{
27    std::map<std::string, std::function<int()>> dispatcherTable {
28        { "success", std::bind(&TestClient::SuccessCommand, this)},
29        { "fail", std::bind(&TestClient::FailCommand, this)},
30    };
31
32    auto entry = dispatcherTable.find(cmd);
33    if (entry != dispatcherTable.end()) {
34        entry->second();
35        LOGI("TestClient DispatcherCmd cmd: %{public}s", cmd.c_str());
36        return true;
37    } else {
38        LOGI("Unknown commond: %{public}s", cmd.c_str());
39        return false;
40    }
41}
42
43int TestClient::SuccessCommand()
44{
45    Session *session = SessionManager::getInstance().GetSessionById(sessionId_);
46    uint32_t id = session->GetMessageId();
47
48    std::unique_ptr<PtJson> request = PtJson::CreateObject();
49    request->Add("id", id);
50    request->Add("method", "Test.success");
51
52    std::unique_ptr<PtJson> params = PtJson::CreateObject();
53    request->Add("params", params);
54
55    std::string message = request->Stringify();
56    if (session->ClientSendReq(message)) {
57        session->GetDomainManager().SetDomainById(id, "Test");
58    }
59    return 0;
60}
61
62int TestClient::FailCommand()
63{
64    Session *session = SessionManager::getInstance().GetSessionById(sessionId_);
65    uint32_t id = session->GetMessageId();
66
67    std::unique_ptr<PtJson> request = PtJson::CreateObject();
68    request->Add("id", id);
69    request->Add("method", "Test.fail");
70
71    std::unique_ptr<PtJson> params = PtJson::CreateObject();
72    request->Add("params", params);
73
74    std::string message = request->Stringify();
75    if (session->ClientSendReq(message)) {
76        session->GetDomainManager().SetDomainById(id, "Test");
77    }
78    return 0;
79}
80} // OHOS::ArkCompiler::Toolchain