From 80e6c7cfdbab45985cb7b8686766af52c896e80f Mon Sep 17 00:00:00 2001 From: wangzhengquan <wangzhengquan85@126.com> Date: 星期六, 13 六月 2020 15:32:56 +0800 Subject: [PATCH] update --- test/nng/pubsub.c | 92 ++++++++++++++++++++++++++++++ service/Makefile | 7 +- test/Makefile | 26 +++++--- Make.defines.linux | 2 4 files changed, 114 insertions(+), 13 deletions(-) diff --git a/Make.defines.linux b/Make.defines.linux index 5aa3acf..a8a9bc6 100755 --- a/Make.defines.linux +++ b/Make.defines.linux @@ -18,7 +18,7 @@ # Common temp files to delete from each directory. -TEMPFILES=core core.* *.o temp.* *.out *.a *.so +TEMPFILES=core core.* *.o temp.* *.out *.a *.so $(PROGS) %: %.c $(LIBCOMMON) $(CC) $(CFLAGS) $(filter-out $(LIBCOMMON), $^) -o $@ $(LDFLAGS) $(LDLIBS) diff --git a/service/Makefile b/service/Makefile index 81cb258..67e9f73 100644 --- a/service/Makefile +++ b/service/Makefile @@ -14,15 +14,16 @@ LDLIBS += -ljsoncpp -lnng -lpthread INCLUDE += -I$(ROOT)/device/include -LIB_NETDISK = $(ROOT)/libnetdisk.a -DLIB_NETDISK = $(ROOT)/libnetdisk.so +#LIB_NETDISK = $(ROOT)/libnetdisk.a +#DLIB_NETDISK = $(ROOT)/libnetdisk.so PLATFORM=$(shell $(ROOT)/systype.sh) include $(ROOT)/Make.defines.$(PLATFORM) PROGS = netdisk_service test_client test test_queue test_properties -all: $(PROGS) + +build: $(PROGS) netdisk_service: $(ROOT)/device/hcnetdisk.c $(ROOT)/device/netdisk_factory.c login_store.c request_handler.c properties_config.c diff --git a/test/Makefile b/test/Makefile index c8fa824..a002692 100755 --- a/test/Makefile +++ b/test/Makefile @@ -2,22 +2,30 @@ # Makefile for common library. # ROOT=.. -LDLIBS+=-Wl,-rpath=../hclib:../hclib/HCNetSDKCom:../common -LDDIR += -L$(ROOT)/hcnetdisk_wrapper -L../hclib -L../hclib/HCNetSDKCom -LDLIBS += -lhcnetsdk -lhpr -lHCCore -LDLIBS += -ljsoncpp -lnng -lpthread -LIB_NETDISK = libnetdisk.a -DLIB_NETDISK = libnetdisk.so +LDLIBS+=-Wl,-rpath=$(ROOT)/lib/hc:$(ROOT)/lib/hc/HCNetSDKCom:$(ROOT)/common:$(ROOT)/lib/jsoncpp +# 娴峰悍鍖呰矾寰� +LDDIR += -L$(ROOT)/lib/hc -L$(ROOT)/lib/hc/HCNetSDKCom +# 寮�婧愬伐鍏峰寘璺緞 +LDDIR += -L$(ROOT)/lib/jsoncpp -L$(ROOT)/lib/nng + +# 娴峰悍鍖� +LDLIBS += -lhcnetsdk -lhpr -lHCCore +# 寮�婧愬伐鍏峰寘 +LDLIBS += -ljsoncpp -lnng -lpthread + +INCLUDE += -I$(ROOT)/device/include + PLATFORM=$(shell $(ROOT)/systype.sh) include $(ROOT)/Make.defines.$(PLATFORM) - -all: nng/reqrep test +PROGS = nng/pubsub + +build: $(PROGS) clean: - rm -f test *.o a.out core temp.* *.a *.so + rm $(TEMPFILES) include $(ROOT)/Make.common.inc diff --git a/test/nng/pubsub.c b/test/nng/pubsub.c new file mode 100644 index 0000000..794cd6c --- /dev/null +++ b/test/nng/pubsub.c @@ -0,0 +1,92 @@ +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <time.h> +#include <unistd.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; +} \ No newline at end of file -- Gitblit v1.8.0