NNG是一个消息传递框架,用来解决常见的消息传递问题。NNG是nanomsg的继任版本,纯c语言开发,工作模式分为一下几种:
Pipeline单向管道
此模式可用于解决生产者/消费者问题,包括负载均衡。
消息从推侧流向拉侧。
如果多个同级节点连接在一起,则模式会尝试公平分配。
pipeline.cpp
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
// #include <unistd.h> sleep(1);
#include <Windows.h> // Windows系统下用Sleep
#include <nng/nng.h>
#include <nng/protocol/pipeline0/pull.h>
#include <nng/protocol/pipeline0/push.h>
#define NODE0 "node0"
#define NODE1 "node1"
void
fatal(const char* func, int rv)
{
fprintf(stderr, "%s: %s\n", func, nng_strerror(rv));
exit(1);
}
int
node0(const char* url)
{
nng_socket sock;
int rv;
if ((rv = nng_pull0_open(&sock)) != 0) {
fatal("nng_pull0_open", rv);
}
if ((rv = nng_listen(sock, url, NULL, 0)) != 0) {
fatal("nng_listen", rv);
}
for (;;) {
char* buf = NULL;
size_t sz;
if ((rv = nng_recv(sock, &buf, &sz, NNG_FLAG_ALLOC)) != 0) {
fatal("nng_recv", rv);
}
printf("NODE0: RECEIVED \"%s\"\n", buf);
nng_free(buf, sz);
}
}
int
node1(const char* url, char* msg)
{
int sz_msg = strlen(msg) + 1; // '\0' too
nng_socket sock;
int rv;
int bytes;
if ((rv = nng_push0_open(&sock)) != 0) {
fatal("nng_push0_open", rv);
}
if ((rv = nng_dial(sock, url, NULL, 0)) != 0) {
fatal("nng_dial", rv);
}
printf("NODE1: SENDING \"%s\"\n", msg);
if ((rv = nng_send(sock, msg, strlen(msg) + 1, 0)) != 0) {
fatal("nng_send", rv);
}
Sleep(1); // wait for messages to flush before shutting down
nng_close(sock);
return (0);
}
int
main(int argc, char** argv)
{
if ((argc > 1) && (strcmp(NODE0, argv[1]) == 0))
return (node0(argv[2]));
if ((argc > 2) && (strcmp(NODE1, argv[1]) == 0))
return (node1(argv[2], argv[3]));
fprintf(stderr, "Usage: pipeline %s|%s <URL> <ARG> ...'\n",
NODE0, NODE1);
return (1);
}
执行
./pipeline node0 ipc:///tmp/pipeline.ipc & node0=$! && sleep 1
./pipeline node1 ipc:///tmp/pipeline.ipc "Hello, World!"
./pipeline node1 ipc:///tmp/pipeline.ipc "Goodbye."
输出:
NODE1: SENDING "Hello, World!"
NODE0: RECEIVED "Hello, World!"
NODE1: SENDING "Goodbye."
NODE0: RECEIVED "Goodbye."
Request/Reply (我问,你答)
请求/应答用于同步通信,其中每个问题都有一个答案,例如远程过程调用(RPC)。
与Pipeline一样,它也可以执行负载平衡。
这是套件中唯一可靠的消息传递模式,因为如果请求与响应不匹配,它会自动重试。
reqprep.cpp
#define _CRT_SECURE_NO_WARNINGS true
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <nng/nng.h>
#include <nng/protocol/reqrep0/rep.h>
#include <nng/protocol/reqrep0/req.h>
#define NODE0 "node0"
#define NODE1 "node1"
char DATE[] = "DATE";
void
fatal(const char* func, int rv)
{
fprintf(stderr, "%s: %s\n", func, nng_strerror(rv));
exit(1);
}
char*
date(void)
{
time_t now = time(&now);
struct tm* info = localtime(&now);
char* text = asctime(info);
text[strlen(text) - 1] = '\0'; // remove '\n'
return (text);
}
int
node0(const char* url)
{
nng_socket sock;
int rv;
if ((rv = nng_rep0_open(&sock)) != 0) {
fatal("nng_rep0_open", rv);
}
if ((rv = nng_listen(sock, url, NULL, 0)) != 0) {
fatal("nng_listen", rv);
}
for (;;) {
char* buf = NULL;
size_t sz;
if ((rv = nng_recv(sock, &buf, &sz, NNG_FLAG_ALLOC)) != 0) {
fatal("nng_recv", rv);
}
if ((sz == (strlen(DATE) + 1)) && (strcmp(DATE, buf) == 0)) {
printf("NODE0: RECEIVED DATE REQUEST\n");
char* d = date();
printf("NODE0: SENDING DATE %s\n", d);
if ((rv = nng_send(sock, d, strlen(d) + 1, 0)) != 0) {
fatal("nng_send", rv);
}
}
nng_free(buf, sz);
}
}
int
node1(const char* url)
{
nng_socket sock;
int rv;
size_t sz;
char* buf = NULL;
if ((rv = nng_req0_open(&sock)) != 0) {
fatal("nng_socket", rv);
}
if ((rv = nng_dial(sock, url, NULL, 0)) != 0) {
fatal("nng_dial", rv);
}
printf("NODE1: SENDING DATE REQUEST %s\n", DATE);
if ((rv = nng_send(sock, DATE, strlen(DATE) + 1, 0)) != 0) {
fatal("nng_send", rv);
}
if ((rv = nng_recv(sock, &buf, &sz, NNG_FLAG_ALLOC)) != 0) {
fatal("nng_recv", rv);
}
printf("NODE1: RECEIVED DATE %s\n", buf);
nng_free(buf, sz);
nng_close(sock);
return (0);
}
int
main(const int argc, const char** argv)
{
if ((argc > 1) && (strcmp(NODE0, argv[1]) == 0))
return (node0(argv[2]));
if ((argc > 1) && (strcmp(NODE1, argv[1]) == 0))
return (node1(argv[2]));
fprintf(stderr, "Usage: reqrep %s|%s <URL> ...\n", NODE0, NODE1);
return (1);
}
执行
reqrep node0 ipc:///tmp/reqrep.ipc & node0=$! && sleep 1
reqrep node1 ipc:///tmp/reqrep.ipc
输出结果
NODE1: SENDING DATE REQUEST DATE
NODE1: RECEIVED DATE Tue Aug 27 14:10:23 2024
NODE0: RECEIVED DATE REQUEST
NODE0: SENDING DATE Tue Aug 27 09:47:29 2024
Pair (Two Way Radio)
一对一双向通信,类似对讲机。
当存在一对一的对等关系时,使用配对模式。一次只能有一个对等体连接到另一个对等方,但两个对等体都可以自由发言。
pair.cpp
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// #include <unistd.h>
#include <Windows.h>
#include <nng/nng.h>
#include <nng/protocol/pair0/pair.h>
char NODE0[] = "node0";
char NODE1[] = "node1";
void
fatal(const char* func, int rv)
{
fprintf(stderr, "%s: %s\n", func, nng_strerror(rv));
exit(1);
}
int
send_name(nng_socket sock, char* name)
{
int rv;
printf("%s: SENDING \"%s\"\n", name, name);
if ((rv = nng_send(sock, name, strlen(name) + 1, 0)) != 0) {
fatal("nng_send", rv);
}
return (rv);
}
int
recv_name(nng_socket sock, char* name)
{
char* buf = NULL;
int rv;
size_t sz;
if ((rv = nng_recv(sock, &buf, &sz, NNG_FLAG_ALLOC)) == 0) {
printf("%s: RECEIVED \"%s\"\n", name, buf);
nng_free(buf, sz);
}
return (rv);
}
int
send_recv(nng_socket sock, char* name)
{
int rv;
if ((rv = nng_setopt_ms(sock, NNG_OPT_RECVTIMEO, 100)) != 0) {
fatal("nng_setopt_ms", rv);
}
for (;;) {
recv_name(sock, name);
Sleep(1);
send_name(sock, name);
}
}
int
node0(const char* url)
{
nng_socket sock;
int rv;
if ((rv = nng_pair0_open(&sock)) != 0) {
fatal("nng_pair0_open", rv);
}
if ((rv = nng_listen(sock, url, NULL, 0)) != 0) {
fatal("nng_listen", rv);
}
return (send_recv(sock, NODE0));
}
int
node1(const char* url)
{
nng_socket sock;
int rv;
Sleep(1);
if ((rv = nng_pair0_open(&sock)) != 0) {
fatal("nng_pair0_open", rv);
}
if ((rv = nng_dial(sock, url, NULL, 0)) != 0) {
fatal("nng_dial", rv);
}
return (send_recv(sock, NODE1));
}
int
main(int argc, char** argv)
{
if ((argc > 1) && (strcmp(NODE0, argv[1]) == 0))
return (node0(argv[2]));
if ((argc > 1) && (strcmp(NODE1, argv[1]) == 0))
return (node1(argv[2]));
fprintf(stderr, "Usage: pair %s|%s <URL> <ARG> ...\n", NODE0, NODE1);
return 1;
}
这里假设用的字符串是ASCII字符。
执行
pair node0 ipc:///tmp/pair.ipc & node0=$!
pair node1 ipc:///tmp/pair.ipc & node1=$!
输出
Pub/Sub (Topics & Broadcast)
单向广播
pubsub.cpp
#define _CRT_SECURE_NO_WARNINGS true
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
//#include <unistd.h>
#include <Windows.h>
#include <nng/nng.h>
#include <nng/protocol/pubsub0/pub.h>
#include <nng/protocol/pubsub0/sub.h>
#define SERVER "server"
#define CLIENT "client"
void
fatal(const char* func, int rv)
{
fprintf(stderr, "%s: %s\n", func, nng_strerror(rv));
}
char*
date(void)
{
time_t now = time(&now);
struct tm* info = localtime(&now);
char* text = asctime(info);
text[strlen(text) - 1] = '\0'; // remove '\n'
return (text);
}
int
server(const char* url)
{
nng_socket sock;
int rv;
if ((rv = nng_pub0_open(&sock)) != 0) {
fatal("nng_pub0_open", rv);
}
if ((rv = nng_listen(sock, url, NULL, 0)) < 0) {
fatal("nng_listen", rv);
}
for (;;) {
char* d = date();
printf("SERVER: PUBLISHING DATE %s\n", d);
if ((rv = nng_send(sock, d, strlen(d) + 1, 0)) != 0) {
fatal("nng_send", rv);
}
Sleep(1);
}
}
int
client(const char* url, const char* name)
{
nng_socket sock;
int rv;
if ((rv = nng_sub0_open(&sock)) != 0) {
fatal("nng_sub0_open", rv);
}
// subscribe to everything (empty means all topics)
if ((rv = nng_setopt(sock, NNG_OPT_SUB_SUBSCRIBE, "", 0)) != 0) {
fatal("nng_setopt", rv);
}
if ((rv = nng_dial(sock, url, NULL, 0)) != 0) {
fatal("nng_dial", rv);
}
for (;;) {
char* buf = NULL;
size_t sz;
if ((rv = nng_recv(sock, &buf, &sz, NNG_FLAG_ALLOC)) != 0) {
fatal("nng_recv", rv);
}
printf("CLIENT (%s): RECEIVED %s\n", name, buf);
nng_free(buf, sz);
}
}
int
main(const int argc, const char** argv)
{
if ((argc >= 2) && (strcmp(SERVER, argv[1]) == 0))
return (server(argv[2]));
if ((argc >= 3) && (strcmp(CLIENT, argv[1]) == 0))
return (client(argv[2], argv[3]));
fprintf(stderr, "Usage: pubsub %s|%s <URL> <ARG> ...\n",
SERVER, CLIENT);
return 1;
}
执行
./pubsub server ipc:///tmp/pubsub.ipc & server=$! && sleep 1
./pubsub client ipc:///tmp/pubsub.ipc client0 & client0=$!
./pubsub client ipc:///tmp/pubsub.ipc client1 & client1=$!
./pubsub client ipc:///tmp/pubsub.ipc client2 & client2=$!
输出结果
SERVER: PUBLISHING DATE Sat Sep 7 17:40:11 2013
SERVER: PUBLISHING DATE Sat Sep 7 17:40:12 2013
SERVER: PUBLISHING DATE Sat Sep 7 17:40:13 2013
CLIENT (client2): RECEIVED Sat Sep 7 17:40:13 2013
CLIENT (client0): RECEIVED Sat Sep 7 17:40:13 2013
CLIENT (client1): RECEIVED Sat Sep 7 17:40:13 2013
SERVER: PUBLISHING DATE Sat Sep 7 17:40:14 2013
CLIENT (client2): RECEIVED Sat Sep 7 17:40:14 2013
CLIENT (client1): RECEIVED Sat Sep 7 17:40:14 2013
CLIENT (client0): RECEIVED Sat Sep 7 17:40:14 2013
SERVER: PUBLISHING DATE Sat Sep 7 17:40:15 2013
CLIENT (client1): RECEIVED Sat Sep 7 17:40:15 2013
CLIENT (client2): RECEIVED Sat Sep 7 17:40:15 2013
CLIENT (client0): RECEIVED Sat Sep 7 17:40:15 2013
SERVER: PUBLISHING DATE Sat Sep 7 17:40:16 2013
CLIENT (client1): RECEIVED Sat Sep 7 17:40:16 2013
CLIENT (client2): RECEIVED Sat Sep 7 17:40:16 2013
CLIENT (client0): RECEIVED Sat Sep 7 17:40:16 2013
Last updated 2018-02-15 00:01:10 UTC
Survey (Everybody Votes)
用于多节点表决或者服务发现。
survey.cpp
#define _CRT_SECURE_NO_WARNINGS true
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <Windows.h>
#include <nng/nng.h>
#include <nng/protocol/survey0/survey.h>
#include <nng/protocol/survey0/respond.h>
#define SERVER "server"
#define CLIENT "client"
#define DATE "DATE"
void
fatal(const char* func, int rv)
{
fprintf(stderr, "%s: %s\n", func, nng_strerror(rv));
exit(1);
}
char*
date(void)
{
time_t now = time(&now);
struct tm* info = localtime(&now);
char* text = asctime(info);
text[strlen(text) - 1] = '\0'; // remove '\n'
return (text);
}
int
server(const char* url)
{
nng_socket sock;
int rv;
if ((rv = nng_surveyor0_open(&sock)) != 0) {
fatal("nng_surveyor0_open", rv);
}
if ((rv = nng_listen(sock, url, NULL, 0)) != 0) {
fatal("nng_listen", rv);
}
for (;;) {
printf("SERVER: SENDING DATE SURVEY REQUEST\n");
if ((rv = nng_send(sock, (void *)DATE, strlen(DATE) + 1, 0)) != 0) {
fatal("nng_send", rv);
}
for (;;) {
char* buf = NULL;
size_t sz;
rv = nng_recv(sock, &buf, &sz, NNG_FLAG_ALLOC);
if (rv == NNG_ETIMEDOUT) {
break;
}
if (rv != 0) {
fatal("nng_recv", rv);
}
printf("SERVER: RECEIVED \"%s\" SURVEY RESPONSE\n",
buf);
nng_free(buf, sz);
}
printf("SERVER: SURVEY COMPLETE\n");
}
}
int
client(const char* url, const char* name)
{
nng_socket sock;
int rv;
if ((rv = nng_respondent0_open(&sock)) != 0) {
fatal("nng_respondent0_open", rv);
}
if ((rv = nng_dial(sock, url, NULL, NNG_FLAG_NONBLOCK)) != 0) {
fatal("nng_dial", rv);
}
for (;;) {
char* buf = NULL;
size_t sz;
if ((rv = nng_recv(sock, &buf, &sz, NNG_FLAG_ALLOC)) == 0) {
printf("CLIENT (%s): RECEIVED \"%s\" SURVEY REQUEST\n",
name, buf);
nng_free(buf, sz);
char* d = date();
printf("CLIENT (%s): SENDING DATE SURVEY RESPONSE\n",
name);
if ((rv = nng_send(sock, d, strlen(d) + 1, 0)) != 0) {
fatal("nng_send", rv);
}
}
}
}
int
main(const int argc, const char** argv)
{
if ((argc >= 2) && (strcmp(SERVER, argv[1]) == 0))
return (server(argv[2]));
if ((argc >= 3) && (strcmp(CLIENT, argv[1]) == 0))
return (client(argv[2], argv[3]));
fprintf(stderr, "Usage: survey %s|%s <URL> <ARG> ...\n",
SERVER, CLIENT);
return 1;
}
执行
urvey server ipc:///tmp/survey.ipc & server=$!
survey client ipc:///tmp/survey.ipc client0 & client0=$!
survey client ipc:///tmp/survey.ipc client1 & client1=$!
survey client ipc:///tmp/survey.ipc client2 & client2=$!
输出结果
SERVER: SENDING DATE SURVEY REQUEST
SERVER: SURVEY COMPLETE
SERVER: SENDING DATE SURVEY REQUEST
CLIENT (client2): RECEIVED "DATE" SURVEY REQUEST
CLIENT (client0): RECEIVED "DATE" SURVEY REQUEST
CLIENT (client1): RECEIVED "DATE" SURVEY REQUEST
CLIENT (client2): SENDING DATE SURVEY RESPONSE
CLIENT (client1): SENDING DATE SURVEY RESPONSE
CLIENT (client0): SENDING DATE SURVEY RESPONSE
SERVER: RECEIVED "Tue Jan 9 11:33:40 2018" SURVEY RESPONSE
SERVER: RECEIVED "Tue Jan 9 11:33:40 2018" SURVEY RESPONSE
SERVER: RECEIVED "Tue Jan 9 11:33:40 2018" SURVEY RESPONSE
SERVER: SURVEY COMPLETE
SERVER: SENDING DATE SURVEY REQUEST
CLIENT (client2): RECEIVED "DATE" SURVEY REQUEST
CLIENT (client2): SENDING DATE SURVEY RESPONSE
CLIENT (client0): RECEIVED "DATE" SURVEY REQUEST
CLIENT (client0): SENDING DATE SURVEY RESPONSE
CLIENT (client1): RECEIVED "DATE" SURVEY REQUEST
CLIENT (client1): SENDING DATE SURVEY RESPONSE
SERVER: RECEIVED "Tue Jan 9 11:33:41 2018" SURVEY RESPONSE
SERVER: RECEIVED "Tue Jan 9 11:33:41 2018" SURVEY RESPONSE
SERVER: RECEIVED "Tue Jan 9 11:33:41 2018" SURVEY RESPONSE
Bus (Routing)
网状连接通信,每个加入节点都可以发送/接受广播消息。
bus.cpp
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <Windows.h>
#include <nng/nng.h>
#include <nng/protocol/bus0/bus.h>
void
fatal(const char* func, int rv)
{
fprintf(stderr, "%s: %s\n", func, nng_strerror(rv));
exit(1);
}
int
node(int argc, char** argv)
{
nng_socket sock;
int rv;
size_t sz;
if ((rv = nng_bus0_open(&sock)) != 0) {
fatal("nng_bus0_open", rv);
}
if ((rv = nng_listen(sock, argv[2], NULL, 0)) != 0) {
fatal("nng_listen", rv);
}
Sleep(3000); // wait for peers to bind
if (argc >= 3) {
for (int x = 3; x < argc; x++) {
if ((rv = nng_dial(sock, argv[x], NULL, 0)) != 0) {
fatal("nng_dial", rv);
}
}
}
Sleep(1000); // wait for connects to establish
// SEND
sz = strlen(argv[1]) + 1; // '\0' too
printf("%s: SENDING '%s' ONTO BUS\n", argv[1], argv[1]);
if ((rv = nng_send(sock, argv[1], sz, 0)) != 0) {
fatal("nng_send", rv);
}
// RECV
for (;;) {
char* buf = NULL;
size_t sz;
if ((rv = nng_recv(sock, &buf, &sz, NNG_FLAG_ALLOC)) != 0) {
if (rv == NNG_ETIMEDOUT) {
fatal("nng_recv", rv);
}
}
printf("%s: RECEIVED '%s' FROM BUS\n", argv[1], buf);
nng_free(buf, sz);
}
nng_close(sock);
return (0);
}
int
main(int argc, char** argv)
{
if (argc >= 3) {
return (node(argc, argv));
}
fprintf(stderr, "Usage: bus <NODE_NAME> <URL> <URL> ...\n");
return 1;
}
执行
bus node0 ipc:///tmp/node0.ipc ipc:///tmp/node1.ipc ipc:///tmp/node2.ipc & node0=$!
bus node1 ipc:///tmp/node1.ipc ipc:///tmp/node2.ipc ipc:///tmp/node3.ipc & node1=$!
bus node2 ipc:///tmp/node2.ipc ipc:///tmp/node3.ipc & node2=$!
bus node3 ipc:///tmp/node3.ipc ipc:///tmp/node0.ipc & node3=$!
输出
node0: SENDING 'node0' ONTO BUS
node1: SENDING 'node1' ONTO BUS
node2: SENDING 'node2' ONTO BUS
node3: SENDING 'node3' ONTO BUS
node0: RECEIVED 'node1' FROM BUS
node0: RECEIVED 'node2' FROM BUS
node0: RECEIVED 'node3' FROM BUS
node1: RECEIVED 'node0' FROM BUS
node1: RECEIVED 'node2' FROM BUS
node1: RECEIVED 'node3' FROM BUS
node2: RECEIVED 'node0' FROM BUS
node2: RECEIVED 'node1' FROM BUS
node2: RECEIVED 'node3' FROM BUS
node3: RECEIVED 'node0' FROM BUS
node3: RECEIVED 'node1' FROM BUS
node3: RECEIVED 'node2' FROM BUS
相关链接
https://nanomsg.org/gettingstarted/nng/index.html
https://www.codetds.com/article/14592458