helics  3.3.0
AsioBrokerServer.hpp
1 /*
2 Copyright (c) 2017-2022,
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 Json::Value* 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 Json::Value* 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 {
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 Json::Value* /*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::Echo
Definition: Echo.hpp:27
helics::CoreType::UDP
@ UDP
use UDP packets to send the data
helics::apps::Player
Definition: Player.hpp:43
helics::apps::AsioBrokerServer::startServer
virtual void startServer(const Json::Value *, const std::shared_ptr< TypedBrokerServer > &) override
Definition: AsioBrokerServer.hpp:99
helics::apps::Tracer
Definition: Tracer.hpp:26
helics::apps::AsioBrokerServer
Definition: AsioBrokerServer.hpp:92
helics::apps::TypedBrokerServer::generateMessageResponse
static ActionMessage generateMessageResponse(const ActionMessage &rxcmd, portData &pdata, CoreType ctype)
Definition: TypedBrokerServer.cpp:65
helics::BrokerApp
Definition: application_api/BrokerApp.hpp:26
HELICS_CORE_TYPE_UDP
@ HELICS_CORE_TYPE_UDP
Definition: helics_enums.h:41
helics::helicsCLI11App
Definition: helicsCLI11.hpp:42
helics::apps::Source
Definition: Source.hpp:64
helics::apps::Clone
Definition: Clone.hpp:28
helics::cleanupHelicsLibrary
void cleanupHelicsLibrary()
Definition: Federate.cpp:38
helics::apps::TypedBrokerServer::processArgs
virtual void processArgs(std::string_view args)
Definition: TypedBrokerServer.cpp:99
helics::isProtocolCommand
bool isProtocolCommand(const ActionMessage &command) noexcept
Definition: ActionMessage.hpp:226
helics
the main namespace for the helics co-simulation library User functions will be in the helics namespac...
Definition: AsyncFedCallInfo.hpp:14
helics::apps::TypedBrokerServer
Definition: TypedBrokerServer.hpp:24
helics::BrokerKeeper
Definition: application_api/BrokerApp.hpp:186
helics::CoreType::TCP
@ TCP
use a generic TCP protocol message stream to send messages
helics::apps::AsioBrokerServer::stopServer
virtual void stopServer() override
Definition: AsioBrokerServer.hpp:104
HELICS_CORE_TYPE_TCP
@ HELICS_CORE_TYPE_TCP
Definition: helics_enums.h:39
helics::apps::Recorder
Definition: Recorder.hpp:27