本文主要是介绍使用nanomsg实现多子节点时序or数据同步,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
前言
本文通过简绍nanomsg基本通信模式结构,并采用PIPELINE、PUBSUB模式相互配合的方式来实现多子节点时序or数据同步;设计中使用PIPELINE模式用来采集子节点的同步时序或同步数据,如果子节点都同步之后使用PUBSUB模式群发订阅消息发送事件来通知子节点。
设计结构图
-
PIPELINE模式
聚合来自多个来源的消息,并在多个目的地之间进行负载平衡。node0只能send,node1只能recv。
- PUBSUB 模式
发布者向订阅者推送消息。只有订阅该频道的订阅者才能收到该频道的消息。
- 多子节点时序or数据同步设计图
实现代码
/*% ./test_demo tcp://127.0.0.1:5555 -s &% ./test_demo tcp://127.0.0.1:5555 Garrett% ./test_demo tcp://127.0.0.1:5555 Silent_DXX
*/#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <thread>#include <nanomsg/nn.h>
#include <nanomsg/pipeline.h>
#include <nanomsg/pubsub.h>void fatal(const char *func) {std::cout << func << ":" << nn_strerror(nn_errno()) << std::endl;exit(1);
}/* The server runs forever. */
int server(const char *url)
{int fd[2];int rc;/* Create the socket 0. */fd[0] = nn_socket (AF_SP, NN_PULL);if (fd[0] < 0) {fprintf (stderr, "nn_socket: %s\n", nn_strerror (nn_errno ()));return (-1);}if (nn_bind (fd[0], url) < 0) {fprintf (stderr, "nn_bind: %s\n", nn_strerror (nn_errno ()));nn_close (fd[0]);return (-1);}/* Create the socket 1. */fd[1] = nn_socket (AF_SP, NN_PUB);if (fd[1] < 0) {fprintf (stderr, "nn_socket: %s\n", nn_strerror (nn_errno ()));return (-1);}char pub_url[128];memcpy(pub_url, url, strlen(url) + 1);pub_url[strlen(pub_url) - 1] += 1;if (nn_bind (fd[1], pub_url) < 0) {fprintf (stderr, "nn_bind: %s\n", nn_strerror (nn_errno ()));nn_close (fd[1]);return (-1);}printf("Wait for clinet conneted...");getchar();static int sync_flag = 0;for (;;) {uint32_t* buf = NULL;int bytes;if ((bytes = nn_recv(fd[0], &buf, NN_MSG, 0)) < 0) {fatal("nn_recv");}printf("NODE0: RECEIVED \"%d\"\n", *(uint32_t*)buf);nn_freemsg(buf);sync_flag++;uint32_t subs;uint32_t* msg;uint32_t rev_flag = 0x55aa;msg = &rev_flag;subs = (uint32_t) nn_get_statistic (fd[1], NN_STAT_CURRENT_CONNECTIONS);if (sync_flag >= subs) {sync_flag = 0;printf("Send event, subs: %d\n", subs);rc = nn_send (fd[1], msg, sizeof (uint32_t), 0);if (rc < 0) {fprintf (stderr, "nn_send: %s (ignoring)\n", nn_strerror (nn_errno ()));}}}nn_close (fd[0]);return (-1);
}/* The client runs just once, and then returns. */
int client (const char *url, const char *username)
{int fd[2];int rc;int bytes;uint32_t *msg;uint32_t time = 0;msg = &time;/* Create the socket 0. */if ((fd[0] = nn_socket(AF_SP, NN_PUSH)) < 0) {fatal("nn_socket");}if ((rc = nn_connect(fd[0], url)) < 0) {fatal("nn_connect");}/* Create the socket 1. */fd[1] = nn_socket (AF_SP, NN_SUB);if (fd[1] < 0) {fprintf (stderr, "nn_socket: %s\n", nn_strerror (nn_errno ()));return (-1);}char pub_url[128];memcpy(pub_url, url, strlen(url) + 1);pub_url[strlen(pub_url) - 1] += 1;if (nn_connect (fd[1], pub_url) < 0) {fprintf (stderr, "nn_socket: %s\n", nn_strerror (nn_errno ()));nn_close (fd[1]);return (-1);}/* We want all messages, so just subscribe to the empty value. */if (nn_setsockopt (fd[1], NN_SUB, NN_SUB_SUBSCRIBE, "", 0) < 0) {fprintf (stderr, "nn_setsockopt: %s\n", nn_strerror (nn_errno ()));nn_close (fd[1]);return (-1);}for (;;) {printf("%s : SENDING \"%d\"\n", __func__, time);if ((bytes = nn_send(fd[0], msg, sizeof(uint32_t), 0)) < 0) {fatal("nn_send");}time++;uint8_t msg[2 * sizeof (uint32_t)];rc = nn_recv (fd[1], msg, sizeof (msg), 0);printf("Event: 0x%x\n", *(uint32_t*)&msg);if (rc < 0) {fprintf (stderr, "nn_recv: %s\n", nn_strerror (nn_errno ()));break;}sleep(2);}return 0;
}int main (int argc, char **argv)
{int rc;if (argc < 3) {fprintf (stderr, "Usage: %s <url> [-s|name]\n", argv[0]);exit (EXIT_FAILURE);}if (strcmp (argv[2], "-s") == 0) {rc = server (argv[1]);} else {rc = client (argv[1], argv[2]);}exit (rc == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
}
测试
- 启动服务端
./test_demo.exe tcp://127.0.0.1:5555 -s
- 启动一个节点
./test_demo.exe tcp://127.0.0.1:5555 Garrett1
- 启动第二个节点
./test_demo.exe tcp://127.0.0.1:5555 Garrett2
- 测试输出
服务器:
$ ./test_demo.exe tcp://127.0.0.1:5555 -s
Wait for clinet conneted...
NODE0: RECEIVED "0"
NODE0: RECEIVED "0"
Send event, subs: 2
NODE0: RECEIVED "1"
NODE0: RECEIVED "1"
Send event, subs: 2
NODE0: RECEIVED "2"
NODE0: RECEIVED "2"
Send event, subs: 2
NODE0: RECEIVED "3"
NODE0: RECEIVED "3"
Send event, subs: 2
NODE0: RECEIVED "4"
NODE0: RECEIVED "4"
Send event, subs: 2
NODE0: RECEIVED "5"
NODE0: RECEIVED "5"
Send event, subs: 2
NODE0: RECEIVED "6"
NODE0: RECEIVED "6"
Send event, subs: 2
NODE0: RECEIVED "7"
NODE0: RECEIVED "7"
Send event, subs: 2
节点:
$ ./test_demo.exe tcp://127.0.0.1:5555 Garrett1
client : SENDING "0"
Event: 0x55aa
client : SENDING "1"
Event: 0x55aa
client : SENDING "2"
Event: 0x55aa
client : SENDING "3"
Event: 0x55aa
client : SENDING "4"
Event: 0x55aa
client : SENDING "5"
Event: 0x55aa
client : SENDING "6"
Event: 0x55aa
client : SENDING "7"
Event: 0x55aa
这篇关于使用nanomsg实现多子节点时序or数据同步的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!