#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,int *returnValue) { // 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"); } (*returnValue) = -1; return; } printf("Connection Stablished to %s\n",address); if(strlen(password)>0) { redisReply *reply= redisCommand(context, "AUTH %s", password); if (reply->type == REDIS_REPLY_ERROR) { printf("Authentication failed.\n"); (*returnValue) = -1; return; } else { printf("Authentication is done.\n"); } freeReplyObject(reply); (*returnValue) = 1; } // key = datakey; key = malloc(sizeof(char) * (strlen(datakey)+1)); strcpy(key,datakey); } 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 (key=%s)\n",key); reply = redisCommand(context, "GET %s.in",key); // printf("data read from redis: %ld chars\n",strlen(reply->str)); // printf("len(reply->str): %ld\n",strlen(reply->str)); // result = (char*) malloc(strlen(reply->str)); result = (char*) malloc(*len); // printf("after malloc"); strcpy(result,reply->str); *len = strlen(result); // printf("before free"); freeReplyObject(reply); // printf("after free"); return result; } void deallocData() { free(result); free(context); free(key); }