PageRenderTime 323ms CodeModel.GetById 141ms app.highlight 5ms RepoModel.GetById 176ms app.codeStats 0ms

/hiredis/example-libevent.c

http://github.com/nicolasff/webdis
C | 52 lines | 43 code | 7 blank | 2 comment | 7 complexity | 67a0af5e511b19e86000d5bcabcdc1c6 MD5 | raw file
 1#include <stdio.h>
 2#include <stdlib.h>
 3#include <string.h>
 4#include <signal.h>
 5#include "hiredis.h"
 6#include "async.h"
 7#include "adapters/libevent.h"
 8
 9void getCallback(redisAsyncContext *c, void *r, void *privdata) {
10    redisReply *reply = r;
11    if (reply == NULL) return;
12    printf("argv[%s]: %s\n", (char*)privdata, reply->str);
13
14    /* Disconnect after receiving the reply to GET */
15    redisAsyncDisconnect(c);
16}
17
18void connectCallback(const redisAsyncContext *c, int status) {
19    if (status != REDIS_OK) {
20        printf("Error: %s\n", c->errstr);
21        return;
22    }
23    printf("Connected...\n");
24}
25
26void disconnectCallback(const redisAsyncContext *c, int status) {
27    if (status != REDIS_OK) {
28        printf("Error: %s\n", c->errstr);
29        return;
30    }
31    printf("Disconnected...\n");
32}
33
34int main (int argc, char **argv) {
35    signal(SIGPIPE, SIG_IGN);
36    struct event_base *base = event_base_new();
37
38    redisAsyncContext *c = redisAsyncConnect("127.0.0.1", 6379);
39    if (c->err) {
40        /* Let *c leak for now... */
41        printf("Error: %s\n", c->errstr);
42        return 1;
43    }
44
45    redisLibeventAttach(c,base);
46    redisAsyncSetConnectCallback(c,connectCallback);
47    redisAsyncSetDisconnectCallback(c,disconnectCallback);
48    redisAsyncCommand(c, NULL, NULL, "SET key %b", argv[argc-1], strlen(argv[argc-1]));
49    redisAsyncCommand(c, getCallback, (char*)"end-1", "GET key");
50    event_base_dispatch(base);
51    return 0;
52}