/*
Copyright 2009 Nathan Phillip Brink, Ethan Zonca, Matthew Orlando
This file is a part of DistRen.
DistRen is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
DistRen is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with DistRen. If not, see .
*/
#include "mysql.h"
#include
#include "common/asprintf.h"
#include "common/protocol.h"
#include
#include
#include
#include
#include
#include
/**
local types
*/
#define SEVENTY_FIVE 75
#define FORTY_TWO 42
struct distrend_mysql_con
{
MYSQL *mysqlconn;
short pointlesscheck;
};
struct distrend_mysql_result
{
MYSQL_RES *mysqlresult;
short pointlesscheck;
};
/**
funcs
*/
int mysqlConnect(struct distrend_mysql_con **conn)
{
MYSQL *mysqlconn;
char *server = "zserver1";
char *user = "distren";
char *password = "secretpassword";
char *database = "distren";
mysqlconn = mysql_init(NULL);
mysql_options(mysqlconn, MYSQL_OPT_RECONNECT,"true");
if(!mysql_real_connect(mysqlconn, server, user, password, database, 0, NULL, 0))
{
fprintf(stderr, "%s\n", mysql_error(mysqlconn));
return 1;
}
*conn = malloc(sizeof(struct distrend_mysql_con));
if(!*conn)
{
mysql_close(mysqlconn);
mysql_server_end();
return 2;
}
(*conn)->mysqlconn = mysqlconn;
(*conn)->pointlesscheck = SEVENTY_FIVE;
return 0;
}
int mysqlDisconnect(distrend_mysql_con_t conn)
{
/**
check if this handle is valid
*/
if(conn->pointlesscheck != SEVENTY_FIVE)
fprintf(stderr, "warning, I was passed a bad struct distrend_mysql_con...\n");
/**
invalidate handle :-D
*/
conn->pointlesscheck ++;
mysql_close(conn->mysqlconn);
mysql_server_end();
free(conn);
return 0;
}
distrend_mysql_result_t mysqlQuery(distrend_mysql_con_t conn, char *query)
{
MYSQL_RES *result;
distrend_mysql_result_t distrenresult;
/**
pointless sanity check
*/
if(conn->pointlesscheck != SEVENTY_FIVE)
fprintf(stderr, "warning, I was passed a bad struct distrend_mysql_con...\n");
/** make sure that connection is still alive
*/
if(mysql_ping(conn->mysqlconn))
fprintf(stderr, "MySQL Connection _was_ broken or may be broken, I'm not sure exactly what this return value means\n");
if (mysql_query(conn->mysqlconn, query))
fprintf(stderr, "%s\n", mysql_error(conn->mysqlconn));
result = mysql_use_result(conn->mysqlconn);
distrenresult = malloc(sizeof(struct distrend_mysql_result));
if(!distrenresult)
{
mysql_free_result(result);
return NULL;
}
distrenresult->mysqlresult = result;
distrenresult->pointlesscheck = FORTY_TWO;
return distrenresult;
}
int mysqlResultFree(distrend_mysql_result_t result)
{
if(result->pointlesscheck != FORTY_TWO)
fprintf(stderr, "%s:%d: I didn't get the type of handle I wanted\n", __FILE__, __LINE__);
mysql_free_result(result->mysqlresult);
free(result);
return 0;
}
/**
Querying Functions
*/
/** Finish-Setter: Sets a frame to the "completed" status.*/
// @QUERY: update `distren`.`Job` set `Finish_Confirmed`=1 where `Job_Key`='1' and `Frame` = somenumber;
void finish_frame(int slaveKey, int jobKey, int frameNum, distrend_mysql_con_t conn)
{
MYSQL_RES *result;
MYSQL_ROW row;
char *query = "update `distren`.`Job` set `Finish_Confirmed`=1 where `Job_Key`='1' and `Frame` = somenumber;";
result = mysqlQuery(conn, query);
// Check to make sure the slavekey is the one who rendered the frame before finishing :D
/*
distrenjob->frameset[frame].status = FRAMESETSTATUS_DONE;
distrenjob->total_render_time = distrenjob->total_render_time + (clock() - distrenjob->frameset[frame].start_time);
distrenjob->completed_frames ++;
distrenjob->assigned_frames --;
geninfo->total_frames_rendered ++; //< Increase total frames var for stats
update_general_info(geninfo);
updateJobStatsXML(distrenjob);
*/
}
/** Changes the priority of an existing (and maybe running) job. @arg head I may end up changing the head if job == head */
// @QUERY: update `distren`.`Job` set `Priority`='12', `Finish_Confirmed`=1 where `Job_Key`='1';
int change_job_priority(int jobKey, int newPriority, distrend_mysql_con_t conn){
/*
struct distrenjob *current_job;
struct distrenjob *prev_job;
char *serialname;
distrenjob_remove(geninfo, job);
job->priority = new_priority;
prev_job = &geninfo->head;
if(job->frameset[0].status == FRAMESETSTATUS_UNASSIGNED)
// if job was not yet started
{
distrenjob_enqueue(geninfo, job);
return 0;
}
// iterate through linked list of jobs
for(current_job = &geninfo->head;
current_job != NULL
&& job->priority > current_job->priority;
current_job = current_job->next)
prev_job = current_job;
prev_job->next = job;
job->next = current_job;
update_xml_joblist(geninfo);
// reserialize after changes
job_getserialfilename(&serialname, geninfo, job->jobnum, 0);
distrenjob_serialize(job, serialname);
free(serialname);
*/
return 0;
}
/**
Frame Finder: Finds a frame for a slave to render
*/
// @QUERY: Frame_Get()
int find_jobframe(int slaveKey, int *jobKey, int *frameNum, distrend_mysql_con_t conn)
{
/*
if(geninfo->hibernate)
return 1;
unsigned int frame_counter;
unsigned short int found;
struct distrenjob *distrenjob_ptr;
found = 0;
// iterate through jobs from first to last
for(distrenjob_ptr = geninfo->head.next; distrenjob_ptr && !distrenjob_ptr->hibernate; distrenjob_ptr = distrenjob_ptr->next)
{
for(frame_counter = (distrenjob_ptr->prev_frame_index + 1); frame_counter < distrenjob_ptr->total_frames; frame_counter ++)
{
if(distrenjob_ptr->frameset[frame_counter].status == FRAMESETSTATUS_UNASSIGNED) // jobframe found
{
found = 1;
distrenjob_ptr->frameset[frame_counter].status = FRAMESETSTATUS_ASSIGNED;
distrenjob_ptr->frameset[frame_counter].start_time = clock();
distrenjob_ptr->assigned_frames++;
distrenjob_ptr->prev_frame_index = frame_counter;
updateJobStatsXML(distrenjob_ptr);
}
if(found)
break;
}
if(found)
break;
}
if(!found)
{
fprintf(stderr, "No more jobs to render\n");
sleep(1); //< @todo eliminate the need for this line
return 1;
}
*job = distrenjob_ptr;
*frame = &distrenjob_ptr->frameset[frame_counter];
*/
return 0;
}