Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/ipc.h>
- #include <sys/shm.h>
- #include <stdlib.h>
- #include <string.h>
- #include "shmemtypes.h"
- #include <hiredis/hiredis.h>
- #include <mariadb/mysql.h>
- int count_puts=0;
- int put_redis(redisContext *c, char * f);
- int get_redis(redisContext *c );
- unsigned int flag = 0;
- unsigned int port = 3306;
- static char *host="localhost";
- static char *user="root";
- static char *pass="";
- static char *dbname="res_1917";
- static char *unix_socket = NULL;
- MYSQL *conn;
- void sqlconnect(){
- conn = mysql_init(NULL);
- if (!(mysql_real_connect(conn,host,user,pass,dbname,port,unix_socket,flag))){
- exit(1);
- }
- }
- char st[255];
- void sqlQuery(char *string){
- // printf(string);
- char er[4040];
- char er2[2000];
- snprintf(er2, sizeof er2, "%s", string );
- snprintf(er, sizeof er, "INSERT INTO group_1917 (diap, data) VALUES ('0','%s');", er2 );
- printf("Длина строки - %d символов \n",strlen (string));
- printf("Длина строки - %d символов \n",strlen (er));
- printf("Длина строки - %s символов \n",er);
- sprintf(st ,"INSERT INTO group_1917 (diap, data) VALUES ('0','%s');",er );
- if (mysql_query(conn,er))
- {
- fprintf(stderr, "%s\n", mysql_error(conn));
- }
- }
- void sqlDisconect() {
- mysql_close(conn);
- }
- int main(int argc, char *argv[]) {
- key_t key;
- int shmid;
- struct memory_block *mblock;
- key = ftok(FTOK_FILE,1);
- if (key == -1){
- printf("Failed to generate unique key. Server compiler with a wrong name?\n");
- return EXIT_FAILURE;
- }
- shmid = shmget(key, sizeof(struct memory_block), 0666 | IPC_CREAT);
- //
- redisContext *c = redisConnect("127.0.0.1", 6379);
- if (c == NULL || c->err) {
- if (c) {
- printf("Error: %s\n", c->errstr);
- // handle error
- } else {
- printf("Can't allocate redis context\n");
- }
- }
- ///
- printf("Server start\n");
- mblock = (struct memory_block *) shmat(shmid,0,0);
- mblock->turn = CLIENT;
- mblock->server_lock = FREE;
- mblock->client_lock =FREE;
- mblock->readlast = SERVER;
- /// Work whis generator
- /*
- while (strcmp("q\n",mblock->srting) != 0){
- mblock->server_lock =BUSY;
- mblock->turn = CLIENT;
- while ((mblock->client_lock ==BUSY) && (mblock->turn == CLIENT));
- if (mblock->readlast==CLIENT){
- mblock->readlast = SERVER;
- put_redis(c, mblock->srting);
- count_puts++;
- if(strcmp("q\n",mblock->srting) != 0)
- strcpy(mblock->srting, "Ok");
- mblock->server_lock = FREE;
- }
- }*/
- sqlconnect();
- get_redis(c);
- sqlDisconect();
- printf("Server got q and exits\n");
- shmdt((void *) mblock);
- shmctl(shmid,IPC_RMID,0);
- return EXIT_SUCCESS;
- }
- char * key;
- unsigned long i;
- redisReply * typeReply;
- redisReply * lengthReply;
- long double counts[5];
- long double sums[5];
- int get_redis(redisContext * c ){
- redisReply *keysReply;
- redisReply *reply;
- keysReply = redisCommand(c, "LRANGE input_float_0 0 -1");
- /* for(i=0;i<keysReply->elements;i++)
- {
- key = keysReply->element[i]->str;
- // printf("value = %s\n", key);
- reply = redisCommand(c, "APPEND group_0 %s;", key);
- // printf("SET: %s\n", reply->str);
- freeReplyObject(reply);
- }
- */
- reply = redisCommand(c, "GET group_0");
- sqlQuery(reply->str);
- }
- int put_redis(redisContext *c ,char * f){
- redisReply *reply;
- char s[20];
- snprintf(s, sizeof s, "%s_%c", "input_float", f[0] );
- // rt_0_count
- // printf("%s\n", s);
- // SET rt_0 9.5556877;
- // SET rt_9_0 9.56646
- // GET rt_9_*
- printf("RPUSH %s %s\n", "input_float", f);
- reply = redisCommand(c, "RPUSH %s %s", s, f);
- // reply = redisCommand(c, "SET %s%u %s", "rt_", count_puts, f);
- printf("SET: %s\n", reply->str);
- freeReplyObject(reply);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement