Advertisement
Svyatoch

Untitled

Nov 22nd, 2018
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.08 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <sys/ipc.h>
  4. #include <sys/shm.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include "shmemtypes.h"
  8.  
  9. #include <hiredis/hiredis.h>
  10.  
  11.  
  12. #include <mariadb/mysql.h>
  13.  
  14.  
  15.  
  16. int count_puts=0;
  17.  
  18. int put_redis(redisContext *c, char * f);
  19. int get_redis(redisContext *c );
  20.  
  21.  
  22. unsigned int flag = 0;
  23. unsigned int port = 3306;
  24. static char *host="localhost";
  25. static char *user="root";
  26. static char *pass="";
  27. static char *dbname="res_1917";
  28.  
  29. static char *unix_socket = NULL;
  30.  
  31. MYSQL *conn;
  32.  
  33. void sqlconnect(){
  34.     conn = mysql_init(NULL);
  35.     if (!(mysql_real_connect(conn,host,user,pass,dbname,port,unix_socket,flag))){
  36.         exit(1);
  37.     }
  38. }
  39.  
  40. char st[255];
  41. void sqlQuery(char *string){
  42. //    printf(string);
  43.     char er[4040];
  44.     char er2[2000];
  45.     snprintf(er2, sizeof er2, "%s", string  );
  46.     snprintf(er, sizeof er, "INSERT INTO group_1917 (diap, data) VALUES ('0','%s');", er2  );
  47.     printf("Длина строки  - %d символов \n",strlen (string));
  48.     printf("Длина строки  - %d символов \n",strlen (er));
  49.     printf("Длина строки  - %s символов \n",er);
  50.  
  51.     sprintf(st ,"INSERT INTO group_1917 (diap, data) VALUES ('0','%s');",er );
  52.     if (mysql_query(conn,er))
  53.     {
  54.         fprintf(stderr, "%s\n", mysql_error(conn));
  55.     }
  56. }
  57. void sqlDisconect() {
  58.     mysql_close(conn);
  59. }
  60. int main(int argc, char *argv[]) {
  61.     key_t  key;
  62.     int shmid;
  63.     struct memory_block *mblock;
  64.     key = ftok(FTOK_FILE,1);
  65.  
  66.  
  67.     if (key == -1){
  68.         printf("Failed to generate unique key. Server compiler with a wrong name?\n");
  69.         return EXIT_FAILURE;
  70.     }
  71.  
  72.     shmid = shmget(key, sizeof(struct memory_block), 0666 | IPC_CREAT);
  73.  
  74.     //
  75.  
  76.     redisContext *c = redisConnect("127.0.0.1", 6379);
  77.     if (c == NULL || c->err) {
  78.         if (c) {
  79.             printf("Error: %s\n", c->errstr);
  80.             // handle error
  81.         } else {
  82.             printf("Can't allocate redis context\n");
  83.         }
  84.     }
  85.     ///
  86.     printf("Server start\n");
  87.  
  88.     mblock = (struct memory_block *) shmat(shmid,0,0);
  89.     mblock->turn = CLIENT;
  90.     mblock->server_lock = FREE;
  91.     mblock->client_lock =FREE;
  92.     mblock->readlast = SERVER;
  93.  
  94.  
  95.  /// Work whis generator
  96. /*
  97.     while (strcmp("q\n",mblock->srting) != 0){
  98.         mblock->server_lock =BUSY;
  99.         mblock->turn = CLIENT;
  100.         while ((mblock->client_lock ==BUSY) && (mblock->turn == CLIENT));
  101.         if (mblock->readlast==CLIENT){
  102.             mblock->readlast = SERVER;
  103.             put_redis(c, mblock->srting);
  104.             count_puts++;
  105.             if(strcmp("q\n",mblock->srting) != 0)
  106.                 strcpy(mblock->srting, "Ok");
  107.             mblock->server_lock = FREE;
  108.         }
  109.     }*/
  110.  
  111.     sqlconnect();
  112.     get_redis(c);
  113.     sqlDisconect();
  114.     printf("Server got q and exits\n");
  115.     shmdt((void *) mblock);
  116.     shmctl(shmid,IPC_RMID,0);
  117.     return EXIT_SUCCESS;
  118. }
  119.  
  120.  
  121. char * key;
  122. unsigned long i;
  123.  
  124. redisReply * typeReply;
  125. redisReply * lengthReply;
  126.  
  127. long double counts[5];
  128. long double sums[5];
  129.  
  130. int get_redis(redisContext * c ){
  131.     redisReply *keysReply;
  132.     redisReply *reply;
  133.  
  134.     keysReply = redisCommand(c, "LRANGE input_float_0 0 -1");
  135.   /*  for(i=0;i<keysReply->elements;i++)
  136.     {
  137.         key = keysReply->element[i]->str;
  138. //        printf("value = %s\n", key);
  139.  
  140.         reply = redisCommand(c, "APPEND group_0 %s;", key);
  141. //        printf("SET: %s\n", reply->str);
  142.  
  143.         freeReplyObject(reply);
  144.     }
  145.     */
  146.     reply = redisCommand(c, "GET group_0");
  147.  
  148.     sqlQuery(reply->str);
  149. }
  150. int put_redis(redisContext *c ,char * f){
  151.     redisReply *reply;
  152.     char s[20];
  153.     snprintf(s, sizeof s, "%s_%c", "input_float", f[0]  );
  154. //    rt_0_count
  155. //    printf("%s\n", s);
  156. //    SET rt_0 9.5556877;
  157. //    SET rt_9_0 9.56646
  158. //    GET rt_9_*
  159.  
  160.  
  161.     printf("RPUSH %s %s\n", "input_float", f);
  162.        reply = redisCommand(c, "RPUSH %s %s", s, f);
  163. //       reply = redisCommand(c, "SET %s%u %s", "rt_", count_puts, f);
  164.     printf("SET: %s\n", reply->str);
  165.     freeReplyObject(reply);
  166.  
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement