create cmake project structure and c++ test app
This commit is contained in:
parent
1e357d1448
commit
6c33ce8deb
4 changed files with 123 additions and 0 deletions
12
CMakeLists.txt
Normal file
12
CMakeLists.txt
Normal file
|
@ -0,0 +1,12 @@
|
|||
cmake_minimum_required(VERSION 3.12)
|
||||
|
||||
project(doompanning)
|
||||
|
||||
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
||||
|
||||
## Create binaries in the root of the build directory
|
||||
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
|
||||
set(CMAKE_VERBOSE_MAKEFILE ON)
|
||||
|
||||
add_subdirectory(external)
|
||||
add_subdirectory(src)
|
8
external/CMakeLists.txt
vendored
Normal file
8
external/CMakeLists.txt
vendored
Normal file
|
@ -0,0 +1,8 @@
|
|||
option(NNG_SETSTACKSIZE "Use rlimit for thread stack size" ON)
|
||||
set(NNG_TESTS OFF)
|
||||
set(NNG_ENABLE_NNGCAT OFF)
|
||||
add_subdirectory(nng)
|
||||
|
||||
option(SPDLOG_FMT_EXTERNAL "Use external fmt library instead of bundled" OFF)
|
||||
option(SPDLOG_INSTALL "Generate the install target" ON)
|
||||
add_subdirectory(spdlog)
|
8
src/CMakeLists.txt
Normal file
8
src/CMakeLists.txt
Normal file
|
@ -0,0 +1,8 @@
|
|||
find_package(nng CONFIG REQUIRED)
|
||||
find_package(Threads)
|
||||
add_executable(doompanning doompanning.cc)
|
||||
target_link_libraries(doompanning
|
||||
PRIVATE nng::nng
|
||||
PRIVATE spdlog::spdlog
|
||||
)
|
||||
target_compile_features(doompanning PRIVATE cxx_std_17)
|
95
src/doompanning.cc
Normal file
95
src/doompanning.cc
Normal file
|
@ -0,0 +1,95 @@
|
|||
#include <cstdlib>
|
||||
#include <nng/nng.h>
|
||||
#include <nng/protocol/pubsub0/pub.h>
|
||||
#include <nng/protocol/pubsub0/sub.h>
|
||||
#include <spdlog/spdlog.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
void dp_nng_fatal(const std::string &msg, int rv)
|
||||
{
|
||||
spdlog::error("{}: {}", msg, nng_strerror(rv));
|
||||
abort();
|
||||
}
|
||||
|
||||
void dp_errno_fatal(const std::string &msg)
|
||||
{
|
||||
spdlog::error("{}: {}", msg, strerror(errno));
|
||||
abort();
|
||||
}
|
||||
|
||||
nng_socket make_ctrl_pub(const char *url)
|
||||
{
|
||||
nng_socket sock;
|
||||
|
||||
if (auto res = nng_pub0_open(&sock))
|
||||
dp_nng_fatal("make_ctrl_pub/nng_pub0_open", res);
|
||||
|
||||
if (auto res = nng_listen(sock, url, nullptr, 0))
|
||||
dp_nng_fatal("make_ctrl_pub/nng_listen", res);
|
||||
|
||||
return sock;
|
||||
}
|
||||
|
||||
nng_socket make_ctrl_sub(const char *url)
|
||||
{
|
||||
nng_socket sock;
|
||||
|
||||
if (auto res = nng_sub0_open(&sock))
|
||||
dp_nng_fatal("make_ctrl_pub/nng_sub0_open", res);
|
||||
|
||||
if (auto res = nng_listen(sock, url, nullptr, 0))
|
||||
dp_nng_fatal("make_ctrl_sub/nng_listen", res);
|
||||
|
||||
return sock;
|
||||
}
|
||||
|
||||
nng_socket make_doom_pub(const char *url)
|
||||
{
|
||||
}
|
||||
|
||||
nng_socket make_doom_sub(const char *url)
|
||||
{
|
||||
}
|
||||
|
||||
static const char *CtrlUrl = "ipc://666_ctrl.socket";
|
||||
static const char *DoomUrl = "ipc://666_doom.socket";
|
||||
static const size_t NumDooms = 2;
|
||||
|
||||
int run_doom(size_t id)
|
||||
{
|
||||
spdlog::debug("doom#{} running", id);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
spdlog::set_level(spdlog::level::debug);
|
||||
spdlog::set_pattern("[%H:%M:%S.%e] [pid=%P] [%^%l%$] %v");
|
||||
spdlog::info("Hello, doomed world!");
|
||||
|
||||
for (size_t i=0; i<NumDooms; ++i)
|
||||
{
|
||||
auto res = fork();
|
||||
|
||||
if (res == -1)
|
||||
dp_errno_fatal("spawn doom");
|
||||
else if (res == 0)
|
||||
return run_doom(i);
|
||||
else
|
||||
spdlog::debug("spawned doom#{}", i);
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
auto ctrlPubSock = make_ctrl_pub(CtrlUrl);
|
||||
auto ctrlSubSock = make_ctrl_sub(DoomUrl);
|
||||
|
||||
if (auto res = nng_send(ctrlPubSock, (void *)"foobar", 7, 0))
|
||||
dp_nng_fatal("ctrl send", res);
|
||||
|
||||
|
||||
nng_close(ctrlPubSock);
|
||||
nng_close(ctrlSubSock);
|
||||
return 0;
|
||||
}
|
Loading…
Reference in a new issue