From 591aacee97f4a6486631c38a6b418e20b2c4109c Mon Sep 17 00:00:00 2001 From: wangzhengquan <wangzhengquan85@126.com> Date: 星期四, 10 九月 2020 14:56:47 +0800 Subject: [PATCH] update --- .gitignore | 1 test/nng/core | 0 test/nng/bus.c | 74 ++++++++++++ device/libnetdisk.so | 0 service/netdisk_service | 0 test/nng/新建文本文档.txt | 103 +++++++++++++++++ test/nng/bus | 0 test/nng/server.c | 71 +++++++++++ test/nng/bus.sh | 7 + test/Makefile | 2 test/nng/server | 0 device/libnetdisk.a | 0 test/nng/client.c | 71 +++++++++++ test/nng/client | 0 test/nng/pubsub | 0 15 files changed, 327 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 59e0058..6c02413 100644 --- a/.gitignore +++ b/.gitignore @@ -22,7 +22,6 @@ # Compiled Static libraries *.lai *.la -*.lib # Executables *.exe diff --git a/device/libnetdisk.a b/device/libnetdisk.a index 26feba9..53c4d53 100644 --- a/device/libnetdisk.a +++ b/device/libnetdisk.a Binary files differ diff --git a/device/libnetdisk.so b/device/libnetdisk.so index c92a81a..d2801cf 100755 --- a/device/libnetdisk.so +++ b/device/libnetdisk.so Binary files differ diff --git a/service/netdisk_service b/service/netdisk_service index f7f8b76..32878a5 100755 --- a/service/netdisk_service +++ b/service/netdisk_service Binary files differ diff --git a/test/Makefile b/test/Makefile index 7dafde0..ea865cf 100755 --- a/test/Makefile +++ b/test/Makefile @@ -19,7 +19,7 @@ include $(ROOT)/Make.defines.$(PLATFORM) -PROGS = nng/pubsub +PROGS = nng/server nng/client build: $(PROGS) diff --git a/test/nng/bus b/test/nng/bus new file mode 100755 index 0000000..55fd19f --- /dev/null +++ b/test/nng/bus Binary files differ diff --git a/test/nng/bus.c b/test/nng/bus.c new file mode 100644 index 0000000..d239efb --- /dev/null +++ b/test/nng/bus.c @@ -0,0 +1,74 @@ + + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.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(1); // 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(1); // 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; +} \ No newline at end of file diff --git a/test/nng/bus.sh b/test/nng/bus.sh new file mode 100755 index 0000000..51aac1e --- /dev/null +++ b/test/nng/bus.sh @@ -0,0 +1,7 @@ +./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 node1 ipc:///tmp/node1.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=$! +sleep 5 +kill $node0 $node1 $node2 $node3 \ No newline at end of file diff --git a/test/nng/client b/test/nng/client new file mode 100755 index 0000000..40a2908 --- /dev/null +++ b/test/nng/client Binary files differ diff --git a/test/nng/client.c b/test/nng/client.c new file mode 100644 index 0000000..2a50790 --- /dev/null +++ b/test/nng/client.c @@ -0,0 +1,71 @@ +#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" +#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 +client(const char *url) +{ + nng_socket sock; + int rv; + size_t sz; + char * buf; + char sendbuf[512]; + + 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); + while(true) { + printf("say something:\n"); + scanf("%s", sendbuf); + if ((rv = nng_send(sock, sendbuf, strlen(sendbuf)+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) +{ + + char *url = "ipc:///tmp/reqrep.ipc"; + client(url); +} \ No newline at end of file diff --git a/test/nng/core b/test/nng/core new file mode 100644 index 0000000..4ec6d6a --- /dev/null +++ b/test/nng/core Binary files differ diff --git a/test/nng/pubsub b/test/nng/pubsub index 76667af..6662c11 100755 --- a/test/nng/pubsub +++ b/test/nng/pubsub Binary files differ diff --git a/test/nng/server b/test/nng/server new file mode 100755 index 0000000..3820aad --- /dev/null +++ b/test/nng/server Binary files differ diff --git a/test/nng/server.c b/test/nng/server.c new file mode 100644 index 0000000..f7f878b --- /dev/null +++ b/test/nng/server.c @@ -0,0 +1,71 @@ +#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" +#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_rep0_open(&sock)) != 0) { + fatal("nng_rep0_open", rv); + } + if ((rv = nng_listen(sock, url, NULL, 0)) != 0) { + fatal("nng_listen", rv); + } + char sendbuf[512]; + 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); + char *d = date(); + sprintf(sendbuf, "%s: %s", d, buf); + printf("NODE0: SENDING DATE %s\n", d); + if ((rv = nng_send(sock, sendbuf, strlen(sendbuf) + 1, 0)) != 0) { + fatal("nng_send", rv); + } + nng_free(buf, sz); + } +} + + + +int +main(const int argc, const char **argv) +{ + + char *url = "ipc:///tmp/reqrep.ipc"; + server(url); +} \ No newline at end of file diff --git "a/test/nng/\346\226\260\345\273\272\346\226\207\346\234\254\346\226\207\346\241\243.txt" "b/test/nng/\346\226\260\345\273\272\346\226\207\346\234\254\346\226\207\346\241\243.txt" new file mode 100755 index 0000000..04510c9 --- /dev/null +++ "b/test/nng/\346\226\260\345\273\272\346\226\207\346\234\254\346\226\207\346\241\243.txt" @@ -0,0 +1,103 @@ +#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" +#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 +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 \ No newline at end of file -- Gitblit v1.8.0