helics  3.5.2
NetworkBroker_impl.hpp
1 /*
2 Copyright (c) 2017-2024,
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 #pragma once
8 
9 #include "../core/CoreTypes.hpp"
10 #include "../core/helicsCLI11.hpp"
11 #include "NetworkBroker.hpp"
12 
13 #include <iostream>
14 #include <memory>
15 #include <string>
16 
17 namespace helics {
18 constexpr const char* tstr[] = {"default",
19  "ZeroMQ",
20  "MPI",
21  "TEST",
22  "IPC",
23  "interprocess",
24  "TCP",
25  "UDP",
26  "undef",
27  "nng",
28  "ZMQ_SS",
29  "TCPSS",
30  "undef",
31  "undef",
32  "http",
33  "unknown"};
34 
35 constexpr const char* tcodeStr(int tcode)
36 {
37  return ((tcode >= 0) && (tcode < 15)) ? tstr[tcode] : tstr[15];
38 }
39 
40 constexpr const char* defInterface[] = {"127.0.0.1",
41  "127.0.0.1",
42  "tcp://127.0.0.1",
43  "_ipc_broker",
44  ""};
45 
46 template<class COMMS, gmlc::networking::InterfaceTypes baseline, int tcode>
49 {
50  netInfo.server_mode = NetworkBrokerData::ServerModeOptions::SERVER_DEFAULT_ACTIVE;
51 }
52 
53 template<class COMMS, gmlc::networking::InterfaceTypes baseline, int tcode>
54 NetworkBroker<COMMS, baseline, tcode>::NetworkBroker(std::string_view broker_name):
55  CommsBroker<COMMS, CoreBroker>(broker_name)
56 {
57  netInfo.server_mode = NetworkBrokerData::ServerModeOptions::SERVER_DEFAULT_ACTIVE;
58 }
59 
60 template<class COMMS, gmlc::networking::InterfaceTypes baseline, int tcode>
61 std::shared_ptr<helicsCLI11App> NetworkBroker<COMMS, baseline, tcode>::generateCLI()
62 {
63  auto app = CoreBroker::generateCLI();
64  CLI::App_p netApp = netInfo.commandLineParser(defInterface[static_cast<int>(baseline)], false);
65  app->add_subcommand(netApp);
66  return app;
67 }
68 
69 template<class COMMS, gmlc::networking::InterfaceTypes baseline, int tcode>
71 {
72  std::lock_guard<std::mutex> lock(dataMutex);
73  if ((netInfo.brokerName.empty()) && (netInfo.brokerAddress.empty())) {
75  }
76  netInfo.useJsonSerialization = BrokerBase::useJsonSerialization;
77  netInfo.observer = BrokerBase::observer;
79  CommsBroker<COMMS, CoreBroker>::comms->loadNetworkInfo(netInfo);
81 
82  auto res = CommsBroker<COMMS, CoreBroker>::comms->connect();
83  if (res) {
84  if (netInfo.portNumber < 0) {
85  netInfo.portNumber = CommsBroker<COMMS, CoreBroker>::comms->getPort();
86  }
87  }
88  return res;
89 }
90 
91 template<class COMMS, gmlc::networking::InterfaceTypes baseline, int tcode>
93 {
94  using InterfaceTypes = gmlc::networking::InterfaceTypes;
95  std::string add;
96  if (CommsBroker<COMMS, CoreBroker>::comms->isConnected()) {
97  add = CommsBroker<COMMS, CoreBroker>::comms->getAddress();
98  } else {
99  std::lock_guard<std::mutex> lock(dataMutex);
100  switch (baseline) {
101  case InterfaceTypes::TCP:
102  case InterfaceTypes::IP:
103  case InterfaceTypes::UDP:
104  if (!netInfo.localInterface.empty() && (netInfo.localInterface.back() == '*')) {
105  add = gmlc::networking::makePortAddress(
106  netInfo.localInterface.substr(0, netInfo.localInterface.size() - 1),
107  netInfo.portNumber);
108  } else {
109  add = gmlc::networking::makePortAddress(netInfo.localInterface,
110  netInfo.portNumber);
111  }
112  break;
113  case InterfaceTypes::INPROC:
114  case InterfaceTypes::IPC:
115  default:
116  if (!netInfo.localInterface.empty()) {
117  add = netInfo.localInterface;
118  } else {
120  }
121  break;
122  }
123  }
124  return add;
125 }
126 
127 } // namespace helics
bool observer
flag indicating that the broker is an observer only
Definition: BrokerBase.hpp:88
Time networkTimeout
timeout to establish a socket connection before giving up
Definition: BrokerBase.hpp:64
bool useJsonSerialization
Definition: BrokerBase.hpp:149
Definition: CoreBroker.hpp:98
virtual std::shared_ptr< helicsCLI11App > generateCLI() override
Definition: CoreBroker.cpp:2352
virtual const std::string & getIdentifier() const override final
Definition: CoreBroker.hpp:306
virtual void setAsRoot() override final
Definition: CoreBroker.cpp:2369
ServerModeOptions server_mode
setup a server mode
Definition: NetworkBrokerData.hpp:58
NetworkBroker(bool rootBroker=false) noexcept
Definition: NetworkBroker_impl.hpp:47
virtual bool brokerConnect() override
Definition: NetworkBroker_impl.hpp:70
NetworkBrokerData netInfo
structure containing the networking information
Definition: NetworkBroker.hpp:31
virtual std::shared_ptr< helicsCLI11App > generateCLI() override
Definition: NetworkBroker_impl.hpp:61
virtual std::string generateLocalAddressString() const override
Definition: NetworkBroker_impl.hpp:92
the main namespace for the helics co-simulation library User functions will be in the helics namespac...
Definition: AsyncFedCallInfo.hpp:14