2023-02-03 23:33:37 +01:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <nng/nng.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
2023-02-08 06:39:23 +01:00
|
|
|
#include "dp_common.h"
|
|
|
|
#include "log.h"
|
2023-02-03 23:33:37 +01:00
|
|
|
|
2023-02-08 06:39:23 +01:00
|
|
|
static const char *CtrlUrl = "ipc://666_ctrl.socket"; // controller publishes here
|
|
|
|
static const char *DoomUrl = "ipc://666_doom.socket"; // dooms publish here
|
|
|
|
//static const char *CtrlUrl = "tcp://127.0.0.1:6666"; // controller publishes here
|
|
|
|
//static const char *DoomUrl = "tcp://127.0.0.1:6667"; // dooms publish here
|
|
|
|
static const size_t NumDooms = 3;
|
2023-02-03 23:33:37 +01:00
|
|
|
|
2023-02-04 01:56:30 +01:00
|
|
|
int run_controller(const size_t numDooms)
|
2023-02-03 23:33:37 +01:00
|
|
|
{
|
2023-02-04 01:56:30 +01:00
|
|
|
nng_set_ncpu_max(1);
|
|
|
|
nng_set_pool_thread_limit_max(1);
|
|
|
|
nng_set_resolve_thread_max(1);
|
|
|
|
|
|
|
|
auto pubSock = make_ctrl_pub(CtrlUrl);
|
|
|
|
auto subSock = make_ctrl_sub(DoomUrl);
|
|
|
|
|
|
|
|
size_t readyCount = 0;
|
|
|
|
|
2023-02-08 06:39:23 +01:00
|
|
|
log_debug("ctrl waiting for hello from dooms");
|
2023-02-04 01:56:30 +01:00
|
|
|
while (readyCount < numDooms)
|
|
|
|
{
|
|
|
|
char *buf = nullptr;
|
|
|
|
size_t sz = 0;
|
|
|
|
|
|
|
|
if (auto res = nng_recv(subSock, &buf, &sz, NNG_FLAG_ALLOC))
|
|
|
|
dp_nng_fatal("run_ctrl/sub/go", res);
|
|
|
|
|
2023-02-08 06:39:23 +01:00
|
|
|
log_debug("ctrl received %zu bytes from a doom", sz);
|
2023-02-04 01:56:30 +01:00
|
|
|
nng_free(buf, sz);
|
|
|
|
++readyCount;
|
|
|
|
}
|
|
|
|
|
2023-02-08 06:39:23 +01:00
|
|
|
log_debug("ctrl is unleashing dooms");
|
2023-02-04 01:56:30 +01:00
|
|
|
|
|
|
|
if (auto res = nng_send(pubSock, (void *)&readyCount, sizeof(readyCount), 0))
|
|
|
|
dp_nng_fatal("run_ctrl/pub/unleash", res);
|
|
|
|
|
|
|
|
nng_close(pubSock);
|
|
|
|
nng_close(subSock);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int run_doom(const size_t id)
|
|
|
|
{
|
2023-02-08 06:39:23 +01:00
|
|
|
log_debug("doom#%zu started", id);
|
2023-02-04 01:56:30 +01:00
|
|
|
|
|
|
|
nng_set_ncpu_max(1);
|
|
|
|
nng_set_pool_thread_limit_max(1);
|
|
|
|
nng_set_resolve_thread_max(1);
|
|
|
|
|
|
|
|
auto pubSock = make_doom_pub(DoomUrl);
|
|
|
|
auto subSock = make_doom_sub(CtrlUrl);
|
|
|
|
|
|
|
|
|
|
|
|
while (true) // FIXME: ctrl code does not work this way
|
|
|
|
{
|
2023-02-08 06:39:23 +01:00
|
|
|
log_debug("doom%zu sending hello", id);
|
2023-02-04 01:56:30 +01:00
|
|
|
if (auto res = nng_send(pubSock, (void *)&id, sizeof(id), 0))
|
|
|
|
dp_nng_fatal("run_doom/pub/hello", res);
|
|
|
|
|
|
|
|
{
|
|
|
|
char *buf = nullptr;
|
|
|
|
size_t sz = 0;
|
|
|
|
|
2023-02-08 06:39:23 +01:00
|
|
|
log_debug("doom%zu waiting for unleash", id);
|
2023-02-04 01:56:30 +01:00
|
|
|
auto res = nng_recv(subSock, &buf, &sz, NNG_FLAG_ALLOC | NNG_FLAG_NONBLOCK);
|
|
|
|
|
|
|
|
if (res == NNG_EAGAIN)
|
|
|
|
{
|
|
|
|
usleep(10 * 1000);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (res)
|
|
|
|
dp_nng_fatal("run_doom/sub/go", res);
|
|
|
|
|
2023-02-08 06:39:23 +01:00
|
|
|
log_debug("doom%zu unleashed by %zu bytes!", id, sz);
|
2023-02-04 01:56:30 +01:00
|
|
|
nng_free(buf, sz);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nng_close(pubSock);
|
|
|
|
nng_close(subSock);
|
|
|
|
|
2023-02-03 23:33:37 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2023-02-08 06:39:23 +01:00
|
|
|
log_info("doompanning ctrl starting");
|
2023-02-03 23:33:37 +01:00
|
|
|
|
|
|
|
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
|
2023-02-08 06:39:23 +01:00
|
|
|
log_debug("spawned doom#%zu", i);
|
2023-02-03 23:33:37 +01:00
|
|
|
}
|
|
|
|
|
2023-02-04 01:56:30 +01:00
|
|
|
return run_controller(NumDooms);
|
2023-02-03 23:33:37 +01:00
|
|
|
}
|