Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <mysql.h>
- #include <sys/socket.h>
- #include <sys/types.h>
- #include <fcntl.h>
- #include <netinet/in.h>
- #include <unistd.h>
- #include <time.h>
- #include <pthread.h>
- void *openSocket(void *arg);
- 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);
- void error(const char *msg);
- int addContent(unsigned int id, unsigned int type, unsigned int videoQ, unsigned int audioQ, char start[], char end[]);
- int updateDeviceSettings(unsigned int id, unsigned int videoQ, unsigned int audioQ, unsigned int location, unsigned int adhoc, unsigned int preference);
- int write_content(char query[]);
- int read_content(char query[]);
- unsigned int fixEndianess(unsigned char* array, int length);
- #define THREADPOOLSIZE 5
- #define BUFFSIZE 256
- #define DIRECTORYSIZE 56
- /* Compile with gcc -o -Wall server $(mysql_config --cflags) server.c $(mysql_config --libs) -pthread */
- void error(const char *msg)
- {
- perror(msg);
- exit(1);
- }
- int main(){
- int i;
- pthread_t sockets[THREADPOOLSIZE];
- for(i = 0;i < THREADPOOLSIZE; i++){
- pthread_create(&sockets[i], NULL, openSocket, NULL);
- }
- return (0);
- }
- void *openSocket(void *arg){
- struct sockaddr_in adrServer, adrClient;
- unsigned char buff[256];
- int sfdClient, sfdServer;
- int numChar = 1;
- int ServerAddrLen = sizeof(adrServer), ClientAddrLen = sizeof(adrClient);
- int outfd, status, numWrite = -1;
- int sqlstatus = 0, newDeviceID = 0;
- char directory[56];
- unsigned int id[4], type[2], audioQ[2], videoQ[2], location[2], preview[2], preference[2], adhoc[2], size[32];
- char start[32], end[32];
- pid_t pID, w;
- time_t start_time, end_time;
- while(1){
- //open the socket
- sfdServer = socket(AF_INET, SOCK_STREAM, 0);
- if (sfdServer < 0)
- error("ERROR opening socket");
- //set buffer values to zero
- bzero((char *) &adrServer, sizeof(adrServer));
- //Set the server address family to IP, not UNIX
- adrServer.sin_family = AF_INET;
- //set server to pull the IP using INADDR_ANY
- adrServer.sin_addr.s_addr = INADDR_ANY;
- //set arbitrary port of 3550
- adrServer.sin_port = htons(3550);
- //bind server socketfd
- if (bind(sfdServer, (struct sockaddr*)&adrServer, ServerAddrLen) < 0)
- error("ERROR binding");
- if(listen(sfdServer, 5))
- error("ERROR listening");
- int count = 0;
- printf("Waiting for connections....\n");
- sfdClient = accept(sfdServer, (struct sockaddr*)&adrClient, &ClientAddrLen);
- if(sfdClient < 0)
- error("ERROR accepting");
- printf("Connection Established.\n");
- //start timer
- start_time = time(NULL);
- //reading header info
- printf("Reading heading information.\n");
- numChar = read(sfdClient, buff, 14);
- parseHeader(buff, directory, id, type, audioQ, videoQ, location, preview, preference, adhoc, size, start, end);
- //checks if it was a settings update or not
- if(numChar != 0){
- //creates a file with both read/write perms in the directory specified by parseHeader
- outfd=open(directory, O_RDWR | O_CREAT, 0644);
- if (outfd < 0)
- error("Error opening file.");
- bzero(buff, 14);
- printf("Receiving video from client.\n");
- //recieving file
- while(numChar != 0){
- numChar = read(sfdClient, buff, 256);
- numWrite = write(outfd,buff,numChar);
- //set buffer to zero
- bzero(buff, 256);
- printf("NumChar: %d \n numWrite: %d \n", numChar, numWrite);
- }
- if ((unsigned int)type == 2) {
- printf("Video File Received" );
- /*pid_t pID = fork();
- if(pID == -1){
- error("Fork error: \n");
- }
- //child
- if(pID == 0) {
- // use execv with ffmpeg
- // then use waitpid until conversion finished
- // write to database
- } else {
- //w = wait(&status);
- //addContent(unsigned int id, unsigned int type, unsigned int videoQ, unsigned int audioQ, char start[], char end[])
- }*/
- //settings update
- } else{
- if(id == 0){
- newDeviceID = addDeviceSettings(type, videoQ, audioQ, location+1, adhoc+1, preference+1);
- //send to parse into header for device
- //write the header back to device
- //numWrite = write(sfdClient, buffer, 14);
- }else{
- sqlstatus = updateDeviceSettings(id, videoQ, audioQ, location+1, adhoc+1, preference+1);
- }
- printf("Settings update sent.\n");
- }
- //stop timer
- end_time = time(NULL);
- printf("Seconds passed: %.2f\n", difftime(end_time,start_time));
- close(sfdClient);
- }
- }
- return;
- }
- //returns the full directory listing for the file
- 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){
- int i;
- for(i=0;i<DIRECTORYSIZE;i++){
- directory[i] = 0;
- }
- strcat(directory,"/home/media/");
- unsigned int type=0, size=0, start=0, end=0, id=0, videoQ=0, audioQ=0, location=0, preview=0, preference=0, adhoc=0;
- unsigned short settings=0;
- char cId[4], cStart[32];
- char *extention;
- char sTime[80], eTime[80];
- time_t startTime, endTime;
- struct tm ts1, ts2;
- settings = (unsigned short) fixEndianess(header,2);
- printf("\n%x",settings);
- *id = (settings>>10) & 0x3F;
- *type = (settings>>8) & 0x3;
- *audioQ = (settings>>6) & 0x3;
- *videoQ = (settings>>4) & 0x3;
- *location = (settings>>3) & 0x1;
- *preview = (settings>>2) & 0x1;
- *preference = (settings>>1) & 0x1;
- *adhoc = settings & 0x1;
- *size = fixEndianess(&(header[2]),4);
- start = fixEndianess(&(header[6]),4);
- end = fixEndianess(&(header[10]),4);
- //convert start and end times to proper formats
- startTime = start;
- ts1 = *localtime(&startTime);
- strftime(sTime, sizeof(sTime),"%Y-%m-%d %H:%M:%S", &ts1);
- endTime = end;
- ts2 = *localtime(&endTime);
- strftime(eTime, sizeof(eTime),"%Y-%m-%d %H:%M:%S", &ts2);
- *start = sTime;
- *end = eTime;
- printf("\nID: %u\n",id);
- printf("Type: %u\n",type);
- printf("Audio Quality: %u\n",audioQ);
- printf("Video Quality: %u\n",videoQ);
- printf("File location: %u\n",location);
- printf("Preview on/off: %u\n",preview);
- printf("Connection Preference: %u\n",preference);
- printf("Adhoc permission: %u\n",adhoc);
- printf("File Size: %u\n",size);
- printf("Start Time: %s\n",sTime);
- printf("End Time: %s\n",eTime);
- //settings update
- if(type == 0){
- return;
- //sending video need directory
- } else{
- switch(type){
- case 1:
- extention = ".jpg";
- break;
- case 2:
- extention = ".mp4";
- break;
- case 3:
- extention = ".amr";
- break;
- default:
- extention = ".mp4";
- break;
- }
- //addContent(id, type, videoQ, audioQ, sTime, eTime);
- sprintf(cId, "%i\0", id);
- sprintf(cStart, "%i\0", start);
- strcat(directory, cId);
- strcat(directory,"/");
- strcat(directory, cStart);
- strcat(directory, extention);
- return;
- }
- }
- //new device
- unsigned int addDeviceSettings(unsigned int type, unsigned int videoQ, unsigned int audioQ, unsigned int location, unsigned int adhoc, unsigned int preference){
- //INSERT INTO `helios_device` (recording_mode, visual_quality, audio_quality, storage_location, adhoc, connection_preference)
- //VALUES({type},{videoQ},{audioQ},{location},{adhoc},{preference})
- char query[256] = """INSERT INTO helios_device (recording_mode, visual_quality, audio_quality, storage_location, adhoc, connection_preference) VALUES(";
- char cType[2];
- char cVideoQ[2];
- char cAudioQ[2];
- char cLocation[2];
- char cAdhoc[2];
- char cPreference[2];
- unsigned int id[4] = {1};
- int status;
- //convert unsigned ints to strings
- sprintf(cType,"%i\0", type);
- sprintf(cVideoQ, "%i\0", videoQ);
- sprintf(cAudioQ, "%i\0", audioQ);
- sprintf(cLocation, "%i\0", location);
- sprintf(cAdhoc, "%i\0", adhoc);
- sprintf(cPreference, "%i\0", preference);
- //insert the parameters into query string
- strcat(query, cType);
- strcat(query, ", ");
- strcat(query, cVideoQ);
- strcat(query, ", ");
- strcat(query, cAudioQ);
- strcat(query, ", ");
- strcat(query, cLocation);
- strcat(query, ", ");
- strcat(query, cAdhoc);
- strcat(query, ", ");
- strcat(query, cPreference);
- strcat(query, ")""");
- status = write_content(query, id);
- return id;
- }
- //builds the query to write to the DB, sends query to write_content
- int addContent(unsigned int id, unsigned int type, unsigned int videoQ, unsigned int audioQ, char start[], char end[]){
- char query[1024] = """INSERT INTO helios_content (device_id, recording_mode, video_quality, audio_quality, start, end) VALUES(";
- char cId[4];
- char cType[2];
- char cVideoQ[2];
- char cAudioQ[2];
- //char cStart[32];
- //char cEnd[32];
- //convert unsigned ints to strings
- sprintf(cId, "%i\0", id);
- sprintf(cType, "%i\0", type);
- sprintf(cVideoQ,"%i\0", videoQ);
- sprintf(cAudioQ, "%i\0", audioQ);
- //insert the parameters into query string
- strcat(query, cId);
- strcat(query, ", ");
- strcat(query, cType);
- strcat(query, ", ");
- strcat(query, cVideoQ);
- strcat(query, ", ");
- strcat(query, cAudioQ);
- strcat(query, ", ");
- strcat(query, start);
- strcat(query, ", ");
- strcat(query, end);
- strcat(query, ")""");
- printf("\n%s",query);
- return write_content(query, 0);
- }
- //builds the query that updates the settings in the DB using write_content
- int updateDeviceSettings(unsigned int id, unsigned int videoQ, unsigned int audioQ, unsigned int location, unsigned int adhoc, unsigned int preference){
- /* SQL Command
- UPDATE `seniordesign`.`helios_device` SET video_quality = {videoQ},audio_quality = {audioQ},storage_location = {location},adhoc = {adhoc},
- connection_preference = {preference} WHERE helios_device.id ={id};
- */
- char query[1024] = """UPDATE `seniordesign`.`helios_device` SET video_quality=";
- char cId[4];
- char cVideoQ[2];
- char cAudioQ[2];
- char cLocation[4];
- char cAdhoc[2];
- char cPreference[2];
- //convert unsigned ints to strings
- sprintf(cId, "%i\0", id);
- sprintf(cVideoQ,"%i\0", videoQ);
- sprintf(cAudioQ, "%i\0", audioQ);
- sprintf(cLocation, "%i\0", location);
- sprintf(cAdhoc, "%i\0", adhoc);
- sprintf(cPreference, "%i\0", preference);
- //insert the parameters into query string
- strcat(query, cVideoQ);
- strcat(query, ", audio_quality=");
- strcat(query, cAudioQ);
- strcat(query, ", storage_location=");
- strcat(query, cLocation);
- strcat(query, ", adhoc=");
- strcat(query, cAdhoc);
- strcat(query, ", connection_preference=");
- strcat(query, cPreference);
- strcat(query, " WHERE helios_device.id=");
- strcat(query, cId);
- strcat(query, ";""");
- printf("\n%s",query);
- return write_content(query, 0);
- }
- int readSettings(int id){
- //SQL Command
- //"SELECT `recording_mode`+0 , `visual_quality` +0, `audio_quality` +0, `storage_location`+0 , `adhoc` +0, `connection_preference`+0 FROM `helios_device` WHERE `id` ={id}"
- 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` =";
- char cId[4];
- sprintf(cId,"%i\0",id);
- strcat(query, cId);
- strcat(query,";""");
- return read_content(query);
- }
- //writes query to DB . If ID is 0 then its an UPDATE, else its an INSERT
- int write_content(char query[], int* id ){
- MYSQL *mysql;
- MYSQL_RES *result;
- MYSQL_ROW row;
- MYSQL_FIELD *fields;
- //DB info
- char *server = "localhost";
- char *user = "seniordesign";
- char *password = "aud}hopv";
- char *database = "seniordesign";
- int status;
- mysql = mysql_init(NULL);
- if (mysql_real_connect (mysql, server, user, password,
- database, 0, NULL, CLIENT_MULTI_STATEMENTS) == NULL)
- {
- printf("mysql_real_connect() failed\n");
- mysql_close(mysql);
- exit(1);
- }
- if(id == 0){
- status = mysql_query(mysql,query);
- } else{
- status = mysql_query(mysql,query);
- *id = mysql_insert_id(mysql);
- }
- if (status){
- printf("Could not execute statement(s)\n");
- mysql_close(mysql);
- exit(0);
- return -1;
- }
- /* process each statement result */
- do {
- /* did current statement return data? */
- result = mysql_store_result(mysql);
- if (result)
- {
- /* yes; process rows and free the result set */
- while ((row = mysql_fetch_row(result)) != NULL)
- mysql_free_result(result);
- }
- else /* no result set or error */
- {
- if (mysql_field_count(mysql) == 0)
- {
- printf("%lld rows affected\n",
- mysql_affected_rows(mysql));
- }
- else /* some error occurred */
- {
- printf("Could not retrieve result set\n");
- break;
- }
- }
- /* more results? -1 = no, >0 = error, 0 = yes (keep looping) */
- if ((status = mysql_next_result(mysql)) > 0)
- printf("Could not execute statement\n");
- return -1;
- } while (status == 0);
- mysql_close(mysql);
- }
- //reads settings from DB
- int read_content(char query[]){
- MYSQL *mysql;
- MYSQL_RES *result;
- MYSQL_ROW row;
- MYSQL_FIELD *fields;
- //DB info
- char *server = "localhost";
- char *user = "seniordesign";
- char *password = "aud}hopv";
- char *database = "seniordesign";
- //local variables
- int status;
- unsigned int settings[8];
- mysql = mysql_init(NULL);
- if (mysql_real_connect (mysql, server, user, password,
- database, 0, NULL, CLIENT_MULTI_STATEMENTS) == NULL)
- {
- printf("mysql_real_connect() failed\n");
- mysql_close(mysql);
- exit(1);
- }
- status = mysql_query(mysql,query);
- if (status){
- printf("Could not execute statement(s)\n");
- mysql_close(mysql);
- exit(0);
- return -1;
- }
- /* process each statement result */
- do {
- /* did current statement return data? */
- result = mysql_store_result(mysql);
- if (result)
- {
- /* yes; process rows and free the result set */
- while ((row = mysql_fetch_row(result)) != NULL)
- for(i = 0; i < 6; i++){
- settings[i] = atoi(row[i]);
- }
- mysql_free_result(result);
- }
- else /* no result set or error */
- {
- if (mysql_field_count(mysql) == 0)
- {
- printf("%lld rows affected\n",
- mysql_affected_rows(mysql));
- }
- else /* some error occurred */
- {
- printf("Could not retrieve result set\n");
- break;
- }
- }
- /* more results? -1 = no, >0 = error, 0 = yes (keep looping) */
- if ((status = mysql_next_result(mysql)) > 0)
- printf("Could not execute statement\n");
- return -1;
- } while (status == 0);
- //makeHeader(settings);
- mysql_close(mysql);
- }
- unsigned int fixEndianess(unsigned char* array, int length){
- unsigned int value = 0;
- int i;
- for(i = 0;i<length;i++){
- value += array[i]<<(i*8);
- }
- return value;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement