1 /**
2 * Copyright (c) 2022 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 "endpoint.h"
17
18 #include "utils/json_parser.h"
19 #include "utils/logger.h"
20
21 #include <optional>
22
23 namespace panda::tooling::inspector {
HandleMessage(const std::string & message)24 void EndpointBase::HandleMessage(const std::string &message)
25 {
26 JsonObject request(message);
27 if (!request.IsValid()) {
28 LOG(INFO, DEBUGGER) << "Invalid request: " << message;
29 return;
30 }
31
32 LOG(DEBUG, DEBUGGER) << "Received " << message;
33
34 auto id = request.GetValue<JsonObject::NumT>("id");
35 auto method = request.GetValue<JsonObject::StringT>("method");
36 auto result = request.GetValue<JsonObject::JsonObjPointer>("result");
37
38 if (method != nullptr && result == nullptr) {
39 if (auto handler = methodHandlers_.find(*method); handler != methodHandlers_.end()) {
40 auto *params = request.GetValue<JsonObject::JsonObjPointer>("params");
41
42 JsonObject empty;
43
44 handler->second(id != nullptr ? std::make_optional(*id) : std::nullopt,
45 params != nullptr ? **params : empty);
46 } else {
47 LOG(WARNING, DEBUGGER) << "Unsupported method: " << message;
48 }
49 } else if (result != nullptr && method == nullptr) {
50 if (id == nullptr) {
51 LOG(INFO, DEBUGGER) << "Response object with no \"id\": " << message;
52 return;
53 }
54
55 if (auto handler = resultHandlers_.extract(*id)) {
56 handler.mapped()(**result);
57 }
58 } else {
59 LOG(INFO, DEBUGGER) << "Expected either 'method' or 'result' parameter";
60 }
61 }
62 } // namespace panda::tooling::inspector
63