diff --git a/lib/mysql.hpp b/lib/mysql.hpp index 662d0cb..0b9dd82 100644 --- a/lib/mysql.hpp +++ b/lib/mysql.hpp @@ -8,8 +8,9 @@ #include #include #include -#include "ctime" +#include #include +using namespace std; #include #include @@ -18,14 +19,19 @@ #include #include #include +using namespace sql; +using namespace mysql; + +#ifdef MYSQL_USE_ASYNCO +#include "../../asynco/lib/asynco.hpp" +#include "../../asynco/lib/timers.hpp" +using namespace marcelb::asynco; +#endif + #define unlimited 0 #define reconnectSleep 1000 // in us -using namespace std; -using namespace sql; -using namespace mysql; - namespace marcelb { namespace mysql { @@ -34,17 +40,6 @@ namespace mysql { */ #define MYSQL_PERIODIC_INTERNAL_TIME 5000 -/** - * An enumeration of how periodic functions will be run - * internal - run periodic_maintenance() i new thread - * external - expects periodic_maintenance() to be run periodically outside the library - * -*/ -enum class time_loop_type { - internal, - external -}; - /** * A class for creating sql responses */ @@ -105,8 +100,11 @@ class MySQL { string path, username, password, database; uint32_t pool_size; bool run_tloop = true; +#ifdef MYSQL_USE_ASYNCO + periodic p_loop; +#else future tloop_future; - time_loop_type tloop_type; +#endif time_t last_loop_time; /** @@ -167,7 +165,7 @@ public: * username, password, database name, * and number of active connections (optional) */ - MySQL(const string _path, const string _username, const string _password, const string _db, const uint32_t _available = 1, const time_loop_type _engine_type = time_loop_type::internal); + MySQL(const string _path, const string _username, const string _password, const string _db, const uint32_t _available = 1); /** * Execute the SQL statement @@ -214,12 +212,6 @@ public: return result; } - /** - * If you are using an external periodic motor, - * please call this function in it for proper operation at a certain time interval. - * You can use the default MYSQL_PERIODIC_INTERNAL_TIME - */ - void tloop(uint32_t b, uint32_t e); /** * Destruktor diff --git a/src/mysql.cpp b/src/mysql.cpp index bd7b64f..60c6758 100644 --- a/src/mysql.cpp +++ b/src/mysql.cpp @@ -1,25 +1,39 @@ #include "../lib/mysql.hpp" -marcelb::mysql::MySQL::MySQL(const string _path, const string _username, const string _password, const string _db, const uint32_t _available, const time_loop_type _engine_type) { - path = _path; - username = _username; - password = _password; - database = _db; - pool_size = _available > 0 ? _available : 1; - tloop_type = _engine_type; - +marcelb::mysql::MySQL::MySQL(const string _path, const string _username, const string _password, const string _db, const uint32_t _available): +#ifdef MYSQL_USE_ASYNCO + p_loop(periodic( [&] () { + cout << "U asynco" << endl; + try { + auto start = rtime_ms(); + _tloop(0, connection_pool.size()); + cout << "loop--------------------------- nema error, trajalo: " << rtime_ms() - start << endl; + } catch (...) { + // cout << "Bude neki error u loopu" << endl; + if(on_error) { + on_error("Bude neki error u loopu"); + } + } + }, MYSQL_PERIODIC_INTERNAL_TIME)), +#else + tloop_future (async(launch::async, [&](){ + while (run_tloop) { + cout << "U STD async" << endl; + usleep(MYSQL_PERIODIC_INTERNAL_TIME*1000); + _tloop(0, connection_pool.size()); + } + return; + })), +#endif + path(_path), + username(_username), + password(_password), + database(_db), + pool_size(_available > 0 ? _available : 1) { + drv = get_mysql_driver_instance(); connect_pool(); - if (tloop_type == time_loop_type::internal) { - tloop_future = async(launch::async, [&](){ - while (run_tloop) { - usleep(MYSQL_PERIODIC_INTERNAL_TIME*1000); - _tloop(0, connection_pool.size()); - } - return; - }); - } // set on initialization to avoid the error last_loop_time = time(nullptr); } @@ -157,23 +171,9 @@ void marcelb::mysql::MySQL::release_connection(Connection* connection) { } marcelb::mysql::MySQL::~MySQL() { - if (tloop_type == time_loop_type::internal) { - run_tloop = false; + run_tloop = false; +#ifndef MYSQL_USE_ASYNCO tloop_future.get(); - } else { - run_tloop = false; - } - +#endif disconnect_pool(); -} - - -void marcelb::mysql::MySQL::tloop(uint32_t b, uint32_t e) { - if (tloop_type == time_loop_type::internal) { - if (on_error) { - on_error("Can't start external call tloop, internal is active!"); - } - return; - } - _tloop(b,e); } \ No newline at end of file diff --git a/test/compile.sh b/test/compile.sh index b71c1b3..acb252a 100644 --- a/test/compile.sh +++ b/test/compile.sh @@ -1 +1,2 @@ -g++ test.cpp ../src/* -o test.o -lmysqlcppconn -lpthread \ No newline at end of file +# g++ -DMYSQL_USE_ASYNCO test.cpp ../src/* ../../asynco/src/* -o test.o -lmysqlcppconn -lpthread +g++ test.cpp ../src/* ../../asynco/src/* -o test.o -lmysqlcppconn -lpthread \ No newline at end of file diff --git a/test/test.cpp b/test/test.cpp index 044f790..6fa79a5 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -4,19 +4,20 @@ using namespace std; using namespace chrono; -#include "../lib/mysql.hpp" -using namespace marcelb::mysql; - #include "../../asynco/lib/asynco.hpp" #include "../../asynco/lib/timers.hpp" using namespace marcelb::asynco; +#include "../lib/mysql.hpp" +using namespace marcelb::mysql; + + int main() { auto inis = rtime_ms(); try { - const int n = 30; + const int n = 5; // MySQL mydb("tcp://192.168.2.10:3306", "dinio", "H€r5elfInd1aH@nds", "dinio", 5, time_loop_type::internal); - MySQL mydb("tcp://bitelex.ddns.net:3306", "dinio", "H€r5elfInd1aH@nds", "dinio", n, time_loop_type::external); + MySQL mydb("tcp://bitelex.ddns.net:3306", "dinio", "H€r5elfInd1aH@nds", "dinio", n); // MySQL mydb("tcp://bitelex.ddns.net:3306", "dinio", "H€r5elfInd1aH@nds", "dinio", 5); cout << "init: " << rtime_ms() - inis << endl; @@ -25,23 +26,23 @@ int main() { cout << error << endl; }; - periodic mysql_tloop ( [&mydb] () { - auto l_start = rtime_ms(); - vector> to_wait; - for (int i=0, old_i=0; i> to_wait; + // for (int i=0, old_i=0; i("SELECT id,domain FROM records WHERE enabled = 1;"); cout << response.affected << " " << response.have_result << endl; @@ -89,7 +90,7 @@ while (true) { } }); - auto a2 = nonsync ( [&mydb] () { + auto a2 = async_ ( [&mydb] () { try { auto response = mydb.exec("SELECT zonename,auth_key FROM zones;"); cout << response.affected << " " << response.have_result << endl; @@ -108,7 +109,7 @@ while (true) { } }); - auto a3 = nonsync ( [&mydb] () { + auto a3 = async_ ( [&mydb] () { try { auto response = mydb.exec("SELECT username,email FROM users WHERE enabled = 1;"); cout << response.affected << " " << response.have_result << endl; @@ -127,7 +128,7 @@ while (true) { } }); - auto a4 = nonsync ( [&mydb] () { + auto a4 = async_ ( [&mydb] () { try { auto response = mydb.exec("SELECT id,domain FROM records WHERE enabled = 1;"); cout << response.affected << " " << response.have_result << endl; @@ -146,7 +147,7 @@ while (true) { } }); - auto a5 = nonsync ( [&mydb] () { + auto a5 = async_ ( [&mydb] () { try { auto response = mydb.exec("SELECT zonename,auth_key FROM zones;"); cout << response.affected << " " << response.have_result << endl; @@ -165,7 +166,7 @@ while (true) { } }); - auto a6 = nonsync ( [&mydb] () { + auto a6 = async_ ( [&mydb] () { try { auto response = mydb.exec("SELECT username,email FROM users WHERE enabled = 1;"); cout << response.affected << " " << response.have_result << endl; @@ -184,12 +185,12 @@ while (true) { } }); - wait(a1); - wait(a2); - wait(a3); - wait(a4); - wait(a5); - wait(a6); + await_(a1); + await_(a2); + await_(a3); + await_(a4); + await_(a5); + await_(a6); auto end = high_resolution_clock::now();