Simulation Core
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

125 рядки
3.7 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. extern void set_fortran_string(void* fstring_ptr, int length, const char* str);
  8. void addnums( int* a, int* b )
  9. {
  10. int c = (*a) + (*b); /* convert pointers to values, then add them */
  11. printf("sum of %i and %i is %i\n", (*a), (*b), c );
  12. }
  13. void initConnection(char *address, int *port,char * password,char *datakey,int *returnValue)
  14. {
  15. // printf("Intializing Connection with %s@%s:%d\n",password,address,*port);
  16. context = redisConnect(address,*port);//"127.0.0.1", 6379);
  17. if (context == NULL || context->err) {
  18. if (context) {
  19. printf("Error: %s\n", context->errstr);
  20. // handle error
  21. } else {
  22. printf("Can't allocate redis context\n");
  23. }
  24. (*returnValue) = -1;
  25. return;
  26. }
  27. printf("Connection Stablished to %s\n",address);
  28. if(strlen(password)>0)
  29. {
  30. // printf("Authenticating with password %s (len={%zu})",password,strlen(password));
  31. redisReply *reply= redisCommand(context, "AUTH %s", password);
  32. if (reply->type == REDIS_REPLY_ERROR) {
  33. printf("Authentication failed.\n");
  34. (*returnValue) = -1;
  35. return;
  36. }
  37. else {
  38. printf("Authentication is done.\n");
  39. }
  40. freeReplyObject(reply);
  41. (*returnValue) = 1;
  42. }
  43. // key = datakey;
  44. key = malloc(sizeof(char) * (strlen(datakey)+1));
  45. strcpy(key,datakey);
  46. }
  47. void setData(char *part, char *data)
  48. {
  49. redisReply *reply;
  50. // printf("%zu chars written\n",strlen(data));
  51. reply = redisCommand(context, "SET %s.%s %s",key,part,data);
  52. freeReplyObject(reply);
  53. }
  54. void getData_bystr(void *s)
  55. {
  56. redisReply *reply;
  57. reply = redisCommand(context, "GET %s.in",key);
  58. // printf("strlen read from redis: %ld", strlen(reply->str));
  59. // strncpy(s,reply->str,strlen(reply->str));
  60. // printf("strlen read from redis: %ld", strlen(result));
  61. // printf("before free");
  62. freeReplyObject(reply);
  63. // printf("after free");
  64. set_fortran_string(s, strlen(reply->str), reply->str);
  65. return;
  66. }
  67. void getData_byfile()
  68. {
  69. redisReply *reply;
  70. reply = redisCommand(context, "GET %s.in",key);
  71. char filename[100];
  72. sprintf(filename,"/var/tmp/%s.txt",key);
  73. printf("filename=%s\n",filename);
  74. FILE* file = fopen(filename, "w");
  75. if (file == NULL) {
  76. printf("Failed to open the file.\n");
  77. return;
  78. }
  79. // Write a string to the file
  80. const char* message = "Hello, world!";
  81. fprintf(file, "%s", message);
  82. // Close the file
  83. fclose(file);
  84. freeReplyObject(reply);
  85. printf("String written to file successfully.\n");
  86. return;
  87. }
  88. char *getData(int *len)
  89. {
  90. redisReply *reply;
  91. // printf("reading data from redis (key=%s)\n",key);
  92. reply = redisCommand(context, "GET %s.in",key);
  93. printf("data read from redis: %ld chars\n",strlen(reply->str));
  94. // printf("len(reply->str): %ld\n",strlen(reply->str));
  95. // result = (char*) malloc(strlen(reply->str));
  96. result = (char*) malloc(*len);
  97. // printf("after malloc");
  98. strcpy(result,reply->str);
  99. int paddingLength = (*len) - strlen(reply->str);
  100. printf("len, padding length = %d, %d\n",*len,paddingLength);
  101. if (paddingLength > 0) {
  102. memset(result + strlen(reply->str), ' ', paddingLength-1);
  103. // result[*len-1] = '\0';
  104. }
  105. *len = strlen(reply->str);
  106. // printf("before free");
  107. freeReplyObject(reply);
  108. // printf("after free");
  109. return result;
  110. }
  111. void deallocData()
  112. {
  113. free(result);
  114. free(context);
  115. free(key);
  116. }