#include #include #include #include "../include/hiredis.h" redisContext *context; char *result,*key; extern void set_fortran_string(void* fstring_ptr, int length, const char* str); 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) { // printf("Authenticating with password %s (len={%zu})",password,strlen(password)); 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); } void getData_bystr(void *s) { redisReply *reply; reply = redisCommand(context, "GET %s.in",key); // printf("strlen read from redis: %ld", strlen(reply->str)); // strncpy(s,reply->str,strlen(reply->str)); // printf("strlen read from redis: %ld", strlen(result)); // printf("before free"); freeReplyObject(reply); // printf("after free"); set_fortran_string(s, strlen(reply->str), reply->str); return; } void getData_byfile() { redisReply *reply; reply = redisCommand(context, "GET %s.in",key); char filename[100]; sprintf(filename,"/var/tmp/%s.txt",key); printf("filename=%s\n",filename); FILE* file = fopen(filename, "w"); if (file == NULL) { printf("Failed to open the file.\n"); return; } // Write a string to the file const char* message = "Hello, world!"; fprintf(file, "%s", message); // Close the file fclose(file); freeReplyObject(reply); printf("String written to file successfully.\n"); return; } 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); int paddingLength = (*len) - strlen(reply->str); printf("len, padding length = %d, %d\n",*len,paddingLength); if (paddingLength > 0) { memset(result + strlen(reply->str), ' ', paddingLength-1); // result[*len-1] = '\0'; } *len = strlen(reply->str); // printf("before free"); freeReplyObject(reply); // printf("after free"); return result; } void deallocData() { free(result); free(context); free(key); }