helics  3.3.0
NetworkCommsInterface.hpp
1 /*
2 Copyright (c) 2017-2022,
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 {
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,
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};
66  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::CommsInterface::brokerTargetAddress
std::string brokerTargetAddress
the base for the broker address
Definition: CommsInterface.hpp:125
helics::CommsInterface::propertyLock
bool propertyLock()
Definition: CommsInterface.cpp:153
helics::NetworkCommsInterface::setFlag
virtual void setFlag(std::string_view flag, bool val) override
Definition: NetworkCommsInterface.cpp:209
helics::NetworkBrokerData::connectionPort
int connectionPort
the port number for connecting
Definition: NetworkBrokerData.hpp:40
helics::CommsInterface::name
std::string name
the name of the object
Definition: CommsInterface.hpp:123
helics::NetworkBrokerData::use_os_port
bool use_os_port
specify that any automatic port allocation should use operating system allocation
Definition: NetworkBrokerData.hpp:50
helics::CommsInterface::thread_generation
thread_generation
Definition: CommsInterface.hpp:29
helics::NetworkCommsInterface::setPortNumber
void setPortNumber(int localPortNumber)
Definition: NetworkCommsInterface.cpp:190
helics::NetworkCommsInterface::PortNumber
std::atomic< int > PortNumber
port to use for the local connection
Definition: NetworkCommsInterface.hpp:60
helics::NetworkBrokerData::appendNameToAddress
bool appendNameToAddress
flag indicating that the name should be appended to the address
Definition: NetworkBrokerData.hpp:53
helics::NetworkCommsInterface::forceConnection
bool forceConnection
Definition: NetworkCommsInterface.hpp:68
helics::CommsInterface::brokerInitString
std::string brokerInitString
the initialization string for any automatically generated broker
Definition: CommsInterface.hpp:128
helics::NetworkCommsInterface::getPortNumber
int getPortNumber() const
Definition: NetworkCommsInterface.hpp:52
helics::NetworkBrokerData
Definition: NetworkBrokerData.hpp:23
helics::GlobalFederateId
Definition: GlobalFederateId.hpp:72
helics::NetworkCommsInterface::getAddress
std::string getAddress() const
Definition: NetworkCommsInterface.cpp:260
helics::ActionMessage
Definition: ActionMessage.hpp:30
helics::NetworkCommsInterface::generateReplyToIncomingMessage
ActionMessage generateReplyToIncomingMessage(ActionMessage &cmd)
Definition: NetworkCommsInterface.cpp:226
helics::ActionMessage::counter
uint16_t counter
26 counter for filter tracking or message counter
Definition: ActionMessage.hpp:41
helics::NetworkBrokerData::brokerPort
int brokerPort
the port number to use for the main broker interface
Definition: NetworkBrokerData.hpp:39
helics::NetworkCommsInterface::useOsPortAllocation
bool useOsPortAllocation
use the operating system to allocate a port number
Definition: NetworkCommsInterface.hpp:62
helics::NetworkCommsInterface::setBrokerPort
void setBrokerPort(int brokerPortNumber)
Definition: NetworkCommsInterface.cpp:170
helics::CommsInterface::thread_generation::dual
@ dual
indicate that separate threads are used, 1 for transmission and 1 for reception
helics::NetworkCommsInterface::setAutomaticPortStartPort
void setAutomaticPortStartPort(int startingPort)
Definition: NetworkCommsInterface.cpp:201
helics::NetworkCommsInterface::autoPortNumber
bool autoPortNumber
use an automatic port numbering based on broker responses
Definition: NetworkCommsInterface.hpp:61
helics::ActionMessage::messageID
int32_t messageID
8 – message ID for a variety of purposes
Definition: ActionMessage.hpp:36
helics::ActionMessage::setExtraData
void setExtraData(int32_t data)
Definition: ActionMessage.hpp:157
helics::CommsInterface::useJsonSerialization
bool useJsonSerialization
true to make all connections use JSON serialization
Definition: CommsInterface.hpp:143
helics::NetworkCommsInterface::getDefaultBrokerPort
virtual int getDefaultBrokerPort() const =0
helics::NetworkCommsInterface::findOpenPort
int findOpenPort(int count, std::string_view host)
Definition: NetworkCommsInterface.cpp:178
helics::NetworkCommsInterface::brokerPort
int brokerPort
standardized broker port to use for connection to the brokers
Definition: NetworkCommsInterface.hpp:59
helics::NetworkBrokerData::noAckConnection
bool noAckConnection
Definition: NetworkBrokerData.hpp:54
helics::CommsInterface::localTargetAddress
std::string localTargetAddress
the base for the receive address
Definition: CommsInterface.hpp:124
helics::CommsInterface::brokerName
std::string brokerName
Definition: CommsInterface.hpp:126
helics::NetworkCommsInterface::NetworkCommsInterface
NetworkCommsInterface(gmlc::networking::InterfaceTypes type, CommsInterface::thread_generation threads=CommsInterface::thread_generation::dual) noexcept
Definition: NetworkCommsInterface.cpp:22
helics::CommsInterface
Definition: CommsInterface.hpp:25
helics::NetworkBrokerData::portStart
int portStart
the starting port for automatic port definitions
Definition: NetworkBrokerData.hpp:42
helics::NetworkCommsInterface::loadNetworkInfo
virtual void loadNetworkInfo(const NetworkBrokerData &netInfo) override
Definition: NetworkCommsInterface.cpp:97
helics::NetworkCommsInterface::noAckConnection
bool noAckConnection
flag to bypass the connection acknowledge requirement
Definition: NetworkCommsInterface.hpp:64
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::CommsInterface::loadNetworkInfo
virtual void loadNetworkInfo(const NetworkBrokerData &netInfo)
Definition: CommsInterface.cpp:104
helics::CommsInterface::serverMode
bool serverMode
some comms have a server mode and non-server mode
Definition: CommsInterface.hpp:141
helics::NetworkCommsInterface::encrypted
bool encrypted
Definition: NetworkCommsInterface.hpp:65
helics::NetworkCommsInterface::getPort
int getPort() const
Definition: NetworkCommsInterface.hpp:85
helics::CommsInterface::mRequireBrokerConnection
bool mRequireBrokerConnection
specify that the comms should assume we have a broker
Definition: CommsInterface.hpp:139
helics::NetworkCommsInterface
Definition: NetworkCommsInterface.hpp:18
helics::NetworkBrokerData::portNumber
int portNumber
the port number for the local interface
Definition: NetworkBrokerData.hpp:38
helics::NetworkCommsInterface::appendNameToAddress
bool appendNameToAddress
flag to append the name to the network address
Definition: NetworkCommsInterface.hpp:63
helics::NetworkBrokerData::useJsonSerialization
bool useJsonSerialization
for message serialization use JSON
Definition: NetworkBrokerData.hpp:56
helics::NetworkBrokerData::maxRetries
int maxRetries
the maximum number of retries to establish a network connection
Definition: NetworkBrokerData.hpp:45
helics::CommsInterface::setFlag
virtual void setFlag(std::string_view flag, bool val)
Definition: CommsInterface.cpp:531
helics::ActionMessage::source_id
GlobalFederateId source_id
12 – for federate_id or route_id
Definition: ActionMessage.hpp:37