| helics 3.6.1

AsioBrokerServer.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 <memory>
13 #include <string>
14 #include <utility>
15
16 #if defined(HELICS_ENABLE_TCP_CORE) || defined(HELICS_ENABLE_UDP_CORE)
17
18 # include "gmlc/networking/AsioContextManager.h"
19
20 # include <mutex>
21 # include <thread>
22
23 # ifdef HELICS_ENABLE_TCP_CORE
24 namespace gmlc::networking {
25 class TcpServer;
26 class TcpConnection;
27 } // namespace gmlc::networking
28 # endif
29
30 namespace helics {
31
32 # ifdef HELICS_ENABLE_UDP_CORE
33 namespace udp {
34 class UdpServer;
35 }
36 # endif
37 class Broker;
38 namespace apps {
39
41 class AsioBrokerServer: public TypedBrokerServer {
42 public:
43 AsioBrokerServer() = default;
44 explicit AsioBrokerServer(std::string_view server_name): name_(server_name) {}
46 virtual void startServer(const nlohmann::json* val,
47 const std::shared_ptr<TypedBrokerServer>& ptr) override;
49 virtual void stopServer() override;
50 virtual void processArgs(std::string_view args) override;
51 void enableTcpServer(bool enabled) { tcp_enabled_ = enabled; }
52 void enableUdpServer(bool enabled) { udp_enabled_ = enabled; }
53
54 private:
55 void mainLoop();
56 # ifdef HELICS_ENABLE_TCP_CORE
57 std::shared_ptr<gmlc::networking::TcpServer> loadTCPserver(asio::io_context& ioctx);
58 static void loadTCPServerData(portData& pdata);
59 std::size_t
60 tcpDataReceive(const std::shared_ptr<gmlc::networking::TcpConnection>& connection,
61 const char* data,
62 size_t bytes_received);
63 std::shared_ptr<gmlc::networking::TcpServer> tcpserver;
64 portData tcpPortData;
65 # endif
66 # ifdef HELICS_ENABLE_UDP_CORE
67 std::shared_ptr<udp::UdpServer> loadUDPserver(asio::io_context& ioctx);
68 static void loadUDPServerData(portData& pdata);
69
70 bool udpDataReceive(const std::shared_ptr<udp::UdpServer>& server,
71 const char* data,
72 size_t bytes_received);
73 std::shared_ptr<udp::UdpServer> udpserver;
74 portData udpPortData;
75 # endif
76
77 std::thread mainLoopThread;
78 std::mutex threadGuard;
79
80 const nlohmann::json* config_{nullptr};
81 const std::string name_;
82 bool tcp_enabled_{false};
83 bool udp_enabled_{false};
84 };
85 } // namespace apps
86 } // namespace helics
87
88 #else
89 namespace helics {
90 namespace apps {
92 class AsioBrokerServer: public TypedBrokerServer {
93 public:
94 AsioBrokerServer() = default;
95 explicit AsioBrokerServer(std::string_view /*server_name*/) {}
96 void enableTcpServer(bool /*enabled*/) {}
97 void enableUdpServer(bool /*enabled*/) {}
99 virtual void startServer(const nlohmann::json* /*val*/,
100 const std::shared_ptr<TypedBrokerServer>& /*ptr*/) override
101 {
102 }
104 virtual void stopServer() override {}
105 };
106 } // namespace apps
107 } // namespace helics
108 #endif
helics::apps::AsioBrokerServer
Definition: AsioBrokerServer.hpp:92
helics::apps::AsioBrokerServer::startServer
virtual void startServer(const nlohmann::json *, const std::shared_ptr< TypedBrokerServer > &) override
Definition: AsioBrokerServer.hpp:99
helics::apps::AsioBrokerServer::stopServer
virtual void stopServer() override
Definition: AsioBrokerServer.hpp:104
helics::apps::TypedBrokerServer
Definition: TypedBrokerServer.hpp:24
helics::apps::TypedBrokerServer::processArgs
virtual void processArgs(std::string_view args)
Definition: TypedBrokerServer.cpp:101
helics
the main namespace for the helics co-simulation library User functions will be in the helics namespac...
Definition: AsyncFedCallInfo.hpp:14