ensuring no duplicate rx hostname port combo (#604)

* rx_hostname and port combo to one, or hostname to all, or a vector of hostnames and ports, ignoring none or empty, then verifying no duplicates for the host port combo including from shared memory

* extracted function for rx_hostname (#694)

* c++14 revert

* unique hostname-port combo for port, hostname, rx_tcpport (#696)

* verify unique combo for rx_port as well

* check unique hostname-port combo also when setting control port, hostname, rx_hostname and rx_tcpport

---------

Co-authored-by: Erik Fröjdh <erik.frojdh@gmail.com>
Co-authored-by: Erik Frojdh <erik.frojdh@psi.ch>
This commit is contained in:
Dhanya Thattil
2023-03-20 12:30:12 +01:00
committed by GitHub
parent c9215a6d9b
commit b67c6dea08
11 changed files with 228 additions and 44 deletions

View File

@ -1182,7 +1182,8 @@ Result<std::string> Detector::getRxHostname(Positions pos) const {
}
void Detector::setRxHostname(const std::string &receiver, Positions pos) {
pimpl->Parallel(&Module::setReceiverHostname, pos, receiver,
auto host = pimpl->verifyUniqueRxHost(receiver, pos);
pimpl->Parallel(&Module::setReceiverHostname, pos, host.first, host.second,
pimpl->getInitialChecks());
updateRxRateCorrections();
}
@ -1190,17 +1191,15 @@ void Detector::setRxHostname(const std::string &receiver, Positions pos) {
void Detector::setRxHostname(const std::vector<std::string> &name) {
// set all to same rx_hostname
if (name.size() == 1) {
pimpl->Parallel(&Module::setReceiverHostname, {}, name[0],
pimpl->getInitialChecks());
auto host = pimpl->verifyUniqueRxHost(name[0], {});
pimpl->Parallel(&Module::setReceiverHostname, {}, host.first,
host.second, pimpl->getInitialChecks());
} else {
if ((int)name.size() != size()) {
throw RuntimeError(
"Receiver hostnames size " + std::to_string(name.size()) +
" does not match detector size " + std::to_string(size()));
}
auto hosts = pimpl->verifyUniqueRxHost(name);
// set each rx_hostname
for (int idet = 0; idet < size(); ++idet) {
pimpl->Parallel(&Module::setReceiverHostname, {idet}, name[idet],
pimpl->Parallel(&Module::setReceiverHostname, {idet},
hosts[idet].first, hosts[idet].second,
pimpl->getInitialChecks());
}
}
@ -1217,10 +1216,12 @@ void Detector::setRxPort(int port, int module_id) {
for (auto &it : port_list) {
it = port++;
}
// no need to verify hostname-port combo as unique port(incremented)
for (int idet = 0; idet < size(); ++idet) {
pimpl->Parallel(&Module::setReceiverPort, {idet}, port_list[idet]);
}
} else {
pimpl->verifyUniqueRxHost(port, module_id);
pimpl->Parallel(&Module::setReceiverPort, {module_id}, port);
}
}
@ -2460,10 +2461,12 @@ Result<int> Detector::getControlPort(Positions pos) const {
}
void Detector::setControlPort(int value, Positions pos) {
pimpl->verifyUniqueDetHost(value, pos);
pimpl->Parallel(&Module::setControlPort, pos, value);
}
Result<int> Detector::getStopPort(Positions pos) const {
// not verifying unique stop port (control port is sufficient)
return pimpl->Parallel(&Module::getStopPort, pos);
}

View File

@ -279,31 +279,14 @@ void DetectorImpl::setHostname(const std::vector<std::string> &name) {
}
}
void DetectorImpl::addModule(const std::string &hostname) {
LOG(logINFO) << "Adding module " << hostname;
int port = DEFAULT_TCP_CNTRL_PORTNO;
std::string host = hostname;
auto res = split(hostname, ':');
if (res.size() > 1) {
host = res[0];
port = StringTo<int>(res[1]);
}
if (host != "localhost") {
for (auto &module : modules) {
if (module->getHostname() == host) {
LOG(logWARNING)
<< "Module " << host << "already part of the Detector!"
<< std::endl
<< "Remove it before adding it back in a new position!";
return;
}
}
}
void DetectorImpl::addModule(const std::string &name) {
LOG(logINFO) << "Adding module " << name;
auto host = verifyUniqueDetHost(name);
std::string hostname = host.first;
int port = host.second;
// get type by connecting
detectorType type = Module::getTypeFromDetector(host, port);
detectorType type = Module::getTypeFromDetector(hostname, port);
// gotthard cannot have more than 2 modules (50um=1, 25um=2
if ((type == GOTTHARD || type == GOTTHARD2) && modules.size() > 2) {
@ -316,7 +299,7 @@ void DetectorImpl::addModule(const std::string &hostname) {
shm()->totalNumberOfModules = modules.size();
modules[pos]->setControlPort(port);
modules[pos]->setStopPort(port + 1);
modules[pos]->setHostname(host, shm()->initialChecks);
modules[pos]->setHostname(hostname, shm()->initialChecks);
// module type updated by now
shm()->detType = Parallel(&Module::getDetectorType, {})
@ -1538,6 +1521,129 @@ defs::xy DetectorImpl::calculatePosition(int moduleIndex,
return pos;
}
void DetectorImpl::verifyUniqueDetHost(const int port,
std::vector<int> positions) const {
// port for given positions
if (positions.empty() || (positions.size() == 1 && positions[0] == -1)) {
positions.resize(modules.size());
std::iota(begin(positions), end(positions), 0);
}
std::vector<std::pair<std::string, int>> hosts(size());
for (auto it : positions) {
hosts[it].second = port;
}
verifyUniqueHost(true, hosts);
}
void DetectorImpl::verifyUniqueRxHost(const int port,
const int moduleId) const {
std::vector<std::pair<std::string, int>> hosts(size());
hosts[moduleId].second = port;
verifyUniqueHost(false, hosts);
}
std::pair<std::string, int>
DetectorImpl::verifyUniqueDetHost(const std::string &name) {
// extract port
// C++17 could be auto [hostname, port] = ParseHostPort(name);
auto res = ParseHostPort(name);
std::string hostname = res.first;
int port = res.second;
if (port == 0) {
port = DEFAULT_TCP_CNTRL_PORTNO;
}
int detSize = size();
// mod not yet added
std::vector<std::pair<std::string, int>> hosts(detSize + 1);
hosts[detSize].first = hostname;
hosts[detSize].second = port;
verifyUniqueHost(true, hosts);
return std::make_pair(hostname, port);
}
std::pair<std::string, int>
DetectorImpl::verifyUniqueRxHost(const std::string &name,
std::vector<int> positions) const {
// no checks if setting to none
if (name == "none" || name.empty()) {
return make_pair(name, 0);
}
// extract port
// C++17 could be auto [hostname, port] = ParseHostPort(name);
auto res = ParseHostPort(name);
std::string hostname = res.first;
int port = res.second;
// hostname and port for given positions
if (positions.empty() || (positions.size() == 1 && positions[0] == -1)) {
positions.resize(modules.size());
std::iota(begin(positions), end(positions), 0);
}
std::vector<std::pair<std::string, int>> hosts(size());
for (auto it : positions) {
hosts[it].first = hostname;
hosts[it].second = port;
}
verifyUniqueHost(false, hosts);
return std::make_pair(hostname, port);
}
std::vector<std::pair<std::string, int>>
DetectorImpl::verifyUniqueRxHost(const std::vector<std::string> &names) const {
if ((int)names.size() != size()) {
throw RuntimeError(
"Receiver hostnames size " + std::to_string(names.size()) +
" does not match detector size " + std::to_string(size()));
}
// extract ports
std::vector<std::pair<std::string, int>> hosts;
for (const auto &name : names) {
hosts.push_back(ParseHostPort(name));
}
verifyUniqueHost(false, hosts);
return hosts;
}
void DetectorImpl::verifyUniqueHost(
bool isDet, std::vector<std::pair<std::string, int>> &hosts) const {
// fill from shm if not provided
for (int i = 0; i != size(); ++i) {
if (hosts[i].first.empty()) {
hosts[i].first = (isDet ? modules[i]->getHostname()
: modules[i]->getReceiverHostname());
}
if (hosts[i].second == 0) {
hosts[i].second = (isDet ? modules[i]->getControlPort()
: modules[i]->getReceiverPort());
}
}
// remove the ones without a hostname
hosts.erase(std::remove_if(hosts.begin(), hosts.end(),
[](const std::pair<std::string, int> &x) {
return (x.first == "none" ||
x.first.empty());
}),
hosts.end());
// must be unique
if (hasDuplicates(hosts)) {
throw RuntimeError(
"Cannot set due to duplicate hostname-port number pairs.");
}
for (auto it : hosts) {
LOG(logDEBUG) << it.first << " " << it.second << std::endl;
}
}
defs::ROI DetectorImpl::getRxROI() const {
if (shm()->detType == CHIPTESTBOARD) {
throw RuntimeError("RxRoi not implemented for this Detector");

View File

@ -300,6 +300,17 @@ class DetectorImpl : public virtual slsDetectorDefs {
Positions pos = {});
void setDefaultDac(defs::dacIndex index, int defaultValue,
defs::detectorSettings sett, Positions pos);
void verifyUniqueDetHost(const int port, std::vector<int> positions) const;
void verifyUniqueRxHost(const int port, const int moduleId) const;
std::pair<std::string, int> verifyUniqueDetHost(const std::string &name);
std::pair<std::string, int>
verifyUniqueRxHost(const std::string &name,
std::vector<int> positions) const;
std::vector<std::pair<std::string, int>>
verifyUniqueRxHost(const std::vector<std::string> &names) const;
defs::ROI getRxROI() const;
void setRxROI(const defs::ROI arg);
void clearRxROI();
@ -396,6 +407,10 @@ class DetectorImpl : public virtual slsDetectorDefs {
defs::xy getPortGeometry() const;
defs::xy calculatePosition(int moduleIndex, defs::xy geometry) const;
void
verifyUniqueHost(bool isDet,
std::vector<std::pair<std::string, int>> &hosts) const;
const int detectorIndex{0};
SharedMemory<sharedDetector> shm{0, -1};
SharedMemory<CtbConfig> ctb_shm{0, -1, CtbConfig::shm_tag()};

View File

@ -1329,30 +1329,33 @@ std::string Module::getReceiverHostname() const {
return std::string(shm()->rxHostname);
}
void Module::setReceiverHostname(const std::string &receiverIP,
void Module::setReceiverHostname(const std::string &hostname, const int port,
const bool initialChecks) {
LOG(logDEBUG1) << "Setting up Receiver hostname with " << receiverIP;
{
std::ostringstream oss;
oss << "Setting up Receiver hostname with " << hostname;
if (port != 0) {
oss << " at port " << port;
}
LOG(logDEBUG1) << oss.str();
}
if (getRunStatus() == RUNNING) {
throw RuntimeError("Cannot set receiver hostname. Acquisition already "
"running. Stop it first.");
}
if (receiverIP == "none") {
if (hostname == "none") {
memset(shm()->rxHostname, 0, MAX_STR_LENGTH);
strcpy_safe(shm()->rxHostname, "none");
shm()->useReceiverFlag = false;
return;
}
// start updating
std::string host = receiverIP;
auto res = split(host, ':');
if (res.size() > 1) {
host = res[0];
shm()->rxTCPPort = std::stoi(res[1]);
strcpy_safe(shm()->rxHostname, hostname.c_str());
if (port != 0) {
shm()->rxTCPPort = port;
}
strcpy_safe(shm()->rxHostname, host.c_str());
shm()->useReceiverFlag = true;
try {

View File

@ -281,7 +281,7 @@ class Module : public virtual slsDetectorDefs {
* ************************************************/
bool getUseReceiverFlag() const;
std::string getReceiverHostname() const;
void setReceiverHostname(const std::string &receiver,
void setReceiverHostname(const std::string &hostname, const int port,
const bool initialChecks);
int getReceiverPort() const;
int setReceiverPort(int port_number);