Simulation Core
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

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