1 /**
2 * Copyright (c) 2024-2025 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 "connection/server_endpoint_base.h"
17
18 #include "utils/logger.h"
19
20 namespace ark::tooling::inspector {
21
22 /// Empty response, returned on success
23 class EmptyResponse final : public JsonSerializable {
24 public:
25 EmptyResponse() = default;
26
27 DEFAULT_COPY_SEMANTIC(EmptyResponse);
28 DEFAULT_MOVE_SEMANTIC(EmptyResponse);
29
30 ~EmptyResponse() override = default;
31
Serialize(JsonObjectBuilder & builder) const32 void Serialize([[maybe_unused]] JsonObjectBuilder &builder) const override {}
33 };
34
Call(const std::string & sessionId,const char * method,std::function<void (JsonObjectBuilder &)> && params)35 void ServerEndpointBase::Call(const std::string &sessionId, const char *method,
36 std::function<void(JsonObjectBuilder &)> &¶ms)
37 {
38 EndpointBase::Call(sessionId, std::nullopt, method, std::move(params));
39 }
40
OnCall(const char * method,Handler && handler)41 void ServerEndpointBase::OnCall(const char *method, Handler &&handler)
42 {
43 EndpointBase::OnCall(method, [this, handler = std::move(handler)](auto &sessionId, auto id, auto ¶ms) {
44 if (!id) {
45 LOG(INFO, DEBUGGER) << "Invalid request: request has no \"id\"";
46 return;
47 }
48
49 // Execute the handler's code
50 auto optResult = handler(sessionId, params);
51 if (optResult) {
52 if (*optResult) {
53 JsonSerializable &res = **optResult;
54 Reply(sessionId, *id, res);
55 } else {
56 Reply(sessionId, *id, EmptyResponse());
57 }
58 } else {
59 ReplyError(sessionId, *id, std::move(optResult.Error()));
60 }
61 });
62 }
63 } // namespace ark::tooling::inspector
64