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 b72d61c
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 3 deletions.
19 changes: 19 additions & 0 deletions .cproject
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,24 @@
</tool>
</toolChain>
</folderInfo>
<folderInfo id="cdt.managedbuild.toolchain.gnu.base.2018240843.1756755366" name="/" resourcePath="examples">
<toolChain id="cdt.managedbuild.toolchain.gnu.base.1872719987" name="Linux GCC" superClass="cdt.managedbuild.toolchain.gnu.base" unusedChildren="">
<tool id="cdt.managedbuild.tool.gnu.archiver.base.142140314" name="GCC Archiver" superClass="cdt.managedbuild.tool.gnu.archiver.base.1422446010"/>
<tool id="cdt.managedbuild.tool.gnu.cpp.compiler.base.846657740" name="GCC C++ Compiler" superClass="cdt.managedbuild.tool.gnu.cpp.compiler.base.1815812035"/>
<tool id="cdt.managedbuild.tool.gnu.c.compiler.base.1716784206" name="GCC C Compiler" superClass="cdt.managedbuild.tool.gnu.c.compiler.base.415530852">
<inputType id="cdt.managedbuild.tool.gnu.c.compiler.input.2140904408" superClass="cdt.managedbuild.tool.gnu.c.compiler.input"/>
</tool>
<tool id="cdt.managedbuild.tool.gnu.c.linker.base.2010450577" name="GCC C Linker" superClass="cdt.managedbuild.tool.gnu.c.linker.base.31299320"/>
<tool id="cdt.managedbuild.tool.gnu.cpp.linker.base.754977188" name="GCC C++ Linker" superClass="cdt.managedbuild.tool.gnu.cpp.linker.base.1554120195"/>
<tool id="cdt.managedbuild.tool.gnu.assembler.base.559620659" name="GCC Assembler" superClass="cdt.managedbuild.tool.gnu.assembler.base.281878813">
<inputType id="cdt.managedbuild.tool.gnu.assembler.input.957053907" superClass="cdt.managedbuild.tool.gnu.assembler.input"/>
</tool>
</toolChain>
</folderInfo>
<sourceEntries>
<entry excluding="examples" flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name=""/>
<entry flags="VALUE_WORKSPACE_PATH" kind="sourcePath" name="examples"/>
</sourceEntries>
</configuration>
</storageModule>
<storageModule moduleId="org.eclipse.cdt.core.externalSettings"/>
Expand All @@ -53,4 +71,5 @@
</scannerConfigBuildInfo>
</storageModule>
<storageModule moduleId="org.eclipse.cdt.internal.ui.text.commentOwnerProjectMappings"/>
<storageModule moduleId="refreshScope"/>
</cproject>
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
59 changes: 59 additions & 0 deletions examples/hello.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#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) {

// Execute 'PING "Hello World!"' 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 b72d61c

Please sign in to comment.