From f38fd36e5947fed43920ae23eb6d40c41eda0e58 Mon Sep 17 00:00:00 2001 From: Richard Peters Date: Fri, 15 Nov 2024 14:17:48 +0100 Subject: [PATCH 1/2] Initial commit multiple HTTP connections (cherry picked from commit 9a627827bf4800b6c27555dcf56ecf3a77187822) (cherry picked from commit 16bf54dc469150ea35b583fddcb2cfd03f4eb2f0) --- postmaster/instantiations/HttpServer.cpp | 12 ++++++++++-- postmaster/instantiations/HttpServer.hpp | 12 +++++++++++- postmaster/prototype_win/Main.cpp | 2 +- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/postmaster/instantiations/HttpServer.cpp b/postmaster/instantiations/HttpServer.cpp index ad9fc29..ef19c54 100644 --- a/postmaster/instantiations/HttpServer.cpp +++ b/postmaster/instantiations/HttpServer.cpp @@ -5,9 +5,18 @@ namespace main_ HttpServer::HttpServer(services::ConnectionFactory& connectionFactory, services::ConfigurationStoreAccess& hostname, services::ConfigurationStoreAccess& attributes, services::ConfigurationStoreAccess& password, application::Authentication& authentication, application::PostmasterDiscovery& postmasterDiscovery, - UartCreator& uartProgrammerCreator, UartCreator& uartExternalCreator, hal::Flash& upgradeFlash, hal::Reset& reset, const infra::Function& reporter, const infra::Function& receivingTarget, const infra::Function& receivingSelf, hal::GpioPin& resetTarget, hal::GpioPin& boot0) + const infra::Function& reporter, const infra::Function& receivingTarget, const infra::Function& receivingSelf) : HttpServerFrontEnd(connectionFactory, hostname, attributes, password, authentication, reporter, receivingTarget, receivingSelf) , discovery("discovery", postmasterDiscovery) + { + server.AddPage(discovery); + } + + HttpServerSingleConnection::HttpServerSingleConnection(services::ConnectionFactory& connectionFactory, services::ConfigurationStoreAccess& hostname, + services::ConfigurationStoreAccess& attributes, services::ConfigurationStoreAccess& password, + application::Authentication& authentication, application::PostmasterDiscovery& postmasterDiscovery, + UartCreator& uartProgrammerCreator, UartCreator& uartExternalCreator, hal::Flash& upgradeFlash, hal::Reset& reset, const infra::Function& reporter, const infra::Function& receivingTarget, const infra::Function& receivingSelf, hal::GpioPin& resetTarget, hal::GpioPin& boot0) + : HttpServer(connectionFactory, hostname, attributes, password, authentication, postmasterDiscovery, reporter, receivingTarget, receivingSelf) , uartProgrammerCreator(uartProgrammerCreator) , uartExternalCreator(uartExternalCreator) , selfFirmwarePage{ authentication, "firmware/self" } @@ -25,6 +34,5 @@ namespace main_ server.AddPage(targetUartExternal.page); server.AddPage(targetUartEchoProgrammer.page); server.AddPage(targetUartEchoExternal.page); - server.AddPage(discovery); } } diff --git a/postmaster/instantiations/HttpServer.hpp b/postmaster/instantiations/HttpServer.hpp index 562d963..2013dd0 100644 --- a/postmaster/instantiations/HttpServer.hpp +++ b/postmaster/instantiations/HttpServer.hpp @@ -14,6 +14,17 @@ namespace main_ : public HttpServerFrontEnd { HttpServer(services::ConnectionFactory& connectionFactory, services::ConfigurationStoreAccess& hostname, + services::ConfigurationStoreAccess& attributes, services::ConfigurationStoreAccess& password, + application::Authentication& authentication, application::PostmasterDiscovery& postmasterDiscovery, + const infra::Function& reporter, const infra::Function& receivingTarget, const infra::Function& receivingSelf); + + application::HttpPageDiscovery discovery; + }; + + struct HttpServerSingleConnection + : public HttpServer + { + HttpServerSingleConnection(services::ConnectionFactory& connectionFactory, services::ConfigurationStoreAccess& hostname, services::ConfigurationStoreAccess& attributes, services::ConfigurationStoreAccess& password, application::Authentication& authentication, application::PostmasterDiscovery& postmasterDiscovery, UartCreator& uartProgrammerCreator, UartCreator& uartExternalCreator, hal::Flash& upgradeFlash, hal::Reset& reset, const infra::Function& reporter, const infra::Function& receivingTarget, const infra::Function& receivingSelf, hal::GpioPin& resetTarget, hal::GpioPin& boot0); @@ -21,7 +32,6 @@ namespace main_ UartCreator& uartProgrammerCreator; UartCreator& uartExternalCreator; - application::HttpPageDiscovery discovery; application::AuthenticatedHttpPage::WithPage selfFirmwarePage; main_::SelfProgrammer selfProgrammer; diff --git a/postmaster/prototype_win/Main.cpp b/postmaster/prototype_win/Main.cpp index c212059..e7868ba 100644 --- a/postmaster/prototype_win/Main.cpp +++ b/postmaster/prototype_win/Main.cpp @@ -148,7 +148,7 @@ int main() static GpioPinStub resetTarget; static GpioPinStub boot0; static application::Authentication authentication{ passwordAccess, randomDataGenerator }; - static main_::HttpServer httpServer{ networkAdapter.ConnectionFactory(), hostnameAccess, attributesAccess, passwordAccess, authentication, mdnsDiscovery.discovery, serialCreator, serialCreator, flash, reset, [](bool open, services::IPAddress address) {}, [](bool receiving) {}, [](bool receiving) {}, resetTarget, boot0 }; + static main_::HttpServerSingleConnection httpServer{ networkAdapter.ConnectionFactory(), hostnameAccess, attributesAccess, passwordAccess, authentication, mdnsDiscovery.discovery, serialCreator, serialCreator, flash, reset, [](bool open, services::IPAddress address) {}, [](bool receiving) {}, [](bool receiving) {}, resetTarget, boot0 }; static main_::EchoServer echoServer{ networkAdapter.ConnectionFactory(), serialCreator, 1235 }; static main_::SingleConnectionLink link(httpServer.server, echoServer.listener); From f01aa622877d0211972ea0b439be84a0a6cbf2b1 Mon Sep 17 00:00:00 2001 From: Richard Peters Date: Sat, 18 Jan 2025 20:54:21 +0100 Subject: [PATCH 2/2] Accept multiple connections on port 80, one connection on port 8080 --- amp-embedded-infra-lib | 2 +- postmaster/frontend/ReportingHttpServer.cpp | 96 +++++++++++++++++-- postmaster/frontend/ReportingHttpServer.hpp | 64 +++++++++++-- postmaster/instantiations/EchoServer.cpp | 14 +-- postmaster/instantiations/EchoServer.hpp | 7 +- postmaster/instantiations/HttpServer.cpp | 8 +- postmaster/instantiations/HttpServer.hpp | 9 +- .../instantiations/HttpServerFrontEnd.cpp | 4 +- .../instantiations/HttpServerFrontEnd.hpp | 9 +- .../instantiations_st/EthernetPostmaster.hpp | 2 +- postmaster/instantiations_st/HttpServerSt.hpp | 2 +- .../instantiations_st/NetworkConnected.hpp | 14 ++- postmaster/prototype_win/Main.cpp | 5 +- 13 files changed, 188 insertions(+), 48 deletions(-) diff --git a/amp-embedded-infra-lib b/amp-embedded-infra-lib index 6c84af5..2179beb 160000 --- a/amp-embedded-infra-lib +++ b/amp-embedded-infra-lib @@ -1 +1 @@ -Subproject commit 6c84af5324c79219059d1066cc5ce3aad8f2b927 +Subproject commit 2179bebda670a47a1970fa4fa8703a2d8d7969d1 diff --git a/postmaster/frontend/ReportingHttpServer.cpp b/postmaster/frontend/ReportingHttpServer.cpp index 19f64f3..44a4661 100644 --- a/postmaster/frontend/ReportingHttpServer.cpp +++ b/postmaster/frontend/ReportingHttpServer.cpp @@ -14,13 +14,93 @@ namespace application reporter(false, services::IPAddress()); } - ReportingHttpServer::ReportingHttpServer(infra::BoundedString& buffer, services::ConnectionFactory& connectionFactory, uint16_t port, const infra::Function& reporter) - : SingleConnectionListener(connectionFactory, port, { connectionCreator }) + ReportingHttpServer::ReportingHttpServer(ConnectionAllocator& allocator, services::ConnectionFactory& connectionFactory, uint16_t port, const infra::Function& reporter) + : connectionAllocator(allocator) , reporter(reporter) - , buffer(buffer) - , connectionCreator([this](infra::Optional& value, services::IPAddress address) - { - value.Emplace(this->buffer, *this, this->reporter, address); - }) - {} + { + listener = connectionFactory.Listen(port, *this, services::IPVersions::both); + } + + void ReportingHttpServer::Stop(const infra::Function& onDone, bool force) + { + listener = nullptr; + waitingCreatedObserver = nullptr; + + StopConnections(onDone, force); + } + + void ReportingHttpServer::SetNewConnectionStrategy(services::NewConnectionStrategy& newConnectionStrategy) + { + this->newConnectionStrategy = &newConnectionStrategy; + } + + void ReportingHttpServer::StopCurrentConnection(void* listener) + { + StopConnections([this]() + { + newConnectionStrategy->StartNewConnection(); + }, + false); + } + + void ReportingHttpServer::StartNewConnection() + { + connectionAllocator.OnAllocatable(nullptr); + auto connectionObserver = connectionAllocator.Allocate(*this, reporter, waitingAddress); + if (connectionObserver != nullptr) + waitingCreatedObserver(connectionObserver); + else + { + connectionAllocator.OnAllocatable([this]() + { + newConnectionStrategy->StartNewConnection(); + }); + + CloseOrAbortAllConnections(false); + } + } + + void ReportingHttpServer::ConnectionAccepted(infra::AutoResetFunction connectionObserver)>&& createdObserver, services::IPAddress address) + { + waitingCreatedObserver = std::move(createdObserver); + waitingAddress = address; + + newConnectionStrategy->StartNewConnection(); + } + + void ReportingHttpServer::StopConnections(const infra::Function& onDone, bool force) + { + onStopConnectionsDone = onDone; + + if (connectionAllocator.NoneAllocated()) + onStopConnectionsDone(); + else + connectionAllocator.OnAllocatable([this, force]() + { + StopConnections(onStopConnectionsDone, force); + }); + + CloseOrAbortAllConnections(force); + } + + void ReportingHttpServer::CloseOrAbortAllConnections(bool force) + { + for (auto& connection : connections) + if (force) + connection.Abort(); + else + connection.Close(); + } + + ReportingHttpServer::Connection::Connection(ReportingHttpServer& httpServer, const infra::Function& reporter, services::IPAddress address) + : ReportingHttpServerConnectionObserver::WithBuffer(httpServer, reporter, address) + , httpServer(httpServer) + { + httpServer.connections.push_back(*this); + } + + ReportingHttpServer::Connection::~Connection() + { + httpServer.connections.erase(*this); + } } diff --git a/postmaster/frontend/ReportingHttpServer.hpp b/postmaster/frontend/ReportingHttpServer.hpp index 5672690..ca815d1 100644 --- a/postmaster/frontend/ReportingHttpServer.hpp +++ b/postmaster/frontend/ReportingHttpServer.hpp @@ -1,6 +1,8 @@ #ifndef POSTMASTER_REPORTING_HTTP_SERVER_HPP #define POSTMASTER_REPORTING_HTTP_SERVER_HPP +#include "infra/util/IntrusiveList.hpp" +#include "infra/util/SharedObjectAllocatorFixedSize.hpp" #include "services/network/HttpServer.hpp" namespace application @@ -9,6 +11,10 @@ namespace application : public services::HttpServerConnectionObserver { public: + template + using WithBuffer = infra::WithStorage>; + ReportingHttpServerConnectionObserver(infra::BoundedString& buffer, HttpPageServer& httpServer, const infra::Function& reporter, services::IPAddress address); ~ReportingHttpServerConnectionObserver(); @@ -17,19 +23,63 @@ namespace application }; class ReportingHttpServer - : public services::SingleConnectionListener - , public services::HttpPageServer + : public services::HttpPageServer + , public services::NewConnectionStrategy + , private services::ServerConnectionObserverFactory { public: - template<::size_t BufferSize> - using WithBuffer = infra::WithStorage>; + static constexpr std::size_t BufferSize = 8192; + + class Connection + : public infra::IntrusiveList::NodeType + , public ReportingHttpServerConnectionObserver::WithBuffer + { + public: + Connection(ReportingHttpServer& httpServer, const infra::Function& reporter, services::IPAddress address); + Connection(const Connection& other) = delete; + Connection& operator=(const Connection& other) = delete; + ~Connection(); + + private: + ReportingHttpServer& httpServer; + }; + + using ConnectionAllocator = infra::SharedObjectAllocator& reporter, services::IPAddress address)>; + + using ConnectionAllocatorFixedSize = infra::SharedObjectAllocatorFixedSize& reporter, services::IPAddress address)>; + + template + using WithConnections = infra::WithStorage>; + + ReportingHttpServer(ConnectionAllocator& allocator, services::ConnectionFactory& connectionFactory, uint16_t port, const infra::Function& reporter); - ReportingHttpServer(infra::BoundedString& buffer, services::ConnectionFactory& connectionFactory, uint16_t port, const infra::Function& reporter); + void Stop(const infra::Function& onDone, bool force); + void SetNewConnectionStrategy(services::NewConnectionStrategy& newConnectionStrategy); + + // Implementation of NewConnectionStrategy + void StopCurrentConnection(void* listener) override; + void StartNewConnection() override; + + private: + // Implementation of ServerConnectionObserverFactory + void ConnectionAccepted(infra::AutoResetFunction connectionObserver)>&& createdObserver, services::IPAddress address) override; + + void StopConnections(const infra::Function& onDone, bool force); + void CloseOrAbortAllConnections(bool force); private: + services::NewConnectionStrategy* newConnectionStrategy = this; infra::Function reporter; - infra::BoundedString& buffer; - infra::Creator connectionCreator; + infra::SharedPtr listener; + + ConnectionAllocator& connectionAllocator; + infra::IntrusiveList connections; + + infra::AutoResetFunction connectionObserver)> waitingCreatedObserver; + services::IPAddress waitingAddress; + + infra::Function onStopConnectionsDone; }; } diff --git a/postmaster/instantiations/EchoServer.cpp b/postmaster/instantiations/EchoServer.cpp index 02e0767..2b119b7 100644 --- a/postmaster/instantiations/EchoServer.cpp +++ b/postmaster/instantiations/EchoServer.cpp @@ -21,7 +21,7 @@ namespace main_ return echoConnection; } - SingleConnectionLink::SingleConnectionLink(services::SingleConnectionListener& listener1, services::SingleConnectionListener& listener2) + SingleConnectionLink::SingleConnectionLink(application::ReportingHttpServer& listener1, services::SingleConnectionListener& listener2) : listener1(listener1) , listener2(listener2) { @@ -29,16 +29,16 @@ namespace main_ listener2.SetNewConnectionStrategy(*this); } - void SingleConnectionLink::StopCurrentConnection(services::SingleConnectionListener& listener) + void SingleConnectionLink::StopCurrentConnection(void* listener) { - if (&listener == &listener1) + if (listener == &listener1) listener1RequestedNewConnection = true; - if (&listener == &listener2) + if (listener == &listener2) listener2RequestedNewConnection = true; numStopped = 0; - listener1.StopCurrentConnection(listener1); - listener2.StopCurrentConnection(listener2); + listener1.StopCurrentConnection(&listener1); + listener2.StopCurrentConnection(&listener2); } void SingleConnectionLink::StartNewConnection() @@ -53,7 +53,7 @@ namespace main_ listener1.StartNewConnection(); if (listener2RequestedNewConnection) - StopCurrentConnection(listener1); + StopCurrentConnection(&listener1); } else if (listener2RequestedNewConnection) { diff --git a/postmaster/instantiations/EchoServer.hpp b/postmaster/instantiations/EchoServer.hpp index 0865ccf..437d9ad 100644 --- a/postmaster/instantiations/EchoServer.hpp +++ b/postmaster/instantiations/EchoServer.hpp @@ -1,3 +1,4 @@ +#include "postmaster/frontend/ReportingHttpServer.hpp" #include "postmaster/instantiations/UartCreator.hpp" #include "protobuf/echo/ServiceForwarder.hpp" #include "services/network/Connection.hpp" @@ -42,15 +43,15 @@ namespace main_ : private services::NewConnectionStrategy { public: - SingleConnectionLink(services::SingleConnectionListener& listener1, services::SingleConnectionListener& listener2); + SingleConnectionLink(application::ReportingHttpServer& listener1, services::SingleConnectionListener& listener2); private: - void StopCurrentConnection(services::SingleConnectionListener& listener) override; + void StopCurrentConnection(void* listener) override; void StartNewConnection() override; private: uint32_t numStopped = 0; - services::SingleConnectionListener& listener1; + application::ReportingHttpServer& listener1; services::SingleConnectionListener& listener2; bool listener1RequestedNewConnection = false; diff --git a/postmaster/instantiations/HttpServer.cpp b/postmaster/instantiations/HttpServer.cpp index ef19c54..e61f3ac 100644 --- a/postmaster/instantiations/HttpServer.cpp +++ b/postmaster/instantiations/HttpServer.cpp @@ -2,21 +2,21 @@ namespace main_ { - HttpServer::HttpServer(services::ConnectionFactory& connectionFactory, services::ConfigurationStoreAccess& hostname, + HttpServer::HttpServer(application::ReportingHttpServer::ConnectionAllocator& allocator, services::ConnectionFactory& connectionFactory, uint16_t port, services::ConfigurationStoreAccess& hostname, services::ConfigurationStoreAccess& attributes, services::ConfigurationStoreAccess& password, application::Authentication& authentication, application::PostmasterDiscovery& postmasterDiscovery, const infra::Function& reporter, const infra::Function& receivingTarget, const infra::Function& receivingSelf) - : HttpServerFrontEnd(connectionFactory, hostname, attributes, password, authentication, reporter, receivingTarget, receivingSelf) + : HttpServerFrontEnd(allocator, connectionFactory, port, hostname, attributes, password, authentication, reporter, receivingTarget, receivingSelf) , discovery("discovery", postmasterDiscovery) { server.AddPage(discovery); } - HttpServerSingleConnection::HttpServerSingleConnection(services::ConnectionFactory& connectionFactory, services::ConfigurationStoreAccess& hostname, + HttpServerSingleConnection::HttpServerSingleConnection(services::ConnectionFactory& connectionFactory, uint16_t port, services::ConfigurationStoreAccess& hostname, services::ConfigurationStoreAccess& attributes, services::ConfigurationStoreAccess& password, application::Authentication& authentication, application::PostmasterDiscovery& postmasterDiscovery, UartCreator& uartProgrammerCreator, UartCreator& uartExternalCreator, hal::Flash& upgradeFlash, hal::Reset& reset, const infra::Function& reporter, const infra::Function& receivingTarget, const infra::Function& receivingSelf, hal::GpioPin& resetTarget, hal::GpioPin& boot0) - : HttpServer(connectionFactory, hostname, attributes, password, authentication, postmasterDiscovery, reporter, receivingTarget, receivingSelf) + : HttpServer::WithConnections<1>(connectionFactory, port, hostname, attributes, password, authentication, postmasterDiscovery, reporter, receivingTarget, receivingSelf) , uartProgrammerCreator(uartProgrammerCreator) , uartExternalCreator(uartExternalCreator) , selfFirmwarePage{ authentication, "firmware/self" } diff --git a/postmaster/instantiations/HttpServer.hpp b/postmaster/instantiations/HttpServer.hpp index 2013dd0..be21f87 100644 --- a/postmaster/instantiations/HttpServer.hpp +++ b/postmaster/instantiations/HttpServer.hpp @@ -13,7 +13,10 @@ namespace main_ struct HttpServer : public HttpServerFrontEnd { - HttpServer(services::ConnectionFactory& connectionFactory, services::ConfigurationStoreAccess& hostname, + template + using WithConnections = infra::WithStorage>; + + HttpServer(application::ReportingHttpServer::ConnectionAllocator& allocator, services::ConnectionFactory& connectionFactory, uint16_t port, services::ConfigurationStoreAccess& hostname, services::ConfigurationStoreAccess& attributes, services::ConfigurationStoreAccess& password, application::Authentication& authentication, application::PostmasterDiscovery& postmasterDiscovery, const infra::Function& reporter, const infra::Function& receivingTarget, const infra::Function& receivingSelf); @@ -22,9 +25,9 @@ namespace main_ }; struct HttpServerSingleConnection - : public HttpServer + : public HttpServer::WithConnections<1> { - HttpServerSingleConnection(services::ConnectionFactory& connectionFactory, services::ConfigurationStoreAccess& hostname, + HttpServerSingleConnection(services::ConnectionFactory& connectionFactory, uint16_t port, services::ConfigurationStoreAccess& hostname, services::ConfigurationStoreAccess& attributes, services::ConfigurationStoreAccess& password, application::Authentication& authentication, application::PostmasterDiscovery& postmasterDiscovery, UartCreator& uartProgrammerCreator, UartCreator& uartExternalCreator, hal::Flash& upgradeFlash, hal::Reset& reset, const infra::Function& reporter, const infra::Function& receivingTarget, const infra::Function& receivingSelf, hal::GpioPin& resetTarget, hal::GpioPin& boot0); diff --git a/postmaster/instantiations/HttpServerFrontEnd.cpp b/postmaster/instantiations/HttpServerFrontEnd.cpp index d18f158..da1d0b6 100644 --- a/postmaster/instantiations/HttpServerFrontEnd.cpp +++ b/postmaster/instantiations/HttpServerFrontEnd.cpp @@ -2,12 +2,12 @@ namespace main_ { - HttpServerFrontEnd::HttpServerFrontEnd(services::ConnectionFactory& connectionFactory, services::ConfigurationStoreAccess& hostname, + HttpServerFrontEnd::HttpServerFrontEnd(application::ReportingHttpServer::ConnectionAllocator& allocator, services::ConnectionFactory& connectionFactory, uint16_t port, services::ConfigurationStoreAccess& hostname, services::ConfigurationStoreAccess& attributes, services::ConfigurationStoreAccess& password, application::Authentication& authentication, const infra::Function& reporter, const infra::Function& receivingTarget, const infra::Function& receivingSelf) : configuration{ authentication, "configuration", hostname, attributes, password } - , server{ connectionFactory, 80, reporter } + , server{ allocator, connectionFactory, port, reporter } { server.AddPage(frontendHtml); server.AddPage(frontendCss); diff --git a/postmaster/instantiations/HttpServerFrontEnd.hpp b/postmaster/instantiations/HttpServerFrontEnd.hpp index d40843b..7c34657 100644 --- a/postmaster/instantiations/HttpServerFrontEnd.hpp +++ b/postmaster/instantiations/HttpServerFrontEnd.hpp @@ -13,15 +13,14 @@ namespace main_ { struct HttpServerFrontEnd - : public services::Stoppable { - HttpServerFrontEnd(services::ConnectionFactory& connectionFactory, services::ConfigurationStoreAccess& hostname, + HttpServerFrontEnd(application::ReportingHttpServer::ConnectionAllocator& allocator, services::ConnectionFactory& connectionFactory, uint16_t port, services::ConfigurationStoreAccess& hostname, services::ConfigurationStoreAccess& attributes, services::ConfigurationStoreAccess& password, application::Authentication& authentication, const infra::Function& reporter, const infra::Function& receivingTarget, const infra::Function& receivingSelf); - void Stop(const infra::Function& onDone) override + void Stop(const infra::Function& onDone) { - server.Stop(onDone); + server.Stop(onDone, true); } services::HttpPageWithContent frontendHtml{ "", page_contents::indexHtml, "text/html;charset=UTF-8" }; @@ -29,7 +28,7 @@ namespace main_ services::HttpPageWithContent frontendJs{ "upload.js", page_contents::uploadJs, "text/javascript" }; application::AuthenticatedHttpPage::WithPage configuration; - application::ReportingHttpServer::WithBuffer<8192> server; + application::ReportingHttpServer server; }; } diff --git a/postmaster/instantiations_st/EthernetPostmaster.hpp b/postmaster/instantiations_st/EthernetPostmaster.hpp index 735b79e..3a666ad 100644 --- a/postmaster/instantiations_st/EthernetPostmaster.hpp +++ b/postmaster/instantiations_st/EthernetPostmaster.hpp @@ -42,7 +42,7 @@ namespace main_ application::Authentication authentication; infra::Creator networkCreator; - main_::Ethernet<3, 0, 4> ethernet; + main_::Ethernet<4, 0, 9> ethernet; }; } diff --git a/postmaster/instantiations_st/HttpServerSt.hpp b/postmaster/instantiations_st/HttpServerSt.hpp index 6c4fc91..60308a5 100644 --- a/postmaster/instantiations_st/HttpServerSt.hpp +++ b/postmaster/instantiations_st/HttpServerSt.hpp @@ -11,7 +11,7 @@ namespace main_ { struct HttpServerSt - : public HttpServer + : public HttpServerSingleConnection { HttpServerSt(services::ConnectionFactory& connectionFactory, services::ConfigurationStoreAccess& hostname, services::ConfigurationStoreAccess& attributes, services::ConfigurationStoreAccess& password, diff --git a/postmaster/instantiations_st/NetworkConnected.hpp b/postmaster/instantiations_st/NetworkConnected.hpp index 6213505..dc80fa7 100644 --- a/postmaster/instantiations_st/NetworkConnected.hpp +++ b/postmaster/instantiations_st/NetworkConnected.hpp @@ -21,7 +21,7 @@ namespace main_ hal::Flash& upgradePack, UartCreator& uartProgrammerCreator, UartCreator& uartExternalCreator, hal::Reset& reset, application::ViewStatus& viewStatus) : mdns{ *hostname, lightweightIp, lightweightIp, lightweightIp.GetIPv4Address(), Postmaster::generated::VERSION, Postmaster::generated::VERSION_FULL, *attributes } , mdnsDiscovery{ lightweightIp, lightweightIp } - , httpServer{ lightweightIp, hostname, attributes, password, authentication, mdnsDiscovery.discovery, uartProgrammerCreator, uartExternalCreator, upgradePack, reset, [&viewStatus](bool open, services::IPAddress address) + , httpServerSingleConnection{ lightweightIp, hostname, attributes, password, authentication, mdnsDiscovery.discovery, uartProgrammerCreator, uartExternalCreator, upgradePack, reset, [&viewStatus](bool open, services::IPAddress address) { viewStatus.SetConnectionOpen(open, address); }, @@ -39,15 +39,21 @@ namespace main_ void Stop(const infra::Function& onDone) override { - httpServer.Stop(onDone); + this->onDone = onDone; + httpServerSingleConnection.Stop([this]() + { + httpServer.Stop(this->onDone); + }); } main_::Mdns mdns; main_::MdnsDiscovery mdnsDiscovery; - main_::HttpServerSt httpServer; + main_::HttpServer::WithConnections<5> httpServer; + main_::HttpServerSt httpServerSingleConnection; main_::EchoServer echoServer; - main_::SingleConnectionLink link{ httpServer.server, echoServer.listener }; + main_::SingleConnectionLink link{ httpServerSingleConnection.server, echoServer.listener }; main_::TraceForwarderSt traceForwarder; + infra::Function onDone; }; } diff --git a/postmaster/prototype_win/Main.cpp b/postmaster/prototype_win/Main.cpp index e7868ba..40dc72a 100644 --- a/postmaster/prototype_win/Main.cpp +++ b/postmaster/prototype_win/Main.cpp @@ -148,9 +148,10 @@ int main() static GpioPinStub resetTarget; static GpioPinStub boot0; static application::Authentication authentication{ passwordAccess, randomDataGenerator }; - static main_::HttpServerSingleConnection httpServer{ networkAdapter.ConnectionFactory(), hostnameAccess, attributesAccess, passwordAccess, authentication, mdnsDiscovery.discovery, serialCreator, serialCreator, flash, reset, [](bool open, services::IPAddress address) {}, [](bool receiving) {}, [](bool receiving) {}, resetTarget, boot0 }; + static main_::HttpServer::WithConnections<5> httpServer{ networkAdapter.ConnectionFactory(), 80, hostnameAccess, attributesAccess, passwordAccess, authentication, mdnsDiscovery.discovery, [](bool open, services::IPAddress address) {}, [](bool receiving) {}, [](bool receiving) {} }; + static main_::HttpServerSingleConnection httpServerSingleConnection{ networkAdapter.ConnectionFactory(), 8080, hostnameAccess, attributesAccess, passwordAccess, authentication, mdnsDiscovery.discovery, serialCreator, serialCreator, flash, reset, [](bool open, services::IPAddress address) {}, [](bool receiving) {}, [](bool receiving) {}, resetTarget, boot0 }; static main_::EchoServer echoServer{ networkAdapter.ConnectionFactory(), serialCreator, 1235 }; - static main_::SingleConnectionLink link(httpServer.server, echoServer.listener); + static main_::SingleConnectionLink link(httpServerSingleConnection.server, echoServer.listener); networkAdapter.Run(); }