unilink  0.4.3
A simple C++ library for unified async communication
config_factory.cc
Go to the documentation of this file.
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 "config_factory.hpp"
18 
19 #include <mutex>
20 
21 #include "config_manager.hpp"
22 
23 namespace unilink {
24 namespace config {
25 
26 std::shared_ptr<ConfigManagerInterface> ConfigFactory::singleton_instance_ = nullptr;
27 std::mutex ConfigFactory::singleton_mutex_;
28 
29 std::shared_ptr<ConfigManagerInterface> ConfigFactory::create() { return std::make_shared<ConfigManager>(); }
30 
31 std::shared_ptr<ConfigManagerInterface> ConfigFactory::create_with_defaults() {
32  auto config = create();
34  return config;
35 }
36 
37 std::shared_ptr<ConfigManagerInterface> ConfigFactory::create_from_file(const std::string& filepath) {
38  auto config = create();
39  if (!config->load_from_file(filepath)) {
40  // If loading fails, fall back to defaults
42  }
43  return config;
44 }
45 
46 std::shared_ptr<ConfigManagerInterface> ConfigFactory::get_singleton() {
47  std::lock_guard<std::mutex> lock(singleton_mutex_);
48  if (!singleton_instance_) {
49  singleton_instance_ = create_with_defaults();
50  }
51  return singleton_instance_;
52 }
53 
54 void ConfigPresets::setup_tcp_client_defaults(std::shared_ptr<ConfigManagerInterface> config) {
55  // TCP Client default configuration
56  config->set("tcp.client.host", std::string("localhost"));
57  config->set("tcp.client.port", 8080);
58  config->set("tcp.client.retry_interval_ms", 1000);
59  config->set("tcp.client.max_retries", 5);
60  config->set("tcp.client.connection_timeout_ms", 5000);
61  config->set("tcp.client.keep_alive", true);
62  config->set("tcp.client.buffer_size", 4096);
63 }
64 
65 void ConfigPresets::setup_tcp_server_defaults(std::shared_ptr<ConfigManagerInterface> config) {
66  // TCP Server default configuration
67  config->set("tcp.server.host", std::string("0.0.0.0"));
68  config->set("tcp.server.port", 8080);
69  config->set("tcp.server.max_connections", 100);
70  config->set("tcp.server.connection_timeout_ms", 30000);
71  config->set("tcp.server.keep_alive", true);
72  config->set("tcp.server.buffer_size", 4096);
73  config->set("tcp.server.backlog", 128);
74 }
75 
76 void ConfigPresets::setup_serial_defaults(std::shared_ptr<ConfigManagerInterface> config) {
77  // Serial communication default configuration
78  config->set("serial.port", std::string("/dev/ttyUSB0"));
79  config->set("serial.baud_rate", 9600);
80  config->set("serial.data_bits", 8);
81  config->set("serial.stop_bits", 1);
82  config->set("serial.parity", std::string("none"));
83  config->set("serial.flow_control", std::string("none"));
84  config->set("serial.timeout_ms", 1000);
85  config->set("serial.retry_interval_ms", 1000);
86  config->set("serial.max_retries", 3);
87 }
88 
89 void ConfigPresets::setup_logging_defaults(std::shared_ptr<ConfigManagerInterface> config) {
90  // Logging default configuration
91  config->set("logging.level", std::string("info"));
92  config->set("logging.enable_console", true);
93  config->set("logging.enable_file", false);
94  config->set("logging.file_path", std::string("unilink.log"));
95  config->set("logging.max_file_size_mb", 10);
96  config->set("logging.max_files", 5);
97  config->set("logging.format", std::string("[%Y-%m-%d %H:%M:%S] [%l] %v"));
98 }
99 
100 void ConfigPresets::setup_all_defaults(std::shared_ptr<ConfigManagerInterface> config) {
103  setup_serial_defaults(config);
104  setup_logging_defaults(config);
105 
106  // Global settings
107  config->set("global.thread_pool_size", 4);
108  config->set("global.enable_metrics", false);
109  config->set("global.metrics_port", 9090);
110  config->set("global.config_file", std::string("unilink.conf"));
111 }
112 
113 } // namespace config
114 } // namespace unilink