| helics 3.6.1

NetworkCore_impl.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 #pragma once
8
9 #include "../core/helicsCLI11.hpp"
10 #include "NetworkCore.hpp"
11
12 #include <memory>
13 #include <string>
14
15 namespace helics {
16 constexpr const char* defBrokerInterface[] = {"127.0.0.1",
17 "127.0.0.1",
18 "tcp://127.0.0.1",
19 "_ipc_broker",
20 ""};
21 constexpr const char* defLocalInterface[] = {"127.0.0.1", "127.0.0.1", "tcp://127.0.0.1", "", ""};
22
23 template<class COMMS, InterfaceTypes baseline>
24 NetworkCore<COMMS, baseline>::NetworkCore() noexcept
25 {
26 netInfo.server_mode = NetworkBrokerData::ServerModeOptions::SERVER_DEFAULT_DEACTIVATED;
27 }
28
29 template<class COMMS, InterfaceTypes baseline>
30 NetworkCore<COMMS, baseline>::NetworkCore(std::string_view coreName):
31 CommsBroker<COMMS, CommonCore>(coreName)
32 {
33 netInfo.server_mode = NetworkBrokerData::ServerModeOptions::SERVER_DEFAULT_DEACTIVATED;
34 }
35
36 template<class COMMS, InterfaceTypes baseline>
37 std::shared_ptr<helicsCLI11App> NetworkCore<COMMS, baseline>::generateCLI()
38 {
39 auto app = CommonCore::generateCLI();
40 CLI::App_p netApp =
41 netInfo.commandLineParser(defLocalInterface[static_cast<int>(baseline)], false);
42 app->add_subcommand(netApp);
43 return app;
44 }
45
46 template<class COMMS, InterfaceTypes baseline>
47 bool NetworkCore<COMMS, baseline>::brokerConnect()
48 {
49 std::lock_guard<std::mutex> lock(dataMutex);
50 if (netInfo.brokerAddress.empty()) // cores require a broker
51 {
52 netInfo.brokerAddress = defBrokerInterface[static_cast<int>(baseline)];
53 }
54 CommsBroker<COMMS, CommonCore>::comms->setRequireBrokerConnection(true);
55 netInfo.useJsonSerialization = BrokerBase::useJsonSerialization;
56 netInfo.observer = BrokerBase::observer;
57 CommsBroker<COMMS, CommonCore>::comms->setName(CommonCore::getIdentifier());
58 CommsBroker<COMMS, CommonCore>::comms->loadNetworkInfo(netInfo);
59 CommsBroker<COMMS, CommonCore>::comms->setTimeout(BrokerBase::networkTimeout.to_ms());
60 auto res = CommsBroker<COMMS, CommonCore>::comms->connect();
61 if (res) {
62 if (netInfo.portNumber < 0) {
63 netInfo.portNumber = CommsBroker<COMMS, CommonCore>::comms->getPort();
64 }
65 }
66 return res;
67 }
68
69 template<class COMMS, InterfaceTypes baseline>
70 std::string NetworkCore<COMMS, baseline>::generateLocalAddressString() const
71 {
72 std::string add;
73 if (CommsBroker<COMMS, CommonCore>::comms->isConnected()) {
74 add = CommsBroker<COMMS, CommonCore>::comms->getAddress();
75 } else {
76 std::lock_guard<std::mutex> lock(dataMutex);
77 switch (baseline) {
78 case InterfaceTypes::TCP:
79 case InterfaceTypes::IP:
80 case InterfaceTypes::UDP:
81 if (!netInfo.localInterface.empty() && (netInfo.localInterface.back() == '*')) {
82 add = gmlc::networking::makePortAddress(
83 netInfo.localInterface.substr(0, netInfo.localInterface.size() - 1),
84 netInfo.portNumber);
85 } else {
86 add = gmlc::networking::makePortAddress(netInfo.localInterface,
87 netInfo.portNumber);
88 }
89 break;
90 case InterfaceTypes::INPROC:
91 case InterfaceTypes::IPC:
92 default:
93 if (!netInfo.localInterface.empty()) {
94 add = netInfo.localInterface;
95 } else {
96 add = CommonCore::getIdentifier();
97 }
98 break;
99 }
100 }
101 return add;
102 }
103
104 } // namespace helics
helics::CommonCore
Definition: CommonCore.hpp:74
helics::CommsBroker
Definition: CommsBroker.hpp:21
helics::NetworkCore
Definition: NetworkCore.hpp:19
helics::NetworkCore::NetworkCore
NetworkCore() noexcept
Definition: NetworkCore_impl.hpp:24
helics
the main namespace for the helics co-simulation library User functions will be in the helics namespac...
Definition: AsyncFedCallInfo.hpp:14