Simulation Core
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

168 lines
5.0 KiB

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <signal.h>
  5. #include "../include/hiredis.h"
  6. #include "../include/async.h"
  7. #include "../include/adapters/libevent.h"
  8. redisAsyncContext * asyncContext;
  9. redisContext *context;
  10. char *result,*key,*pass,*channel_in,*channel_out;
  11. extern void test();
  12. extern void setTongLever(int *v);
  13. extern void ButtonPress_Slips();
  14. void addnums( int* a, int* b )
  15. {
  16. int c = (*a) + (*b); /* convert pointers to values, then add them */
  17. printf("sum of %i and %i is %i\n", (*a), (*b), c );
  18. }
  19. void initConnection(char *address, int *port,char * password,char *datakey,int *returnValue)
  20. {
  21. context = redisConnect(address,*port);
  22. asyncContext = redisAsyncConnect(address, *port);
  23. if (context == NULL || context->err || asyncContext==NULL || asyncContext->err)
  24. {
  25. if (context) {
  26. printf("Error: %s\n", context->errstr);
  27. } else {
  28. printf("Can't allocate redis context\n");
  29. }
  30. (*returnValue) = -1;
  31. if (asyncContext) {
  32. printf("Error: %s\n", asyncContext->errstr);
  33. } else {
  34. printf("Can't allocate Redis Async Context\n");
  35. }
  36. (*returnValue) = -1;
  37. return;
  38. }
  39. printf("Connection Stablished to %s\n",address);
  40. if(strlen(password)>0)
  41. {
  42. // printf("Authenticating with password %s (len={%zu})",password,strlen(password));
  43. redisReply *reply= redisCommand(context, "AUTH %s", password);
  44. redisAsyncCommand(asyncContext, NULL,NULL,"AUTH %s", password);
  45. if (reply->type == REDIS_REPLY_ERROR) {
  46. printf("Authentication failed.\n");
  47. (*returnValue) = -1;
  48. return;
  49. }
  50. else {
  51. printf("Authentication is done.\n");
  52. }
  53. freeReplyObject(reply);
  54. (*returnValue) = 1;
  55. }
  56. key = malloc((sizeof(char) * (strlen(datakey)+1)));
  57. pass = malloc((sizeof(char) * (strlen(password)+1)));
  58. channel_in = malloc((sizeof(char) * (strlen(datakey)+7)));
  59. channel_out = malloc((sizeof(char) * (strlen(datakey)+8)));
  60. strcpy(key,datakey);
  61. strcpy(pass,password);
  62. strcpy(channel_in,datakey);
  63. strcpy(channel_out,datakey);
  64. strcat(channel_in,".ch_in");
  65. strcat(channel_out,".ch_out");
  66. }
  67. void setData(char *part, char *data)
  68. {
  69. redisReply *reply;
  70. // printf("%zu chars written\n",strlen(data));
  71. reply = redisCommand(context, "SET %s.%s %s",key,part,data);
  72. freeReplyObject(reply);
  73. }
  74. // void getData_bystr(void *s)
  75. // {
  76. // redisReply *reply;
  77. // reply = redisCommand(context, "GET %s.in",key);
  78. // freeReplyObject(reply);
  79. // set_fortran_string(s, strlen(reply->str), reply->str);
  80. // return;
  81. // }
  82. char *getData(int *len)
  83. {
  84. redisReply *reply;
  85. reply = redisCommand(context, "GET %s.in",key);
  86. result = (char*) malloc(*len);
  87. strcpy(result,reply->str);
  88. int paddingLength = (*len) - strlen(reply->str);
  89. if (paddingLength > 0) {
  90. memset(result + strlen(reply->str), ' ', paddingLength-1);
  91. }
  92. *len = strlen(reply->str);
  93. freeReplyObject(reply);
  94. return result;
  95. }
  96. void deallocData()
  97. {
  98. free(result);
  99. free(context);
  100. free(key);
  101. }
  102. void onMessage(redisAsyncContext * c, void *reply, void * privdata) {
  103. int j,v;
  104. redisReply * r = reply;
  105. if (reply == NULL) return;
  106. printf("got a message of type: %i\n", r->type);
  107. if (r->type == REDIS_REPLY_ARRAY && r->elements==3) {
  108. if(strcmp(r->element[0]->str,"message")==0)
  109. if(strcmp(r->element[1]->str,channel_in)==0)
  110. {
  111. char *fn = r->element[2]->str;
  112. printf("calling %s\n",fn);
  113. if(strcmp(fn,"test")==0)
  114. test();
  115. else if(strcmp(fn,"TONG_LEVER_MAKEUP")==0){
  116. v = -1;
  117. setTongLever(&v);
  118. }
  119. else if(strcmp(fn,"TONG_LEVER_BREAKOUT")==0){
  120. v=1;
  121. setTongLever(&v);
  122. }else if(strcmp(fn,"BUTTON_PRESS_SLIPS")==0){
  123. ButtonPress_Slips();
  124. }
  125. else
  126. printf("message: %s\n",r->element[2]->str);
  127. }
  128. }
  129. else{
  130. for(int i=0;i<r->elements;i++)
  131. printf("%d) %s",i,r->element[i]->str);
  132. }
  133. }
  134. void listenTochannel()
  135. {
  136. printf("Listening To channel (C)\n");
  137. signal(SIGPIPE, SIG_IGN);
  138. struct event_base * base = event_base_new();
  139. redisLibeventAttach(asyncContext, base);
  140. redisAsyncCommand(asyncContext, onMessage, NULL, "SUBSCRIBE %s",channel_in);
  141. printf("Subscribed to channel %s\n",channel_in);
  142. event_base_dispatch(base);
  143. }
  144. void publishMessageToChannel(const char *message) {
  145. redisReply *reply;
  146. reply = redisCommand(context, "PUBLISH %s %s", channel_out, message);
  147. if (reply == NULL) {
  148. printf("Error: publishMessageToChannel failed.\n");
  149. }
  150. freeReplyObject(reply);
  151. }