Advertisement
Guest User

Untitled

a guest
Aug 8th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 14.33 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <mysql.h>
  5. #include <sys/socket.h>
  6. #include <sys/types.h>
  7. #include <fcntl.h>
  8. #include <netinet/in.h>
  9. #include <unistd.h>
  10. #include <time.h>
  11. #include <pthread.h>
  12.  
  13. void *openSocket(void *arg);
  14. void parseHeader(unsigned char* header, char* directory, unsigned int* id, unsigned int* type, unsigned int* audioQ, unsigned int* videoQ, unsigned int* location, unsigned int* preview, unsigned int* preference, unsigned int* adhoc, unsigned int* size, char* start, char* end);
  15. void error(const char *msg);
  16. int addContent(unsigned int id, unsigned int type, unsigned int videoQ, unsigned int audioQ, char start[], char end[]);
  17. int updateDeviceSettings(unsigned int id, unsigned int videoQ, unsigned int audioQ, unsigned int location, unsigned int adhoc, unsigned int preference);
  18. int write_content(char query[]);
  19. int read_content(char query[]);
  20. unsigned int fixEndianess(unsigned char* array, int length);
  21.  
  22.  
  23. #define THREADPOOLSIZE 5
  24. #define BUFFSIZE 256
  25. #define DIRECTORYSIZE 56
  26.  
  27. /* Compile with gcc -o -Wall server $(mysql_config --cflags) server.c $(mysql_config --libs) -pthread */
  28.  
  29. void error(const char *msg)
  30. {
  31.     perror(msg);
  32.     exit(1);
  33. }
  34.  
  35. int main(){
  36.    
  37.     int i;
  38.    
  39.     pthread_t sockets[THREADPOOLSIZE];
  40.    
  41.     for(i = 0;i < THREADPOOLSIZE; i++){
  42.         pthread_create(&sockets[i], NULL, openSocket, NULL);
  43.     }
  44.    
  45.     return (0);
  46. }
  47.  
  48. void *openSocket(void *arg){
  49.    
  50.     struct sockaddr_in adrServer, adrClient;
  51.    
  52.     unsigned char buff[256];
  53.    
  54.     int sfdClient, sfdServer;
  55.     int numChar = 1;
  56.     int ServerAddrLen = sizeof(adrServer), ClientAddrLen = sizeof(adrClient);
  57.     int outfd, status, numWrite = -1;
  58.     int sqlstatus = 0, newDeviceID = 0;
  59.     char directory[56];
  60.     unsigned int id[4], type[2], audioQ[2], videoQ[2], location[2], preview[2], preference[2], adhoc[2], size[32];
  61.     char start[32], end[32];
  62.    
  63.     pid_t pID, w;
  64.     time_t start_time, end_time;
  65.    
  66.     while(1){
  67.     //open the socket
  68.     sfdServer = socket(AF_INET, SOCK_STREAM, 0);
  69.     if (sfdServer < 0)
  70.         error("ERROR opening socket");
  71.     //set buffer values to zero    
  72.     bzero((char *) &adrServer, sizeof(adrServer));
  73.     //Set the server address family to IP, not UNIX
  74.     adrServer.sin_family = AF_INET;
  75.     //set server to pull the IP using INADDR_ANY
  76.     adrServer.sin_addr.s_addr = INADDR_ANY;
  77.     //set arbitrary port of 3550
  78.     adrServer.sin_port = htons(3550);
  79.    
  80.     //bind server socketfd
  81.     if (bind(sfdServer, (struct sockaddr*)&adrServer, ServerAddrLen) < 0)
  82.         error("ERROR binding");
  83.     if(listen(sfdServer, 5))
  84.         error("ERROR listening");
  85.    
  86.         int count = 0;
  87.        
  88.         printf("Waiting for connections....\n");
  89.         sfdClient = accept(sfdServer, (struct sockaddr*)&adrClient, &ClientAddrLen);
  90.         if(sfdClient < 0)
  91.             error("ERROR accepting");
  92.    
  93.         printf("Connection Established.\n");
  94.  
  95.         //start timer
  96.         start_time = time(NULL);
  97.  
  98.         //reading header info
  99.         printf("Reading heading information.\n");
  100.         numChar = read(sfdClient, buff, 14);
  101.         parseHeader(buff, directory, id, type, audioQ, videoQ, location, preview, preference, adhoc, size, start, end);
  102.      
  103.         //checks if it was a settings update or not
  104.         if(numChar != 0){
  105.             //creates a file with both read/write perms in the directory specified by parseHeader
  106.             outfd=open(directory, O_RDWR | O_CREAT, 0644);
  107.             if (outfd < 0)
  108.                 error("Error opening file.");
  109.             bzero(buff, 14);
  110.             printf("Receiving video from client.\n");
  111.                 //recieving file
  112.                 while(numChar != 0){
  113.                     numChar = read(sfdClient, buff, 256);
  114.                     numWrite = write(outfd,buff,numChar);
  115.                     //set buffer to zero   
  116.                     bzero(buff, 256);
  117.  
  118.                     printf("NumChar: %d \n numWrite: %d \n", numChar, numWrite);
  119.                 }
  120.             if ((unsigned int)type == 2) {
  121.                 printf("Video File Received" );
  122.                 /*pid_t pID = fork();
  123.                
  124.                 if(pID == -1){
  125.                     error("Fork error: \n");
  126.                 }
  127.                 //child
  128.                 if(pID == 0) {
  129.                     // use execv with ffmpeg
  130.                     // then use waitpid until conversion finished
  131.                     // write to database
  132.                 } else {
  133.                     //w = wait(&status);
  134.                     //addContent(unsigned int id, unsigned int type, unsigned int videoQ, unsigned int audioQ, char start[], char end[])
  135.                 }*/
  136.    
  137.            
  138.         //settings update
  139.         } else{
  140.             if(id == 0){
  141.                 newDeviceID = addDeviceSettings(type, videoQ, audioQ, location+1, adhoc+1, preference+1);
  142.                 //send to parse into header for device
  143.                 //write the header back to device
  144.                 //numWrite = write(sfdClient,  buffer, 14);
  145.             }else{
  146.                 sqlstatus = updateDeviceSettings(id, videoQ, audioQ, location+1, adhoc+1, preference+1);
  147.             }
  148.             printf("Settings update sent.\n");
  149.            
  150.         }
  151.  
  152.         //stop timer
  153.         end_time = time(NULL);
  154.         printf("Seconds passed: %.2f\n", difftime(end_time,start_time));
  155.  
  156.         close(sfdClient);
  157.  
  158.         }
  159.    
  160.     }
  161.     return;
  162. }
  163.  
  164. //returns the full directory listing for the file
  165. void parseHeader(unsigned char* header, char* directory, unsigned int* id, unsigned int* type, unsigned int* audioQ, unsigned int* videoQ, unsigned int* location, unsigned int* preview, unsigned int* preference, unsigned int* adhoc, unsigned int* size, char* start, char* end){
  166.  
  167.     int i;
  168.     for(i=0;i<DIRECTORYSIZE;i++){
  169.         directory[i] = 0;  
  170.     }
  171.     strcat(directory,"/home/media/");
  172.    
  173.     unsigned int type=0, size=0, start=0, end=0, id=0, videoQ=0, audioQ=0, location=0, preview=0, preference=0, adhoc=0;
  174.     unsigned short settings=0;
  175.    
  176.     char cId[4], cStart[32];
  177.     char *extention;
  178.     char sTime[80], eTime[80];
  179.     time_t startTime, endTime;
  180.     struct tm ts1, ts2;
  181.    
  182.    
  183.         settings = (unsigned short) fixEndianess(header,2);
  184.         printf("\n%x",settings);
  185.         *id = (settings>>10) & 0x3F;
  186.         *type = (settings>>8) & 0x3;
  187.         *audioQ = (settings>>6) & 0x3;
  188.         *videoQ = (settings>>4) & 0x3;
  189.         *location = (settings>>3) & 0x1;
  190.         *preview = (settings>>2) & 0x1;
  191.         *preference = (settings>>1) & 0x1;
  192.         *adhoc = settings & 0x1;
  193.  
  194.         *size = fixEndianess(&(header[2]),4);
  195.         start = fixEndianess(&(header[6]),4);
  196.         end = fixEndianess(&(header[10]),4);
  197.    
  198.  
  199.     //convert start and end times to proper formats
  200.     startTime = start;
  201.     ts1 = *localtime(&startTime);
  202.     strftime(sTime, sizeof(sTime),"%Y-%m-%d %H:%M:%S", &ts1);
  203.    
  204.     endTime = end;
  205.     ts2 = *localtime(&endTime);
  206.     strftime(eTime, sizeof(eTime),"%Y-%m-%d %H:%M:%S", &ts2);
  207.    
  208.     *start = sTime;
  209.     *end = eTime;
  210.    
  211.     printf("\nID: %u\n",id);
  212.     printf("Type: %u\n",type);
  213.     printf("Audio Quality: %u\n",audioQ);
  214.     printf("Video Quality: %u\n",videoQ);
  215.     printf("File location: %u\n",location);
  216.     printf("Preview on/off: %u\n",preview);
  217.     printf("Connection Preference: %u\n",preference);
  218.     printf("Adhoc permission: %u\n",adhoc);
  219.     printf("File Size: %u\n",size);
  220.     printf("Start Time: %s\n",sTime);
  221.     printf("End Time: %s\n",eTime);
  222.    
  223.     //settings update
  224.     if(type == 0){
  225.         return;
  226.     //sending video need directory
  227.     } else{
  228.         switch(type){
  229.             case 1:
  230.                 extention = ".jpg";
  231.                 break;
  232.             case 2:
  233.                 extention = ".mp4";
  234.                 break;
  235.             case 3:
  236.                 extention = ".amr";
  237.                 break;
  238.             default:
  239.                 extention = ".mp4";
  240.                 break;
  241.             }  
  242.    
  243.         //addContent(id, type, videoQ, audioQ, sTime, eTime);
  244.        
  245.         sprintf(cId, "%i\0", id);
  246.         sprintf(cStart, "%i\0", start);
  247.        
  248.         strcat(directory, cId);
  249.         strcat(directory,"/");
  250.         strcat(directory, cStart);
  251.         strcat(directory, extention);
  252.    
  253.         return;
  254.     }
  255. }
  256.  
  257. //new device
  258. unsigned int addDeviceSettings(unsigned int type, unsigned int videoQ, unsigned int audioQ, unsigned int location, unsigned int adhoc, unsigned int preference){
  259. //INSERT INTO `helios_device` (recording_mode, visual_quality, audio_quality, storage_location, adhoc, connection_preference)
  260. //VALUES({type},{videoQ},{audioQ},{location},{adhoc},{preference})
  261.  
  262.     char query[256] = """INSERT INTO helios_device (recording_mode, visual_quality, audio_quality, storage_location, adhoc, connection_preference) VALUES(";
  263.  
  264.     char cType[2];
  265.     char cVideoQ[2];
  266.     char cAudioQ[2];
  267.     char cLocation[2];
  268.     char cAdhoc[2];
  269.     char cPreference[2];
  270.     unsigned int id[4] = {1};
  271.     int status;
  272.    
  273.     //convert unsigned ints to strings
  274.     sprintf(cType,"%i\0", type);
  275.     sprintf(cVideoQ, "%i\0", videoQ);
  276.     sprintf(cAudioQ, "%i\0", audioQ);
  277.     sprintf(cLocation, "%i\0", location);
  278.     sprintf(cAdhoc, "%i\0", adhoc);
  279.     sprintf(cPreference, "%i\0", preference);
  280.    
  281.     //insert the parameters into query string
  282.     strcat(query, cType);
  283.     strcat(query, ", ");
  284.     strcat(query, cVideoQ);
  285.     strcat(query, ", ");
  286.     strcat(query, cAudioQ);
  287.     strcat(query, ", ");
  288.     strcat(query, cLocation);
  289.     strcat(query, ", ");
  290.     strcat(query, cAdhoc);
  291.     strcat(query, ", ");
  292.     strcat(query, cPreference);
  293.     strcat(query, ")""");
  294.    
  295.     status = write_content(query, id);
  296.    
  297.     return id;
  298. }
  299. //builds the query to write to the DB, sends query to write_content
  300. int addContent(unsigned int id, unsigned int type, unsigned int videoQ, unsigned int audioQ, char start[], char end[]){
  301.  
  302.     char query[1024] = """INSERT INTO helios_content (device_id, recording_mode, video_quality, audio_quality, start, end) VALUES(";
  303.     char cId[4];
  304.     char cType[2];
  305.     char cVideoQ[2];
  306.     char cAudioQ[2];
  307.     //char cStart[32];
  308.     //char cEnd[32];
  309.    
  310.     //convert unsigned ints to strings
  311.     sprintf(cId, "%i\0", id);
  312.     sprintf(cType, "%i\0", type);
  313.     sprintf(cVideoQ,"%i\0", videoQ);
  314.     sprintf(cAudioQ, "%i\0", audioQ);
  315.    
  316.     //insert the parameters into query string
  317.     strcat(query, cId);
  318.     strcat(query, ", ");
  319.     strcat(query, cType);
  320.     strcat(query, ", ");
  321.     strcat(query, cVideoQ);
  322.     strcat(query, ", ");
  323.     strcat(query, cAudioQ);
  324.     strcat(query, ", ");
  325.     strcat(query, start);
  326.     strcat(query, ", ");
  327.     strcat(query, end);
  328.     strcat(query, ")""");
  329.    
  330.     printf("\n%s",query);
  331.    
  332.     return write_content(query, 0);
  333. }
  334.  
  335. //builds the query that updates the settings in the DB using write_content
  336. int updateDeviceSettings(unsigned int id, unsigned int videoQ, unsigned int audioQ, unsigned int location, unsigned int adhoc, unsigned int preference){
  337. /* SQL Command
  338. UPDATE  `seniordesign`.`helios_device` SET video_quality =  {videoQ},audio_quality =  {audioQ},storage_location =  {location},adhoc = {adhoc},
  339. connection_preference =  {preference} WHERE  helios_device.id ={id};
  340. */
  341.    
  342.     char query[1024] = """UPDATE `seniordesign`.`helios_device` SET video_quality=";
  343.     char cId[4];
  344.     char cVideoQ[2];
  345.     char cAudioQ[2];
  346.     char cLocation[4];
  347.     char cAdhoc[2];
  348.     char cPreference[2];
  349.    
  350.     //convert unsigned ints to strings
  351.     sprintf(cId, "%i\0", id);
  352.     sprintf(cVideoQ,"%i\0", videoQ);
  353.     sprintf(cAudioQ, "%i\0", audioQ);
  354.     sprintf(cLocation, "%i\0", location);
  355.     sprintf(cAdhoc, "%i\0", adhoc);
  356.     sprintf(cPreference, "%i\0", preference);
  357.    
  358.     //insert the parameters into query string
  359.     strcat(query, cVideoQ);
  360.     strcat(query, ", audio_quality=");
  361.     strcat(query, cAudioQ);
  362.     strcat(query, ", storage_location=");
  363.     strcat(query, cLocation);
  364.     strcat(query, ", adhoc=");
  365.     strcat(query, cAdhoc);
  366.     strcat(query, ", connection_preference=");
  367.     strcat(query, cPreference);
  368.     strcat(query, " WHERE helios_device.id=");
  369.     strcat(query, cId);
  370.     strcat(query, ";""");
  371.    
  372.     printf("\n%s",query);
  373.    
  374.     return write_content(query, 0);
  375.    
  376. }
  377.  
  378. int readSettings(int id){
  379. //SQL Command
  380. //"SELECT  `recording_mode`+0 ,  `visual_quality` +0,  `audio_quality` +0,  `storage_location`+0 ,  `adhoc` +0,  `connection_preference`+0 FROM  `helios_device` WHERE  `id` ={id}"
  381.  
  382.     char query[1024] = """SELECT  `recording_mode`+0 ,  `visual_quality` +0,  `audio_quality` +0,  `storage_location`+0 ,  `adhoc` +0,  `connection_preference`+0 FROM  `helios_device` WHERE  `id` =";
  383.     char cId[4];
  384.     sprintf(cId,"%i\0",id);
  385.    
  386.     strcat(query, cId);
  387.     strcat(query,";""");
  388.    
  389.     return read_content(query);
  390. }
  391.  
  392. //writes query to DB . If ID is 0 then its an UPDATE, else its an INSERT
  393. int write_content(char query[], int* id ){
  394.     MYSQL *mysql;
  395.     MYSQL_RES *result;
  396.     MYSQL_ROW row;
  397.     MYSQL_FIELD *fields;
  398.  
  399.     //DB info
  400.     char *server = "localhost";
  401.     char *user = "seniordesign";
  402.     char *password = "aud}hopv";
  403.     char *database = "seniordesign";
  404.     int status;
  405.  
  406.    mysql = mysql_init(NULL);
  407.  
  408.     if (mysql_real_connect (mysql, server, user, password,
  409.          database, 0, NULL, CLIENT_MULTI_STATEMENTS) == NULL)
  410.     {
  411.       printf("mysql_real_connect() failed\n");
  412.       mysql_close(mysql);
  413.       exit(1);
  414.     }
  415.     if(id == 0){
  416.         status = mysql_query(mysql,query);
  417.     } else{
  418.         status = mysql_query(mysql,query);
  419.         *id = mysql_insert_id(mysql);
  420.     }
  421.  
  422.     if (status){
  423.         printf("Could not execute statement(s)\n");
  424.         mysql_close(mysql);
  425.         exit(0);
  426.         return -1;
  427.     }
  428.  
  429.     /* process each statement result */
  430.     do {
  431.       /* did current statement return data? */
  432.       result = mysql_store_result(mysql);
  433.       if (result)
  434.       {
  435.         /* yes; process rows and free the result set */
  436.         while ((row = mysql_fetch_row(result)) != NULL)
  437.          
  438.  
  439.         mysql_free_result(result);
  440.       }
  441.       else    /* no result set or error */
  442.       {
  443.         if (mysql_field_count(mysql) == 0)
  444.         {
  445.           printf("%lld rows affected\n",
  446.                 mysql_affected_rows(mysql));
  447.         }
  448.         else  /* some error occurred */
  449.         {
  450.           printf("Could not retrieve result set\n");
  451.           break;
  452.         }
  453.       }
  454.       /* more results? -1 = no, >0 = error, 0 = yes (keep looping) */
  455.       if ((status = mysql_next_result(mysql)) > 0)
  456.         printf("Could not execute statement\n");
  457.         return -1;
  458.     } while (status == 0);
  459.  
  460.         mysql_close(mysql);
  461. }
  462.  
  463. //reads settings from DB
  464. int read_content(char query[]){
  465.     MYSQL *mysql;
  466.     MYSQL_RES *result;
  467.     MYSQL_ROW row;
  468.     MYSQL_FIELD *fields;
  469.  
  470.     //DB info
  471.     char *server = "localhost";
  472.     char *user = "seniordesign";
  473.     char *password = "aud}hopv";
  474.     char *database = "seniordesign";
  475.    
  476.     //local variables
  477.     int status;
  478.     unsigned int settings[8];
  479.    
  480.  
  481.    mysql = mysql_init(NULL);
  482.  
  483.     if (mysql_real_connect (mysql, server, user, password,
  484.          database, 0, NULL, CLIENT_MULTI_STATEMENTS) == NULL)
  485.     {
  486.       printf("mysql_real_connect() failed\n");
  487.       mysql_close(mysql);
  488.       exit(1);
  489.     }
  490.  
  491.     status = mysql_query(mysql,query);
  492.  
  493.     if (status){
  494.         printf("Could not execute statement(s)\n");
  495.         mysql_close(mysql);
  496.         exit(0);
  497.         return -1;
  498.     }
  499.  
  500.     /* process each statement result */
  501.     do {
  502.       /* did current statement return data? */
  503.       result = mysql_store_result(mysql);
  504.       if (result)
  505.       {
  506.         /* yes; process rows and free the result set */
  507.         while ((row = mysql_fetch_row(result)) != NULL)
  508.         for(i = 0; i < 6; i++){
  509.         settings[i] = atoi(row[i]);
  510.       }
  511.  
  512.         mysql_free_result(result);
  513.       }
  514.       else    /* no result set or error */
  515.       {
  516.         if (mysql_field_count(mysql) == 0)
  517.         {
  518.           printf("%lld rows affected\n",
  519.                 mysql_affected_rows(mysql));
  520.         }
  521.         else  /* some error occurred */
  522.         {
  523.           printf("Could not retrieve result set\n");
  524.           break;
  525.         }
  526.       }
  527.       /* more results? -1 = no, >0 = error, 0 = yes (keep looping) */
  528.       if ((status = mysql_next_result(mysql)) > 0)
  529.         printf("Could not execute statement\n");
  530.         return -1;
  531.     } while (status == 0);
  532.        
  533.         //makeHeader(settings);
  534.         mysql_close(mysql);
  535. }
  536.  
  537. unsigned int fixEndianess(unsigned char* array, int length){
  538.    
  539.     unsigned int value = 0;
  540.     int i;
  541.     for(i = 0;i<length;i++){
  542.         value += array[i]<<(i*8);
  543.     }
  544.    
  545.     return value;
  546.    
  547. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement