| helics 3.6.1

NetworkCommsInterface.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 "CommsInterface.hpp"
10 #include "helics/helics-config.h"
11
12 #include <map>
13 #include <set>
14 #include <string>
15
16 namespace helics {
18 class NetworkCommsInterface: public CommsInterface {
19 private:
20 class PortAllocator {
21 public:
22 PortAllocator();
24 int findOpenPort(int count, std::string_view host = "localhost");
25 void setStartingPortNumber(int startPort) { startingPort = startPort; }
26 int getDefaultStartingPort() const { return startingPort; }
27 void addUsedPort(int port);
28 void addUsedPort(std::string_view host, int port);
29
30 private:
31 int startingPort = -1;
32 std::map<std::string_view, std::set<int>> usedPort;
33 std::map<std::string_view, int> nextPorts;
34 bool isPortUsed(std::string_view host, int port) const;
35 std::set<std::string> hosts;
36 std::string_view addNewHost(std::string_view newHost);
37 };
38
39 public:
41 explicit NetworkCommsInterface(gmlc::networking::InterfaceTypes type,
42 CommsInterface::thread_generation threads =
43 CommsInterface::thread_generation::dual) noexcept;
44
46 virtual void loadNetworkInfo(const NetworkBrokerData& netInfo) override;
48 void setBrokerPort(int brokerPortNumber);
50 void setPortNumber(int localPortNumber);
52 int getPortNumber() const { return PortNumber.load(); }
54 void setAutomaticPortStartPort(int startingPort);
56 virtual void setFlag(std::string_view flag, bool val) override;
57
58 protected:
59 int brokerPort{-1};
60 std::atomic<int> PortNumber{-1};
61 bool autoPortNumber{true};
62 bool useOsPortAllocation{false};
63 bool appendNameToAddress{false};
64 bool noAckConnection{false};
65 bool encrypted{false};
68 bool forceConnection{false};
69 const gmlc::networking::InterfaceTypes networkType;
70 gmlc::networking::InterfaceNetworks network{gmlc::networking::InterfaceNetworks::IPV4};
71 std::atomic<bool> hasBroker{false};
72 int maxRetries{5}; // the maximum number of network retries
73
74 private:
75 PortAllocator openPorts;
76
77 public:
79 int findOpenPort(int count, std::string_view host);
81 ActionMessage generateReplyToIncomingMessage(ActionMessage& cmd);
82
83 public:
85 int getPort() const { return PortNumber; }
87 std::string getAddress() const;
89 virtual int getDefaultBrokerPort() const = 0;
90
91 protected:
92 ActionMessage generatePortRequest(int cnt = 1) const;
93 void loadPortDefinitions(const ActionMessage& cmd);
94 };
95
96 } // namespace helics
helics::ActionMessage
Definition: ActionMessage.hpp:30
helics::CommsInterface
Definition: CommsInterface.hpp:25
helics::CommsInterface::thread_generation
thread_generation
Definition: CommsInterface.hpp:29
helics::CommsInterface::thread_generation::dual
@ dual
indicate that separate threads are used, 1 for transmission and 1 for reception
helics::NetworkBrokerData
Definition: NetworkBrokerData.hpp:23
helics::NetworkCommsInterface
Definition: NetworkCommsInterface.hpp:18
helics::NetworkCommsInterface::encrypted
bool encrypted
Definition: NetworkCommsInterface.hpp:65
helics::NetworkCommsInterface::generateReplyToIncomingMessage
ActionMessage generateReplyToIncomingMessage(ActionMessage &cmd)
Definition: NetworkCommsInterface.cpp:225
helics::NetworkCommsInterface::setPortNumber
void setPortNumber(int localPortNumber)
Definition: NetworkCommsInterface.cpp:189
helics::NetworkCommsInterface::NetworkCommsInterface
NetworkCommsInterface(gmlc::networking::InterfaceTypes type, CommsInterface::thread_generation threads=CommsInterface::thread_generation::dual) noexcept
Definition: NetworkCommsInterface.cpp:22
helics::NetworkCommsInterface::getPortNumber
int getPortNumber() const
Definition: NetworkCommsInterface.hpp:52
helics::NetworkCommsInterface::useOsPortAllocation
bool useOsPortAllocation
use the operating system to allocate a port number
Definition: NetworkCommsInterface.hpp:62
helics::NetworkCommsInterface::brokerPort
int brokerPort
standardized broker port to use for connection to the brokers
Definition: NetworkCommsInterface.hpp:59
helics::NetworkCommsInterface::forceConnection
bool forceConnection
Definition: NetworkCommsInterface.hpp:68
helics::NetworkCommsInterface::setAutomaticPortStartPort
void setAutomaticPortStartPort(int startingPort)
Definition: NetworkCommsInterface.cpp:200
helics::NetworkCommsInterface::getPort
int getPort() const
Definition: NetworkCommsInterface.hpp:85
helics::NetworkCommsInterface::autoPortNumber
bool autoPortNumber
use an automatic port numbering based on broker responses
Definition: NetworkCommsInterface.hpp:61
helics::NetworkCommsInterface::setBrokerPort
void setBrokerPort(int brokerPortNumber)
Definition: NetworkCommsInterface.cpp:169
helics::NetworkCommsInterface::getAddress
std::string getAddress() const
Definition: NetworkCommsInterface.cpp:259
helics::NetworkCommsInterface::getDefaultBrokerPort
virtual int getDefaultBrokerPort() const =0
helics::NetworkCommsInterface::noAckConnection
bool noAckConnection
flag to bypass the connection acknowledge requirement
Definition: NetworkCommsInterface.hpp:64
helics::NetworkCommsInterface::PortNumber
std::atomic< int > PortNumber
port to use for the local connection
Definition: NetworkCommsInterface.hpp:60
helics::NetworkCommsInterface::setFlag
virtual void setFlag(std::string_view flag, bool val) override
Definition: NetworkCommsInterface.cpp:208
helics::NetworkCommsInterface::loadNetworkInfo
virtual void loadNetworkInfo(const NetworkBrokerData &netInfo) override
Definition: NetworkCommsInterface.cpp:96
helics::NetworkCommsInterface::appendNameToAddress
bool appendNameToAddress
flag to append the name to the network address
Definition: NetworkCommsInterface.hpp:63
helics::NetworkCommsInterface::findOpenPort
int findOpenPort(int count, std::string_view host)
Definition: NetworkCommsInterface.cpp:177
helics
the main namespace for the helics co-simulation library User functions will be in the helics namespac...
Definition: AsyncFedCallInfo.hpp:14