From 07e35d14230a75e7650f838c534c141de5401f09 Mon Sep 17 00:00:00 2001
From: wangzhengquan <wangzhengquan85@126.com>
Date: 星期二, 09 六月 2020 17:41:27 +0800
Subject: [PATCH] update

---
 test/nng/pubsub.c           |   91 +++++++++
 test/nng/reqrep             |    0 
 test/nng/survey             |    0 
 device/include/logintable.h |    0 
 test/nng/bus.c              |   80 ++++++++
 test/nng/survey.c           |  118 +++++++++++
 test/nng/reqrep.c           |  106 ++++++++++
 test/nng/pipeline.c         |   82 ++++++++
 device/hcnetdisk.c          |    0 
 device/include/hcnetdisk.h  |    0 
 test/nng/pair.c             |  104 ++++++++++
 test/nng/pipeline           |    0 
 device/include/netdisk.h    |    0 
 test/nng/bus                |    0 
 device/test                 |    0 
 test/Makefile               |    2 
 device/netdisk.c            |    0 
 device/test.c               |    0 
 test/nng/pair               |    0 
 device/data/login.dat       |    0 
 device/Makefile             |    0 
 test/nng/pubsub             |    0 
 22 files changed, 582 insertions(+), 1 deletions(-)

diff --git a/netdisk/Makefile b/device/Makefile
similarity index 100%
rename from netdisk/Makefile
rename to device/Makefile
diff --git a/netdisk/data/login.dat b/device/data/login.dat
similarity index 100%
rename from netdisk/data/login.dat
rename to device/data/login.dat
diff --git a/netdisk/hcnetdisk.c b/device/hcnetdisk.c
similarity index 100%
rename from netdisk/hcnetdisk.c
rename to device/hcnetdisk.c
diff --git a/netdisk/include/hcnetdisk.h b/device/include/hcnetdisk.h
similarity index 100%
rename from netdisk/include/hcnetdisk.h
rename to device/include/hcnetdisk.h
diff --git a/netdisk/include/logintable.h b/device/include/logintable.h
similarity index 100%
rename from netdisk/include/logintable.h
rename to device/include/logintable.h
diff --git a/netdisk/include/netdisk.h b/device/include/netdisk.h
similarity index 100%
rename from netdisk/include/netdisk.h
rename to device/include/netdisk.h
diff --git a/netdisk/netdisk.c b/device/netdisk.c
similarity index 100%
rename from netdisk/netdisk.c
rename to device/netdisk.c
diff --git a/netdisk/test b/device/test
similarity index 100%
rename from netdisk/test
rename to device/test
Binary files differ
diff --git a/netdisk/test.c b/device/test.c
similarity index 100%
rename from netdisk/test.c
rename to device/test.c
diff --git a/test/Makefile b/test/Makefile
index 5a35da1..be2e246 100755
--- a/test/Makefile
+++ b/test/Makefile
@@ -5,7 +5,7 @@
 LDLIBS+=-Wl,-rpath=../hclib:../hclib/HCNetSDKCom:../common
 LDDIR += -L$(ROOT)/hcnetdisk_wrapper -L../hclib -L../hclib/HCNetSDKCom
 LDLIBS +=  -lhcnetsdk -lhpr -lHCCore 
-LDLIBS += -lpthread  -ljsoncpp
+LDLIBS += -lpthread  -ljsoncpp -lnanomsg
 LIB_NETDISK = libnetdisk.a
 DLIB_NETDISK = libnetdisk.so
 PLATFORM=$(shell $(ROOT)/systype.sh)
diff --git a/test/nng/bus b/test/nng/bus
new file mode 100755
index 0000000..56ab3c4
--- /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..3403814
--- /dev/null
+++ b/test/nng/bus.c
@@ -0,0 +1,80 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <nanomsg/nn.h>
+#include <nanomsg/bus.h>
+
+void
+fatal(const char *func)
+{
+  fprintf(stderr, "%s: %s\n", func, nn_strerror(nn_errno()));
+  exit(1);
+}
+
+int
+node(const int argc, const char **argv)
+{
+  int sock;
+
+  if ((sock = nn_socket (AF_SP, NN_BUS)) < 0)
+  {
+    fatal("nn_socket");
+  }
+  if (nn_bind(sock, argv[2]) < 0)
+  {
+    fatal("nn_bind");
+  }
+
+  sleep(1); // wait for peers to bind
+  if (argc >= 3)
+  {
+    for (int x = 3; x < argc; x++)
+    {
+      if (nn_connect(sock, argv[x]) < 0)
+      {
+        fatal("nn_connect");
+      }
+    }
+  }
+  sleep(1); // wait for connections
+  int to = 100;
+  if (nn_setsockopt(sock, NN_SOL_SOCKET, NN_RCVTIMEO, &to,
+                    sizeof (to)) < 0)
+  {
+    fatal("nn_setsockopt");
+  }
+
+  // SEND
+  int sz_n = strlen(argv[1]) + 1; // '\0' too
+  printf("%s: SENDING '%s' ONTO BUS\n", argv[1], argv[1]);
+  if (nn_send(sock, argv[1], sz_n, 0) < 0)
+  {
+    fatal("nn_send");
+  }
+
+  // RECV
+  for (;;)
+  {
+    char *buf = NULL;
+    int recv = nn_recv(sock, &buf, NN_MSG, 0);
+    if (recv >= 0)
+    {
+      printf("%s: RECEIVED '%s' FROM BUS\n", argv[1], buf);
+      nn_freemsg(buf);
+    }
+  }
+  return (nn_shutdown(sock, 0));
+}
+
+int
+main(int argc, const char **argv)
+{
+  if (argc >= 3)
+  {
+    return (node(argc, argv));
+  }
+  fprintf(stderr, "Usage: bus <NODE_NAME> <URL> <URL> ...\n");
+  return 1;
+}
diff --git a/test/nng/pair b/test/nng/pair
new file mode 100755
index 0000000..6cd4091
--- /dev/null
+++ b/test/nng/pair
Binary files differ
diff --git a/test/nng/pair.c b/test/nng/pair.c
new file mode 100644
index 0000000..faa4416
--- /dev/null
+++ b/test/nng/pair.c
@@ -0,0 +1,104 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <nanomsg/nn.h>
+#include <nanomsg/pair.h>
+
+#define NODE0 "node0"
+#define NODE1 "node1"
+
+void
+fatal(const char *func)
+{
+        fprintf(stderr, "%s: %s\n", func, nn_strerror(nn_errno()));
+        exit(1);
+}
+
+int
+send_name(int sock, const char *name)
+{
+        printf("%s: SENDING \"%s\"\n", name, name);
+        int sz_n = strlen(name) + 1; // '\0' too
+        return (nn_send(sock, name, sz_n, 0));
+}
+
+int
+recv_name(int sock, const char *name)
+{
+        char *buf = NULL;
+        int result = nn_recv(sock, &buf, NN_MSG, 0);
+        if (result > 0) {
+                printf("%s: RECEIVED \"%s\"\n", name, buf); 
+                nn_freemsg(buf);
+        }
+        return (result);
+}
+
+int
+send_recv(int sock, const char *name)
+{
+        int to = 100;
+        if (nn_setsockopt(sock, NN_SOL_SOCKET, NN_RCVTIMEO, &to, sizeof (to)) < 0) {
+                fatal("nn_setsockopt");
+        }
+
+        for (;;) {
+                recv_name(sock, name);
+                sleep(1);
+                send_name(sock, name);
+        }
+}
+
+int
+node0(const char *url)
+{
+        int sock;
+        if ((sock = nn_socket(AF_SP, NN_PAIR)) < 0) {
+                fatal("nn_socket");
+        }
+         if (nn_bind(sock, url) < 0) {
+                fatal("nn_bind");
+        }
+        return (send_recv(sock, NODE0));
+}
+
+int
+node1(const char *url)
+{
+        int sock;
+        if ((sock = nn_socket(AF_SP, NN_PAIR)) < 0) {
+                fatal("nn_socket");
+        }
+        if (nn_connect(sock, url) < 0) {
+                fatal("nn_connect");
+        }
+        return (send_recv(sock, NODE1));
+}
+
+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: pair %s|%s <URL> <ARG> ...\n", NODE0, NODE1);
+        return 1;
+}
+
+/**
+
+
+Compilation
+gcc pair.c -lnanomsg -o pair
+
+Execution
+./pair node0 ipc:///tmp/pair.ipc & node0=$!
+./pair node1 ipc:///tmp/pair.ipc & node1=$!
+sleep 3
+kill $node0 $node1
+*/
\ No newline at end of file
diff --git a/test/nng/pipeline b/test/nng/pipeline
new file mode 100755
index 0000000..6504afb
--- /dev/null
+++ b/test/nng/pipeline
Binary files differ
diff --git a/test/nng/pipeline.c b/test/nng/pipeline.c
new file mode 100644
index 0000000..730e5c8
--- /dev/null
+++ b/test/nng/pipeline.c
@@ -0,0 +1,82 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+#include <nanomsg/nn.h>
+#include <nanomsg/pipeline.h>
+
+#define NODE0 "node0"
+#define NODE1 "node1"
+
+void
+fatal(const char *func)
+{
+        fprintf(stderr, "%s: %s\n", func, nn_strerror(nn_errno()));
+        exit(1);
+}
+
+int
+node0(const char *url)
+{
+        int sock;
+        int rv;
+
+        if ((sock = nn_socket(AF_SP, NN_PULL)) < 0) {
+                fatal("nn_socket");
+        }
+        if ((rv = nn_bind(sock, url)) < 0) {
+                fatal("nn_bind");
+        }
+        for (;;) {
+                char *buf = NULL;
+                int bytes;
+                if ((bytes = nn_recv(sock, &buf, NN_MSG, 0)) < 0) {
+                        fatal("nn_recv");
+                }
+                printf("NODE0: RECEIVED \"%s\"\n", buf); 
+                nn_freemsg(buf);
+        }
+}
+
+int
+node1(const char *url, const char *msg)
+{
+        int sz_msg = strlen(msg) + 1; // '\0' too
+        int sock;
+        int rv;
+        int bytes;
+
+        if ((sock = nn_socket(AF_SP, NN_PUSH)) < 0) {
+                fatal("nn_socket");
+        }
+        if ((rv = nn_connect(sock, url)) < 0) {
+                fatal("nn_connect");
+        }
+        printf("NODE1: SENDING \"%s\"\n", msg);
+        if ((bytes = nn_send(sock, msg, sz_msg, 0)) < 0) {
+                fatal("nn_send");
+        }
+        sleep(1); // wait for messages to flush before shutting down
+        return (nn_shutdown(sock, 0));
+}
+
+int
+main(const int argc, const 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."
+kill $node0
+*/
\ No newline at end of file
diff --git a/test/nng/pubsub b/test/nng/pubsub
new file mode 100755
index 0000000..11b7616
--- /dev/null
+++ b/test/nng/pubsub
Binary files differ
diff --git a/test/nng/pubsub.c b/test/nng/pubsub.c
new file mode 100644
index 0000000..a8f2702
--- /dev/null
+++ b/test/nng/pubsub.c
@@ -0,0 +1,91 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+
+#include <nanomsg/nn.h>
+#include <nanomsg/pubsub.h>
+
+#define SERVER "server"
+#define CLIENT "client"
+
+void
+fatal(const char *func)
+{
+        fprintf(stderr, "%s: %s\n", func, nn_strerror(nn_errno()));
+}
+
+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)
+{
+        int sock;
+
+        if ((sock = nn_socket(AF_SP, NN_PUB)) < 0) {
+                fatal("nn_socket");
+        }
+          if (nn_bind(sock, url) < 0) {
+                fatal("nn_bind");
+        }
+        for (;;) {
+                char *d = date();
+                int sz_d = strlen(d) + 1; // '\0' too
+                printf("SERVER: PUBLISHING DATE %s\n", d);
+                int bytes = nn_send(sock, d, sz_d, 0);
+                if (bytes < 0) {
+                        fatal("nn_send");
+                }
+                sleep(1);
+        }
+}
+
+int
+client(const char *url, const char *name)
+{
+        int sock;
+
+        if ((sock = nn_socket(AF_SP, NN_SUB)) < 0) {
+                fatal("nn_socket");
+        }
+
+        // subscribe to everything ("" means all topics)
+        if (nn_setsockopt(sock, NN_SUB, NN_SUB_SUBSCRIBE, "", 0) < 0) {
+                fatal("nn_setsockopt");
+        }
+        if (nn_connect(sock, url) < 0) {
+                fatal("nn_connet");
+        }
+        for (;;) {
+                char *buf = NULL;
+                int bytes = nn_recv(sock, &buf, NN_MSG, 0);
+                if (bytes < 0) {
+                        fatal("nn_recv");
+                }
+                printf("CLIENT (%s): RECEIVED %s\n", name, buf); 
+                nn_freemsg(buf);
+        }
+}
+
+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;
+}
\ No newline at end of file
diff --git a/test/nng/reqrep b/test/nng/reqrep
new file mode 100755
index 0000000..ff3e569
--- /dev/null
+++ b/test/nng/reqrep
Binary files differ
diff --git a/test/nng/reqrep.c b/test/nng/reqrep.c
new file mode 100644
index 0000000..e6f17c7
--- /dev/null
+++ b/test/nng/reqrep.c
@@ -0,0 +1,106 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <time.h>
+#include <nanomsg/nn.h>
+#include <nanomsg/reqrep.h>
+
+#define NODE0 "node0"
+#define NODE1 "node1"
+#define DATE "DATE"
+
+void
+fatal(const char *func)
+{
+        fprintf(stderr, "%s: %s\n", func, nn_strerror(nn_errno()));
+        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)
+{
+        int sz_date = strlen(DATE) + 1; // '\0' too
+        int sock;
+        int rv;
+
+        if ((sock = nn_socket(AF_SP, NN_REP)) < 0) {
+                fatal("nn_socket");
+        }
+          if ((rv = nn_bind(sock, url)) < 0) {
+                fatal("nn_bind");
+        }
+        for (;;) {
+                char *buf = NULL;
+                int bytes;
+                if ((bytes = nn_recv(sock, &buf, NN_MSG, 0)) < 0) {
+                        fatal("nn_recv");
+                }
+                if ((bytes == (strlen(DATE) + 1)) && (strcmp(DATE, buf) == 0)) {
+                        printf("NODE0: RECEIVED DATE REQUEST\n");
+                        char *d = date();
+                        int sz_d = strlen(d) + 1; // '\0' too
+                        printf("NODE0: SENDING DATE %s\n", d);
+                        if ((bytes = nn_send(sock, d, sz_d, 0)) < 0) {
+                                fatal("nn_send");
+                        }
+                }
+                nn_freemsg(buf);
+        }
+}
+
+int
+node1(const char *url)
+{
+        int sz_date = strlen(DATE) + 1; // '\0' too
+        char *buf = NULL;
+        int bytes = -1;
+        int sock;
+        int rv;
+
+        if ((sock = nn_socket(AF_SP, NN_REQ)) < 0) {
+                fatal("nn_socket");
+        }
+        if ((rv = nn_connect (sock, url)) < 0) {
+                fatal("nn_connect");
+        }
+        printf("NODE1: SENDING DATE REQUEST %s\n", DATE);
+        if ((bytes = nn_send(sock, DATE, sz_date, 0)) < 0) {
+                fatal("nn_send");
+        }
+        if ((bytes = nn_recv(sock, &buf, NN_MSG, 0)) < 0) {
+                fatal("nn_recv");
+        }
+        printf("NODE1: RECEIVED DATE %s\n", buf);  
+        nn_freemsg(buf);
+        return (nn_shutdown(sock, 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
+kill $node0
+*/
\ No newline at end of file
diff --git a/test/nng/survey b/test/nng/survey
new file mode 100755
index 0000000..9de5e0d
--- /dev/null
+++ b/test/nng/survey
Binary files differ
diff --git a/test/nng/survey.c b/test/nng/survey.c
new file mode 100644
index 0000000..d116020
--- /dev/null
+++ b/test/nng/survey.c
@@ -0,0 +1,118 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+
+#include <nanomsg/nn.h>
+#include <nanomsg/survey.h>
+
+#define SERVER "server"
+#define CLIENT "client"
+#define DATE   "DATE"
+
+void
+fatal(const char *func)
+{
+  fprintf(stderr, "%s: %s\n", func, nn_strerror(nn_errno()));
+  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)
+{
+  int sock;
+
+  if ((sock = nn_socket (AF_SP, NN_SURVEYOR)) < 0)
+  {
+    fatal("nn_socket");
+  }
+  if (nn_bind(sock, url)  < 0)
+  {
+    fatal("nn_bind");
+  }
+  for (;;)
+  {
+    printf("SERVER: SENDING DATE SURVEY REQUEST\n");
+    int bytes = nn_send(sock, DATE, strlen(DATE) + 1, 0);
+    if (bytes < 0)
+    {
+      fatal("nn_send");
+    }
+
+    for (;;)
+    {
+      char *buf = NULL;
+      int bytes = nn_recv(sock, &buf, NN_MSG, 0);
+      if (bytes < 0)
+      {
+        if (nn_errno() == ETIMEDOUT)
+        {
+          break;
+        }
+        fatal("nn_recv");
+      }
+      printf("SERVER: RECEIVED \"%s\" SURVEY RESPONSE\n", buf);
+      nn_freemsg(buf);
+    }
+
+    printf("SERVER: SURVEY COMPLETE\n");
+    sleep(1); // Start another survey in a second
+  }
+}
+
+int
+client(const char *url, const char *name)
+{
+  int sock;
+
+  if ((sock = nn_socket(AF_SP, NN_RESPONDENT)) < 0)
+  {
+    fatal("nn_socket");
+  }
+  if (nn_connect (sock, url) < 0)
+  {
+    fatal("nn_connect");
+  }
+  for (;;)
+  {
+    char *buf = NULL;
+    int bytes = nn_recv(sock, &buf, NN_MSG, 0);
+    if (bytes >= 0)
+    {
+      printf("CLIENT (%s): RECEIVED \"%s\" SURVEY REQUEST\n", name, buf);
+      nn_freemsg(buf);
+      char *d = date();
+      int sz_d = strlen(d) + 1; // '\0' too
+      printf("CLIENT (%s): SENDING DATE SURVEY RESPONSE\n", name);
+      if (nn_send(sock, d, sz_d, 0) < 0)
+      {
+        fatal("nn_send");
+      }
+    }
+  }
+}
+
+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;
+}

--
Gitblit v1.8.0