Распределенная служба кодирования — страница 5

  • Просмотров 260
  • Скачиваний 5
  • Размер файла 38
    Кб

&servaddr.sin_addr, myaddr, sizeof(myaddr)); printf("serv addres %s\nserv port %d\n", myaddr, SERV_PORT); if ( (bind( fdserver, (struct sockaddr *) &servaddr, sizeof(servaddr))) < 0) { printf("binding error\n"); close(fdserver); return 1; } if ( (listen(fdserver, 1024)) < 0) { printf("listening error\n"); close(fdserver); return 1; } char strrecv[MAXLINE], *sendflag, *sendname, *sendlink, *command; char SQL_QUESTION[] = "SELECT * FROM goal WHERE at_work = 0"; char SQL_UPDATE[] = "UPDATE rsk.goal g SET at_work = 1 WHERE link = \""; //char *strsend //strsend = (char *) malloc ( sizeof( char ) * MAXLINE ); command = (char *) malloc ( sizeof( char ) * MAXLINE); sendflag = (char *) malloc ( sizeof( char ) * MAXLINE);

sendname = (char *) malloc ( sizeof( char ) * MAXLINE); sendlink = (char *) malloc ( sizeof( char ) * MAXLINE); for(;;) { client_len = sizeof(cliaddr); if ((fdclient = accept(fdserver, (struct sockaddr *) &cliaddr, &client_len)) < 0) { printf("accepting error\n"); close(fdserver); return 1; } if ( ( childpid = fork() ) == 0 ) // дочерний процесс { /*** child ***/ close(fdserver); // закрываем прослушивающий сокет /** send encoding script **/ int exitflag = 0; do { // Выполняем SQL-запрос strcpy(command, SQL_QUESTION); if( mysql_query((MYSQL *)rskdb, command) != 0) { printf("Error: can't execute SQL-query\n"); send(fdclient, "exit", MAXLINE, 0); break; } // Получаем

дескриптор результирующей таблицы table = mysql_store_result((MYSQL *)rskdb); if(table == NULL) { printf("Error: can't get the result description\n"); send(fdclient, "exit", MAXLINE, 0); break; } // Если имеется хотя бы одна запись - выводим // список каталогов if(mysql_num_rows(table) > 0) { row = mysql_fetch_row(table); sendlink = row[1]; sendname = row[3]; strcpy(command, SQL_UPDATE); // ... " strcat(command, sendlink); // link strcat(command, "\""); // " mysql_query((MYSQL *)rskdb, command); send(fdclient, "next goal", MAXLINE, 0); printf("next goal\n"); printf("I send this name : %s\n", sendname); send(fdclient,

sendname, MAXLINE, 0); printf("I send this link : %s\n", sendlink); send(fdclient, sendlink, MAXLINE, 0); } else { send(fdclient, "exit", MAXLINE, 0); exitflag = 1; printf("Задачи кончились !!! bingo gl hf\n"); break; } // Освобождаем память, занятую результирующей таблицей mysql_free_result(table); // тут мы сможем узнать удачно завершилось кодирование или нет recv(fdclient, strrecv, MAXLINE, 0); printf("client answer %s\n", strrecv); } while (exitflag != 1); close(fdserver); exit(0); } } /** end **/ // Закрываем соединение с сервером базы данных mysql_close(rskdb); close(fdserver); return 0;

} server.conf ***port 2011 ***mysql_host localhost ***mysql_user rsk ***mysql_pass 123 ***mysql_database rsk ***mysql_port 3306 ***end Кодирующий скрипт encode_script.sh #!/bin/bash # $1 - source file # $2 - target file # $3 - tmp file echo "sourse $1" echo target $2 echo tmp $3 echo streams $4 cp $1 $3 mv $3 $2 exit Клиент-Приложение client.c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <strings.h> #include <sys/socket.h> #include <sys/types.h> #include <unistd.h> #include <arpa/inet.h> #include <netinet/in.h> #include <errno.h> #include <curl.h> #include <types.h> #include <sys/stat.h> #include <fcntl.h> #include <easy.h>

#define MAXLINE 300 #define FILELINE 200 #define OPT_SIZE 1000 #define SCRIPT "encode_script.sh" #define MYTMP "tmp_files/tmp" #define FILETYPE ".mp4" struct FtpFile { const char *filename; FILE *stream; }; static size_t my_fwrite(void *buffer, size_t size, size_t nmemb, void *stream) { struct FtpFile *out=(struct FtpFile *)stream; if(out && !out->stream) { /* open file for writing */ out->stream=fopen(out->filename, "wb"); if(!out->stream) return -1; /* failure, can't open file to write */ } return fwrite(buffer, size, nmemb, out->stream); } static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *stream) { /* in real-world cases, this would probably get this data differently as this fread() stuff is