PageRenderTime 21ms CodeModel.GetById 11ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/hiredis/example-ae.c

http://github.com/nicolasff/webdis
C | 56 lines | 44 code | 9 blank | 3 comment | 7 complexity | 8e4347022dbc8abb11bcd3d4300554f5 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/ae.h"
 8
 9/* Put event loop in the global scope, so it can be explicitly stopped */
10static aeEventLoop *loop;
11
12void getCallback(redisAsyncContext *c, void *r, void *privdata) {
13    redisReply *reply = r;
14    if (reply == NULL) return;
15    printf("argv[%s]: %s\n", (char*)privdata, reply->str);
16
17    /* Disconnect after receiving the reply to GET */
18    redisAsyncDisconnect(c);
19}
20
21void connectCallback(const redisAsyncContext *c, int status) {
22    if (status != REDIS_OK) {
23        printf("Error: %s\n", c->errstr);
24        return;
25    }
26    printf("Connected...\n");
27}
28
29void disconnectCallback(const redisAsyncContext *c, int status) {
30    if (status != REDIS_OK) {
31        printf("Error: %s\n", c->errstr);
32        return;
33    }
34    printf("Disconnected...\n");
35}
36
37int main (int argc, char **argv) {
38    signal(SIGPIPE, SIG_IGN);
39
40    redisAsyncContext *c = redisAsyncConnect("127.0.0.1", 6379);
41    if (c->err) {
42        /* Let *c leak for now... */
43        printf("Error: %s\n", c->errstr);
44        return 1;
45    }
46
47    loop = aeCreateEventLoop();
48    redisAeAttach(loop, c);
49    redisAsyncSetConnectCallback(c,connectCallback);
50    redisAsyncSetDisconnectCallback(c,disconnectCallback);
51    redisAsyncCommand(c, NULL, NULL, "SET key %b", argv[argc-1], strlen(argv[argc-1]));
52    redisAsyncCommand(c, getCallback, (char*)"end-1", "GET key");
53    aeMain(loop);
54    return 0;
55}
56