Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <QtCore>
- #include <QtSql>
- #include <QString>
- #include <iostream>
- #include <string>
- #include <unistd.h>
- #include <stdio.h>
- #include <sstream>
- #include <fstream>
- using namespace std;
- int state;
- int ERRORFOUND = 0;
- //Database info
- QString DB_HOST;
- QString DB_NAME;
- QString DB_USER;
- QString DB_PASSWORD;
- //Prerequirements for later functions
- QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
- QSqlQuery query(db);
- //Local slave info stored on local system
- int LOC_SLAVE_ID;
- QString LOC_SLAVE_STATUS;
- int LOC_SLAVE_AVAILABLE;
- int LOC_SLAVE_ASSIGNED;
- unsigned long int LOC_SLAVE_FRAME;
- QString LOC_SLAVE_IP;
- QString LOC_RENDER_NAME;
- //Database variables
- QString DB_SLAVE_IP;
- int VARIABLE_COMPARISON;
- #define STR_EQUAL 0
- //CFG File
- ifstream FILE_CONFIG("settings.cfg");
- string line;
- int line_number = 0;
- void CFG_LOAD()
- {
- if(FILE_CONFIG.is_open())
- {
- cout << "Successful settings.cfg read" << endl;
- while(getline(FILE_CONFIG, line))
- {
- line_number++;
- if(line_number == 1)
- {
- DB_HOST = QString::fromStdString(line);
- }
- else if(line_number == 2)
- {
- DB_NAME = QString::fromStdString(line);
- }
- else if(line_number == 3)
- {
- DB_USER = QString::fromStdString(line);
- }
- else if(line_number == 4)
- {
- DB_PASSWORD = QString::fromStdString(line);
- }
- else if(line_number == 5)
- {
- cout << "Done!" << endl;
- break;
- }
- cout << line << endl;
- }
- FILE_CONFIG.close();
- }
- else
- {
- cout << "Failed to open settings.cfg" << endl;
- ERRORFOUND = 1;
- }
- }
- void DB_CONNECT()//Connect to database
- {
- cout << "DEBUG: DB_CONNECT()" << endl;//Comment if needed
- db.setHostName(DB_HOST);
- db.setPort(3306);
- db.setDatabaseName(DB_NAME);
- db.setUserName(DB_USER);
- db.setPassword(DB_PASSWORD);
- }
- void DB_FIRSTLOG()//Requests return data entries if already exists
- {
- cout << "DEBUG: DB_FIRSTLOG()" << endl;
- //Force LOC_SLAVE_IP to 10.0.0.12 for testing
- LOC_SLAVE_IP = "10.0.0.6";
- //Retrieve variable from database for comparison
- query.prepare("SELECT IP, ID FROM SLAVES WHERE IP = :IP");
- query.bindValue(":IP", LOC_SLAVE_IP);
- query.exec();
- //query.next();
- //qDebug() << db.lastError();
- DB_SLAVE_IP = query.value(0).toString();
- LOC_SLAVE_ID = query.value(1).toInt();
- DB_SLAVE_IP = "10.0.0.5";
- qDebug() << DB_SLAVE_IP << endl;
- if(QString::compare(LOC_SLAVE_IP, DB_SLAVE_IP) == STR_EQUAL)
- {
- cout << "ENTRY FOUND" << endl;
- query.prepare("UPDATE SLAVES SET STATUS = 'AVAILABLE' where ID = :ID");
- query.bindValue(":ID", LOC_SLAVE_ID);
- state = 1;
- //qDebug() << db.lastError();
- }
- else if(QString::compare(LOC_SLAVE_IP, DB_SLAVE_IP) != STR_EQUAL)
- {
- cout << "CREATING ENTRY" << endl;
- //query.prepare("INSERT INTO SLAVES (IP, STATUS) VALUES(:IP, 'AVAILABLE')");
- //query.bindValue(":IP", LOC_SLAVE_IP);
- query.prepare("INSERT INTO SLAVES (IP, STATUS, FRAME) VALUES(:IP, :STATUS, 0)");
- query.bindValue(":IP", LOC_SLAVE_IP);
- query.bindValue(":STATUS", "AVAILABLE");
- query.exec();
- //qDebug() << db.lastError();
- }
- //qDebug() << db.lastError();
- state = 1;
- }
- void DB_SCAN()//Checks for entry changes and a
- {
- //load DB contents into memory
- query.prepare("SELECT STATUS, FRAME FROM SLAVES WHERE IP = :IP");
- query.bindValue(":IP", LOC_SLAVE_IP);
- query.exec();
- query.next();
- LOC_SLAVE_STATUS = query.value(0).toString();
- LOC_SLAVE_FRAME = query.value(1).toInt();
- query.prepare("SELECT RENDERNAME FROM RENDER");
- query.exec();
- query.next();
- LOC_RENDER_NAME = query.value(0).toString();
- cout << "FRAME: " << LOC_SLAVE_FRAME << endl;
- if(LOC_SLAVE_STATUS == "ASSIGNED")
- {
- cout << "ASSIGNED = TRUE" << endl << endl;
- query.prepare("UPDATE SLAVES SET STATUS = 'RENDERING' WHERE ID = :ID");
- query.bindValue(":ID", LOC_SLAVE_ID);
- query.exec();
- state = 2;
- }
- else if(LOC_SLAVE_STATUS == "AVAILABLE")
- {
- cout << "AVAILABLE" << endl;
- }
- }
- void DB_RENDER()
- {
- cout << "DB_RENDER" << endl;
- stringstream ssFRAME;//Declare variable ssFRAME
- ssFRAME << LOC_SLAVE_FRAME;//Send LOC_SLAVE_FRAME contents to ssFRAME
- string strFRAME = ssFRAME.str();//Declare variable strFRAME equal to ssFRAME.str()
- string strRENDER_NAME = LOC_RENDER_NAME.toUtf8().constData();
- string rendercommand = "blender -b " + strRENDER_NAME + ".blend -o /home/user/rendered/f_########## -f " + strFRAME;
- cout << rendercommand << endl;
- if(system(rendercommand.c_str()) == 0)
- {
- cout << "Success" << endl;
- //cleanup
- query.prepare("UPDATE SLAVES SET STATUS = 'AVAILABLE', FRAME = 0 WHERE ID = :ID");
- query.bindValue(":ID", LOC_SLAVE_ID);
- query.exec();
- state = 1;
- }
- else
- {
- cout << "Fail" << endl;
- }
- }
- int main()
- {
- cout << "Starting up..." << endl;
- //run startup functions
- CFG_LOAD();
- DB_CONNECT();
- if(!db.open()){qDebug() << db.lastError();}
- state = 0;//State 0 = first time startup
- while(true)
- {
- if(ERRORFOUND == 1)
- {
- return 1;
- break;
- }
- cout << "Loop" << endl;
- if(state == 0)//CHECK DB FOR PREVIOUS INFO
- {
- DB_FIRSTLOG();
- }
- else if(state == 1)//PARSE ONLY SELECTED DB IP
- {
- cout << "DB_SCAN" << endl;
- DB_SCAN();
- }
- else if(state == 2)
- {
- DB_RENDER();
- }
- usleep(1600000);
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment