fastcgi++  3.1alpha
A C++ FastCGI/Web API
Fastcgipp::SQL::Connection Class Reference

Handles low level communication with "the other side". More...

#include <fastcgi++/sql/connection.hpp>

Collaboration diagram for Fastcgipp::SQL::Connection:
[legend]

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, Connm_connections
 Container associating sockets with their receive buffers. More...
 
std::deque< Querym_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...
 

Detailed Description

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().

Date
October 7, 2018
Author
Eddie Carle <eddie.nosp@m.@isa.nosp@m.tec.c.nosp@m.a>

Definition at line 113 of file connection.hpp.

Constructor & Destructor Documentation

◆ ~Connection()

Fastcgipp::SQL::Connection::~Connection ( )

Definition at line 236 of file connection.cpp.

◆ Connection()

Fastcgipp::SQL::Connection::Connection ( )
inline

Definition at line 175 of file connection.hpp.

Member Function Documentation

◆ connect()

void Fastcgipp::SQL::Connection::connect ( )
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().

Here is the caller graph for this function:

◆ connected()

bool Fastcgipp::SQL::Connection::connected ( )
inlineprivate

Are we fully connected?

Definition at line 190 of file connection.hpp.

Referenced by handler().

Here is the caller graph for this function:

◆ handler()

void Fastcgipp::SQL::Connection::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().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ init()

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.

Parameters
[in]hostHostname/address/socket of the SQL server.
[in]dbDatabase name
[in]usernameServer username
[in]passwordServer password
[in]portServer port number
[in]concurrencyHow many connnections/simultaneous queries?
[in]messageTypeType value for Message sent via callback.
[in]retryIntervalHow many seconds before retrying a bad connection to the SQL server?

Definition at line 249 of file connection.cpp.

◆ join()

void Fastcgipp::SQL::Connection::join ( )

Block until a stop() or terminate() is completed

Definition at line 230 of file connection.cpp.

◆ kill()

void Fastcgipp::SQL::Connection::kill ( std::map< socket_t, Conn >::iterator &  conn)
private

Kill and destroy this specific connection.

Definition at line 332 of file connection.cpp.

Referenced by handler().

Here is the caller graph for this function:

◆ killAll()

void Fastcgipp::SQL::Connection::killAll ( )
private

Kill and destroy everything!!

Definition at line 344 of file connection.cpp.

Referenced by handler().

Here is the caller graph for this function:

◆ queue()

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.

◆ start()

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().

Here is the call graph for this function:

◆ stop()

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

See also
join()

Definition at line 207 of file connection.cpp.

◆ terminate()

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.

See also
join()

Definition at line 213 of file connection.cpp.

◆ wake()

void Fastcgipp::SQL::Connection::wake ( )
private

Call this to wakeup the thread if it's sleeping.

Definition at line 241 of file connection.cpp.

References FAIL_LOG.

Member Data Documentation

◆ m_concurrency

unsigned Fastcgipp::SQL::Connection::m_concurrency
private

How many concurrent queries shall we allow?

Definition at line 251 of file connection.hpp.

◆ m_connections

std::map<socket_t, Conn> Fastcgipp::SQL::Connection::m_connections
private

Container associating sockets with their receive buffers.

Definition at line 206 of file connection.hpp.

Referenced by handler().

◆ m_db

std::string Fastcgipp::SQL::Connection::m_db
private

Database name.

Definition at line 236 of file connection.hpp.

◆ m_host

std::string Fastcgipp::SQL::Connection::m_host
private

Hostname/address/socket of server.

Definition at line 233 of file connection.hpp.

◆ m_initialized

bool Fastcgipp::SQL::Connection::m_initialized
private

True if we're initialized.

Definition at line 196 of file connection.hpp.

◆ m_messageType

unsigned Fastcgipp::SQL::Connection::m_messageType
private

Callback message type ID.

Definition at line 254 of file connection.hpp.

Referenced by handler().

◆ m_mutex

std::mutex Fastcgipp::SQL::Connection::m_mutex
private

Always practice safe threading.

Definition at line 227 of file connection.hpp.

Referenced by handler().

◆ m_password

std::string Fastcgipp::SQL::Connection::m_password
private

Server password.

Definition at line 242 of file connection.hpp.

◆ m_poll

Poll Fastcgipp::SQL::Connection::m_poll
private

The poll group.

Definition at line 257 of file connection.hpp.

Referenced by handler().

◆ m_port

std::string Fastcgipp::SQL::Connection::m_port
private

Server port.

Definition at line 245 of file connection.hpp.

◆ m_queue

std::deque<Query> Fastcgipp::SQL::Connection::m_queue
private

Buffer for transmitting data

Definition at line 215 of file connection.hpp.

Referenced by handler().

◆ m_retry

unsigned Fastcgipp::SQL::Connection::m_retry
private

Connection retry interval

Definition at line 248 of file connection.hpp.

Referenced by handler().

◆ m_stop

std::atomic_bool Fastcgipp::SQL::Connection::m_stop
private

True when handler() should be stopping.

Definition at line 221 of file connection.hpp.

Referenced by handler().

◆ m_terminate

std::atomic_bool Fastcgipp::SQL::Connection::m_terminate
private

True when handler() should be terminating.

Definition at line 218 of file connection.hpp.

Referenced by handler().

◆ m_thread

std::thread Fastcgipp::SQL::Connection::m_thread
private

Thread our handler is running in.

Definition at line 224 of file connection.hpp.

◆ m_username

std::string Fastcgipp::SQL::Connection::m_username
private

Server username.

Definition at line 239 of file connection.hpp.

◆ m_wakeSockets

socket_t Fastcgipp::SQL::Connection::m_wakeSockets[2]
private

A pair of sockets for wakeup purposes.

Definition at line 230 of file connection.hpp.

Referenced by handler().


The documentation for this class was generated from the following files: