fastcgi++  3.1alpha
A C++ FastCGI/Web API
manager.hpp
Go to the documentation of this file.
1 
10 /*******************************************************************************
11 * Copyright (C) 2016 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_MANAGER_HPP
30 #define FASTCGIPP_MANAGER_HPP
31 
32 #include <map>
33 #include <list>
34 #include <algorithm>
35 #include <thread>
36 #include <mutex>
37 #include <shared_mutex>
38 #include <memory>
39 #include <functional>
40 #include <condition_variable>
41 
42 #include "fastcgi++/protocol.hpp"
44 #include "fastcgi++/request.hpp"
45 
47 namespace Fastcgipp
48 {
50 
64  class Manager_base
65  {
66  public:
68 
71  Manager_base(unsigned threads);
72 
73  ~Manager_base();
74 
76 
86  void terminate();
87 
89 
98  void stop();
99 
101 
104  void start();
105 
107  void join();
108 
110 
116  static void setupSignals();
117 
119 
125  bool listen()
126  {
127  return m_transceiver.listen();
128  }
129 
131 
145  bool listen(
146  const char* name,
147  uint32_t permissions = 0xffffffffUL,
148  const char* owner = nullptr,
149  const char* group = nullptr)
150  {
151  return m_transceiver.listen(name, permissions, owner, group);
152  }
153 
155 
166  bool listen(
167  const char* interface,
168  const char* service)
169  {
170  return m_transceiver.listen(interface, service);
171  }
172 
174  void push(Protocol::RequestId id, Message&& message);
175 
177 
181  void reuseAddress(bool value)
182  {
184  }
185 
187 
195  void resizeThreads(unsigned threads);
196 
197  protected:
199  virtual std::unique_ptr<Request_base> makeRequest(
201  const Protocol::Role& role,
202  bool kill) =0;
203 
206 
207  private:
209  std::queue<Protocol::RequestId> m_tasks;
210 
213 
216 
218  std::shared_timed_mutex m_requestsMutex;
219 
221  std::queue<std::pair<Message, Socket>> m_messages;
222 
225 
227  void handler();
228 
230 
234  inline void localHandler();
235 
237  bool m_terminate;
238 
240  bool m_stop;
241 
244 
246  std::vector<std::thread> m_threads;
247 
249  std::condition_variable m_wake;
250 
252  static void signalHandler(int signum);
253 
256 
257 #if FASTCGIPP_LOG_LEVEL > 3
258  std::atomic_ullong m_requestCount;
260 
262  size_t m_maxRequests;
263 
265  std::atomic_ullong m_managementRecordCount;
266 
268  std::atomic_ullong m_badSocketMessageCount;
269 
271  std::atomic_ullong m_badSocketKillCount;
272 
274  std::atomic_ullong m_messageCount;
275 
277  unsigned m_activeThreads;
278 
280  unsigned m_maxActiveThreads;
281 #endif
282  };
283 
285 
304  template<class RequestT> class Manager: public Manager_base
305  {
306  public:
308 
311  Manager(unsigned threads = std::thread::hardware_concurrency()):
312  Manager_base(threads)
313  {}
314 
315  private:
317  std::unique_ptr<Request_base> makeRequest(
318  const Protocol::RequestId& id,
319  const Protocol::Role& role,
320  bool kill)
321  {
322  using namespace std::placeholders;
323 
324  std::unique_ptr<RequestT> request(new RequestT);
325  request->configure(
326  id,
327  role,
328  kill,
329  std::bind(&Transceiver::send, &m_transceiver, _1, _2, _3),
330  std::bind(&Manager_base::push, this, id, _1));
331  return request;
332  }
333 
334  };
335 }
336 
337 #endif
Fastcgipp::Protocol::Role
Role
Defines the possible roles a FastCGI application may play.
Definition: protocol.hpp:180
Fastcgipp::Manager_base::setupSignals
static void setupSignals()
Configure the handlers for POSIX signals.
Definition: manager.cpp:100
Fastcgipp::Manager::Manager
Manager(unsigned threads=std::thread::hardware_concurrency())
Sole constructor.
Definition: manager.hpp:328
Fastcgipp::Manager_base::stop
void stop()
Call from any thread to stop the Manager.
Definition: manager.cpp:68
Fastcgipp::Transceiver::listen
bool listen()
Listen to the default Fastcgi socket.
Definition: transceiver.hpp:162
Fastcgipp::Manager_base::start
void start()
Call from any thread to start the Manager.
Definition: manager.cpp:76
Fastcgipp::Manager_base::localHandler
void localHandler()
Handles management messages.
Definition: manager.cpp:143
Fastcgipp::Manager_base::terminate
void terminate()
Call from any thread to terminate the Manager.
Definition: manager.cpp:60
Fastcgipp::Logging::mutex
std::mutex mutex
Thread safe the logging mechanism.
Definition: log.cpp:102
Fastcgipp::Manager_base::instance
static Manager_base * instance
Pointer to the Manager object.
Definition: manager.hpp:289
Fastcgipp::Manager
General task and protocol management class.
Definition: manager.hpp:321
Fastcgipp::Manager_base::signalHandler
static void signalHandler(int signum)
General function to handler POSIX signals.
Definition: manager.cpp:112
Fastcgipp::Manager_base::handler
void handler()
General handling function to have it's own thread.
Definition: manager.cpp:254
Fastcgipp::Manager_base::m_transceiver
Transceiver m_transceiver
Handles low level communication with the other side.
Definition: manager.hpp:239
Fastcgipp::Transceiver::reuseAddress
void reuseAddress(bool value)
Should we set socket option to reuse address.
Definition: transceiver.hpp:215
Fastcgipp::Manager_base::Manager_base
Manager_base(unsigned threads)
Sole constructor.
Definition: manager.cpp:34
Fastcgipp::Manager_base::m_requests
Protocol::Requests< std::unique_ptr< Request_base > > m_requests
An associative container for our requests.
Definition: manager.hpp:249
Fastcgipp::Manager_base::push
void push(Protocol::RequestId id, Message &&message)
Pass a message to a request.
Definition: manager.cpp:332
Fastcgipp::Transceiver
Handles low level communication with "the other side".
Definition: transceiver.hpp:75
Fastcgipp::Manager_base::m_messagesMutex
std::mutex m_messagesMutex
Thread safe our local messages.
Definition: manager.hpp:258
Fastcgipp::Protocol::Requests
std::map< RequestId, T, RequestId::Less > Requests
A simple associative container that indexes with RequestId.
Definition: protocol.hpp:155
Fastcgipp::Manager_base::resizeThreads
void resizeThreads(unsigned threads)
Call before start to change the number of threads.
Definition: manager.cpp:417
Fastcgipp::Manager_base::m_threads
std::vector< std::thread > m_threads
Threads our manager is running in.
Definition: manager.hpp:280
transceiver.hpp
Declares the Fastcgipp::Transceiver class.
Fastcgipp::Manager_base::reuseAddress
void reuseAddress(bool value)
Should we set socket option to reuse address.
Definition: manager.hpp:215
Fastcgipp::Manager_base::~Manager_base
~Manager_base()
Definition: manager.cpp:428
Fastcgipp::Manager_base::m_startStopMutex
std::mutex m_startStopMutex
Thread safe starting and stopping.
Definition: manager.hpp:277
Fastcgipp
Topmost namespace for the fastcgi++ library.
Definition: fcgistreambuf.cpp:34
protocol.hpp
Declares everything for relating to the FastCGI protocol itself.
Fastcgipp::Manager_base::join
void join()
Block until a stop() or terminate() is called and completed.
Definition: manager.cpp:91
Fastcgipp::Manager_base::m_requestsMutex
std::shared_timed_mutex m_requestsMutex
Thread safe our requests.
Definition: manager.hpp:252
Fastcgipp::Manager_base::m_wake
std::condition_variable m_wake
Condition variable to wake handler() threads up.
Definition: manager.hpp:283
Fastcgipp::Manager_base::m_messages
std::queue< std::pair< Message, Socket > > m_messages
Local messages.
Definition: manager.hpp:255
Fastcgipp::Protocol::RequestId
A unique identifier for each FastCGI request.
Definition: protocol.hpp:88
Fastcgipp::Manager_base::listen
bool listen()
Listen to the default Fastcgi socket.
Definition: manager.hpp:159
Fastcgipp::Manager_base
General task and protocol management class base.
Definition: manager.hpp:81
Fastcgipp::Manager::makeRequest
std::unique_ptr< Request_base > makeRequest(const Protocol::RequestId &id, const Protocol::Role &role, bool kill)
Make a request object.
Definition: manager.hpp:334
Fastcgipp::Manager_base::m_tasksMutex
std::mutex m_tasksMutex
Thread safe our tasks.
Definition: manager.hpp:246
Fastcgipp::Manager_base::m_tasks
std::queue< Protocol::RequestId > m_tasks
Queue for pending tasks.
Definition: manager.hpp:243
Fastcgipp::Transceiver::send
void send(const Socket &socket, Block &&data, bool kill)
Queue up a block of data for transmission.
Definition: transceiver.cpp:205
Fastcgipp::Manager_base::m_stop
bool m_stop
True when the manager should be stopping.
Definition: manager.hpp:274
Fastcgipp::Manager_base::m_terminate
bool m_terminate
True when the manager should be terminating.
Definition: manager.hpp:271
Fastcgipp::Manager_base::makeRequest
virtual std::unique_ptr< Request_base > makeRequest(const Protocol::RequestId &id, const Protocol::Role &role, bool kill)=0
Make a request object.
request.hpp
Declares the Request class.