| helics 3.6.1

helicsWebServer.hpp
1 /*
2 Copyright (c) 2017-2025,
3 Battelle Memorial Institute; Lawrence Livermore National Security, LLC; Alliance for Sustainable
4 Energy, LLC. See the top-level NOTICE for additional details. All rights reserved.
5 SPDX-License-Identifier: BSD-3-Clause
6 */
7
8 #pragma once
9
10 #include "TypedBrokerServer.hpp"
11
12 #include <atomic>
13 #include <memory>
14 #include <mutex>
15 #include <string>
16 #include <thread>
17 #include <utility>
18
19 namespace helics::apps {
20
21 class IocWrapper;
22
24 class WebServer: public TypedBrokerServer {
25 public:
27 static constexpr int defaultHttpPort{43542};
29 static constexpr int defaultWebSocketPort{43543};
30 WebServer() = default;
31 explicit WebServer(std::string_view server_name): mName(server_name) {}
33 virtual void startServer(const nlohmann::json* val,
34 const std::shared_ptr<TypedBrokerServer>& ptr) override;
36 virtual void stopServer() override;
38 virtual void processArgs(std::string_view args) override;
40 void enableHttpServer(bool enabled) { mHttpEnabled = enabled; }
42 void enableWebSocketServer(bool enabled) { mWebsocketEnabled = enabled; }
43
44 private:
45 void mainLoop(std::shared_ptr<WebServer> keepAlive);
46 std::atomic<bool> running{false};
47 std::shared_ptr<IocWrapper> context;
48 std::thread mainLoopThread;
49 std::mutex threadGuard;
50
51 const nlohmann::json* config{nullptr};
52 const std::string mName;
53 std::string mArgs;
54 std::string mHttpAddress;
55 int mHttpPort{defaultHttpPort};
56 std::string mWebsocketAddress;
57 int mWebsocketPort{defaultWebSocketPort};
58 bool mHttpEnabled{false};
59 bool mWebsocketEnabled{false};
60 int mInterfaceNetwork{0};
61 std::atomic<bool> executing{false};
62 };
63 } // namespace helics::apps
helics::apps::TypedBrokerServer
Definition: TypedBrokerServer.hpp:24
helics::apps::WebServer
Definition: helicsWebServer.hpp:24
helics::apps::WebServer::defaultHttpPort
static constexpr int defaultHttpPort
default port for the HTTP web server
Definition: helicsWebServer.hpp:27
helics::apps::WebServer::defaultWebSocketPort
static constexpr int defaultWebSocketPort
default port for the Websocket server
Definition: helicsWebServer.hpp:29
helics::apps::WebServer::stopServer
virtual void stopServer() override
Definition: helicsWebServer.cpp:1052
helics::apps::WebServer::enableHttpServer
void enableHttpServer(bool enabled)
Definition: helicsWebServer.hpp:40
helics::apps::WebServer::startServer
virtual void startServer(const nlohmann::json *val, const std::shared_ptr< TypedBrokerServer > &ptr) override
Definition: helicsWebServer.cpp:1026
helics::apps::WebServer::processArgs
virtual void processArgs(std::string_view args) override
Definition: helicsWebServer.cpp:970
helics::apps::WebServer::enableWebSocketServer
void enableWebSocketServer(bool enabled)
Definition: helicsWebServer.hpp:42