helics  3.0.1
TcpComms.h
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 "../NetworkCommsInterface.hpp"
10 #include "gmlc/containers/BlockingQueue.hpp"
11 
12 #include <atomic>
13 #include <memory>
14 #include <set>
15 #include <string>
16 
17 class AsioContextManager;
18 namespace asio {
19 class io_context;
20 } // namespace asio
21 
22 namespace helics {
23 namespace tcp {
24  class TcpConnection;
25 
27  class TcpComms final: public NetworkCommsInterface {
28  public:
30  TcpComms() noexcept;
32  ~TcpComms();
34  virtual void loadNetworkInfo(const NetworkBrokerData& netInfo) override;
35 
36  virtual void setFlag(const std::string& flag, bool val) override;
37 
38  private:
39  bool reuse_address = false;
40  virtual int getDefaultBrokerPort() const override;
41  virtual void queue_rx_function() override;
42  virtual void queue_tx_function() override;
43 
44  virtual void closeReceiver() override;
45 
47  bool establishBrokerConnection(std::shared_ptr<AsioContextManager>& ioctx,
48  std::shared_ptr<TcpConnection>& brokerConnection);
51  int processIncomingMessage(ActionMessage&& cmd);
52  // promise and future for communicating port number from tx_thread to rx_thread
53  gmlc::containers::BlockingQueue<ActionMessage> rxMessageQueue;
54 
55  void txReceive(const char* data, size_t bytes_received, const std::string& errorMessage);
56 
63  size_t dataReceive(TcpConnection* connection, const char* data, size_t bytes_received);
64 
65  // bool errorHandle()
66  };
67 
68 } // namespace tcp
69 } // 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::tcp::TcpComms::TcpComms
TcpComms() noexcept
Definition: TcpComms.cpp:25
helics::message_process_result::processed
@ processed
the message was used to update the current state
helics::extractInterfaceandPortString
std::pair< std::string, std::string > extractInterfaceandPortString(const std::string &address)
Definition: NetworkBrokerData.cpp:251
AsioContextManager::getContextPointer
static std::shared_ptr< AsioContextManager > getContextPointer(const std::string &contextName=std::string())
Definition: AsioContextManager.cpp:38
helics::CommsInterface::logWarning
void logWarning(const std::string &message) const
Definition: CommsInterface.cpp:571
helics::extractInterfaceandPort
std::pair< std::string, int > extractInterfaceandPort(const std::string &address)
Definition: NetworkBrokerData.cpp:227
helics::CommsInterface::disconnecting
std::atomic< bool > disconnecting
flag indicating that the comm system is in the process of disconnecting
Definition: CommsInterface.hpp:157
helics::tcp::TcpComms::setFlag
virtual void setFlag(const std::string &flag, bool val) override
Definition: TcpComms.cpp:40
helics::CommsInterface::logError
void logError(const std::string &message) const
Definition: CommsInterface.cpp:580
helics::NetworkCommsInterface::PortNumber
std::atomic< int > PortNumber
port to use for the local connection
Definition: NetworkCommsInterface.hpp:57
helics::tcp::TcpComms::~TcpComms
~TcpComms()
Definition: TcpComms.cpp:53
helics::CommsInterface::brokerInitString
std::string brokerInitString
the initialization string for any automatically generated broker
Definition: CommsInterface.hpp:128
helics::NetworkBrokerData
Definition: NetworkBrokerData.hpp:36
helics::CommsInterface::connectionTimeout
std::chrono::milliseconds connectionTimeout
Definition: CommsInterface.hpp:145
helics::ActionMessage
Definition: ActionMessage.hpp:30
helics::NetworkCommsInterface::generateReplyToIncomingMessage
ActionMessage generateReplyToIncomingMessage(ActionMessage &cmd)
Definition: NetworkCommsInterface.cpp:194
TcpHelperClasses.h
helics::CommsInterface::txQueue
gmlc::containers::BlockingPriorityQueue< std::pair< route_id, ActionMessage > > txQueue
set of messages waiting to be transmitted
Definition: CommsInterface.hpp:154
helics::CommsInterface::CommsInterface
CommsInterface()=default
AsioContextManager::getBaseContext
asio::io_context & getBaseContext() const
Definition: AsioContextManager.h:116
helics::tcp::TcpComms::loadNetworkInfo
virtual void loadNetworkInfo(const NetworkBrokerData &netInfo) override
Definition: TcpComms.cpp:30
AsioContextManager
Definition: AsioContextManager.h:32
TcpCommsCommon.h
helics::NetworkCommsInterface::autoPortNumber
bool autoPortNumber
use an automatic port numbering based on broker responses
Definition: NetworkCommsInterface.hpp:58
helics::CommsInterface::ActionCallback
std::function< void(ActionMessage &&)> ActionCallback
the callback for what to do with a received message
Definition: CommsInterface.hpp:150
helics::CommsInterface::disconnect
void disconnect()
Definition: CommsInterface.cpp:385
helics::tcp::TcpConnection
Definition: TcpHelperClasses.h:26
helics::NetworkCommsInterface::brokerPort
int brokerPort
standardized broker port to use for connection to the brokers
Definition: NetworkCommsInterface.hpp:56
helics::CommsInterface::localTargetAddress
std::string localTargetAddress
the base for the receive address
Definition: CommsInterface.hpp:124
helics::NetworkBrokerData::reuse_address
bool reuse_address
allow reuse of binding address
Definition: NetworkBrokerData.hpp:60
helics::CommsInterface::brokerName
std::string brokerName
the identifier for the broker
Definition: CommsInterface.hpp:126
helics::CommsInterface::connection_status
connection_status
Definition: CommsInterface.hpp:108
helics::prettyPrintString
std::string prettyPrintString(const ActionMessage &command)
Definition: ActionMessage.cpp:841
helics::isDisconnectCommand
bool isDisconnectCommand(const ActionMessage &command) noexcept
Definition: ActionMessage.hpp:276
helics::CommsInterface::requestDisconnect
std::atomic< bool > requestDisconnect
flag gets set when disconnect is called
Definition: CommsInterface.hpp:148
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::tcp::TcpComms
Definition: TcpComms.h:27
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::actionMessageType
const char * actionMessageType(action_message_def::action_t action)
Definition: ActionMessage.cpp:806
helics::NetworkCommsInterface::setFlag
virtual void setFlag(const std::string &flag, bool val) override
Definition: NetworkCommsInterface.cpp:177
helics::NetworkCommsInterface
Definition: NetworkCommsInterface.hpp:18
helics::InterfaceTypes
InterfaceTypes
Definition: NetworkBrokerData.hpp:24
helics::CommsInterface::maxMessageSize
int maxMessageSize
the maximum message size for the queues (if needed)
Definition: CommsInterface.hpp:146
AsioContextManager::startContextLoop
LoopHandle startContextLoop()
Definition: AsioContextManager.cpp:151
helics::CommsInterface::logMessage
void logMessage(const std::string &message) const
Definition: CommsInterface.cpp:562