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