Advertisement
Guest User

1

a guest
Sep 28th, 2016
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.88 KB | None | 0 0
  1. #include <QtCore>
  2. #include <QtSql>
  3. #include <QString>
  4.  
  5. #include <iostream>
  6. #include <string>
  7. #include <unistd.h>
  8. #include <stdio.h>
  9. #include <sstream>
  10.  
  11. #include <fstream>
  12.  
  13. using namespace std;
  14.  
  15. int state;
  16.  
  17. int ERRORFOUND = 0;
  18.  
  19. //Database info
  20. QString DB_HOST;
  21. QString DB_NAME;
  22. QString DB_USER;
  23. QString DB_PASSWORD;
  24. //Prerequirements for later functions
  25. QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
  26. QSqlQuery query(db);
  27.  
  28. //Local slave info stored on local system
  29. int LOC_SLAVE_ID;
  30. QString LOC_SLAVE_STATUS;
  31. int LOC_SLAVE_AVAILABLE;
  32. int LOC_SLAVE_ASSIGNED;
  33. unsigned long int LOC_SLAVE_FRAME;
  34. QString LOC_SLAVE_IP;
  35.  
  36. QString LOC_RENDER_NAME;
  37. int     LOC_CURRENT_FRAME;
  38. int     LOC_TOTAL_FRAME;
  39.  
  40. //Database variables
  41. QString DB_SLAVE_IP;
  42. int VARIABLE_COMPARISON;
  43. #define STR_EQUAL 0
  44.  
  45. //CFG File
  46. ifstream FILE_CONFIG("settings.cfg");
  47. string line;
  48. int line_number = 0;
  49.  
  50.  
  51.  
  52. void CFG_LOAD()
  53. {
  54.     if(FILE_CONFIG.is_open())
  55.     {
  56.         cout << "Successful settings.cfg read" << endl;
  57.  
  58.         while(getline(FILE_CONFIG, line))
  59.         {
  60.             line_number++;
  61.  
  62.             if(line_number == 1)
  63.             {
  64.                 DB_HOST = QString::fromStdString(line);
  65.             }
  66.  
  67.             else if(line_number == 2)
  68.             {
  69.                 DB_NAME = QString::fromStdString(line);
  70.             }
  71.  
  72.             else if(line_number == 3)
  73.             {
  74.                 DB_USER = QString::fromStdString(line);
  75.             }
  76.  
  77.             else if(line_number == 4)
  78.             {
  79.                 DB_PASSWORD = QString::fromStdString(line);
  80.             }
  81.  
  82.             else if(line_number == 5)
  83.             {
  84.                 cout << "Done!" << endl;
  85.                 break;
  86.             }
  87.  
  88.             cout << line << endl;
  89.         }
  90.  
  91.         FILE_CONFIG.close();
  92.     }
  93.  
  94.     else
  95.     {
  96.         cout << "Failed to open settings.cfg" << endl;
  97.         ERRORFOUND = 1;
  98.     }
  99.  
  100. }
  101.  
  102.  
  103.  
  104. void DB_CONNECT()//Connect to database
  105. {
  106.     cout << "DEBUG: DB_CONNECT()" << endl;//Comment if needed
  107.  
  108.     db.setHostName(DB_HOST);
  109.     db.setPort(3306);
  110.     db.setDatabaseName(DB_NAME);
  111.     db.setUserName(DB_USER);
  112.     db.setPassword(DB_PASSWORD);
  113.  
  114. }
  115.  
  116.  
  117.  
  118. void DB_CLEANUP()
  119. {
  120.     cout << "CLEANUP TABLE: SLAVES" << endl;
  121.  
  122.     query.prepare("DROP TABLE SLAVES");
  123.     query.exec();
  124.     query.next();
  125.  
  126.     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) )");
  127.     query.exec();
  128.     query.next();
  129.  
  130.     query.prepare("SHOW TABLES");
  131.     query.exec();
  132.     query.next();
  133.  
  134.     //query.prepare("INSERT INTO SLAVES (IP, STATUS, FRAME) VALUES (1, 'AVAILABLE', 1) WHERE ID = 1");
  135.     //query.exec();
  136.     //query.next();
  137.  
  138. }
  139.  
  140.  
  141.  
  142. void DB_RENDERINFO()
  143. {
  144.     cout << "DEBUG: DB_RENDERINFO()" << endl;
  145.  
  146.     //Load information about project from the RENDER table
  147.     query.prepare("SELECT RENDERNAME, CURRENTFRAME, TOTALFRAME FROM RENDER");
  148.     query.exec();
  149.     query.next();
  150.  
  151.     LOC_RENDER_NAME = query.value(0).toString();
  152.     LOC_CURRENT_FRAME = query.value(1).toInt();
  153.     LOC_TOTAL_FRAME = query.value(2).toInt();
  154.  
  155.     state = 1;
  156. }
  157.  
  158.  
  159.  
  160. void DB_SCAN()//Checks for entry changes and a
  161. {
  162.     query.prepare("UPDATE RENDER SET CURRENTFRAME = :CURRENTFRAME");
  163.     query.bindValue(":CURRENTFRAME", LOC_CURRENT_FRAME);
  164.     query.exec();
  165.     //query.next();
  166.  
  167.     LOC_SLAVE_ID = 0;
  168.  
  169.     //load DB contents into memory
  170.     query.prepare("SELECT ID FROM SLAVES WHERE STATUS = 'AVAILABLE' LIMIT 1");
  171.     query.exec();
  172.     query.next();
  173.  
  174.     LOC_SLAVE_ID = query.value(0).toInt();
  175.  
  176.     if(LOC_SLAVE_ID > 0)
  177.     {
  178.         query.prepare("UPDATE SLAVES SET STATUS = 'ASSIGNED', FRAME = :FRAME WHERE ID = :ID");
  179.         query.bindValue(":ID", LOC_SLAVE_ID);
  180.         query.bindValue(":FRAME", LOC_CURRENT_FRAME);
  181.         query.exec();
  182.  
  183.         LOC_CURRENT_FRAME++;
  184.     }
  185.  
  186.  
  187.     else
  188.     {
  189.         cout << "Nope" << endl;
  190.     }
  191.  
  192.  
  193. }
  194.  
  195.  
  196.  
  197. void DB_RENDER()
  198. {
  199.     cout << "DB_RENDER" << endl;
  200.  
  201.     stringstream ssFRAME;//Declare variable ssFRAME
  202.     ssFRAME << LOC_SLAVE_FRAME;//Send LOC_SLAVE_FRAME contents to ssFRAME
  203.     string strFRAME = ssFRAME.str();//Declare variable strFRAME equal to ssFRAME.str()
  204.     string strRENDER_NAME = LOC_RENDER_NAME.toUtf8().constData();
  205.     string rendercommand = "blender -b " + strRENDER_NAME + ".blend -o /rendered/f_########## -f " + strFRAME;
  206.  
  207.     cout << rendercommand << endl;
  208.  
  209.     if(system(rendercommand.c_str()) == 0)
  210.     {
  211.         cout << "Success" << endl;
  212.  
  213.         //cleanup
  214.         query.prepare("UPDATE SLAVES SET STATUS = 'AVAILABLE', FRAME = 0 WHERE ID = :ID");
  215.         query.bindValue(":ID", LOC_SLAVE_ID);
  216.         query.exec();
  217.  
  218.         state = 1;
  219.     }
  220.     else
  221.     {
  222.         cout << "Fail" << endl;
  223.     }
  224. }
  225.  
  226.  
  227.  
  228. int main()
  229. {
  230.  
  231.     cout << "Starting up..." << endl;
  232.  
  233.     //run startup functions
  234.     CFG_LOAD();
  235.  
  236.     DB_CONNECT();
  237.  
  238.     cout << "DB_CONNECT() FINISHED" << endl;
  239.  
  240.     //if(!db.open()){qDebug() << db.lastError();}
  241.     db.open();
  242.     query.first();
  243.  
  244.     DB_CLEANUP();
  245.  
  246.     state = 0;//State 0 = first time startup
  247.  
  248.     while(true)
  249.     {
  250.         cout << "Loop" << endl;
  251.  
  252.  
  253.         if(state == 0)//CHECK DB FOR PREVIOUS INFO
  254.         {
  255.             //DB_FIRSTLOG();
  256.  
  257.             DB_RENDERINFO();
  258.  
  259.             //pass to state 1
  260.             state = 1;
  261.         }
  262.  
  263.         else if(state == 1)//PARSE ONLY SELECTED DB IP
  264.         {
  265.             cout << "DB_SCAN" << endl;
  266.             if(LOC_CURRENT_FRAME <= LOC_TOTAL_FRAME)
  267.             {
  268.                 DB_SCAN();
  269.             }
  270.  
  271.             else
  272.             {
  273.                 state = 2;
  274.             }
  275.         }
  276.  
  277.         else if(state == 2)
  278.         {
  279.             //DB_RENDER();
  280.             cout << "DONE" << endl;
  281.         }
  282.  
  283.         usleep(1600000);
  284.     }
  285.  
  286.  
  287.  
  288.     return 0;
  289. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement