Skip to content

Commit

Permalink
Add hello.c example
Browse files Browse the repository at this point in the history
  • Loading branch information
attipaci committed Dec 19, 2024
1 parent 6ef3a86 commit 75e021e
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 3 deletions.
19 changes: 16 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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/*),)
Expand Down Expand Up @@ -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=<path> install')"
@echo " clean Removes intermediate products."
@echo " distclean Deletes all generated files."
Expand Down
11 changes: 11 additions & 0 deletions examples.mk
Original file line number Diff line number Diff line change
@@ -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
60 changes: 60 additions & 0 deletions examples/hello.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include <stdio.h>
#include <stdlib.h>

#include <redisx.h>


/*
* 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;
}

0 comments on commit 75e021e

Please sign in to comment.