Skip to content

Commit

Permalink
More infrastructure for the gateway app.
Browse files Browse the repository at this point in the history
  • Loading branch information
arobenko committed May 15, 2024
1 parent a7c46a0 commit 8d02cd8
Show file tree
Hide file tree
Showing 12 changed files with 285 additions and 14 deletions.
23 changes: 18 additions & 5 deletions .github/workflows/actions_build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,11 @@ jobs:
- name: Create Build Environment
run: cmake -E make_directory ${{runner.workspace}}/build

- name: Install Boost
shell: cmd
run: |
choco install boost-msvc-14.2
- name: Prepare externals
shell: cmd
run: |
Expand All @@ -172,8 +177,8 @@ jobs:
run: |
cmake %GITHUB_WORKSPACE% -A ${{matrix.arch}} -DCMAKE_BUILD_TYPE=${{matrix.type}} -DCMAKE_INSTALL_PREFIX=install ^
-DCMAKE_PREFIX_PATH=${{runner.workspace}}/build/install -DCMAKE_CXX_STANDARD=${{matrix.cpp}} ^
-DCC_MQTTSN_BUILD_UNIT_TESTS=ON ^
-DCC_MQTTSN_BUILD_CLIENT_APPS=OFF -DCC_MQTTSN_BUILD_GATEWAY_APPS=OFF
-DBoost_USE_STATIC_LIBS=ON -DCC_MQTTSN_BUILD_UNIT_TESTS=ON ^
-DCC_MQTTSN_BUILD_CLIENT_APPS=OFF -DCC_MQTTSN_BUILD_GATEWAY_APPS=ON
- name: Build Target
working-directory: ${{runner.workspace}}/build
Expand Down Expand Up @@ -202,6 +207,12 @@ jobs:
- name: Create Build Environment
run: cmake -E make_directory ${{runner.workspace}}/build

- name: Install Boost
if: matrix.arch == 'x64'
shell: cmd
run: |
choco install boost-msvc-14.3
- name: Prepare externals
shell: cmd
run: |
Expand All @@ -223,8 +234,10 @@ jobs:
run: |
cmake %GITHUB_WORKSPACE% -A ${{matrix.arch}} -DCMAKE_BUILD_TYPE=${{matrix.type}} -DCMAKE_INSTALL_PREFIX=install ^
-DCMAKE_PREFIX_PATH=${{runner.workspace}}/build/install -DCMAKE_CXX_STANDARD=${{matrix.cpp}} ^
-DCC_MQTTSN_BUILD_UNIT_TESTS=ON ^
-DCC_MQTTSN_BUILD_CLIENT_APPS=OFF -DCC_MQTTSN_BUILD_GATEWAY_APPS=OFF
-DBoost_USE_STATIC_LIBS=ON -DCC_MQTTSN_BUILD_UNIT_TESTS=ON ^
-DCC_MQTTSN_BUILD_CLIENT_APPS=OFF -DCC_MQTTSN_BUILD_GATEWAY_APPS=${{env.HAS_BOOST}}
env:
HAS_BOOST: "${{ matrix.arch == 'x64' && 'ON' || 'OFF' }}"

- name: Build Target
working-directory: ${{runner.workspace}}/build
Expand All @@ -236,4 +249,4 @@ jobs:
- name: Testing
working-directory: ${{runner.workspace}}/build
shell: cmd
run: ctest -V
run: ctest -V
2 changes: 1 addition & 1 deletion gateway/app/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ if (NOT CC_MQTTSN_BUILD_GATEWAY_APPS)
return ()
endif ()

add_subdirectory (udp)
#add_subdirectory (udp)
add_subdirectory (gateway)
3 changes: 3 additions & 0 deletions gateway/app/gateway/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ function (bin_cc_mqttsn_gateway_app)
set (src
main.cpp
GatewayApp.cpp
GatewayIoClientAcceptor.cpp
GatewayIoClientAcceptor_Udp.cpp
GatewayIoClientSocket.cpp
GatewayProgramOptions.cpp
)

Expand Down
15 changes: 12 additions & 3 deletions gateway/app/gateway/GatewayApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,18 @@ bool GatewayApp::start(int argc, const char* argv[])
m_config.read(stream);
} while (false);

// TODO:
logError() << "NYI" << std::endl;
return false;
m_acceptor = GatewayIoClientAcceptor::create(m_io, m_config);
if (!m_acceptor) {
logError() << "Unknown / unsupported client socket type" << std::endl;
return false;
}

if (!m_acceptor->start()) {
logError() << "Failed to start client socket" << std::endl;
return false;
}

return true;
}

} // namespace cc_mqttsn_gateway_app
2 changes: 2 additions & 0 deletions gateway/app/gateway/GatewayApp.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#pragma once

#include "GatewayIoClientAcceptor.h"
#include "GatewayProgramOptions.h"

#include "cc_mqttsn_gateway/Config.h"
Expand All @@ -27,6 +28,7 @@ class GatewayApp
private:
boost::asio::io_context& m_io;
cc_mqttsn_gateway::Config m_config;
GatewayIoClientAcceptorPtr m_acceptor;
};

} // namespace cc_mqttsn_gateway_app
59 changes: 59 additions & 0 deletions gateway/app/gateway/GatewayIoClientAcceptor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//
// 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 "GatewayIoClientAcceptor.h"

#include "GatewayIoClientAcceptor_Udp.h"

#include <cassert>
#include <type_traits>

namespace cc_mqttsn_gateway_app
{

GatewayIoClientAcceptor::~GatewayIoClientAcceptor() = default;

GatewayIoClientAcceptor::Ptr GatewayIoClientAcceptor::create(boost::asio::io_context& io, const cc_mqttsn_gateway::Config& config)
{
using CreateFunc = Ptr (*)(boost::asio::io_context&, const cc_mqttsn_gateway::Config&);
static const CreateFunc Map[] = {
/* ClientConnectionType_Udp */ &GatewayIoClientAcceptor_Udp::create,
};
static const std::size_t MapSize = std::extent<decltype(Map)>::value;
static_assert(MapSize == cc_mqttsn_gateway::Config::ClientConnectionType_ValuesLimit);

auto idx = static_cast<unsigned>(config.clientConnectionType());
if (MapSize <= idx) {
return Ptr();
}

auto func = Map[idx];
if (func == nullptr) {
return Ptr();
}

return func(io, config);
}

bool GatewayIoClientAcceptor::start()
{
if (!m_newConnectionReportCb) {
return false;
}

return startImpl();
}

void GatewayIoClientAcceptor::reportNewConnection(GatewayIoClientSocketPtr socket)
{
assert(m_newConnectionReportCb);
m_newConnectionReportCb(std::move(socket));
}



} // namespace cc_mqttsn_gateway_app
62 changes: 62 additions & 0 deletions gateway/app/gateway/GatewayIoClientAcceptor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
//
// 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 "GatewayIoClientSocket.h"

#include "cc_mqttsn_gateway/Config.h"

#include <boost/asio.hpp>

#include <functional>
#include <memory>

namespace cc_mqttsn_gateway_app
{

class GatewayIoClientAcceptor
{
public:
using Ptr = std::unique_ptr<GatewayIoClientAcceptor>;
using NewConnectionReportCb = std::function<void (GatewayIoClientSocketPtr)>;

virtual ~GatewayIoClientAcceptor();

static Ptr create(boost::asio::io_context& io, const cc_mqttsn_gateway::Config& config);

bool start();

template <typename TFunc>
void setNewConnectionReportCb(TFunc&& func)
{
m_newConnectionReportCb = std::forward<TFunc>(func);
}

protected:
GatewayIoClientAcceptor(boost::asio::io_context& io) :
m_io(io)
{
};

virtual bool startImpl() = 0;

boost::asio::io_context& io()
{
return m_io;
}

void reportNewConnection(GatewayIoClientSocketPtr socket);

private:
boost::asio::io_context& m_io;
NewConnectionReportCb m_newConnectionReportCb;
};

using GatewayIoClientAcceptorPtr = GatewayIoClientAcceptor::Ptr;

} // namespace cc_mqttsn_gateway_app
32 changes: 32 additions & 0 deletions gateway/app/gateway/GatewayIoClientAcceptor_Udp.cpp
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/.

#include "GatewayIoClientAcceptor_Udp.h"

namespace cc_mqttsn_gateway_app
{

GatewayIoClientAcceptor_Udp::GatewayIoClientAcceptor_Udp(boost::asio::io_context& io, const cc_mqttsn_gateway::Config& config) :
Base(io)
{
static_cast<void>(config);
}

GatewayIoClientAcceptor_Udp::~GatewayIoClientAcceptor_Udp() = default;

GatewayIoClientAcceptor_Udp::Ptr GatewayIoClientAcceptor_Udp::create(boost::asio::io_context& io, const cc_mqttsn_gateway::Config& config)
{
return std::make_unique<GatewayIoClientAcceptor_Udp>(io, config);
}

bool GatewayIoClientAcceptor_Udp::startImpl()
{
// TODO
return false;
}

} // namespace cc_mqttsn_gateway_app
30 changes: 30 additions & 0 deletions gateway/app/gateway/GatewayIoClientAcceptor_Udp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//
// 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 "GatewayIoClientAcceptor.h"

namespace cc_mqttsn_gateway_app
{

class GatewayIoClientAcceptor_Udp : public GatewayIoClientAcceptor
{
using Base = GatewayIoClientAcceptor;
public:
GatewayIoClientAcceptor_Udp(boost::asio::io_context& io, const cc_mqttsn_gateway::Config& config);
virtual ~GatewayIoClientAcceptor_Udp();

static Ptr create(boost::asio::io_context& io, const cc_mqttsn_gateway::Config& config);

protected:
virtual bool startImpl() override;

private:
};

} // namespace cc_mqttsn_gateway_app
18 changes: 18 additions & 0 deletions gateway/app/gateway/GatewayIoClientSocket.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// 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 "GatewayIoClientSocket.h"

#include <type_traits>

namespace cc_mqttsn_gateway_app
{

GatewayIoClientSocket::~GatewayIoClientSocket() = default;


} // namespace cc_mqttsn_gateway_app
48 changes: 48 additions & 0 deletions gateway/app/gateway/GatewayIoClientSocket.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//
// 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 "cc_mqttsn_gateway/Config.h"

#include <boost/asio.hpp>

#include <memory>

namespace cc_mqttsn_gateway_app
{

class GatewayIoClientSocket
{
public:
virtual ~GatewayIoClientSocket();

bool start()
{
return startImpl();
}

protected:
GatewayIoClientSocket(boost::asio::io_context& io) :
m_io(io)
{
};

virtual bool startImpl() = 0;

boost::asio::io_context& io()
{
return m_io;
}

private:
boost::asio::io_context& m_io;
};

using GatewayIoClientSocketPtr = std::unique_ptr<GatewayIoClientSocket>;

} // namespace cc_mqttsn_gateway_app
5 changes: 0 additions & 5 deletions gateway/lib/src/gateway_all.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -327,11 +327,6 @@ void cc_mqttsn_gw_session_set_fwd_enc_session_deleted_cb(
});
}

void cc_mqttsn_gw_session_set_fwd_enc_session_deleted_cb(
CC_MqttsnSessionHandle session,
CC_MqttsnSessionFwdEncSessionDeletedCb cb,
void* data);

void cc_mqttsn_gw_session_set_id(CC_MqttsnSessionHandle session, unsigned char id)
{
if (session.obj == nullptr) {
Expand Down

0 comments on commit 8d02cd8

Please sign in to comment.