Branch data Line data Source code
1 : : /*
2 : : * Copyright 2025 Jinwoo Sung
3 : : *
4 : : * Licensed under the Apache License, Version 2.0 (the "License");
5 : : * you may not use this file except in compliance with the License.
6 : : * You may obtain a copy of the License at
7 : : *
8 : : * http://www.apache.org/licenses/LICENSE-2.0
9 : : *
10 : : * Unless required by applicable law or agreed to in writing, software
11 : : * distributed under the License is distributed on an "AS IS" BASIS,
12 : : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 : : * See the License for the specific language governing permissions and
14 : : * limitations under the License.
15 : : */
16 : :
17 : : #include "wirestead/factory/channel_factory.hpp"
18 : :
19 : : #include "wirestead/transport/serial/boost_serial_port.hpp"
20 : : #include "wirestead/transport/serial/serial.hpp"
21 : : #include "wirestead/transport/tcp_client/tcp_client.hpp"
22 : : #include "wirestead/transport/tcp_server/boost_tcp_acceptor.hpp"
23 : : #include "wirestead/transport/tcp_server/tcp_server.hpp"
24 : : #include "wirestead/transport/udp/udp.hpp"
25 : : #include "wirestead/transport/uds/boost_uds_acceptor.hpp"
26 : : #include "wirestead/transport/uds/uds_client.hpp"
27 : : #include "wirestead/transport/uds/uds_server.hpp"
28 : :
29 : : namespace wirestead {
30 : : namespace factory {
31 : :
32 : 293 : std::shared_ptr<interface::Channel> ChannelFactory::create(const ChannelOptions& options,
33 : : std::shared_ptr<boost::asio::io_context> external_ioc) {
34 : : return std::visit(
35 : 586 : [&external_ioc](const auto& config) -> std::shared_ptr<interface::Channel> {
36 : : using T = std::decay_t<decltype(config)>;
37 : :
38 : : if constexpr (std::is_same_v<T, config::TcpClientConfig>) {
39 : 92 : return create_tcp_client(config, external_ioc);
40 : : } else if constexpr (std::is_same_v<T, config::TcpServerConfig>) {
41 : 120 : return create_tcp_server(config, external_ioc);
42 : : } else if constexpr (std::is_same_v<T, config::SerialConfig>) {
43 : 13 : return create_serial(config, external_ioc);
44 : : } else if constexpr (std::is_same_v<T, config::UdpConfig>) {
45 : 40 : return create_udp(config, external_ioc);
46 : : } else if constexpr (std::is_same_v<T, config::UdsClientConfig>) {
47 : 14 : return create_uds_client(config, external_ioc);
48 : : } else if constexpr (std::is_same_v<T, config::UdsServerConfig>) {
49 : 14 : return create_uds_server(config, external_ioc);
50 : : } else {
51 : : static_assert(std::is_same_v<T, void>, "Unsupported config type");
52 : : return nullptr;
53 : : }
54 : : },
55 : 586 : options);
56 : : }
57 : :
58 : 120 : std::shared_ptr<interface::Channel> ChannelFactory::create_tcp_server(
59 : : const config::TcpServerConfig& cfg, std::shared_ptr<boost::asio::io_context> external_ioc) {
60 [ + + ]: 120 : if (external_ioc) {
61 : 4 : auto acceptor = std::make_unique<transport::BoostTcpAcceptor>(*external_ioc);
62 : 4 : return transport::TcpServer::create(cfg, std::move(acceptor), *external_ioc);
63 : 4 : }
64 : 116 : return transport::TcpServer::create(cfg, cfg.use_shared_context);
65 : : }
66 : :
67 : 92 : std::shared_ptr<interface::Channel> ChannelFactory::create_tcp_client(
68 : : const config::TcpClientConfig& cfg, std::shared_ptr<boost::asio::io_context> external_ioc) {
69 [ + + ]: 92 : if (external_ioc) {
70 : 5 : return transport::TcpClient::create(cfg, *external_ioc);
71 : : }
72 : 87 : return transport::TcpClient::create(cfg);
73 : : }
74 : :
75 : 13 : std::shared_ptr<interface::Channel> ChannelFactory::create_serial(
76 : : const config::SerialConfig& cfg, std::shared_ptr<boost::asio::io_context> external_ioc) {
77 [ + + ]: 13 : if (external_ioc) {
78 : 1 : return transport::Serial::create(cfg, std::make_unique<transport::BoostSerialPort>(*external_ioc), *external_ioc);
79 : : }
80 : 12 : return transport::Serial::create(cfg, cfg.use_shared_context);
81 : : }
82 : :
83 : 40 : std::shared_ptr<interface::Channel> ChannelFactory::create_udp(const config::UdpConfig& cfg,
84 : : std::shared_ptr<boost::asio::io_context> external_ioc) {
85 [ + + ]: 40 : if (external_ioc) {
86 : 3 : return transport::UdpChannel::create(cfg, *external_ioc);
87 : : }
88 : 37 : return transport::UdpChannel::create(cfg);
89 : : }
90 : :
91 : 14 : std::shared_ptr<interface::Channel> ChannelFactory::create_uds_server(
92 : : const config::UdsServerConfig& cfg, std::shared_ptr<boost::asio::io_context> external_ioc) {
93 [ + + ]: 14 : if (external_ioc) {
94 : 4 : auto acceptor = std::make_unique<transport::BoostUdsAcceptor>(*external_ioc);
95 : 4 : return transport::UdsServer::create(cfg, std::move(acceptor), *external_ioc);
96 : 4 : }
97 : 10 : return transport::UdsServer::create(cfg);
98 : : }
99 : :
100 : 14 : std::shared_ptr<interface::Channel> ChannelFactory::create_uds_client(
101 : : const config::UdsClientConfig& cfg, std::shared_ptr<boost::asio::io_context> external_ioc) {
102 [ + + ]: 14 : if (external_ioc) {
103 : 5 : return transport::UdsClient::create(cfg, *external_ioc);
104 : : }
105 : 9 : return transport::UdsClient::create(cfg);
106 : : }
107 : :
108 : : } // namespace factory
109 : : } // namespace wirestead
|