diff options
author | Dmitry Bakhvalov <dizzus@gmail.com> | 2015-07-13 20:26:41 +0300 |
---|---|---|
committer | Jan-Erik Rediger <janerik@fnordig.de> | 2015-07-27 23:19:14 +0200 |
commit | c18a5648181801aa6fddd1856940b7dd20cdf74a (patch) | |
tree | abd8b01a55c461dd5936c282fce466ed6cd78070 /examples | |
parent | 4a632a6038f37b4d72c52f580d6a396c5ee3fe12 (diff) | |
download | hiredict-c18a5648181801aa6fddd1856940b7dd20cdf74a.tar.xz |
Added MacOS X addapter and corresponding example.
Added MacOS X support via CoreFoundation run loop.
Diffstat (limited to 'examples')
-rw-r--r-- | examples/example-macosx.c | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/examples/example-macosx.c b/examples/example-macosx.c new file mode 100644 index 0000000..1b9ef3e --- /dev/null +++ b/examples/example-macosx.c @@ -0,0 +1,66 @@ +// +// Created by Дмитрий Бахвалов on 13.07.15. +// Copyright (c) 2015 Dmitry Bakhvalov. All rights reserved. +// + +#include <stdio.h> + +#include <hiredis.h> +#include <async.h> +#include <adapters/macosx.h> + +void getCallback(redisAsyncContext *c, void *r, void *privdata) { + redisReply *reply = r; + if (reply == NULL) return; + printf("argv[%s]: %s\n", (char*)privdata, reply->str); + + /* Disconnect after receiving the reply to GET */ + redisAsyncDisconnect(c); +} + +void connectCallback(const redisAsyncContext *c, int status) { + if (status != REDIS_OK) { + printf("Error: %s\n", c->errstr); + return; + } + printf("Connected...\n"); +} + +void disconnectCallback(const redisAsyncContext *c, int status) { + if (status != REDIS_OK) { + printf("Error: %s\n", c->errstr); + return; + } + CFRunLoopStop(CFRunLoopGetCurrent()); + printf("Disconnected...\n"); +} + +int main (int argc, char **argv) { + signal(SIGPIPE, SIG_IGN); + + CFRunLoopRef loop = CFRunLoopGetCurrent(); + if( !loop ) { + printf("Error: Cannot get current run lopp\n"); + return 1; + } + + redisAsyncContext *c = redisAsyncConnect("127.0.0.1", 6379); + if (c->err) { + /* Let *c leak for now... */ + printf("Error: %s\n", c->errstr); + return 1; + } + + redisMacOSAttach(c, loop); + + redisAsyncSetConnectCallback(c,connectCallback); + redisAsyncSetDisconnectCallback(c,disconnectCallback); + + redisAsyncCommand(c, NULL, NULL, "SET key %b", argv[argc-1], strlen(argv[argc-1])); + redisAsyncCommand(c, getCallback, (char*)"end-1", "GET key"); + + CFRunLoopRun(); + + return 0; +} + |