2#include <boost/algorithm/string/trim.hpp>
15namespace fs = std::filesystem;
18std::string
impl::hashedFilePath(std::string_view acceptorName, std::string_view requesterName, std::string_view tag,
Rank rank)
20 constexpr int firstLevelLen = 2;
21 std::string
const s = std::string(acceptorName).append(tag).append(requesterName).append(std::to_string(rank));
24 auto p = fs::path(hash.substr(0, firstLevelLen)) / hash.substr(firstLevelLen);
29std::string
impl::localDirectory(std::string_view acceptorName, std::string_view requesterName, std::string_view addressDirectory)
31 std::string directional = std::string(acceptorName).append(
"-").append(requesterName);
33 auto p = fs::path(addressDirectory.begin(), addressDirectory.end()) /
"precice-run" / directional;
57 const auto waitdelay = std::chrono::milliseconds(1);
58 while (!fs::exists(path)) {
59 std::this_thread::sleep_for(waitdelay);
64 std::ifstream ifs(path);
67 PRECICE_DEBUG(
"Opening connection file \"{}\" failed. Retrying", path);
68 std::this_thread::sleep_for(waitdelay);
74 "Unable to establish connection as the connection file \"{}\" couldn't be opened.",
76 std::string addressData;
77 std::getline(ifs, addressData);
79 "Unable to establish connection as the connection file \"{}\" is empty. "
80 "Please report this bug to the preCICE developers.",
82 boost::algorithm::trim_right(addressData);
89 if (!fs::exists(path)) {
90 PRECICE_WARN(
"Cannot clean-up the connection file \"{}\" as it doesn't exist. "
91 "In case of connection problems, please report this to the preCICE developers.",
92 path.generic_string());
95 PRECICE_DEBUG(
"Deleting connection file \"{}\"", path.generic_string());
100 "The connection file \"{}\" wasn't properly removed. "
101 "Make sure to delete the \"precice-run\" directory before restarting the simulation.",
102 path.generic_string());
103 }
catch (
const fs::filesystem_error &e) {
104 PRECICE_WARN(
"Unable to clean-up connection file due to error: {}. "
105 "Make sure to delete the \"precice-run\" directory before restarting the simulation.",
113 auto tmp = fs::path(path +
"~");
116 auto message =
"Unable to establish connection as a {}connection file already exists at \"{}\". "
117 "This is likely a leftover of a previous crash or stop during communication build-up. "
118 "Please remove the \"precice-run\" directory and restart the simulation.";
123 PRECICE_DEBUG(
"Writing temporary connection file \"{}\"", tmp.generic_string());
124 fs::create_directories(tmp.parent_path());
126 std::ofstream ofs(tmp);
129 PRECICE_DEBUG(
"Opening temporary connection file \"{}\" failed. Retrying", tmp.generic_string());
130 std::this_thread::sleep_for(std::chrono::milliseconds{1});
135 PRECICE_CHECK(ofs,
"Unable to establish connection as the temporary connection file \"{}\" couldn't be opened.", tmp.generic_string());
137 "{}\nAcceptor: {}, Requester: {}, Tag: {}, Rank: {}",
141 "Unable to establish connection as the temporary connection file \"{}\" was written, but doesn't exist on disk. "
142 "Please report this bug to the preCICE developers.",
143 tmp.generic_string());
146 fs::rename(tmp, path);
149 "The temporary connection file \"{}\" wasn't properly removed. "
150 "Make sure to delete the \"precice-run\" directory before restarting the simulation.",
151 tmp.generic_string());
153 "Unable to establish connection as the connection file \"{}\" doesn't exist on disk. "
154 "Please report this bug to the preCICE developers.",
#define PRECICE_WARN_IF(condition,...)
#define PRECICE_WARN(...)
#define PRECICE_DEBUG(...)
#define PRECICE_CHECK(check,...)
#define PRECICE_ASSERT(...)
std::string const addressDirectory
std::string const acceptorName
std::string getLocalDirectory() const
Returns the local directory which is used to store the hashed part.
std::string const requesterName
std::string getFilename() const
Returns the full path to the hashed filename.
std::string read() const
Reads the info from the connection info file. Will block, if the the file is not present.
void write(std::string_view info) const
Write the string info, e.g. IP:port to the connection info file.
~ConnectionInfoWriter()
Removes the connection info file and the directories ./precice-run/[hash], is empty.
std::string hashedFilePath(std::string_view acceptorName, std::string_view requesterName, std::string_view meshName, Rank rank)
Returns the file name for the connection information.
std::string localDirectory(std::string_view acceptorName, std::string_view requesterName, std::string_view addressDirectory)
contains the data communication abstraction layer.
std::string preciceHash(std::string_view s)
creates a portable hash of the given input