helics  2.8.1
NetworkCommsInterface.hpp
1 /*
2 Copyright (c) 2017-2021,
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:
23  int findOpenPort(int count, const std::string& host = "localhost");
24  void setStartingPortNumber(int startPort) { startingPort = startPort; }
25  int getDefaultStartingPort() const { return startingPort; }
26  void addUsedPort(int port);
27  void addUsedPort(const std::string& host, int port);
28 
29  private:
30  int startingPort = -1;
31  std::map<std::string, std::set<int>> usedPort;
32  std::map<std::string, int> nextPorts;
33  bool isPortUsed(const std::string& host, int port) const;
34  };
35 
36  public:
40  CommsInterface::thread_generation::dual) noexcept;
41 
43  virtual void loadNetworkInfo(const NetworkBrokerData& netInfo) override;
45  void setBrokerPort(int brokerPortNumber);
47  void setPortNumber(int localPortNumber);
49  int getPortNumber() const { return PortNumber.load(); }
51  void setAutomaticPortStartPort(int startingPort);
53  virtual void setFlag(const std::string& flag, bool val) override;
54 
55  protected:
56  int brokerPort{-1};
57  std::atomic<int> PortNumber{-1};
58  bool autoPortNumber{true};
59  bool useOsPortAllocation{false};
60  bool appendNameToAddress{false};
61  bool noAckConnection{false};
62  const interface_type networkType;
64  std::atomic<bool> hasBroker{false};
65  int maxRetries{5}; // the maximum number of network retries
66 
67  private:
68  PortAllocator openPorts;
69 
70  public:
72  int findOpenPort(int count, const std::string& host);
74  ActionMessage generateReplyToIncomingMessage(ActionMessage& cmd);
75 
76  public:
78  int getPort() const { return PortNumber; }
80  std::string getAddress() const;
82  virtual int getDefaultBrokerPort() const = 0;
83 
84  protected:
85  ActionMessage generatePortRequest(int cnt = 1) const;
86  void loadPortDefinitions(const ActionMessage& cmd);
87 };
88 
89 } // 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:152
helics::interface_type::udp
@ udp
using udp ports for communication
helics::ActionMessage::source_id
global_federate_id source_id
12 – for federate_id or route_id
Definition: ActionMessage.hpp:36
helics::removeProtocol
void removeProtocol(std::string &networkAddress)
Definition: NetworkBrokerData.cpp:265
helics::NetworkBrokerData::connectionPort
int connectionPort
the port number for connecting
Definition: NetworkBrokerData.hpp:53
helics::CommsInterface::name
std::string name
the name of the object
Definition: CommsInterface.hpp:123
helics::NetworkBrokerData::use_os_port
bool use_os_port
Definition: NetworkBrokerData.hpp:61
helics::CommsInterface::thread_generation
thread_generation
Definition: CommsInterface.hpp:30
helics::NetworkCommsInterface::setPortNumber
void setPortNumber(int localPortNumber)
Definition: NetworkCommsInterface.cpp:158
helics::NetworkCommsInterface::PortNumber
std::atomic< int > PortNumber
port to use for the local connection
Definition: NetworkCommsInterface.hpp:57
helics::CommsInterface::setFlag
virtual void setFlag(const std::string &flag, bool val)
Definition: CommsInterface.cpp:530
helics::NetworkBrokerData::appendNameToAddress
bool appendNameToAddress
flag indicating that the name should be appended to the address
Definition: NetworkBrokerData.hpp:64
helics::global_federate_id
Definition: global_federate_id.hpp:68
helics::interface_networks::local
@ local
just open local ports
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:49
helics::NetworkBrokerData
Definition: NetworkBrokerData.hpp:36
helics::NetworkCommsInterface::getAddress
std::string getAddress() const
Definition: NetworkCommsInterface.cpp:228
helics::ActionMessage
Definition: ActionMessage.hpp:29
helics::NetworkCommsInterface::generateReplyToIncomingMessage
ActionMessage generateReplyToIncomingMessage(ActionMessage &cmd)
Definition: NetworkCommsInterface.cpp:194
helics::ActionMessage::counter
uint16_t counter
26 counter for filter tracking or message counter
Definition: ActionMessage.hpp:40
helics::NetworkBrokerData::brokerPort
int brokerPort
the port number to use for the main broker interface
Definition: NetworkBrokerData.hpp:52
helics::NetworkCommsInterface::useOsPortAllocation
bool useOsPortAllocation
use the operating system to allocate a port number
Definition: NetworkCommsInterface.hpp:59
helics::NetworkCommsInterface::setBrokerPort
void setBrokerPort(int brokerPortNumber)
Definition: NetworkCommsInterface.cpp:138
helics::NetworkCommsInterface::setAutomaticPortStartPort
void setAutomaticPortStartPort(int startingPort)
Definition: NetworkCommsInterface.cpp:169
helics::interface_networks
interface_networks
Definition: NetworkBrokerData.hpp:16
helics::NetworkCommsInterface::autoPortNumber
bool autoPortNumber
use an automatic port numbering based on broker responses
Definition: NetworkCommsInterface.hpp:58
helics::generateMatchingInterfaceAddress
std::string generateMatchingInterfaceAddress(const std::string &server, interface_networks network)
Definition: NetworkBrokerData.cpp:582
helics::ActionMessage::messageID
int32_t messageID
8 – message ID for a variety of purposes
Definition: ActionMessage.hpp:35
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:142
helics::NetworkCommsInterface::getDefaultBrokerPort
virtual int getDefaultBrokerPort() const =0
helics::NetworkCommsInterface::brokerPort
int brokerPort
standardized broker port to use for connection to the brokers
Definition: NetworkCommsInterface.hpp:56
helics::NetworkBrokerData::noAckConnection
bool noAckConnection
Definition: NetworkBrokerData.hpp:66
helics::CommsInterface::localTargetAddress
std::string localTargetAddress
the base for the receive address
Definition: CommsInterface.hpp:124
helics::CommsInterface::brokerName
std::string brokerName
the identifier for the broker
Definition: CommsInterface.hpp:126
helics::NetworkCommsInterface::NetworkCommsInterface
NetworkCommsInterface(interface_type type, CommsInterface::thread_generation threads=CommsInterface::thread_generation::dual) noexcept
Definition: NetworkCommsInterface.cpp:17
helics::CommsInterface
Definition: CommsInterface.hpp:26
helics::NetworkBrokerData::portStart
int portStart
the starting port for automatic port definitions
Definition: NetworkBrokerData.hpp:55
helics::NetworkCommsInterface::loadNetworkInfo
virtual void loadNetworkInfo(const NetworkBrokerData &netInfo) override
Definition: NetworkCommsInterface.cpp:77
helics::NetworkCommsInterface::noAckConnection
bool noAckConnection
flag to bypass the connection acknowledge requirement
Definition: NetworkCommsInterface.hpp:61
helics::interface_type::tcp
@ tcp
using tcp ports for communication
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::NetworkCommsInterface::findOpenPort
int findOpenPort(int count, const std::string &host)
Definition: NetworkCommsInterface.cpp:146
helics::CommsInterface::loadNetworkInfo
virtual void loadNetworkInfo(const NetworkBrokerData &netInfo)
Definition: CommsInterface.cpp:104
helics::interface_type
interface_type
Definition: NetworkBrokerData.hpp:24
helics::CommsInterface::serverMode
bool serverMode
some comms have a server mode and non-server mode
Definition: CommsInterface.hpp:140
helics::makePortAddress
std::string makePortAddress(const std::string &networkInterface, int portNumber)
Definition: NetworkBrokerData.cpp:216
helics::NetworkCommsInterface::getPort
int getPort() const
Definition: NetworkCommsInterface.hpp:78
helics::NetworkCommsInterface::setFlag
virtual void setFlag(const std::string &flag, bool val) override
Definition: NetworkCommsInterface.cpp:177
helics::interface_networks::ipv4
@ ipv4
use external ipv4 ports
helics::CommsInterface::mRequireBrokerConnection
bool mRequireBrokerConnection
specify that the comms should assume we have a broker
Definition: CommsInterface.hpp:138
helics::NetworkCommsInterface
Definition: NetworkCommsInterface.hpp:18
helics::stripProtocol
std::string stripProtocol(const std::string &networkAddress)
Definition: NetworkBrokerData.cpp:256
helics::NetworkBrokerData::portNumber
int portNumber
the port number for the local interface
Definition: NetworkBrokerData.hpp:51
helics::NetworkCommsInterface::appendNameToAddress
bool appendNameToAddress
flag to append the name to the network address
Definition: NetworkCommsInterface.hpp:60
helics::NetworkBrokerData::useJsonSerialization
bool useJsonSerialization
for message serialization use JSON
Definition: NetworkBrokerData.hpp:68
helics::NetworkBrokerData::maxRetries
int maxRetries
the maximum number of retries to establish a network connection
Definition: NetworkBrokerData.hpp:58