12个文件已添加
9 文件已重命名
1个文件已修改
| | |
| | | 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) |
New file |
| | |
| | | #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; |
| | | } |
New file |
| | |
| | | #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 |
| | | */ |
New file |
| | |
| | | #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 |
| | | */ |
New file |
| | |
| | | #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; |
| | | } |
New file |
| | |
| | | #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 |
| | | */ |
New file |
| | |
| | | #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; |
| | | } |