helics  3.0.1
AsioBrokerServer.hpp
1 /*
2 Copyright (c) 2017-2021,
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 "helics/common/AsioContextManager.h"
19 
20 # include <mutex>
21 # include <thread>
22 
23 namespace helics {
24 # ifdef HELICS_ENABLE_TCP_CORE
25 namespace tcp {
26  class TcpServer;
27  class TcpConnection;
28 } // namespace tcp
29 # endif
30 # ifdef HELICS_ENABLE_UDP_CORE
31 namespace udp {
32  class UdpServer;
33 }
34 # endif
35 class Broker;
36 namespace apps {
37 
39  class AsioBrokerServer: public TypedBrokerServer {
40  public:
41  AsioBrokerServer() = default;
42  explicit AsioBrokerServer(std::string server_name): name_(std::move(server_name)) {}
44  virtual void startServer(const Json::Value* val) override;
46  virtual void stopServer() override;
47  virtual void processArgs(const std::string& args) override;
48  void enableTcpServer(bool enabled) { tcp_enabled_ = enabled; }
49  void enableUdpServer(bool enabled) { udp_enabled_ = enabled; }
50 
51  private:
52  void mainLoop();
53 # ifdef HELICS_ENABLE_TCP_CORE
54  std::shared_ptr<tcp::TcpServer> loadTCPserver(asio::io_context& ioctx);
55  static void loadTCPServerData(portData& pdata);
56  std::size_t tcpDataReceive(const std::shared_ptr<tcp::TcpConnection>& connection,
57  const char* data,
58  size_t bytes_received);
59  std::shared_ptr<tcp::TcpServer> tcpserver;
60  portData tcpPortData;
61 # endif
62 # ifdef HELICS_ENABLE_UDP_CORE
63  std::shared_ptr<udp::UdpServer> loadUDPserver(asio::io_context& ioctx);
64  static void loadUDPServerData(portData& pdata);
65 
66  bool udpDataReceive(const std::shared_ptr<udp::UdpServer>& server,
67  const char* data,
68  size_t bytes_received);
69  std::shared_ptr<udp::UdpServer> udpserver;
70  portData udpPortData;
71 # endif
72 
73  std::thread mainLoopThread;
74  std::mutex threadGuard;
75 
76  const Json::Value* config_{nullptr};
77  const std::string name_;
78  bool tcp_enabled_{false};
79  bool udp_enabled_{false};
80  };
81 } // namespace apps
82 } // namespace helics
83 
84 #else
85 namespace helics {
86 namespace apps {
89  public:
90  AsioBrokerServer() = default;
91  explicit AsioBrokerServer(std::string /*server_name*/) {}
92  void enableTcpServer(bool /*enabled*/) {}
93  void enableUdpServer(bool /*enabled*/) {}
95  virtual void startServer(const Json::Value* /*val*/) override {}
97  virtual void stopServer() override {}
98  };
99 } // namespace apps
100 } // namespace helics
101 #endif
helics::apps::Echo
Definition: Echo.hpp:26
helics::apps::TypedBrokerServer::processArgs
virtual void processArgs(const std::string &args)
Definition: TypedBrokerServer.cpp:99
helics::CoreType::UDP
@ UDP
use UDP packets to send the data
helics::apps::Player
Definition: Player.hpp:42
AsioContextManager::getContextPointer
static std::shared_ptr< AsioContextManager > getContextPointer(const std::string &contextName=std::string())
Definition: AsioContextManager.cpp:38
helics::apps::Tracer
Definition: Tracer.hpp:25
helics::apps::AsioBrokerServer
Definition: AsioBrokerServer.hpp:88
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::helicsCLI11App
Definition: helicsCLI11.hpp:41
helics::apps::Source
Definition: Source.hpp:60
helics::apps::AsioBrokerServer::startServer
virtual void startServer(const Json::Value *) override
Definition: AsioBrokerServer.hpp:95
helics::apps::Clone
Definition: Clone.hpp:27
helics::cleanupHelicsLibrary
void cleanupHelicsLibrary()
Definition: Federate.cpp:37
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:172
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:97
helics::apps::Recorder
Definition: Recorder.hpp:26