From 75e021e542d828ce2118dd886cd37c16f09e623b Mon Sep 17 00:00:00 2001 From: Attila Kovacs Date: Thu, 19 Dec 2024 13:39:12 +0100 Subject: [PATCH] Add hello.c example --- Makefile | 19 ++++++++++++--- examples.mk | 11 +++++++++ examples/hello.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 87 insertions(+), 3 deletions(-) create mode 100644 examples.mk create mode 100644 examples/hello.c diff --git a/Makefile b/Makefile index 3d2904b..3b20de7 100644 --- a/Makefile +++ b/Makefile @@ -32,13 +32,15 @@ endif # Link against thread lib LDFLAGS += -pthread +export + # Build for distribution .PHONY: distro distro: shared tools $(DOC_TARGETS) # Build everything... .PHONY: all -all: shared static $(DOC_TARGETS) check +all: shared static tools examples $(DOC_TARGETS) check # Shared libraries (versioned and unversioned) .PHONY: shared @@ -53,6 +55,11 @@ static: $(LIB)/libredisx.a tools: shared make -f tools.mk +# Examples +.PHONY: examples +examples: shared + make -f examples.mk + # Run regression tests .PHONY: test test: @@ -147,7 +154,7 @@ INSTALL_PROGRAM ?= install INSTALL_DATA ?= install -m 644 .PHONY: install -install: install-libs install-bin install-man install-headers install-html +install: install-libs install-bin install-man install-headers install-examples install-html .PHONY: install-libs install-libs: @@ -175,13 +182,18 @@ install-man: @install -d $(DESTDIR)$(mandir)/man1 $(INSTALL_DATA) -D man/man1/* $(DESTDIR)$(mandir)/man1 - .PHONY: install-headers install-headers: @echo "installing headers to $(DESTDIR)$(includedir)" install -d $(DESTDIR)$(includedir) $(INSTALL_DATA) -D include/* $(DESTDIR)$(includedir)/ +.PHONY: install-examples +install-examples: + @echo "installing examples to $(DESTDIR)$(docdir)" + install -d $(DESTDIR)$(docdir) + $(INSTALL_DATA) -D examples/* $(DESTDIR)$(docdir)/ + .PHONY: install-html install-html: ifneq ($(wildcard apidoc/html/search/*),) @@ -211,6 +223,7 @@ help: @echo " analyze Performs static analysis with 'cppcheck'." @echo " all All of the above." @echo " distro shared libs and documentation (default target)." + @echo " examples Build example programs." @echo " install Install components (e.g. 'make prefix= install')" @echo " clean Removes intermediate products." @echo " distclean Deletes all generated files." diff --git a/examples.mk b/examples.mk new file mode 100644 index 0000000..9eba1b9 --- /dev/null +++ b/examples.mk @@ -0,0 +1,11 @@ +SRC := examples + +include config.mk + +LDFLAGS += -L$(LIB) -lredisx +LD_LIBRARY_PATH := $(LIB):$(LD_LIBRARY_PATH) + +.PHONY: all +all: $(BIN)/hello + +include build.mk diff --git a/examples/hello.c b/examples/hello.c new file mode 100644 index 0000000..70a4ccc --- /dev/null +++ b/examples/hello.c @@ -0,0 +1,60 @@ +#include +#include + +#include + + +/* + * Pings a Redis server with a hello message, and prints the response, or else an error. + * + * Usage: hello [host] + * + * Options: + * host The Redis server host (default: "127.0.0.1") + * + * Exit status: + * 0 Successful completion + * 1 Could not connect to Redis server + * 2 Bad response from server + * + * Author: Attila Kovacs + * Version: 19 December 2024 + */ +int main(int argc, char *argv[]) { + // Initialize for Redis server at 'my-server.com'. + Redis *redis; + char *host = "127.0.0.1"; // Default host + + if(argc > 1) host = argv[1]; // program argument designates host + + // Initialize with the designated or default host + redis = redisxInit(host); + + // Connect to Redis / Valkey server + if(redis != NULL && redisxConnect(redis, FALSE) == X_SUCCESS) { + + // Run 'GET my-key' query on the server + RESP *reply = redisxRequest(redis, "PING", "Hello World!", NULL, NULL, NULL); + + // Check that we got a response of the expected type (bulk string of any length) + if(redisxCheckRESP(reply, RESP_BULK_STRING, 0) == X_SUCCESS) + printf("%s\n", (char *) reply->value); + else { + fprintf(stderr, "ERROR! bad response\n"); + return 2; + } + + // Clean up + redisxDestroyRESP(reply); + redisxDisconnect(redis); + } + else { + perror("ERROR! could not connect"); + return 1; + } + + // Free up the resources used by the 'redis' instance. + redisxDestroy(redis); + + return 0; +}