helics 3.7.0
Loading...
Searching...
No Matches
NetworkCore_impl.hpp
1/*
2Copyright (c) 2017-2026,
3Battelle Memorial Institute; Lawrence Livermore National Security, LLC; Alliance for Energy
4Innovation LLC. See the top-level NOTICE for additional details. All rights reserved.
5SPDX-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
15namespace helics {
16constexpr const char* defBrokerInterface[] = {"127.0.0.1",
17 "127.0.0.1",
18 "tcp://127.0.0.1",
19 "_ipc_broker",
20 ""};
21constexpr const char* defLocalInterface[] = {"127.0.0.1", "127.0.0.1", "tcp://127.0.0.1", "", ""};
23template<class COMMS, InterfaceTypes baseline>
25{
26 netInfo.server_mode = NetworkBrokerData::ServerModeOptions::SERVER_DEFAULT_DEACTIVATED;
27}
28
29template<class COMMS, InterfaceTypes baseline>
31 CommsBroker<COMMS, CommonCore>(coreName)
32{
33 netInfo.server_mode = NetworkBrokerData::ServerModeOptions::SERVER_DEFAULT_DEACTIVATED;
34}
35
36template<class COMMS, InterfaceTypes baseline>
37std::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
46template<class COMMS, InterfaceTypes baseline>
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
69template<class COMMS, InterfaceTypes baseline>
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
Definition CommonCore.hpp:74
Definition CommsBroker.hpp:21
Definition NetworkCore.hpp:19
NetworkCore() noexcept
Definition NetworkCore_impl.hpp:24
the main namespace for the helics co-simulation library User functions will be in the helics namespac...
Definition AsyncFedCallInfo.hpp:14