Simulation Core
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

170 wiersze
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 *result,*key,*pass,*channel_in,*channel_out;
  11. extern void test();
  12. extern void setTongLever(int *v);
  13. extern void ButtonPress_Slips();
  14. extern void ToggleValve(int *valve_number);
  15. char *clone(char *s)
  16. {
  17. char *r = malloc(strlen(s)+1);
  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. char *getData(int *len)
  75. {
  76. redisReply *reply;
  77. reply = redisCommand(context, "GET %s.in",key);
  78. result = (char*) malloc(*len);
  79. strcpy(result,reply->str);
  80. int paddingLength = (*len) - strlen(reply->str);
  81. if (paddingLength > 0) {
  82. memset(result + strlen(reply->str), ' ', paddingLength-1);
  83. }
  84. *len = strlen(reply->str);
  85. freeReplyObject(reply);
  86. return result;
  87. }
  88. void deallocData()
  89. {
  90. free(result);
  91. free(context);
  92. free(key);
  93. }
  94. void onMessage(redisAsyncContext * c, void *reply, void * privdata) {
  95. int j,v;
  96. redisReply * r = reply;
  97. if (reply == NULL) return;
  98. printf("got a message of type: %i\n", r->type);
  99. if (r->type == REDIS_REPLY_ARRAY && r->elements==3) {
  100. if(strcmp(r->element[0]->str,"message")==0)
  101. if(strcmp(r->element[1]->str,channel_in)==0)
  102. {
  103. // char *message = malloc(srlen(r->element[2]->str));
  104. // strcpy(message,r->element[2]->str);
  105. char delimiter = ',';
  106. char *chptr = strchr(r->element[2]->str, delimiter);
  107. char *fn = strtok(r->element[2]->str,&delimiter);
  108. char *parameter = malloc(20);
  109. parameter = strtok(NULL,&delimiter);
  110. printf("calling %s\n",fn);
  111. if(strcmp(fn,"test")==0)
  112. test();
  113. else if(strcmp(fn,"TONG_LEVER_MAKEUP")==0){
  114. v = -1;
  115. setTongLever(&v);
  116. }
  117. else if(strcmp(fn,"TONG_LEVER_BREAKOUT")==0){
  118. v=1;
  119. setTongLever(&v);
  120. }else if(strcmp(fn,"BUTTON_PRESS_SLIPS")==0){
  121. ButtonPress_Slips();
  122. }else if(strcmp(fn,"ChangeValve")==0){
  123. int valve_number = atoi(parameter);
  124. ToggleValve(&valve_number);
  125. // }else if(strcmp(fn,"KellyConnected")==0){
  126. // KellyConnected();
  127. // }else if(strcmp(fn,"KellyDisconnected")==0){
  128. // KellyDisconnected();
  129. }else
  130. printf("message: %s\n",r->element[2]->str);
  131. }
  132. }
  133. else{
  134. for(int i=0;i<r->elements;i++)
  135. printf("%d) %s",i,r->element[i]->str);
  136. }
  137. }
  138. void listenTochannel()
  139. {
  140. printf("Listening To channel (C)\n");
  141. signal(SIGPIPE, SIG_IGN);
  142. struct event_base * base = event_base_new();
  143. redisLibeventAttach(asyncContext, base);
  144. redisAsyncCommand(asyncContext, onMessage, NULL, "SUBSCRIBE %s",channel_in);
  145. printf("Subscribed to channel %s\n",channel_in);
  146. event_base_dispatch(base);
  147. }
  148. void publishMessageToChannel(const char *message) {
  149. redisReply *reply;
  150. reply = redisCommand(context, "PUBLISH %s %s", channel_out, message);
  151. if (reply == NULL) {
  152. printf("Error: publishMessageToChannel failed.\n");
  153. }
  154. freeReplyObject(reply);
  155. }