#include #include #include #include "../include/hiredis.h" redisContext *context; char *result,*key; void addnums( int* a, int* b ) { int c = (*a) + (*b); /* convert pointers to values, then add them */ printf("sum of %i and %i is %i\n", (*a), (*b), c ); } void initConnection(char *address, int *port,char * password,char *datakey) { printf("Intializing Connection with %s@%s:%d\n",password,address,*port); context = redisConnect(address,*port);//"127.0.0.1", 6379); if (context == NULL || context->err) { if (context) { printf("Error: %s\n", context->errstr); // handle error } else { printf("Can't allocate redis context\n"); } } printf("Connection Stablished to %s",address); if(strlen(password)>0) { redisReply *reply= redisCommand(context, "AUTH %s", password); if (reply->type == REDIS_REPLY_ERROR) { printf("Authentication failed.\n"); } printf("Authentication is done.\n"); freeReplyObject(reply); } // key = datakey; key = malloc(sizeof(char) * (strlen(datakey)+1)); strcpy(key,datakey); printf("datakey = %s with len %ld\n",datakey,strlen(datakey)); printf("..."); } void setData(char *part, char *data) { redisReply *reply; // printf("%zu chars written\n",strlen(data)); reply = redisCommand(context, "SET %s.%s %s",key,part,data); freeReplyObject(reply); } char *getData(int *len) { redisReply *reply; // printf("reading data from redis(c function)\n"); reply = redisCommand(context, "GET %s.in",key); // printf("data read from redis: %s\n",reply->str); // printf("reply->str: %s\n",reply->str); result = (char*) malloc(strlen(reply->str)); strcpy(result,reply->str); *len = strlen(result); freeReplyObject(reply); return result; } void deallocData() { free(result); free(context); free(key); }