84 lines
1.8 KiB
C
84 lines
1.8 KiB
C
|
#include "dp_common.h"
|
||
|
|
||
|
#include <errno.h>
|
||
|
#include <nng/protocol/pubsub0/pub.h>
|
||
|
#include <nng/protocol/pubsub0/sub.h>
|
||
|
#include <stdlib.h>
|
||
|
#include <string.h>
|
||
|
|
||
|
#include "log.h"
|
||
|
|
||
|
void dp_nng_fatal(const char *const msg, int rv)
|
||
|
{
|
||
|
log_error("%s: %s", msg, nng_strerror(rv));
|
||
|
abort();
|
||
|
}
|
||
|
|
||
|
void dp_errno_fatal(const char *const msg)
|
||
|
{
|
||
|
log_error("%s: %s", msg, strerror(errno));
|
||
|
abort();
|
||
|
}
|
||
|
|
||
|
nng_socket make_ctrl_pub(const char *url)
|
||
|
{
|
||
|
nng_socket sock;
|
||
|
int res = 0;
|
||
|
|
||
|
if ((res = nng_pub0_open(&sock)))
|
||
|
dp_nng_fatal("make_ctrl_pub/nng_pub0_open", res);
|
||
|
|
||
|
if ((res = nng_listen(sock, url, NULL, 0)))
|
||
|
dp_nng_fatal("make_ctrl_pub/nng_listen", res);
|
||
|
|
||
|
return sock;
|
||
|
}
|
||
|
|
||
|
nng_socket make_ctrl_sub(const char *url)
|
||
|
{
|
||
|
nng_socket sock;
|
||
|
int res = 0;
|
||
|
|
||
|
if ((res = nng_sub0_open(&sock)))
|
||
|
dp_nng_fatal("make_ctrl_sub/nng_sub0_open", res);
|
||
|
|
||
|
if ((res = nng_setopt(sock, NNG_OPT_SUB_SUBSCRIBE, "", 0)))
|
||
|
dp_nng_fatal("make_ctrl_sub/subscribe", res);
|
||
|
|
||
|
if ((res = nng_listen(sock, url, NULL, 0)))
|
||
|
dp_nng_fatal("make_ctrl_sub/nng_listen", res);
|
||
|
|
||
|
return sock;
|
||
|
}
|
||
|
|
||
|
nng_socket make_doom_pub(const char *url)
|
||
|
{
|
||
|
nng_socket sock;
|
||
|
int res = 0;
|
||
|
|
||
|
if ((res = nng_pub0_open(&sock)))
|
||
|
dp_nng_fatal("make_doom_pub/nng_pub0_open", res);
|
||
|
|
||
|
if ((res = nng_dial(sock, url, NULL, NNG_FLAG_NONBLOCK)))
|
||
|
dp_nng_fatal("make_doom_pub/nng_dial", res);
|
||
|
|
||
|
return sock;
|
||
|
}
|
||
|
|
||
|
nng_socket make_doom_sub(const char *url)
|
||
|
{
|
||
|
nng_socket sock;
|
||
|
int res = 0;
|
||
|
|
||
|
if ((res = nng_sub0_open(&sock)))
|
||
|
dp_nng_fatal("make_doom_sub/nng_sub0_open", res);
|
||
|
|
||
|
if ((res = nng_setopt(sock, NNG_OPT_SUB_SUBSCRIBE, "", 0)))
|
||
|
dp_nng_fatal("make_doom_sub/subscribe", res);
|
||
|
|
||
|
if ((res = nng_dial(sock, url, NULL, NNG_FLAG_NONBLOCK)))
|
||
|
dp_nng_fatal("make_doom_sub/nng_dial", res);
|
||
|
|
||
|
return sock;
|
||
|
}
|