fastcgi++
3.1alpha
A C++ FastCGI/Web API
|
Handles low level communication with "the other side". More...
#include <fastcgi++/sql/connection.hpp>
Classes | |
struct | Conn |
Public Member Functions | |
void | stop () |
Call from any thread to stop the handler() thread. More... | |
void | terminate () |
Call from any thread to terminate the handler() thread. More... | |
void | start () |
Call from any thread to start the handler() thread. More... | |
void | join () |
Block until a stop() or terminate() is completed More... | |
bool | queue (const Query &query) |
Queue up a query. More... | |
void | init (const char *host, const char *db, const char *username, const char *password, const unsigned concurrency=1, const unsigned short port=5432, int messageType=5432, unsigned retryInterval=30) |
Initialize the connection. More... | |
~Connection () | |
Connection () | |
Private Member Functions | |
void | handler () |
General connection handler. More... | |
void | wake () |
Call this to wakeup the thread if it's sleeping. More... | |
void | connect () |
Call this to initiate all connections with the server. More... | |
bool | connected () |
Are we fully connected? More... | |
void | kill (std::map< socket_t, Conn >::iterator &conn) |
Kill and destroy this specific connection. More... | |
void | killAll () |
Kill and destroy everything!! More... | |
Private Attributes | |
bool | m_initialized |
True if we're initialized. More... | |
std::map< socket_t, Conn > | m_connections |
Container associating sockets with their receive buffers. More... | |
std::deque< Query > | m_queue |
Buffer for transmitting data More... | |
std::atomic_bool | m_terminate |
True when handler() should be terminating. More... | |
std::atomic_bool | m_stop |
True when handler() should be stopping. More... | |
std::thread | m_thread |
Thread our handler is running in. More... | |
std::mutex | m_mutex |
Always practice safe threading. More... | |
socket_t | m_wakeSockets [2] |
A pair of sockets for wakeup purposes. More... | |
std::string | m_host |
Hostname/address/socket of server. More... | |
std::string | m_db |
Database name. More... | |
std::string | m_username |
Server username. More... | |
std::string | m_password |
Server password. More... | |
std::string | m_port |
Server port. More... | |
unsigned | m_retry |
Connection retry interval More... | |
unsigned | m_concurrency |
How many concurrent queries shall we allow? More... | |
unsigned | m_messageType |
Callback message type ID. More... | |
Poll | m_poll |
The poll group. More... | |
Handles low level communication with "the other side".
This class handles connections to the database and its queries. It can safely be constructed in the global space. Once constructed call init() to initialize it. Call start() to start the handling thread. Once you're done, call stop() or terminate() to finish things and then call join() to wait for the thread to complete.
Queue up queries with queue().
Definition at line 113 of file connection.hpp.
Fastcgipp::SQL::Connection::~Connection | ( | ) |
Definition at line 236 of file connection.cpp.
|
inline |
Definition at line 175 of file connection.hpp.
|
private |
Call this to initiate all connections with the server.
Definition at line 290 of file connection.cpp.
References Fastcgipp::SQL::Connection::Conn::connection, ERROR_LOG, and Fastcgipp::SQL::Connection::Conn::idle.
Referenced by handler().
|
inlineprivate |
Are we fully connected?
Definition at line 190 of file connection.hpp.
Referenced by handler().
|
private |
General connection handler.
Definition at line 42 of file connection.cpp.
References connect(), connected(), Fastcgipp::Poll::del(), ERROR_LOG, FAIL_LOG, kill(), killAll(), m_connections, m_messageType, m_mutex, m_poll, m_queue, m_retry, m_stop, m_terminate, m_wakeSockets, Fastcgipp::Poll::poll(), and WARNING_LOG.
Referenced by start().
void Fastcgipp::SQL::Connection::init | ( | const char * | host, |
const char * | db, | ||
const char * | username, | ||
const char * | password, | ||
const unsigned | concurrency = 1 , |
||
const unsigned short | port = 5432 , |
||
int | messageType = 5432 , |
||
unsigned | retryInterval = 30 |
||
) |
Initialize the connection.
Note that this function can only be called once.
[in] | host | Hostname/address/socket of the SQL server. |
[in] | db | Database name |
[in] | username | Server username |
[in] | password | Server password |
[in] | port | Server port number |
[in] | concurrency | How many connnections/simultaneous queries? |
[in] | messageType | Type value for Message sent via callback. |
[in] | retryInterval | How many seconds before retrying a bad connection to the SQL server? |
Definition at line 249 of file connection.cpp.
void Fastcgipp::SQL::Connection::join | ( | ) |
Block until a stop() or terminate() is completed
Definition at line 230 of file connection.cpp.
Kill and destroy this specific connection.
Definition at line 332 of file connection.cpp.
Referenced by handler().
|
private |
Kill and destroy everything!!
Definition at line 344 of file connection.cpp.
Referenced by handler().
bool Fastcgipp::SQL::Connection::queue | ( | const Query & | query | ) |
Queue up a query.
Definition at line 275 of file connection.cpp.
References Fastcgipp::SQL::Query::parameters.
void Fastcgipp::SQL::Connection::start | ( | ) |
Call from any thread to start the handler() thread.
If the thread is already running this will do nothing.
Definition at line 219 of file connection.cpp.
References handler().
void Fastcgipp::SQL::Connection::stop | ( | ) |
Call from any thread to stop the handler() thread.
Calling this thread will signal the handler() thread to gracefully stop itself. This means it waits until all queued queries are finished
Definition at line 207 of file connection.cpp.
void Fastcgipp::SQL::Connection::terminate | ( | ) |
Call from any thread to terminate the handler() thread.
Calling this thread will signal the handler() thread to immediately terminate itself. This means it doesn't wait until the currently queued queries are finished.
Definition at line 213 of file connection.cpp.
|
private |
Call this to wakeup the thread if it's sleeping.
Definition at line 241 of file connection.cpp.
References FAIL_LOG.
|
private |
How many concurrent queries shall we allow?
Definition at line 251 of file connection.hpp.
Container associating sockets with their receive buffers.
Definition at line 206 of file connection.hpp.
Referenced by handler().
|
private |
Database name.
Definition at line 236 of file connection.hpp.
|
private |
Hostname/address/socket of server.
Definition at line 233 of file connection.hpp.
|
private |
True if we're initialized.
Definition at line 196 of file connection.hpp.
|
private |
|
private |
Always practice safe threading.
Definition at line 227 of file connection.hpp.
Referenced by handler().
|
private |
Server password.
Definition at line 242 of file connection.hpp.
|
private |
|
private |
Server port.
Definition at line 245 of file connection.hpp.
|
private |
Buffer for transmitting data
Definition at line 215 of file connection.hpp.
Referenced by handler().
|
private |
|
private |
True when handler() should be stopping.
Definition at line 221 of file connection.hpp.
Referenced by handler().
|
private |
True when handler() should be terminating.
Definition at line 218 of file connection.hpp.
Referenced by handler().
|
private |
Thread our handler is running in.
Definition at line 224 of file connection.hpp.
|
private |
Server username.
Definition at line 239 of file connection.hpp.
|
private |
A pair of sockets for wakeup purposes.
Definition at line 230 of file connection.hpp.
Referenced by handler().