-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Preliminary infrastructure for the boost based gateway app.
- Loading branch information
Showing
10 changed files
with
373 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
function (bin_cc_mqttsn_gateway_app) | ||
set (name "cc_mqttsn_gateway_app") | ||
|
||
set (src | ||
main.cpp | ||
GatewayApp.cpp | ||
GatewayProgramOptions.cpp | ||
) | ||
|
||
add_executable(${name} ${src}) | ||
target_link_libraries(${name} cc::${MQTTSN_GATEWAY_LIB_NAME} Boost::system Boost::program_options ${EXTRA_BOOST_TARGETS}) | ||
|
||
install ( | ||
TARGETS ${name} | ||
DESTINATION ${CMAKE_INSTALL_BINDIR}) | ||
|
||
install ( | ||
DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/etc/ | ||
DESTINATION ${CMAKE_INSTALL_SYSCONFDIR}/cc_mqttsn_gateway | ||
) | ||
|
||
endfunction () | ||
|
||
########################################################### | ||
|
||
find_package(Boost COMPONENTS system program_options) | ||
|
||
set (EXTRA_BOOST_TARGETS) | ||
if (WIN32) | ||
find_package (Boost REQUIRED COMPONENTS date_time regex) | ||
set (EXTRA_BOOST_TARGETS Boost::date_time Boost::regex) | ||
endif () | ||
|
||
bin_cc_mqttsn_gateway_app() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// | ||
// Copyright 2024 - 2024 (C). Alex Robenko. All rights reserved. | ||
// | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
#include "GatewayApp.h" | ||
|
||
#include <fstream> | ||
#include <iostream> | ||
|
||
namespace cc_mqttsn_gateway_app | ||
{ | ||
|
||
namespace | ||
{ | ||
|
||
std::ostream& logError() | ||
{ | ||
return std::cerr << "ERROR: "; | ||
} | ||
|
||
std::ostream& logInfo() | ||
{ | ||
return std::cout << "INFO: "; | ||
} | ||
|
||
std::ostream& logWarning() | ||
{ | ||
return std::cout << "WARNING: "; | ||
} | ||
|
||
} // namespace | ||
|
||
|
||
GatewayApp::GatewayApp(boost::asio::io_context& io) : | ||
m_io(io) | ||
{ | ||
} | ||
|
||
GatewayApp::~GatewayApp() = default; | ||
|
||
bool GatewayApp::start(int argc, const char* argv[]) | ||
{ | ||
GatewayProgramOptions opts; | ||
|
||
if (!opts.parseArgs(argc, argv)) { | ||
logError() << "Failed to parse arguments." << std::endl; | ||
return false; | ||
} | ||
|
||
if (opts.helpRequested()) { | ||
std::cout << "Usage: " << argv[0] << " [options...]" << '\n'; | ||
opts.printHelp(); | ||
m_io.stop(); | ||
return true; | ||
} | ||
|
||
auto configFile = opts.configFile(); | ||
do { | ||
if (!configFile.empty()) { | ||
logInfo() << "No configuration file provided, using default configuration." << std::endl; | ||
break; | ||
} | ||
|
||
std::ifstream stream(configFile); | ||
if (!stream) { | ||
logWarning() << "Failed to open configuration file \"" << | ||
configFile << "\", using default configuration." << std::endl; | ||
break; | ||
} | ||
|
||
m_config.read(stream); | ||
} while (false); | ||
|
||
// TODO: | ||
logError() << "NYI" << std::endl; | ||
return false; | ||
} | ||
|
||
} // namespace cc_mqttsn_gateway_app |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// | ||
// Copyright 2024 - 2024 (C). Alex Robenko. All rights reserved. | ||
// | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
#pragma once | ||
|
||
#include "GatewayProgramOptions.h" | ||
|
||
#include "cc_mqttsn_gateway/Config.h" | ||
|
||
#include <boost/asio.hpp> | ||
|
||
namespace cc_mqttsn_gateway_app | ||
{ | ||
|
||
class GatewayApp | ||
{ | ||
public: | ||
GatewayApp(boost::asio::io_context& io); | ||
~GatewayApp(); | ||
|
||
bool start(int argc, const char* argv[]); | ||
|
||
private: | ||
boost::asio::io_context& m_io; | ||
cc_mqttsn_gateway::Config m_config; | ||
}; | ||
|
||
} // namespace cc_mqttsn_gateway_app |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
// | ||
// Copyright 2023 - 2024 (C). Alex Robenko. All rights reserved. | ||
// | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
#include "GatewayProgramOptions.h" | ||
|
||
#include <cassert> | ||
#include <iostream> | ||
#include <map> | ||
|
||
namespace po = boost::program_options; | ||
|
||
namespace cc_mqttsn_gateway_app | ||
{ | ||
|
||
namespace | ||
{ | ||
|
||
using ConnectionTypeMap = std::map<std::string, GatewayProgramOptions::ConnectionType>; | ||
const ConnectionTypeMap& connectionTypeMap() | ||
{ | ||
static const ConnectionTypeMap Map = { | ||
{"udp", GatewayProgramOptions::ConnectionType_Udp}, | ||
}; | ||
|
||
return Map; | ||
} | ||
|
||
std::string supportedConnectionTypes() | ||
{ | ||
std::string result; | ||
for (auto& info : connectionTypeMap()) { | ||
if (!result.empty()) { | ||
result.append(", "); | ||
} | ||
|
||
result.append(info.first); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
const std::string& defaultConnectionType() | ||
{ | ||
auto& map = connectionTypeMap(); | ||
auto iter = | ||
std::find_if( | ||
map.begin(), map.end(), | ||
[](auto& info) | ||
{ | ||
return info.second == GatewayProgramOptions::DefaultConnectionType; | ||
}); | ||
|
||
assert(iter != map.end()); | ||
return iter->first; | ||
} | ||
|
||
} // namespace | ||
|
||
|
||
GatewayProgramOptions::GatewayProgramOptions() | ||
{ | ||
po::options_description opts("Options"); | ||
opts.add_options() | ||
("help,h", "Display help message") | ||
("config,c", po::value<std::string>()->default_value(std::string()), "Configuration file.") | ||
("socket,s", po::value<std::string>()->default_value(defaultConnectionType()), | ||
("Low level connection socket type. Available: " + supportedConnectionTypes()).c_str()) | ||
; | ||
|
||
m_desc.add(opts); | ||
} | ||
|
||
void GatewayProgramOptions::printHelp() | ||
{ | ||
std::cout << m_desc << std::endl; | ||
} | ||
|
||
bool GatewayProgramOptions::parseArgs(int argc, const char* argv[]) | ||
{ | ||
po::store(po::parse_command_line(argc, argv, m_desc), m_vm); | ||
po::notify(m_vm); | ||
|
||
return true; | ||
} | ||
|
||
bool GatewayProgramOptions::helpRequested() const | ||
{ | ||
return m_vm.count("help") > 0U; | ||
} | ||
|
||
std::string GatewayProgramOptions::configFile() const | ||
{ | ||
return m_vm["config"].as<std::string>(); | ||
} | ||
|
||
GatewayProgramOptions::ConnectionType GatewayProgramOptions::connectionType() const | ||
{ | ||
auto socketStr = m_vm["socket"].as<std::string>(); | ||
auto& map = connectionTypeMap(); | ||
auto iter = map.find(socketStr); | ||
if (iter == map.end()) { | ||
return ConnectionType_ValuesLimit; | ||
} | ||
|
||
return iter->second; | ||
} | ||
|
||
} // namespace cc_mqttsn_gateway_app |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// | ||
// Copyright 2023 - 2024 (C). Alex Robenko. All rights reserved. | ||
// | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
#pragma once | ||
|
||
#include <boost/program_options.hpp> | ||
|
||
#include <cstdint> | ||
#include <string> | ||
#include <vector> | ||
|
||
namespace cc_mqttsn_gateway_app | ||
{ | ||
|
||
class GatewayProgramOptions | ||
{ | ||
public: | ||
using OptDesc = boost::program_options::options_description; | ||
|
||
static constexpr std::uint16_t DefaultPort = 1883U; | ||
|
||
enum ConnectionType | ||
{ | ||
ConnectionType_Udp, | ||
ConnectionType_ValuesLimit | ||
}; | ||
|
||
static const ConnectionType DefaultConnectionType = ConnectionType_Udp; | ||
|
||
GatewayProgramOptions(); | ||
|
||
void printHelp(); | ||
|
||
bool parseArgs(int argc, const char* argv[]); | ||
|
||
bool helpRequested() const; | ||
std::string configFile() const; | ||
ConnectionType connectionType() const; | ||
|
||
|
||
private: | ||
|
||
boost::program_options::variables_map m_vm; | ||
OptDesc m_desc; | ||
}; | ||
|
||
} // namespace cc_mqttsn_gateway_app |
File renamed without changes.
Oops, something went wrong.