11#ifndef POSTMASTER_REPORTING_HTTP_SERVER_HPP
22#define POSTMASTER_REPORTING_HTTP_SERVER_HPP
33
4+ #include " infra/util/IntrusiveList.hpp"
5+ #include " infra/util/SharedObjectAllocatorFixedSize.hpp"
46#include " services/network/HttpServer.hpp"
57
68namespace application
@@ -9,6 +11,10 @@ namespace application
911 : public services::HttpServerConnectionObserver
1012 {
1113 public:
14+ template <size_t BufferSize>
15+ using WithBuffer = infra::WithStorage<ReportingHttpServerConnectionObserver,
16+ infra::BoundedString::WithStorage<BufferSize>>;
17+
1218 ReportingHttpServerConnectionObserver (infra::BoundedString& buffer, HttpPageServer& httpServer, const infra::Function<void (bool open, services::IPAddress address)>& reporter, services::IPAddress address);
1319 ~ReportingHttpServerConnectionObserver ();
1420
@@ -17,19 +23,63 @@ namespace application
1723 };
1824
1925 class ReportingHttpServer
20- : public services::SingleConnectionListener
21- , public services::HttpPageServer
26+ : public services::HttpPageServer
27+ , public services::NewConnectionStrategy
28+ , private services::ServerConnectionObserverFactory
2229 {
2330 public:
24- template <::size_t BufferSize>
25- using WithBuffer = infra::WithStorage<ReportingHttpServer, infra::BoundedString::WithStorage<BufferSize>>;
31+ static constexpr std::size_t BufferSize = 8192 ;
32+
33+ class Connection
34+ : public infra::IntrusiveList<Connection>::NodeType
35+ , public ReportingHttpServerConnectionObserver::WithBuffer<BufferSize>
36+ {
37+ public:
38+ Connection (ReportingHttpServer& httpServer, const infra::Function<void (bool open, services::IPAddress address)>& reporter, services::IPAddress address);
39+ Connection (const Connection& other) = delete ;
40+ Connection& operator =(const Connection& other) = delete ;
41+ ~Connection ();
42+
43+ private:
44+ ReportingHttpServer& httpServer;
45+ };
46+
47+ using ConnectionAllocator = infra::SharedObjectAllocator<Connection, void (ReportingHttpServer& httpServer, const infra::Function<void (bool open, services::IPAddress address)>& reporter, services::IPAddress address)>;
48+
49+ using ConnectionAllocatorFixedSize = infra::SharedObjectAllocatorFixedSize<Connection,
50+ void (ReportingHttpServer& httpServer, const infra::Function<void (bool open, services::IPAddress address)>& reporter, services::IPAddress address)>;
51+
52+ template <std::size_t Connections>
53+ using WithConnections = infra::WithStorage<ReportingHttpServer, typename ConnectionAllocatorFixedSize::template WithStorage<Connections>>;
54+
55+ ReportingHttpServer (ConnectionAllocator& allocator, services::ConnectionFactory& connectionFactory, uint16_t port, const infra::Function<void (bool open, services::IPAddress address)>& reporter);
2656
27- ReportingHttpServer (infra::BoundedString& buffer, services::ConnectionFactory& connectionFactory, uint16_t port, const infra::Function<void (bool open, services::IPAddress address)>& reporter);
57+ void Stop (const infra::Function<void ()>& onDone, bool force);
58+ void SetNewConnectionStrategy (services::NewConnectionStrategy& newConnectionStrategy);
59+
60+ // Implementation of NewConnectionStrategy
61+ void StopCurrentConnection (void * listener) override ;
62+ void StartNewConnection () override ;
63+
64+ private:
65+ // Implementation of ServerConnectionObserverFactory
66+ void ConnectionAccepted (infra::AutoResetFunction<void (infra::SharedPtr<services::ConnectionObserver> connectionObserver)>&& createdObserver, services::IPAddress address) override ;
67+
68+ void StopConnections (const infra::Function<void ()>& onDone, bool force);
69+ void CloseOrAbortAllConnections (bool force);
2870
2971 private:
72+ services::NewConnectionStrategy* newConnectionStrategy = this ;
3073 infra::Function<void (bool open, services::IPAddress address)> reporter;
31- infra::BoundedString& buffer;
32- infra::Creator<services::ConnectionObserver, ReportingHttpServerConnectionObserver, void (services::IPAddress address)> connectionCreator;
74+ infra::SharedPtr<void > listener;
75+
76+ ConnectionAllocator& connectionAllocator;
77+ infra::IntrusiveList<Connection> connections;
78+
79+ infra::AutoResetFunction<void (infra::SharedPtr<services::ConnectionObserver> connectionObserver)> waitingCreatedObserver;
80+ services::IPAddress waitingAddress;
81+
82+ infra::Function<void ()> onStopConnectionsDone;
3383 };
3484}
3585
0 commit comments