Nng library and pubsub example

This commit is contained in:
jturnsek 2023-03-05 21:25:02 +01:00 committed by Xiang Xiao
parent d325cf048a
commit b13f70ca59
8 changed files with 418 additions and 0 deletions

22
examples/nng_test/Kconfig Normal file
View File

@ -0,0 +1,22 @@
#
# For a description of the syntax of this configuration file,
# see the file kconfig-language.txt in the NuttX tools repository.
#
config EXAMPLES_NNG_TEST
bool "NNG pubsub program"
default n
---help---
Enable the NNG pubsub program
if EXAMPLES_NNG_TEST
config EXAMPLES_NNG_TEST_STACKSIZE
int "NNG pubsub stack size"
default DEFAULT_TASK_STACKSIZE
config EXAMPLES_NNG_TEST_PRIORITY
int "NNG pubsub task priority"
default 50
endif

View File

@ -0,0 +1,23 @@
############################################################################
# apps/examples/nng_test/Make.defs
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership. The
# ASF licenses this file to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance with the
# License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
############################################################################
ifneq ($(CONFIG_EXAMPLES_NNG_TEST),)
CONFIGURED_APPS += $(APPDIR)/examples/nng_test
endif

View File

@ -0,0 +1,32 @@
############################################################################
# apps/examples/nng_test/Makefile
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership. The
# ASF licenses this file to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance with the
# License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
############################################################################
include $(APPDIR)/Make.defs
MAINSRC = pubsub.c
# Built-in application info
PROGNAME = pubsub
PRIORITY = $(CONFIG_EXAMPLES_NNG_TEST_PRIORITY)
STACKSIZE = $(CONFIG_EXAMPLES_NNG_TEST_STACKSIZE)
MODULE = $(CONFIG_EXAMPLES_NNG_TEST)
include $(APPDIR)/Application.mk

141
examples/nng_test/pubsub.c Normal file
View File

@ -0,0 +1,141 @@
/****************************************************************************
* apps/examples/nng_test/pubsub.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <pthread.h>
#include <fcntl.h>
#include <errno.h>
#include <debug.h>
#include <nng/nng.h>
#include <nng/protocol/pubsub0/pub.h>
#include <nng/protocol/pubsub0/sub.h>
/****************************************************************************
* Public Functions
****************************************************************************/
void fatal(FAR const char *func, int rv)
{
fprintf(stderr, "%s: %s\n", func, nng_strerror(rv));
}
FAR char *date(void)
{
time_t now = time(&now);
FAR struct tm *info = localtime(&now);
FAR char *text = asctime(info);
text[strlen(text) - 1] = '\0';
return text;
}
FAR void *client_thread(pthread_addr_t pvarg)
{
nng_socket sock;
int rv;
sleep(2);
if ((rv = nng_sub0_open(&sock)) != 0)
{
fatal("nng_sub0_open", rv);
return NULL;
}
/* subscribe to everything (empty means all topics) */
if ((rv = nng_setopt(sock, NNG_OPT_SUB_SUBSCRIBE, "", 0)) != 0)
{
fatal("nng_setopt", rv);
return NULL;
}
if ((rv = nng_dial(sock, "ipc:///pubsub.ipc", NULL, 0)) != 0)
{
fatal("nng_dial", rv);
return NULL;
}
for (; ; )
{
FAR char *buf = NULL;
size_t sz;
if ((rv = nng_recv(sock, &buf, &sz, NNG_FLAG_ALLOC)) != 0)
{
fatal("nng_recv", rv);
break;
}
printf("CLIENT: RECEIVED %s\n", buf);
nng_free(buf, sz);
}
return NULL; /* Keeps some compilers from complaining */
}
int main(const int argc, const FAR char *argv[])
{
pthread_t tid;
nng_socket sock;
int rv;
if ((rv = nng_pub0_open(&sock)) != 0)
{
fatal("nng_pub0_open", rv);
return 1;
}
rv = pthread_create(&tid, NULL, client_thread, NULL);
if (rv != 0)
{
fatal("main: Failed to create client thread: %d\n", rv);
return 1;
}
if ((rv = nng_listen(sock, "ipc:///pubsub.ipc", NULL, 0)) < 0)
{
fatal("nng_listen", rv);
return 1;
}
for (; ; )
{
FAR 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);
break;
}
sleep(1);
}
return 1;
}

1
netutils/nng/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/*tar.gz

20
netutils/nng/Kconfig Normal file
View File

@ -0,0 +1,20 @@
#
# For a description of the syntax of this configuration file,
# see the file kconfig-language.txt in the NuttX tools repository.
#
config NETUTILS_NNG
bool "Enable Nanomsg NG"
default n
---help---
Enable Nanomsg-NG library. NNG is a lightweight, broker-less library,
offering a simple API to solve common recurring messaging problems,
such as publish/subscribe, RPC-style request/reply, or service discovery.
if NETUTILS_NNG
config NETUTILS_NNG_VERSION
string "NNG Version"
default "1.5.2"
endif

27
netutils/nng/Make.defs Normal file
View File

@ -0,0 +1,27 @@
############################################################################
# apps/netutils/nng/Make.defs
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership. The
# ASF licenses this file to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance with the
# License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
############################################################################
ifneq ($(CONFIG_NETUTILS_NNG),)
CONFIGURED_APPS += $(APPDIR)/netutils/nng
CFLAGS += ${INCDIR_PREFIX}$(APPDIR)/netutils/nng/nng/include
CXXFLAGS += ${INCDIR_PREFIX}$(APPDIR)/netutils/nng/nng/include
endif

152
netutils/nng/Makefile Normal file
View File

@ -0,0 +1,152 @@
############################################################################
# apps/netutils/nng/Makefile
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership. The
# ASF licenses this file to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance with the
# License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
############################################################################
include $(APPDIR)/Make.defs
NNG_VERSION := $(patsubst "%",%,$(CONFIG_NETUTILS_NNG_VERSION))
NNG_TARBALL = v$(NNG_VERSION).tar.gz
NNG_UNPACK = nng
NNG_SRCDIR = $(NNG_UNPACK)$(DELIM)src
CFLAGS += -I$(NNG_UNPACK)/src
CFLAGS += -DNNG_PLATFORM_POSIX
CFLAGS += -DNNG_HAVE_GETRANDOM
CFLAGS += -DNNG_TRANSPORT_INPROC
CFLAGS += -DNNG_TRANSPORT_IPC
CFLAGS += -DNNG_TRANSPORT_TCP
CFLAGS += -DNNG_TRANSPORT_TLS
CFLAGS += -DNNG_TRANSPORT_WS
CFLAGS += -DNNG_TRANSPORT_WSS
CFLAGS += -DNNG_USE_EVENTFD
CFLAGS += -DNNG_HAVE_EPOLL
CFLAGS += -DNNG_HAVE_EPOLL_CREATE1
DEPPATH += --dep-path $(NNG_SRCDIR)
VPATH += :$(NNG_SRCDIR)
CSRCS = $(NNG_SRCDIR)/nng.c
CSRCS += $(NNG_SRCDIR)/nng_legacy.c
CSRCS += $(NNG_SRCDIR)/core/aio.c
CSRCS += $(NNG_SRCDIR)/core/device.c
CSRCS += $(NNG_SRCDIR)/core/dialer.c
CSRCS += $(NNG_SRCDIR)/core/file.c
CSRCS += $(NNG_SRCDIR)/core/idhash.c
CSRCS += $(NNG_SRCDIR)/core/init.c
CSRCS += $(NNG_SRCDIR)/core/list.c
CSRCS += $(NNG_SRCDIR)/core/listener.c
CSRCS += $(NNG_SRCDIR)/core/lmq.c
CSRCS += $(NNG_SRCDIR)/core/message.c
CSRCS += $(NNG_SRCDIR)/core/msgqueue.c
CSRCS += $(NNG_SRCDIR)/core/options.c
CSRCS += $(NNG_SRCDIR)/core/pollable.c
CSRCS += $(NNG_SRCDIR)/core/panic.c
CSRCS += $(NNG_SRCDIR)/core/pipe.c
CSRCS += $(NNG_SRCDIR)/core/protocol.c
CSRCS += $(NNG_SRCDIR)/core/reap.c
CSRCS += $(NNG_SRCDIR)/core/socket.c
CSRCS += $(NNG_SRCDIR)/core/stats.c
CSRCS += $(NNG_SRCDIR)/core/stream.c
CSRCS += $(NNG_SRCDIR)/core/strs.c
CSRCS += $(NNG_SRCDIR)/core/taskq.c
CSRCS += $(NNG_SRCDIR)/core/tcp.c
CSRCS += $(NNG_SRCDIR)/core/thread.c
CSRCS += $(NNG_SRCDIR)/core/timer.c
CSRCS += $(NNG_SRCDIR)/core/url.c
CSRCS += $(NNG_SRCDIR)/platform/posix/posix_alloc.c
CSRCS += $(NNG_SRCDIR)/platform/posix/posix_atomic.c
CSRCS += $(NNG_SRCDIR)/platform/posix/posix_clock.c
CSRCS += $(NNG_SRCDIR)/platform/posix/posix_debug.c
CSRCS += $(NNG_SRCDIR)/platform/posix/posix_file.c
CSRCS += $(NNG_SRCDIR)/platform/posix/posix_ipcconn.c
CSRCS += $(NNG_SRCDIR)/platform/posix/posix_ipcdial.c
CSRCS += $(NNG_SRCDIR)/platform/posix/posix_ipclisten.c
CSRCS += $(NNG_SRCDIR)/platform/posix/posix_pipe.c
CSRCS += $(NNG_SRCDIR)/platform/posix/posix_resolv_gai.c
CSRCS += $(NNG_SRCDIR)/platform/posix/posix_sockaddr.c
CSRCS += $(NNG_SRCDIR)/platform/posix/posix_tcpconn.c
CSRCS += $(NNG_SRCDIR)/platform/posix/posix_tcpdial.c
CSRCS += $(NNG_SRCDIR)/platform/posix/posix_tcplisten.c
CSRCS += $(NNG_SRCDIR)/platform/posix/posix_thread.c
CSRCS += $(NNG_SRCDIR)/platform/posix/posix_udp.c
CSRCS += $(NNG_SRCDIR)/platform/posix/posix_pollq_epoll.c
CSRCS += $(NNG_SRCDIR)/platform/posix/posix_rand_getrandom.c
#CSRCS += $(NNG_SRCDIR)/platform/posix/posix_rand_urandom.c
CSRCS += $(NNG_SRCDIR)/sp/transport.c
CSRCS += $(NNG_SRCDIR)/sp/protocol/bus0/bus.c
CSRCS += $(NNG_SRCDIR)/sp/protocol/pair0/pair.c
CSRCS += $(NNG_SRCDIR)/sp/protocol/pair1/pair.c
CSRCS += $(NNG_SRCDIR)/sp/protocol/pair1/pair1_poly.c
CSRCS += $(NNG_SRCDIR)/sp/protocol/pipeline0/pull.c
CSRCS += $(NNG_SRCDIR)/sp/protocol/pipeline0/push.c
CSRCS += $(NNG_SRCDIR)/sp/protocol/pubsub0/pub.c
CSRCS += $(NNG_SRCDIR)/sp/protocol/pubsub0/sub.c
CSRCS += $(NNG_SRCDIR)/sp/protocol/pubsub0/xsub.c
CSRCS += $(NNG_SRCDIR)/sp/protocol/reqrep0/rep.c
CSRCS += $(NNG_SRCDIR)/sp/protocol/reqrep0/req.c
CSRCS += $(NNG_SRCDIR)/sp/protocol/reqrep0/xrep.c
CSRCS += $(NNG_SRCDIR)/sp/protocol/reqrep0/xreq.c
CSRCS += $(NNG_SRCDIR)/sp/protocol/survey0/respond.c
CSRCS += $(NNG_SRCDIR)/sp/protocol/survey0/survey.c
CSRCS += $(NNG_SRCDIR)/sp/protocol/survey0/xrespond.c
CSRCS += $(NNG_SRCDIR)/sp/protocol/survey0/xsurvey.c
CSRCS += $(NNG_SRCDIR)/sp/transport/inproc/inproc.c
CSRCS += $(NNG_SRCDIR)/sp/transport/ipc/ipc.c
CSRCS += $(NNG_SRCDIR)/sp/transport/tcp/tcp.c
CSRCS += $(NNG_SRCDIR)/sp/transport/tls/tls.c
CSRCS += $(NNG_SRCDIR)/sp/transport/ws/websocket.c
CSRCS += $(NNG_SRCDIR)/supplemental/base64/base64.c
CSRCS += $(NNG_SRCDIR)/supplemental/http/http_chunk.c
CSRCS += $(NNG_SRCDIR)/supplemental/http/http_client.c
CSRCS += $(NNG_SRCDIR)/supplemental/http/http_conn.c
CSRCS += $(NNG_SRCDIR)/supplemental/http/http_msg.c
CSRCS += $(NNG_SRCDIR)/supplemental/http/http_public.c
CSRCS += $(NNG_SRCDIR)/supplemental/http/http_schemes.c
CSRCS += $(NNG_SRCDIR)/supplemental/http/http_server.c
CSRCS += $(NNG_SRCDIR)/supplemental/sha1/sha1.c
CSRCS += $(NNG_SRCDIR)/supplemental/tls/tls_common.c
#CSRCS += $(NNG_SRCDIR)/supplemental/tls/mbedtls/tls.c
#CSRCS += $(NNG_SRCDIR)/supplemental/util/options.c
CSRCS += $(NNG_SRCDIR)/supplemental/util/platform.c
CSRCS += $(NNG_SRCDIR)/supplemental/websocket/websocket.c
$(NNG_TARBALL):
$(Q) echo "Downloading nng-$(NNG_VERSION)"
$(Q) curl -O -L https://github.com/nanomsg/nng/archive/$(NNG_TARBALL)
$(NNG_UNPACK): $(NNG_TARBALL)
$(Q) tar zxf $(NNG_TARBALL)
$(Q) mv nng-$(NNG_VERSION) $(NNG_UNPACK)
$(Q) touch $(NNG_UNPACK)
# Download and unpack tarball if no git repo found
ifeq ($(wildcard $(NNG_UNPACK)/.git),)
context:: $(NNG_UNPACK)
distclean::
$(call DELFILE, $(NNG_TARBALL))
$(call DELDIR, $(NNG_UNPACK))
endif
include $(APPDIR)/Application.mk