Skip to content

Commit

Permalink
Build all examples successfully with Make
Browse files Browse the repository at this point in the history
Signed-off-by: Björn Svensson <[email protected]>
  • Loading branch information
bjosv committed Jun 26, 2024
1 parent 53f7795 commit 3d9805e
Show file tree
Hide file tree
Showing 15 changed files with 76 additions and 48 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ examples: $(STLIBNAME)

clean:
rm -rf $(OBJ_DIR) $(LIB_DIR) $(TEST_BINS) *.gcda *.gcno *.gcov
make -C examples clean
$(MAKE) -C examples clean

INSTALL?= cp -pPR

Expand Down
24 changes: 21 additions & 3 deletions examples/Makefile
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
CC?=gcc
CXX?=g++

CFLAGS=-Wall -Wextra -g -O2 -I../include/valkey
CFLAGS=-Wall -Wextra -g -O2 -I../include
STLIBNAME=../lib/libvalkey.a

# Define examples
EXAMPLES=example-blocking example-blocking-push example-async-libevent \
example-async-libev example-async-glib example-async-poll
example-async-libev example-async-glib example-async-poll \
example-cluster-async example-cluster-clientside-caching-async \
example-cluster-simple

ifeq ($(USE_SSL),1)
EXAMPLES+=blocking-ssl async-libevent-ssl
EXAMPLES+=example-blocking-ssl example-async-libevent-ssl \
example-cluster-async-tls example-cluster-tls
SSL_STLIBNAME=../lib/libvalkey_ssl.a
SSL_LDFLAGS=-lssl -lcrypto
endif
Expand Down Expand Up @@ -92,5 +95,20 @@ example-async-qt: async-qt.cpp $(STLIBNAME)
$(CXX) -o $@ $(CFLAGS) $(LDFLAGS) -I$(QT_INCLUDE_DIR) -I$(QT_INCLUDE_DIR)/QtCore -L$(QT_LIBRARY_DIR) qt-adapter-moc.o qt-example-moc.o $< -pthread $(STLIBNAME) -lQtCore
endif

example-cluster-async: cluster-async.c $(STLIBNAME)
$(CC) -o $@ $(CFLAGS) $< -levent $(STLIBNAME)

example-cluster-async-tls: cluster-async-tls.c $(STLIBNAME) $(SSL_STLIBNAME)
$(CC) -o $@ $(CFLAGS) $< -levent $(STLIBNAME) $(SSL_STLIBNAME) $(SSL_LDFLAGS)

example-cluster-clientside-caching-async: cluster-clientside-caching-async.c $(STLIBNAME)
$(CC) -o $@ $(CFLAGS) $< -levent $(STLIBNAME)

example-cluster-simple: cluster-simple.c $(STLIBNAME)
$(CC) -o $@ $(CFLAGS) $< $(STLIBNAME)

example-cluster-tls: cluster-tls.c $(STLIBNAME) $(SSL_STLIBNAME)
$(CC) -o $@ $(CFLAGS) $< $(STLIBNAME) $(SSL_STLIBNAME) $(SSL_LDFLAGS)

clean:
rm -f example-* *.o
6 changes: 3 additions & 3 deletions examples/async-glib.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include <stdlib.h>

#include <valkey.h>
#include <async.h>
#include <adapters/glib.h>
#include <valkey/valkey.h>
#include <valkey/async.h>
#include <valkey/adapters/glib.h>

static GMainLoop *mainloop;

Expand Down
6 changes: 3 additions & 3 deletions examples/async-libev.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
#include <string.h>
#include <signal.h>

#include <valkey.h>
#include <async.h>
#include <adapters/libev.h>
#include <valkey/valkey.h>
#include <valkey/async.h>
#include <valkey/adapters/libev.h>

void getCallback(valkeyAsyncContext *c, void *r, void *privdata) {
valkeyReply *reply = r;
Expand Down
8 changes: 4 additions & 4 deletions examples/async-libevent-ssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
#include <string.h>
#include <signal.h>

#include <valkey.h>
#include <valkey_ssl.h>
#include <async.h>
#include <adapters/libevent.h>
#include <valkey/valkey.h>
#include <valkey/valkey_ssl.h>
#include <valkey/async.h>
#include <valkey/adapters/libevent.h>

void getCallback(valkeyAsyncContext *c, void *r, void *privdata) {
valkeyReply *reply = r;
Expand Down
6 changes: 3 additions & 3 deletions examples/async-libevent.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
#include <string.h>
#include <signal.h>

#include <valkey.h>
#include <async.h>
#include <adapters/libevent.h>
#include <valkey/valkey.h>
#include <valkey/async.h>
#include <valkey/adapters/libevent.h>

void getCallback(valkeyAsyncContext *c, void *r, void *privdata) {
valkeyReply *reply = r;
Expand Down
4 changes: 2 additions & 2 deletions examples/async-poll.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
#include <signal.h>
#include <unistd.h>

#include <async.h>
#include <adapters/poll.h>
#include <valkey/async.h>
#include <valkey/adapters/poll.h>

/* Put in the global scope, so that loop can be explicitly stopped */
static int exit_loop = 0;
Expand Down
2 changes: 1 addition & 1 deletion examples/blocking-push.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <valkey.h>
#include <valkey/valkey.h>

#define KEY_COUNT 5

Expand Down
4 changes: 2 additions & 2 deletions examples/blocking-ssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
#include <stdlib.h>
#include <string.h>

#include <valkey.h>
#include <valkey_ssl.h>
#include <valkey/valkey.h>
#include <valkey/valkey_ssl.h>

#ifdef _MSC_VER
#include <winsock2.h> /* For struct timeval */
Expand Down
2 changes: 1 addition & 1 deletion examples/blocking.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <valkey.h>
#include <valkey/valkey.h>

#ifdef _MSC_VER
#include <winsock2.h> /* For struct timeval */
Expand Down
7 changes: 3 additions & 4 deletions examples/cluster-async-tls.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>

#include "adapters/libevent.h"
#include "valkeycluster.h"
#include "valkeycluster_ssl.h"
#include <valkey/adapters/libevent.h>
#include <valkey/valkeycluster.h>
#include <valkey/valkeycluster_ssl.h>

#define CLUSTER_NODE_TLS "127.0.0.1:7300"

Expand Down
18 changes: 12 additions & 6 deletions examples/cluster-async.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#include <stdio.h>
#include <stdlib.h>
#include <valkeycluster/adapters/libevent.h>
#include <valkeycluster/valkeycluster.h>
#include <valkey/adapters/libevent.h>
#include <valkey/valkeycluster.h>

void getCallback(valkeyClusterAsyncContext *cc, void *r, void *privdata) {
valkeyReply *reply = (valkeyReply *)r;
if (reply == NULL) {
if (cc->errstr) {
if (cc->err) {
printf("errstr: %s\n", cc->errstr);
}
return;
Expand All @@ -20,7 +20,7 @@ void getCallback(valkeyClusterAsyncContext *cc, void *r, void *privdata) {
void setCallback(valkeyClusterAsyncContext *cc, void *r, void *privdata) {
valkeyReply *reply = (valkeyReply *)r;
if (reply == NULL) {
if (cc->errstr) {
if (cc->err) {
printf("errstr: %s\n", cc->errstr);
}
return;
Expand All @@ -45,12 +45,18 @@ void disconnectCallback(const valkeyAsyncContext *ac, int status) {
}

int main(int argc, char **argv) {
(void) argc;
(void) argv;
printf("Connecting...\n");
valkeyClusterAsyncContext *cc =
valkeyClusterAsyncConnect("127.0.0.1:7000", VALKEYCLUSTER_FLAG_NULL);
if (cc && cc->err) {
if (!cc) {
printf("Error: Allocation failure\n");
exit(-1);
} else if (cc->err) {
printf("Error: %s\n", cc->errstr);
return 1;
// handle error
exit(-1);
}

struct event_base *base = event_base_new();
Expand Down
7 changes: 5 additions & 2 deletions examples/cluster-clientside-caching-async.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
* messages are received via the registered push callback.
* The disconnect callback should also be used as an indication of invalidation.
*/
#include <valkeycluster/adapters/libevent.h>
#include <valkeycluster/valkeycluster.h>
#include <valkey/adapters/libevent.h>
#include <valkey/valkeycluster.h>

#include <assert.h>
#include <stdio.h>
Expand Down Expand Up @@ -83,6 +83,7 @@ void getCallback1(valkeyClusterAsyncContext *acc, void *r, void *privdata) {

/* Push message callback handling invalidation messages. */
void pushCallback(valkeyAsyncContext *ac, void *r) {
(void) ac;
valkeyReply *reply = r;
if (!(reply->type == VALKEY_REPLY_PUSH && reply->elements == 2 &&
reply->element[0]->type == VALKEY_REPLY_STRING &&
Expand Down Expand Up @@ -139,6 +140,8 @@ void modifyKey(const char *key, const char *value) {
}

int main(int argc, char **argv) {
(void) argc;
(void) argv;
valkeyClusterAsyncContext *acc = valkeyClusterAsyncContextInit();
assert(acc);

Expand Down
13 changes: 7 additions & 6 deletions examples/cluster-simple.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <stdio.h>
#include <stdlib.h>
#include <valkeycluster/valkeycluster.h>
#include <valkey/valkeycluster.h>

int main(int argc, char **argv) {
UNUSED(argc);
Expand All @@ -12,19 +12,20 @@ int main(int argc, char **argv) {
valkeyClusterSetOptionConnectTimeout(cc, timeout);
valkeyClusterSetOptionRouteUseSlots(cc);
valkeyClusterConnect2(cc);
if (cc && cc->err) {
if (!cc) {
printf("Error: Allocation failure\n");
exit(-1);
} else if (cc->err) {
printf("Error: %s\n", cc->errstr);
// handle error
exit(-1);
}

valkeyReply *reply =
(valkeyReply *)valkeyClusterCommand(cc, "SET %s %s", "key", "value");
valkeyReply *reply = valkeyClusterCommand(cc, "SET %s %s", "key", "value");
printf("SET: %s\n", reply->str);
freeReplyObject(reply);

valkeyReply *reply2 =
(valkeyReply *)valkeyClusterCommand(cc, "GET %s", "key");
valkeyReply *reply2 = valkeyClusterCommand(cc, "GET %s", "key");
printf("GET: %s\n", reply2->str);
freeReplyObject(reply2);

Expand Down
15 changes: 8 additions & 7 deletions examples/cluster-tls.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
#include <stdlib.h>
#include <valkey/valkey.h>
#include <valkey/valkey_ssl.h>
#include <valkeycluster/valkeycluster.h>
#include <valkeycluster/valkeycluster_ssl.h>
#include <valkey/valkeycluster.h>
#include <valkey/valkeycluster_ssl.h>

#define CLUSTER_NODE_TLS "127.0.0.1:7301"

Expand Down Expand Up @@ -31,23 +31,24 @@ int main(int argc, char **argv) {
valkeyClusterSetOptionParseSlaves(cc);
valkeyClusterSetOptionEnableSSL(cc, ssl);
valkeyClusterConnect2(cc);
if (cc && cc->err) {
if (!cc) {
printf("Error: Allocation failure\n");
exit(-1);
} else if (cc->err) {
printf("Error: %s\n", cc->errstr);
// handle error
exit(-1);
}

valkeyReply *reply =
(valkeyReply *)valkeyClusterCommand(cc, "SET %s %s", "key", "value");
valkeyReply *reply = valkeyClusterCommand(cc, "SET %s %s", "key", "value");
if (!reply) {
printf("Reply missing: %s\n", cc->errstr);
exit(-1);
}
printf("SET: %s\n", reply->str);
freeReplyObject(reply);

valkeyReply *reply2 =
(valkeyReply *)valkeyClusterCommand(cc, "GET %s", "key");
valkeyReply *reply2 = valkeyClusterCommand(cc, "GET %s", "key");
if (!reply2) {
printf("Reply missing: %s\n", cc->errstr);
exit(-1);
Expand Down

0 comments on commit 3d9805e

Please sign in to comment.