parent
ca7bef08a7
commit
122e52e724
@ -0,0 +1,46 @@
|
||||
//
|
||||
// Created by mwo on 12/07/18.
|
||||
//
|
||||
|
||||
#include "MysqlPing.h"
|
||||
|
||||
|
||||
namespace xmreg
|
||||
{
|
||||
|
||||
MysqlPing::MysqlPing(
|
||||
std::shared_ptr<MySqlConnector> _conn,
|
||||
uint64_t _ping_time)
|
||||
: conn {_conn}, ping_time {_ping_time}
|
||||
{}
|
||||
|
||||
void
|
||||
MysqlPing::operator()()
|
||||
{
|
||||
while (keep_looping)
|
||||
{
|
||||
std::this_thread::sleep_for(chrono::seconds(ping_time));
|
||||
|
||||
if (auto c = conn.lock())
|
||||
{
|
||||
if (!c->ping())
|
||||
{
|
||||
cerr << "Pinging mysql failed. Stoping mysql pinging thread. \n";
|
||||
why_stoped = StopReason::PingFailed;
|
||||
break;
|
||||
}
|
||||
|
||||
cout << "Mysql ping successful. \n" ;
|
||||
}
|
||||
else
|
||||
{
|
||||
cerr << "std::weak_ptr<MySqlConnector> conn expired! \n";
|
||||
why_stoped = StopReason::PointerExpired;
|
||||
break;
|
||||
}
|
||||
|
||||
++counter;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
//
|
||||
// Created by mwo on 12/07/18.
|
||||
//
|
||||
|
||||
#ifndef OPENMONERO_MYSQLPING_H
|
||||
#define OPENMONERO_MYSQLPING_H
|
||||
|
||||
#include "MySqlConnector.h"
|
||||
|
||||
#include <memory>
|
||||
#include <thread>
|
||||
#include <atomic>
|
||||
|
||||
namespace xmreg
|
||||
{
|
||||
|
||||
class MysqlPing
|
||||
{
|
||||
public:
|
||||
|
||||
enum class StopReason {NotYetStopped, PingFailed, PointerExpired};
|
||||
|
||||
MysqlPing(std::shared_ptr<MySqlConnector> _conn, uint64_t _ping_time = 7200);
|
||||
|
||||
void operator()();
|
||||
void stop() {keep_looping = false;}
|
||||
|
||||
uint64_t get_counter() const {return counter;}
|
||||
StopReason get_stop_reason() const {return why_stoped;};
|
||||
|
||||
private:
|
||||
std::weak_ptr<MySqlConnector> conn;
|
||||
uint64_t ping_time; // in seconds
|
||||
atomic<bool> keep_looping {true};
|
||||
atomic<uint64_t> counter {0};
|
||||
atomic<StopReason> why_stoped {StopReason::NotYetStopped};
|
||||
};
|
||||
}
|
||||
|
||||
#endif //OPENMONERO_MYSQLPING_H
|
File diff suppressed because one or more lines are too long
Loading…
Reference in new issue