Advertisement
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;
- int LOC_CURRENT_FRAME;
- int LOC_TOTAL_FRAME;
- //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_CLEANUP()
- {
- cout << "CLEANUP TABLE: SLAVES" << endl;
- query.prepare("DROP TABLE SLAVES");
- query.exec();
- query.next();
- query.prepare("CREATE TABLE SLAVES ( ID int NOT NULL AUTO_INCREMENT, IP varchar(255) NOT NULL, STATUS varchar(255) NOT NULL, FRAME int NOT NULL, PRIMARY KEY (ID) )");
- query.exec();
- query.next();
- query.prepare("SHOW TABLES");
- query.exec();
- query.next();
- //query.prepare("INSERT INTO SLAVES (IP, STATUS, FRAME) VALUES (1, 'AVAILABLE', 1) WHERE ID = 1");
- //query.exec();
- //query.next();
- }
- void DB_RENDERINFO()
- {
- cout << "DEBUG: DB_RENDERINFO()" << endl;
- //Load information about project from the RENDER table
- query.prepare("SELECT RENDERNAME, CURRENTFRAME, TOTALFRAME FROM RENDER");
- query.exec();
- query.next();
- LOC_RENDER_NAME = query.value(0).toString();
- LOC_CURRENT_FRAME = query.value(1).toInt();
- LOC_TOTAL_FRAME = query.value(2).toInt();
- state = 1;
- }
- void DB_SCAN()//Checks for entry changes and a
- {
- query.prepare("UPDATE RENDER SET CURRENTFRAME = :CURRENTFRAME");
- query.bindValue(":CURRENTFRAME", LOC_CURRENT_FRAME);
- query.exec();
- //query.next();
- LOC_SLAVE_ID = 0;
- //load DB contents into memory
- query.prepare("SELECT ID FROM SLAVES WHERE STATUS = 'AVAILABLE' LIMIT 1");
- query.exec();
- query.next();
- LOC_SLAVE_ID = query.value(0).toInt();
- if(LOC_SLAVE_ID > 0)
- {
- query.prepare("UPDATE SLAVES SET STATUS = 'ASSIGNED', FRAME = :FRAME WHERE ID = :ID");
- query.bindValue(":ID", LOC_SLAVE_ID);
- query.bindValue(":FRAME", LOC_CURRENT_FRAME);
- query.exec();
- LOC_CURRENT_FRAME++;
- }
- else
- {
- cout << "Nope" << 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 /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();
- cout << "DB_CONNECT() FINISHED" << endl;
- //if(!db.open()){qDebug() << db.lastError();}
- db.open();
- query.first();
- DB_CLEANUP();
- state = 0;//State 0 = first time startup
- while(true)
- {
- cout << "Loop" << endl;
- if(state == 0)//CHECK DB FOR PREVIOUS INFO
- {
- //DB_FIRSTLOG();
- DB_RENDERINFO();
- //pass to state 1
- state = 1;
- }
- else if(state == 1)//PARSE ONLY SELECTED DB IP
- {
- cout << "DB_SCAN" << endl;
- if(LOC_CURRENT_FRAME <= LOC_TOTAL_FRAME)
- {
- DB_SCAN();
- }
- else
- {
- state = 2;
- }
- }
- else if(state == 2)
- {
- //DB_RENDER();
- cout << "DONE" << endl;
- }
- usleep(1600000);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement