• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef PANDA_TOOLING_INSPECTOR_TEST_CLIENT_H
17 #define PANDA_TOOLING_INSPECTOR_TEST_CLIENT_H
18 
19 #include "../endpoint.h"
20 #include "test_config.h"
21 #include "test_event_loop.h"
22 
23 #include "websocketpp/client.hpp"
24 
25 #include <functional>
26 #include <string_view>
27 #include <system_error>
28 #include <utility>
29 
30 namespace panda {
31 class JsonObject;
32 class JsonObjectBuilder;
33 }  // namespace panda
34 
35 namespace panda::tooling::inspector::test {
36 class TestLogger;
37 class TestServer;
38 
39 class Client : public Endpoint<websocketpp::client<TestConfig>>, public TestEventLoop {
40 public:
41     enum Error {
42         CONNECTION_ALREADY_PINNED = 1,
43         CONNECTION_FAILED,
44     };
45 
Client(std::string_view name,TestLogger & logger)46     Client(std::string_view name, TestLogger &logger) : TestEventLoop(name, logger) {}
47 
48     void Call(
49         const char *method, std::function<void(JsonObjectBuilder &)> &&params = [](auto & /* builder */) {},
50         std::function<void(const JsonObject &)> &&handler = [](auto & /* result */) {});
51 
Call(const char * method,std::function<void (const JsonObject &)> && handler)52     void Call(const char *method, std::function<void(const JsonObject & /* result */)> &&handler)
53     {
54         Call(
55             method, [](auto & /* builder */) {}, std::move(handler));
56     }
57 
58     std::error_code Close();
59 
60     void Connect(TestServer &server, std::function<void(std::error_code)> &&cb);
61 
62     template <typename MethodHandler>
OnCall(const char * method,MethodHandler && handler)63     void OnCall(const char *method, MethodHandler &&handler)
64     {
65         Endpoint::OnCall(method, std::bind(std::forward<MethodHandler>(handler), std::placeholders::_2));
66     }
67 
68 private:
69     unsigned id_ {0};
70 };
71 }  // namespace panda::tooling::inspector::test
72 
73 #endif  // PANDA_TOOLING_INSPECTOR_TEST_CLIENT_H
74