96 lines
2 KiB
C++
96 lines
2 KiB
C++
|
#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;
|
||
|
}
|