preCICE v3.1.1
Loading...
Searching...
No Matches
SocketCommunication.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <boost/asio.hpp>
4#include <map>
5#include <memory>
6#include <set>
7#include <stddef.h>
8#include <string>
9#include <thread>
10
11#include "com/Communication.hpp"
12#include "com/SharedPointer.hpp"
14#include "logging/Logger.hpp"
16#include "utils/networking.hpp"
17
18namespace precice {
19namespace com {
22public:
23 SocketCommunication(unsigned short portNumber = 0,
24 bool reuseAddress = false,
26 std::string addressDirectory = ".");
27
28 explicit SocketCommunication(std::string const &addressDirectory);
29
30 virtual ~SocketCommunication();
31
32 virtual size_t getRemoteCommunicatorSize() override;
33
34 virtual void acceptConnection(std::string const &acceptorName,
35 std::string const &requesterName,
36 std::string const &tag,
37 int acceptorRank,
38 int rankOffset = 0) override;
39
40 virtual void acceptConnectionAsServer(std::string const &acceptorName,
41 std::string const &requesterName,
42 std::string const &tag,
43 int acceptorRank,
44 int requesterCommunicatorSize) override;
45
46 virtual void requestConnection(std::string const &acceptorName,
47 std::string const &requesterName,
48 std::string const &tag,
49 int requesterRank,
50 int requesterCommunicatorSize) override;
51
52 virtual void requestConnectionAsClient(std::string const & acceptorName,
53 std::string const & requesterName,
54 std::string const & tag,
55 std::set<int> const &acceptorRanks,
56 int requesterRank) override;
57
58 virtual void closeConnection() override;
59
61 virtual void send(std::string const &itemToSend, Rank rankReceiver) override;
62
64 virtual void send(precice::span<const int> itemsToSend, Rank rankReceiver) override;
65
67 virtual PtrRequest aSend(precice::span<const int> itemsToSend, Rank rankReceiver) override;
68
70 virtual void send(precice::span<const double> itemsToSend, Rank rankReceiver) override;
71
73 virtual PtrRequest aSend(precice::span<const double> itemsToSend, Rank rankReceiver) override;
74
76 virtual void send(double itemToSend, Rank rankReceiver) override;
77
79 virtual PtrRequest aSend(const double &itemToSend, Rank rankReceiver) override;
80
82 virtual void send(int itemToSend, Rank rankReceiver) override;
83
85 virtual PtrRequest aSend(const int &itemToSend, Rank rankReceiver) override;
86
88 virtual void send(bool itemToSend, Rank rankReceiver) override;
89
91 virtual PtrRequest aSend(const bool &itemToSend, Rank rankReceiver) override;
92
94 virtual void receive(std::string &itemToReceive, Rank rankSender) override;
95
97 virtual void receive(precice::span<int> itemsToReceive, Rank rankSender) override;
98
100 virtual void receive(precice::span<double> itemsToReceive, Rank rankSender) override;
101
103 virtual PtrRequest aReceive(precice::span<double> itemsToReceive,
104 int rankSender) override;
105
107 virtual void receive(double &itemToReceive, Rank rankSender) override;
108
110 virtual PtrRequest aReceive(double &itemToReceive, Rank rankSender) override;
111
113 virtual void receive(int &itemToReceive, Rank rankSender) override;
114
116 virtual PtrRequest aReceive(int &itemToReceive, Rank rankSender) override;
117
119 virtual void receive(bool &itemToReceive, Rank rankSender) override;
120
122 virtual PtrRequest aReceive(bool &itemToReceive, Rank rankSender) override;
123
124 virtual void prepareEstablishment(std::string const &acceptorName,
125 std::string const &requesterName) override;
126
127 virtual void cleanupEstablishment(std::string const &acceptorName,
128 std::string const &requesterName) override;
129
130private:
131 logging::Logger _log{"com::SocketCommunication"};
132
134 unsigned short _portNumber;
135
137
140
143
144 using IOService = boost::asio::io_service;
145 using Socket = boost::asio::ip::tcp::socket;
146 using Work = boost::asio::io_service::work;
147
151
154
156
157 bool isClient();
158 bool isServer();
159
161};
162} // namespace com
163} // namespace precice
Interface for all interprocess communication classes.
Implements Communication by using sockets.
virtual PtrRequest aReceive(precice::span< double > itemsToReceive, int rankSender) override
Asynchronously receives an array of double values.
boost::asio::io_service::work Work
virtual void cleanupEstablishment(std::string const &acceptorName, std::string const &requesterName) override
Clean-up environment used to establish the communication.
virtual void send(std::string const &itemToSend, Rank rankReceiver) override
Sends a std::string to process with given rank.
std::string _addressDirectory
Directory where IP address is exchanged by file.
virtual void acceptConnectionAsServer(std::string const &acceptorName, std::string const &requesterName, std::string const &tag, int acceptorRank, int requesterCommunicatorSize) override
Accepts connection from another communicator, which has to call requestConnectionAsClient().
virtual void requestConnectionAsClient(std::string const &acceptorName, std::string const &requesterName, std::string const &tag, std::set< int > const &acceptorRanks, int requesterRank) override
Connects to another communicator, which has to call acceptConnectionAsServer().
std::string _networkName
Name of network to communicate over.
virtual size_t getRemoteCommunicatorSize() override
Returns the number of processes in the remote communicator.
std::map< int, std::shared_ptr< Socket > > _sockets
Remote rank -> socket map.
boost::asio::ip::tcp::socket Socket
virtual void receive(std::string &itemToReceive, Rank rankSender) override
Receives a std::string from process with given rank.
virtual void closeConnection() override
Disconnects from communication space, i.e. participant.
virtual void prepareEstablishment(std::string const &acceptorName, std::string const &requesterName) override
Prepare environment used to establish the communication.
unsigned short _portNumber
Port used for socket connection.
SocketCommunication(unsigned short portNumber=0, bool reuseAddress=false, std::string networkName=utils::networking::loopbackInterfaceName(), std::string addressDirectory=".")
virtual PtrRequest aSend(precice::span< const int > itemsToSend, Rank rankReceiver) override
Asynchronously sends an array of integer values.
virtual void requestConnection(std::string const &acceptorName, std::string const &requesterName, std::string const &tag, int requesterRank, int requesterCommunicatorSize) override
Connects to another communicator, which has to call acceptConnection().
virtual void acceptConnection(std::string const &acceptorName, std::string const &requesterName, std::string const &tag, int acceptorRank, int rankOffset=0) override
Accepts connection from another communicator, which has to call requestConnection().
std::shared_ptr< IOService > _ioService
This class provides a lightweight logger.
Definition Logger.hpp:16
A C++ 11 implementation of the non-owning C++20 std::span type.
Definition span.hpp:284
std::string loopbackInterfaceName()
Returns the name of the canonical loopback interface on this system.
Definition networking.cpp:5
Main namespace of the precice library.
int Rank
Definition Types.hpp:37