Skip to content

Commit

Permalink
Working on basic server functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
janhenke committed May 23, 2024
1 parent aefcac8 commit d99573b
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 9 deletions.
2 changes: 1 addition & 1 deletion client/app/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ auto main(int argc, char *argv[]) -> int {
boost::program_options::store(boost::program_options::parse_command_line(argc, argv, description), variablesMap);
boost::program_options::notify(variablesMap);

if (variablesMap.count("help")) {
if (variablesMap.count("help") != 0U) {
std::cout << description << '\n';
return EXIT_SUCCESS;
}
Expand Down
1 change: 0 additions & 1 deletion common/src/pimpl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ namespace libmumble_protocol::common {

template<typename T>
class Pimpl {
private:
std::unique_ptr<T> m;

public:
Expand Down
3 changes: 3 additions & 0 deletions server/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,11 @@ set_target_properties(
)

find_package(Boost ${BOOST_REQUIRED_VERSION} REQUIRED COMPONENTS program_options)
pkg_check_modules(libpqxx REQUIRED IMPORTED_TARGET libpqxx)

target_link_libraries(
mumble_server_app
PRIVATE PkgConfig::libpqxx
PRIVATE mumble_server
PRIVATE Boost::boost
PRIVATE Boost::program_options
Expand Down
56 changes: 53 additions & 3 deletions server/app/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,64 @@
//

#include <cstdlib>
#include <iostream>
#include <string_view>

#include <server.hpp>

#include <boost/program_options.hpp>
#include <pqxx/pqxx>
#include <spdlog/spdlog.h>

class PostgreSqlPersistence final : public libmumble_protocol::server::ServerStatePersistence {

pqxx::connection connection_;

public:
explicit PostgreSqlPersistence(std::string_view url) : connection_(pqxx::zview{url}){};
~PostgreSqlPersistence() override = default;

protected:
void dummy() override{};
};

auto main(int argc, char *argv[]) -> int {
(void) argc;
(void) argv;
std::uint16_t port = 0;
std::string cert_file;
std::string key_file;
bool generate_missing_certificate;
std::string database_url;

boost::program_options::options_description description{"libmumble_server example application"};
description.add_options()("help,h", "display help message");
description.add_options()("port,p",
boost::program_options::value<std::uint16_t>(&port)->default_value(
libmumble_protocol::server::MumbleServer::defaultPort),
"port number to use");
description.add_options()("cert,c", boost::program_options::value<std::string>(&cert_file),
"Location of the certificate file");
description.add_options()("key,k", boost::program_options::value<std::string>(&key_file),
"Location of the key file");
description.add_options()("generate-cert", boost::program_options::bool_switch(&generate_missing_certificate),
"generate TLS certificate if the specified file is missing");
description.add_options()("database,d", boost::program_options::value<std::string>(&database_url),
"Connection URL for the postgresql database");

boost::program_options::variables_map variables_map;
boost::program_options::store(boost::program_options::parse_command_line(argc, argv, description), variables_map);
boost::program_options::notify(variables_map);

if (variables_map.count("help") != 0U) {
std::cout << description << '\n';
return EXIT_SUCCESS;
}

#ifndef NDEBUG
spdlog::set_level(spdlog::level::debug);
#endif

libmumble_protocol::server::MumbleServer mumble_server;
PostgreSqlPersistence persistence{database_url};
libmumble_protocol::server::MumbleServer mumble_server{persistence, {cert_file}, {key_file}};

return EXIT_SUCCESS;
}
36 changes: 34 additions & 2 deletions server/lib/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,43 @@

#include <pimpl_impl.hpp>

#include <asio.hpp>

#include <thread>
#include <vector>

namespace libmumble_protocol::server {

struct MumbleServer::Impl final {};
struct MumbleServer::Impl final {

ServerStatePersistence &persistance;

std::vector<std::jthread> thread_handles;

asio::io_context io_context;

Impl(ServerStatePersistence &persistance, const std::filesystem::path &certificate, const std::filesystem::path &key_file,
const std::uint16_t concurrency)
: persistance(persistance) {

(void) certificate;
(void) key_file;

const std::uint16_t thread_count =
concurrency != 0 ? concurrency : static_cast<std::uint16_t>(std::jthread::hardware_concurrency());
for (std::size_t i = 0; i < thread_count; ++i) {
thread_handles.emplace_back(&asio::io_context::run, &io_context);
}
}
~Impl() {
io_context.stop();
for (auto &thread : thread_handles) { thread.join(); }
}
};

MumbleServer::MumbleServer() = default;
MumbleServer::MumbleServer(ServerStatePersistence &server_state_persistance, const std::filesystem::path &certificate,
const std::filesystem::path &key_file, std::uint16_t concurrency)
: pimpl_(server_state_persistance, certificate, key_file, concurrency) {}

MumbleServer::~MumbleServer() = default;

Expand Down
24 changes: 22 additions & 2 deletions server/lib/server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,36 @@

#include <pimpl.hpp>

#include <cstdint>
#include <filesystem>

namespace libmumble_protocol::server {

class ServerStatePersistence {
public:
virtual ~ServerStatePersistence() = default;

protected:
virtual void dummy() = 0;
};

class MUMBLE_SERVER_EXPORT MumbleServer final {
public:
MumbleServer();
static constexpr std::uint16_t defaultPort = 64738;

MumbleServer(ServerStatePersistence &server_state_persistance, const std::filesystem::path &certificate,
const std::filesystem::path &key_file, std::uint16_t concurrency = 0);
MumbleServer(const MumbleServer &other) = delete;
MumbleServer(MumbleServer &&other) noexcept = delete;

auto operator=(const MumbleServer &other) -> MumbleServer & = delete;
auto operator=(MumbleServer &&other) noexcept -> MumbleServer & = delete;

~MumbleServer();

private:
struct Impl;
libmumble_protocol::common::Pimpl<Impl> pimpl_;
common::Pimpl<Impl> pimpl_;
};

}// namespace libmumble_protocol::server
Expand Down
4 changes: 4 additions & 0 deletions vcpkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
"name": "catch2",
"version>=": "3.5.0"
},
{
"name": "libpqxx",
"version>=": "7.9.0"
},
{
"name": "opus",
"version>=": "1.4"
Expand Down

0 comments on commit d99573b

Please sign in to comment.