Added the thread safe TSQueue and using it in mainwindow to replace the adhoc queue implementation.

This commit is contained in:
Eelke Klein 2017-01-03 07:22:36 +01:00
parent c551d982c6
commit 83064ab86b
10 changed files with 253 additions and 27 deletions

68
asyncdbconnection.h Normal file
View file

@ -0,0 +1,68 @@
#ifndef ASYNCDBCONNECTION_H
#define ASYNCDBCONNECTION_H
#include "PgsqlConn.h"
#include <functional>
#include <vector>
class ASyncDBConnection {
public:
enum class State {
NotConnected,
Connecting,
Connected,
QuerySend,
CancelSend
};
using on_result_callback = std::function<void(Pgsql::Result)>;
using on_state_callback = std::function<void(State)>;
ASyncDBConnection();
void setupConnection(const std::string &connstring);
void closeConnection();
void setStateCallback(on_state_callback state_callback);
/** Sends command to the server.
When the result is in and no result_task_queue was passed then on_result will be
called directly within the thread.
If the command gives multiple results on_result will be called for each result.
*/
bool send(const std::string &command, on_result_callback on_result);
private:
class Command {
public:
std::string command;
on_result_callback on_result;
};
/// Contains all the members accessed by the thread
class Thread {
public:
on_state_callback m_state_callback;
std::string m_init_string;
/// Is started as a seperate thread by ASyncDBConnection
void run();
/// Sends a cancel request to the DB server
void cancel();
private:
Pgsql::Connection m_connection;
bool terminateRequested = false; ///< is set when the thread should stop
bool makeConnection();
void communicate();
};
Thread thread;
};
#endif // ASYNCDBCONNECTION_H