Skip to content

Commit

Permalink
Preliminary infrastructure for the boost based gateway app.
Browse files Browse the repository at this point in the history
  • Loading branch information
arobenko committed May 14, 2024
1 parent c9d9e1c commit 07e01ce
Show file tree
Hide file tree
Showing 10 changed files with 373 additions and 8 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/actions_build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
run: sudo apt-get update --fix-missing

- name: Install Packages
run: sudo apt install qtbase5-dev gcc-${{matrix.cc_ver}} g++-${{matrix.cc_ver}}
run: sudo apt install qtbase5-dev libboost-all-dev gcc-${{matrix.cc_ver}} g++-${{matrix.cc_ver}}

- name: Create Build Environment
run: cmake -E make_directory ${{runner.workspace}}/build
Expand Down Expand Up @@ -92,7 +92,7 @@ jobs:
run: sudo apt-get update --fix-missing

- name: Install Packages
run: sudo apt install qtbase5-dev clang-${{matrix.cc_ver}}
run: sudo apt install qtbase5-dev libboost-all-dev clang-${{matrix.cc_ver}}

- name: Create Build Environment
run: cmake -E make_directory ${{runner.workspace}}/build
Expand Down
3 changes: 2 additions & 1 deletion gateway/app/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ if (NOT CC_MQTTSN_BUILD_GATEWAY_APPS)
return ()
endif ()

add_subdirectory (udp)
add_subdirectory (udp)
add_subdirectory (gateway)
34 changes: 34 additions & 0 deletions gateway/app/gateway/CMakeLists.txt
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()
82 changes: 82 additions & 0 deletions gateway/app/gateway/GatewayApp.cpp
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
32 changes: 32 additions & 0 deletions gateway/app/gateway/GatewayApp.h
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
112 changes: 112 additions & 0 deletions gateway/app/gateway/GatewayProgramOptions.cpp
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
51 changes: 51 additions & 0 deletions gateway/app/gateway/GatewayProgramOptions.h
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.
Loading

0 comments on commit 07e01ce

Please sign in to comment.