root/server.hh

Revision ae733bf09b727f294ead488db5ac953eb50f20d8, 5.8 KB (checked in by Antti-Juhani Kaijanaho <antti-juhani@…>, 2 years ago)

[nntp::connection::tick] Kill idle connections (timeout at 2 hours)

Signed-off-by: Antti-Juhani Kaijanaho <antti-juhani@…>

  • Property mode set to 100644
Line 
1/*  This file is part of Alue, the multiprotocol Internet discussion daemon
2
3    Copyright © 2009, 2010 Antti-Juhani Kaijanaho
4
5    Alue is free software: you can redistribute it and/or modify it
6    under the terms of the GNU General Public License as published by
7    the Free Software Foundation, either version 3 of the License, or
8    (at your option) any later version.
9
10    Alue is distributed in the hope that it will be useful, but
11    WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with Alue.  If not, see <http://www.gnu.org/licenses/>.
17
18 */
19
20#ifndef GUARD_SERVER_HPP
21#define GUARD_SERVER_HPP
22
23#include "logger/logger.hh"
24#include "tls/init.hh"
25
26#include <boost/asio.hpp>
27#include <boost/bind.hpp>
28#include <boost/date_time.hpp>
29#include <boost/shared_ptr.hpp>
30#include <fstream>
31#include <set>
32
33namespace db { class db; }
34namespace http { class token; class session; class resource; }
35namespace smtp_client { class smtp_client; }
36
37class server : private boost::noncopyable
38{
39public:
40        class conn_cb;
41        class connection_factory;
42        class connection : private boost::noncopyable
43        {
44        protected:               
45                virtual server::conn_cb get_conn_cb() = 0;
46                friend class server;
47        public:
48                virtual ~connection() {}
49                virtual std::string get_port_name() const = 0;
50                virtual boost::shared_ptr<connection_factory>
51                get_factory() const = 0;
52                virtual void tick(bool stats) = 0;
53        };
54
55        class connection_factory
56        {
57        public:
58                virtual ~connection_factory() {}
59                virtual boost::shared_ptr<connection>
60                operator()(std::string port_name,
61                           boost::asio::ip::tcp::acceptor &acc,
62                           tls::init,
63                           server::conn_cb srv_cb) = 0;
64        };
65
66        class http_resource_factory : private boost::noncopyable
67        {
68        public:
69                virtual ~http_resource_factory() {}
70                virtual boost::shared_ptr<http::resource>
71                operator()(server::conn_cb cb, std::string path) = 0;
72        };
73
74        server(boost::asio::io_service &io_service,
75               tls::init tlsinit,
76               std::map<std::string,
77               std::pair<boost::shared_ptr<connection_factory>, bool> > &);
78
79        void run();
80       
81        void hup();
82       
83        class conn_cb
84        {
85                server &srv;
86                bool do_log;
87                conn_cb(server *srv, bool do_log) : srv(*srv), do_log(do_log) {}
88                friend class server;
89        public:
90                smtp_client::smtp_client &smtpc() const { return *srv.smtpc; }
91                db::db &dbase() const { return *srv.dbase; }
92                boost::shared_ptr<http::resource> get_http_resource
93                (std::string host, std::string path);
94                boost::shared_ptr<http::session> &get_session
95                (std::string cookie) const
96                        { return srv.http_sessions[cookie]; }
97                boost::shared_ptr<http::token> &get_http_token
98                (std::string token) const
99                        { return srv.http_tokens[token]; }
100                bool do_log_protocol_details() const { return do_log; }
101                void active(boost::shared_ptr<connection>);
102                void terminal(boost::shared_ptr<connection>);
103        };
104
105        static void register_http_resource(std::string path,
106                                           http_resource_factory *);
107
108private:
109        boost::asio::io_service &io_service;
110        boost::asio::io_service::strand strand;
111        boost::asio::deadline_timer timer;
112        boost::posix_time::ptime latest_logged_statistics;
113
114        typedef
115        std::map<std::string,
116                 boost::shared_ptr<boost::asio::ip::tcp::acceptor> >
117        acceptors_type;
118        acceptors_type acceptors;
119
120        boost::shared_ptr<smtp_client::smtp_client> smtpc;
121        boost::shared_ptr<db::db> dbase;
122
123        tls::init tlsinit;
124
125        std::map<std::string, boost::shared_ptr<http::session> > http_sessions;
126        std::map<std::string, boost::shared_ptr<http::token> > http_tokens;
127
128        typedef std::map<std::string, http_resource_factory *>
129        http_resources_type;
130        static http_resources_type *http_resources;
131
132        // connections in various states
133        std::map<std::string, boost::shared_ptr<connection> > waiting;
134        std::set<boost::shared_ptr<connection> > active;
135
136        void tick_handler(boost::system::error_code ec);
137
138        void terminate_connection(boost::shared_ptr<connection>);
139        void activate_connection(boost::shared_ptr<connection>);
140        bool replace_waiting(boost::shared_ptr<connection>);
141};
142
143inline void server::register_http_resource(std::string path,
144                                           http_resource_factory *rf)
145{
146        if (http_resources == 0) http_resources = new http_resources_type;
147        if (http_resources->find(path) != http_resources->end())
148                throw std::logic_error(path + " multiply registered");
149        (*http_resources)[path] = rf;
150}
151
152inline void server::conn_cb::terminal(boost::shared_ptr<connection> cp)
153{
154        srv.strand.post(boost::bind(&server::terminate_connection,
155                                    &srv,
156                                    cp));
157}
158
159inline void server::conn_cb::active(boost::shared_ptr<connection> cp)
160{
161        srv.strand.post(boost::bind(&server::activate_connection,
162                                    &srv,
163                                    cp));
164}
165
166#endif /* GUARD_SERVER_HPP */
Note: See TracBrowser for help on using the browser.