example-macosx.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. //
  2. // Created by Дмитрий Бахвалов on 13.07.15.
  3. // Copyright (c) 2015 Dmitry Bakhvalov. All rights reserved.
  4. //
  5. #include <stdio.h>
  6. #include <hiredis.h>
  7. #include <async.h>
  8. #include <adapters/macosx.h>
  9. void 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. /* Disconnect after receiving the reply to GET */
  14. redisAsyncDisconnect(c);
  15. }
  16. void connectCallback(const redisAsyncContext *c, int status) {
  17. if (status != REDIS_OK) {
  18. printf("Error: %s\n", c->errstr);
  19. return;
  20. }
  21. printf("Connected...\n");
  22. }
  23. void disconnectCallback(const redisAsyncContext *c, int status) {
  24. if (status != REDIS_OK) {
  25. printf("Error: %s\n", c->errstr);
  26. return;
  27. }
  28. CFRunLoopStop(CFRunLoopGetCurrent());
  29. printf("Disconnected...\n");
  30. }
  31. int main (int argc, char **argv) {
  32. signal(SIGPIPE, SIG_IGN);
  33. CFRunLoopRef loop = CFRunLoopGetCurrent();
  34. if( !loop ) {
  35. printf("Error: Cannot get current run loop\n");
  36. return 1;
  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. redisMacOSAttach(c, loop);
  45. redisAsyncSetConnectCallback(c,connectCallback);
  46. redisAsyncSetDisconnectCallback(c,disconnectCallback);
  47. redisAsyncCommand(c, NULL, NULL, "SET key %b", argv[argc-1], strlen(argv[argc-1]));
  48. redisAsyncCommand(c, getCallback, (char*)"end-1", "GET key");
  49. CFRunLoopRun();
  50. return 0;
  51. }