Simulation Core
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

redis_io.c 2.0 KiB

1 år sedan
1 år sedan
1 år sedan
1 år sedan
1 år sedan
1 år sedan
1 år sedan
1 år sedan
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include "../include/hiredis.h"
  5. redisContext *context;
  6. char *result,*key;
  7. void addnums( int* a, int* b )
  8. {
  9. int c = (*a) + (*b); /* convert pointers to values, then add them */
  10. printf("sum of %i and %i is %i\n", (*a), (*b), c );
  11. }
  12. void initConnection(char *address, int *port,char * password,char *datakey)
  13. {
  14. printf("Intializing Connection with %s@%s:%d\n",password,address,*port);
  15. context = redisConnect(address,*port);//"127.0.0.1", 6379);
  16. if (context == NULL || context->err) {
  17. if (context) {
  18. printf("Error: %s\n", context->errstr);
  19. // handle error
  20. } else {
  21. printf("Can't allocate redis context\n");
  22. }
  23. }
  24. printf("Connection Stablished to %s",address);
  25. if(strlen(password)>0)
  26. {
  27. redisReply *reply= redisCommand(context, "AUTH %s", password);
  28. if (reply->type == REDIS_REPLY_ERROR) {
  29. printf("Authentication failed.\n");
  30. }
  31. printf("Authentication is done.\n");
  32. freeReplyObject(reply);
  33. }
  34. // key = datakey;
  35. key = malloc(sizeof(char) * (strlen(datakey)+1));
  36. strcpy(key,datakey);
  37. printf("datakey = %s with len %ld\n",datakey,strlen(datakey));
  38. printf("...");
  39. }
  40. void setData(char *part, char *data)
  41. {
  42. redisReply *reply;
  43. // printf("%zu chars written\n",strlen(data));
  44. reply = redisCommand(context, "SET %s.%s %s",key,part,data);
  45. freeReplyObject(reply);
  46. }
  47. char *getData(int *len)
  48. {
  49. redisReply *reply;
  50. // printf("reading data from redis(c function)\n");
  51. reply = redisCommand(context, "GET %s.in",key);
  52. // printf("data read from redis: %s\n",reply->str);
  53. // printf("reply->str: %s\n",reply->str);
  54. result = (char*) malloc(strlen(reply->str));
  55. strcpy(result,reply->str);
  56. *len = strlen(result);
  57. freeReplyObject(reply);
  58. return result;
  59. }
  60. void deallocData()
  61. {
  62. free(result);
  63. free(context);
  64. free(key);
  65. }