Simulation Core
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

79 satır
2.3 KiB

  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,int *returnValue)
  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. (*returnValue) = -1;
  24. return;
  25. }
  26. printf("Connection Stablished to %s\n",address);
  27. if(strlen(password)>0)
  28. {
  29. redisReply *reply= redisCommand(context, "AUTH %s", password);
  30. if (reply->type == REDIS_REPLY_ERROR) {
  31. printf("Authentication failed.\n");
  32. (*returnValue) = -1;
  33. return;
  34. }
  35. else {
  36. printf("Authentication is done.\n");
  37. }
  38. freeReplyObject(reply);
  39. (*returnValue) = 1;
  40. }
  41. // key = datakey;
  42. key = malloc(sizeof(char) * (strlen(datakey)+1));
  43. strcpy(key,datakey);
  44. }
  45. void setData(char *part, char *data)
  46. {
  47. redisReply *reply;
  48. // printf("%zu chars written\n",strlen(data));
  49. reply = redisCommand(context, "SET %s.%s %s",key,part,data);
  50. freeReplyObject(reply);
  51. }
  52. char *getData(int *len)
  53. {
  54. redisReply *reply;
  55. // printf("reading data from redis (key=%s)\n",key);
  56. reply = redisCommand(context, "GET %s.in",key);
  57. // printf("data read from redis: %ld chars\n",strlen(reply->str));
  58. // printf("len(reply->str): %ld\n",strlen(reply->str));
  59. // result = (char*) malloc(strlen(reply->str));
  60. result = (char*) malloc(*len);
  61. // printf("after malloc");
  62. strcpy(result,reply->str);
  63. *len = strlen(result);
  64. // printf("before free");
  65. freeReplyObject(reply);
  66. // printf("after free");
  67. return result;
  68. }
  69. void deallocData()
  70. {
  71. free(result);
  72. free(context);
  73. free(key);
  74. }