fastcgi++  3.1alpha
A C++ FastCGI/Web API
connection.hpp
Go to the documentation of this file.
1 
10 /*******************************************************************************
11 * Copyright (C) 2018 Eddie Carle [eddie@isatec.ca] *
12 * *
13 * This file is part of fastcgi++. *
14 * *
15 * fastcgi++ is free software: you can redistribute it and/or modify it under *
16 * the terms of the GNU Lesser General Public License as published by the Free *
17 * Software Foundation, either version 3 of the License, or (at your option) *
18 * any later version. *
19 * *
20 * fastcgi++ is distributed in the hope that it will be useful, but WITHOUT ANY *
21 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS *
22 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for *
23 * more details. *
24 * *
25 * You should have received a copy of the GNU Lesser General Public License *
26 * along with fastcgi++. If not, see <http://www.gnu.org/licenses/>. *
27 *******************************************************************************/
28 
29 #ifndef FASTCGIPP_CONNECTION_HPP
30 #define FASTCGIPP_CONNECTION_HPP
31 
32 #include <deque>
33 #include <map>
34 #include <functional>
35 #include <atomic>
36 #include <mutex>
37 #include <thread>
38 
39 #include "fastcgi++/sockets.hpp"
40 #include "fastcgi++/message.hpp"
43 
45 namespace Fastcgipp
46 {
48  namespace SQL
49  {
51  struct Query
52  {
54  const char* statement;
55 
57  std::shared_ptr<Parameters_base> parameters;
58 
60  std::shared_ptr<Results_base> results;
61 
63  std::function<void(Message)> callback;
64  };
65 
67 
79  class Connection
80  {
81  public:
83 
90  void stop();
91 
93 
100  void terminate();
101 
103 
106  void start();
107 
109  void join();
110 
112  bool queue(const Query& query);
113 
115 
129  void init(
130  const char* host,
131  const char* db,
132  const char* username,
133  const char* password,
134  const unsigned concurrency=1,
135  const unsigned short port=5432,
136  int messageType=5432,
137  unsigned retryInterval=30);
138 
139  ~Connection();
140 
141  Connection():
142  m_initialized(false)
143  {}
144 
145  private:
147  void handler();
148 
150  void wake();
151 
153  void connect();
154 
156  bool connected()
157  {
158  return m_connections.size() == m_concurrency;
159  }
160 
162  bool m_initialized;
163 
164  struct Conn
165  {
166  bool idle;
167  void* connection;
168  Query query;
169  };
170 
172  std::map<socket_t, Conn> m_connections;
173 
175  void kill(std::map<socket_t, Conn>::iterator& conn);
176 
178  void killAll();
179 
181  std::deque<Query> m_queue;
182 
184  std::atomic_bool m_terminate;
185 
187  std::atomic_bool m_stop;
188 
190  std::thread m_thread;
191 
194 
197 
199  std::string m_host;
200 
202  std::string m_db;
203 
205  std::string m_username;
206 
208  std::string m_password;
209 
211  std::string m_port;
212 
214  unsigned m_retry;
215 
217  unsigned m_concurrency;
218 
220  unsigned m_messageType;
221 
223  Poll m_poll;
224  };
225  }
226 }
227 
228 #endif
Fastcgipp::SQL::Connection::m_wakeSockets
socket_t m_wakeSockets[2]
A pair of sockets for wakeup purposes.
Definition: connection.hpp:230
Fastcgipp::SQL::Connection::m_queue
std::deque< Query > m_queue
Buffer for transmitting data
Definition: connection.hpp:215
parameters.hpp
Declares SQL parameters types.
Fastcgipp::SQL::Connection::handler
void handler()
General connection handler.
Definition: connection.cpp:42
message.hpp
Defines the Message data structure.
Fastcgipp::SQL::Connection::m_host
std::string m_host
Hostname/address/socket of server.
Definition: connection.hpp:233
Fastcgipp::Logging::mutex
std::mutex mutex
Thread safe the logging mechanism.
Definition: log.cpp:102
Fastcgipp::SQL::Query
Structure to hold SQL query data.
Definition: connection.hpp:85
Fastcgipp::SQL::Connection::m_thread
std::thread m_thread
Thread our handler is running in.
Definition: connection.hpp:224
Fastcgipp::SQL::Connection::connect
void connect()
Call this to initiate all connections with the server.
Definition: connection.cpp:290
Fastcgipp::SQL::Connection::m_mutex
std::mutex m_mutex
Always practice safe threading.
Definition: connection.hpp:227
Fastcgipp::SQL::Connection::m_connections
std::map< socket_t, Conn > m_connections
Container associating sockets with their receive buffers.
Definition: connection.hpp:206
Fastcgipp::SQL::Query::results
std::shared_ptr< Results_base > results
Results
Definition: connection.hpp:111
Fastcgipp::SQL::Connection::~Connection
~Connection()
Definition: connection.cpp:236
Fastcgipp::SQL::Connection::queue
bool queue(const Query &query)
Queue up a query.
Definition: connection.cpp:275
Fastcgipp::SQL::Connection::m_stop
std::atomic_bool m_stop
True when handler() should be stopping.
Definition: connection.hpp:221
Fastcgipp::SQL::Connection::Conn::query
Query query
Definition: connection.hpp:202
Fastcgipp::SQL::Connection::Conn::connection
void * connection
Definition: connection.hpp:201
Fastcgipp::SQL::Connection::m_port
std::string m_port
Server port.
Definition: connection.hpp:245
Fastcgipp::SQL::Query::parameters
std::shared_ptr< Parameters_base > parameters
Parameters
Definition: connection.hpp:108
Fastcgipp::SQL::Connection::Connection
Connection()
Definition: connection.hpp:175
results.hpp
Declares SQL Results types.
sockets.hpp
Declares everything for interfaces with OS level sockets.
Fastcgipp::SQL::Query::callback
std::function< void(Message)> callback
Callback function to call when query is complete.
Definition: connection.hpp:114
Fastcgipp::Poll
Class for handling OS level socket polling.
Definition: poll.hpp:76
Fastcgipp::SQL::Connection::Conn::idle
bool idle
Definition: connection.hpp:200
Fastcgipp::SQL::Connection::stop
void stop()
Call from any thread to stop the handler() thread.
Definition: connection.cpp:207
Fastcgipp::SQL::Connection::killAll
void killAll()
Kill and destroy everything!!
Definition: connection.cpp:344
Fastcgipp::SQL::Connection::wake
void wake()
Call this to wakeup the thread if it's sleeping.
Definition: connection.cpp:241
Fastcgipp::SQL::Connection::m_poll
Poll m_poll
The poll group.
Definition: connection.hpp:257
Fastcgipp::SQL::Connection::m_terminate
std::atomic_bool m_terminate
True when handler() should be terminating.
Definition: connection.hpp:218
Fastcgipp::SQL::Connection::m_retry
unsigned m_retry
Connection retry interval
Definition: connection.hpp:248
Fastcgipp::SQL::Connection::m_username
std::string m_username
Server username.
Definition: connection.hpp:239
Fastcgipp
Topmost namespace for the fastcgi++ library.
Definition: fcgistreambuf.cpp:34
Fastcgipp::SQL::Connection::join
void join()
Block until a stop() or terminate() is completed
Definition: connection.cpp:230
Fastcgipp::SQL::Connection::init
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.
Definition: connection.cpp:249
Fastcgipp::SQL::Connection::connected
bool connected()
Are we fully connected?
Definition: connection.hpp:190
Fastcgipp::SQL::Connection
Handles low level communication with "the other side".
Definition: connection.hpp:113
Fastcgipp::SQL::Connection::terminate
void terminate()
Call from any thread to terminate the handler() thread.
Definition: connection.cpp:213
Fastcgipp::SQL::Connection::m_db
std::string m_db
Database name.
Definition: connection.hpp:236
Fastcgipp::SQL::Connection::kill
void kill(std::map< socket_t, Conn >::iterator &conn)
Kill and destroy this specific connection.
Definition: connection.cpp:332
Fastcgipp::SQL::Connection::m_password
std::string m_password
Server password.
Definition: connection.hpp:242
Fastcgipp::SQL::Connection::start
void start()
Call from any thread to start the handler() thread.
Definition: connection.cpp:219
Fastcgipp::SQL::Connection::m_concurrency
unsigned m_concurrency
How many concurrent queries shall we allow?
Definition: connection.hpp:251
Fastcgipp::SQL::Connection::m_initialized
bool m_initialized
True if we're initialized.
Definition: connection.hpp:196
Fastcgipp::SQL::Query::statement
const char * statement
Statement.
Definition: connection.hpp:105
Fastcgipp::socket_t
int socket_t
Our socket identifier type in GNU/Linux is simply an int.
Definition: poll.hpp:65
Fastcgipp::SQL::Connection::m_messageType
unsigned m_messageType
Callback message type ID.
Definition: connection.hpp:254