KISSCPP
a C++ library for rapid application development
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
server.hpp
Go to the documentation of this file.
1 // File : server.hpp
2 // Author: Dirk J. Botha <bothadj@gmail.com>
3 //
4 // This file is part of kisscpp library.
5 //
6 // The kisscpp library is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU Lesser General Public License as published by
8 // the Free Software Foundation, either version 3 of the License, or
9 // (at your option) any later version.
10 //
11 // The kisscpp library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public License
17 // along with the kisscpp library. If not, see <http://www.gnu.org/licenses/>.
18 
19 #ifndef _SERVER_SERVER_HPP
20 #define _SERVER_SERVER_HPP
21 
22 #include <sys/types.h> // getpid()
23 #include <unistd.h> // getpid()
24 #include <sys/stat.h> // umask
25 
26 #include <iostream>
27 #include <fstream>
28 #include <string>
29 #include <vector>
30 #include <cstdlib>
31 #include <ctime>
32 
33 #include <boost/asio.hpp>
34 #include <boost/noncopyable.hpp>
35 #include <boost/shared_ptr.hpp>
36 #include <boost/bind.hpp>
37 #include <boost/filesystem.hpp>
38 
39 namespace bfs = boost::filesystem;
40 
41 #include "io_service_pool.hpp"
42 #include "connection.hpp"
43 #include "request_router.hpp"
44 #include "logstream.hpp"
45 #include "statskeeper.hpp"
46 #include "errorstate.hpp"
47 #include "standard_handlers.hpp"
48 #include "configuration.hpp"
49 
50 namespace kisscpp
51 {
52  class Server : private boost::noncopyable // The top-level class of the server.
53  {
54  public:
55  // Construct the server to listen on the specified TCP address and port
56  explicit Server(std::size_t io_service_pool_size,
57  const std::string& application_id,
58  const std::string& application_instance,
59  bool runAsDaemon = true,
60  const std::string& config_root_path = "",
61  const std::string& address = "get_from_config",
62  const std::string& port = "get_from_config");
63 
65  {
66  removeLockFile();
67  }
68 
69  void run(); // Run the server's io_service loop.
70  void stop(); // stop the server.
71 
72  void register_handler(RequestHandlerPtr _handler);
73 
74  private:
75  void start_accept(); // Initiate an asynchronous accept operation.
76  void handle_accept(const boost::system::error_code& e); // Handle completion of an asynchronous accept operation.
77  void handle_stop(); // Handle a request to stop the server.
78  void handle_log_reopen(); // Handle a request to reopen log.
79  void initialize_standard_handlers();
80  bool checkLockFile (const std::string &appid, const std::string& instance);
81  bool createLockFile(const std::string &appid, const std::string& instance);
82  void removeLockFile();
83  void signalRegistrations();
84  void initializeLogging(bool log2console);
85  void becomeDaemonProcess();
86 
87  IoServicePool io_service_pool_; // The pool of io_service objects used to perform asynchronous operations.
88  boost::asio::signal_set stop_signals_; // The signal_set is used to register for process termination notifications.
89  boost::asio::signal_set log_reopen_signals_; // The signal_set is used to register for process termination notifications.
90  boost::asio::ip::tcp::acceptor acceptor_; // Acceptor used to listen for incoming connections.
91  ConnectionPtr new_connection_; // The next connection to be accepted.
92  RequestRouter request_router_; // The handler for all incoming requests.
93  bfs::path lockFilePath;
94 
95  // Standard Handlers
96  RequestHandlerPtr statsReporter;
97  RequestHandlerPtr errorReporter;
98  RequestHandlerPtr handlerReporter;
99  RequestHandlerPtr logLevelAdjuster;
100  };
101 }
102 
103 #endif
Definition: request_router.hpp:43
Definition: io_service_pool.hpp:32
Definition: server.hpp:52
boost::shared_ptr< Connection > ConnectionPtr
Definition: connection.hpp:65
void run()
Definition: server.cpp:88
void stop()
Definition: server.cpp:104
boost::shared_ptr< RequestHandler > RequestHandlerPtr
Definition: request_handler.hpp:54
~Server()
Definition: server.hpp:64
Server(std::size_t io_service_pool_size, const std::string &application_id, const std::string &application_instance, bool runAsDaemon=true, const std::string &config_root_path="", const std::string &address="get_from_config", const std::string &port="get_from_config")
Definition: server.cpp:24
void register_handler(RequestHandlerPtr _handler)
Definition: server.cpp:119