Changeset - 9b974d776960
[Not reviewed]
default
0 4 0
Nathan Brink (binki) - 16 years ago 2010-02-09 21:55:10
ohnobinki@ohnopublishing.net
abstract away the mysql interface for normaldotcom
4 files changed with 110 insertions and 33 deletions:
0 comments (0 inline, 0 general)
Makefile.am
Show inline comments
 
@@ -51,25 +51,25 @@ SERVER_SOURCES = src/server/slavefuncs.c
 
	src/server/slavefuncs.h \
 
	src/server/distrenjob.c \
 
	src/server/distrenjob.h
 
# distrend:
 
distrend_CFLAGS = $(AM_CFLAGS) $(MYSQL_CFLAGS)
 
distrend_LDFLAGS = $(AM_LDFLAGS) $(MYSQL_LDFLAGS)
 
distrend_SOURCES = $(SERVER_SOURCES) \
 
	src/server/distrend.c \
 
	src/server/distrend.h \
 
	src/server/user_mgr.c \
 
	src/server/user_mgr.h \
 
	src/server/listen.h \
 
	src/server/listen.c \ 
 
	src/server/listen.c \
 
	src/server/mysql.h \
 
	src/server/mysql.c
 
distrend_LDADD = libdistrencommon.la
 
# distrenslave:
 
distrenslave_SOURCES = $(SERVER_SOURCES) \
 
	src/server/slave.c
 
distrenslave_LDADD = libdistrencommon.la
 

	
 

	
 

	
 
# configuration files:
 
dist_sysconf_DATA = etc/distrenclient.conf \
src/server/distrend.c
Show inline comments
 
@@ -155,26 +155,30 @@ int main(int argc, char *argv[])
 

	
 
  /** preset paths */
 
  _distren_asprintf(&general_info.files.geninfo, "%s/general_info.xml",
 
		    general_info.config->datadir);
 

	
 
  if(start_data(&general_info))
 
    {
 
      fprintf(stderr, "%s:%d: start_data() failed\n", __FILE__, __LINE__);
 
      return 1;
 
    }
 

	
 
  /** MySQL Connection */
 
  MYSQL *conn;
 
  *conn = mysqlConnect();
 
  struct distrend_mysql_con *conn;
 
  if(mysqlConnect(&conn))
 
    {
 
      fprintf(stderr, "%s:%d: mysqlConnect() failed\n", __FILE__, __LINE__);
 
      return 1;
 
    }
 

	
 
  /** pre-loaded jobs for testing */
 
  prepare_distrenjob(&general_info, 1, "awesome", "LordOfWar", 8, 1, 100, 640, 480);
 
  prepare_distrenjob(&general_info, 1, "hamburger", "ohnobinki", 3, 1, 50, 1280, 720);
 

	
 
  /** Execute test function */
 
  interactiveTest(test, general_info);
 

	
 
  distrend_listen(general_info.config, &clients);
 

	
 
  /* Main Loop */
 
  while(!general_info.config->die)
 
@@ -197,36 +201,34 @@ int main(int argc, char *argv[])
 
	    {
 
	      fprintf(stderr,"No frames are available to render at this time. Idling...\n");
 
	      sleep(10);
 
	    }
 
	  else
 
	    remotio_send_to_client(frame->num, job->jobnum); // Pseudo-sends data to client
 
	}
 
      /* If the client states that they finished the frame */
 
      	if(clientsays == DISTREN_REQUEST_DONEFRAME){
 
      	  clientstatus = CLIENTSTATUS_IDLE; // Sets the client back to idle
 
      	  finish_frame(&general_info, job, frame->num); // @TODO: Make sure this actually works.
 
      	}
 
      	// Check the connection to the database
 
      	mysqlPing(conn);
 
    } /* while(!general_info.config->die) */
 

	
 
  distrend_unlisten(general_info.config->listens, clients);
 
  distrend_config_free(general_info.config);
 

	
 
  xmlcleanup();
 

	
 
  /** free() paths */
 
  free(general_info.files.geninfo);
 
  mysql_close(conn);
 
  mysqlDisconnect(conn);
 

	
 
  return 0;
 
}
 

	
 
/* ********************** Functions ************************* */
 

	
 
/** Dumps all data in RAM to an xml file (such as current jobs, etc) which is parsed by start_data. Remember to invoke this before shutting down! @TODO: Fill this stub*/
 
int xml_dump()
 
{
 
  return 0;
 
}
 
/**
src/server/mysql.c
Show inline comments
 
@@ -21,49 +21,103 @@
 
#include "mysql.h"
 
#include <mysql/mysql.h>
 

	
 
#include "common/asprintf.h"
 

	
 
#include <stdio.h>
 
#include <string.h>
 
#include <unistd.h>
 
#include <stdlib.h>
 
#include <sys/stat.h>
 
#include <fcntl.h>
 

	
 
MYSQL mysqlConnect(){
 
  MYSQL *conn;
 
/**
 
   local types
 
 */
 

	
 
#define SEVENTY_FIVE 75
 

	
 
struct distrend_mysql_con 
 
{
 
  MYSQL *mysqlconn;
 
  short pointlesscheck;
 
};
 

	
 
struct distrend_mysql_result
 
{
 
  MYSQL_RES *result;
 
};
 

	
 
/** 
 
    funcs
 
 */
 

	
 
int mysqlConnect(struct distrend_mysql_con **conn)
 
{
 
  MYSQL *mysqlconn;
 

	
 
  char *server = "zserver1";
 
  char *user = "distren";
 
  char *password = "secretpassword";
 
  char *database = "distren";
 

	
 
  conn = mysql_init(NULL);
 
  mysql_options(conn, MYSQL_OPT_RECONNECT,"true");
 
  mysqlconn = mysql_init(NULL);
 
  mysql_options(mysqlconn, MYSQL_OPT_RECONNECT,"true");
 

	
 
  if(!mysql_real_connect(conn, server, user, password, database, 0, NULL, 0))
 
    fprintf(stderr, "%s\n", mysql_error(conn));
 
  if(!mysql_real_connect(mysqlconn, server, user, password, database, 0, NULL, 0))
 
    {
 
      fprintf(stderr, "%s\n", mysql_error(mysqlconn));
 
      return 1;
 
    }
 

	
 
  return *conn;
 
  *conn = malloc(sizeof(struct distrend_mysql_con));
 
  (*conn)->mysqlconn = mysqlconn;
 
  (*conn)->pointlesscheck = SEVENTY_FIVE;
 

	
 
  return 0;
 
}
 

	
 
MYSQL_RES mysqlQuery(MYSQL *conn, char *query){
 
  if (mysql_query(conn, query))
 
     fprintf(stderr, "%s\n", mysql_error(conn));
 

	
 
  MYSQL_RES *result;
 
  result = mysql_use_result(conn);
 
  return *result;
 
}
 
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");
 

	
 
// Check if connection is alive, reconnect if broken.
 
int mysqlPing(MYSQL *conn){
 
  if(mysql_ping(conn)){
 
    fprintf(stderr, "MySQL Connection broken, attempting reconnection...");
 
    return 1;
 
  }
 
  else
 
    return 0; // Connection is still alive
 
  /**
 
     invalidate handle :-D
 
   */
 
  conn->pointlesscheck ++;
 

	
 
  mysql_close(conn->mysqlconn);
 
  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));
 
  return distrenresult;
 
}
 

	
src/server/mysql.h
Show inline comments
 
@@ -13,19 +13,40 @@
 
  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 <http://www.gnu.org/licenses/>.
 

	
 
*/
 

	
 

	
 
#ifndef MYSQL_H_
 
#define MYSQL_H_
 

	
 
#include <mysql/mysql.h>
 
struct distrend_mysql_con;
 
struct distrend_mysql_result;
 

	
 
typedef struct distrend_mysql_con *distrend_mysql_con_t;
 
typedef struct distrend_mysql_restul *distrend_mysql_result_t;
 

	
 
/**
 
   initiates a MySQL connection
 
   @param conn, pointer will be set to the struct
 
   @return 0 on success
 
 */
 
int mysqlConnect(distrend_mysql_con_t *conn);
 

	
 
MYSQL mysqlConnect();
 
MYSQL_RES mysqlQuery(MYSQL *conn, char *query);
 
int mysqlPing(MYSQL *conn);
 
/**
 
   cleans and disconnects MySQL connection
 
   @param conn connection to clean
 
   @return 0 onh success
 
*/
 
int mysqlDiscconnect(distrend_mysql_con_t conn);
 

	
 
/**
 
   performs mysql query.
 
   errors will be logged to the user by this function.
 
   @return pointer to query handle on success, NULL on failure
 
 */
 
distrend_mysql_result_t mysqlQuery(struct distrend_mysql_con *conn, char *query);
 

	
 
#endif /* MYSQL_H_ */
0 comments (0 inline, 0 general)