Guest User

Untitled

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