Fulcrum/SrvMgr.cpp
Calin Culianu 6df916565a
Made app only start listening for client connections after initial sync
Also in this commit various nits and subtle bugs prevented.

Refactored the 'Stats' mechanism to return a generic QVariant rather
than QVariantMap, which is more flexible.

Misc. other nits.
2019-11-27 14:07:09 +02:00

66 lines
1.7 KiB
C++

#include "SrvMgr.h"
#include "Util.h"
#include "Servers.h"
#include <utility>
SrvMgr::SrvMgr(const QList<Options::Interface> & ifaces, QObject *parent)
: Mgr(parent), interfaces(ifaces)
{
}
SrvMgr::~SrvMgr()
{
Debug() << __FUNCTION__ ;
cleanup();
}
// will throw Exception on error from within TcpServer::tryStart
void SrvMgr::startup()
{
if (servers.isEmpty()) {
startServers();
} else {
Error() << __PRETTY_FUNCTION__ << " called with servers already active! FIXME!";
}
}
void SrvMgr::cleanup()
{
for (auto srv : servers) {
delete srv; // will wait for threads to finish
}
servers.clear();
}
// throw Exception on error
void SrvMgr::startServers()
{
const auto num = interfaces.length();
Log() << "SrvMgr: starting " << num << " " << Util::Pluralize("service", num) << " ...";
for (auto iface : interfaces) {
auto srv = new Server(iface.first, iface.second);
servers.push_back(srv); // save server in list unconditionally so we may delete later because tryStart may throw
srv->tryStart();
}
}
auto SrvMgr::stats() const -> Stats
{
QVariantList serverList;
auto servers = this->servers; // copy
const int timeout = kDefaultTimeout / qMax(servers.size(), 1);
for (auto server : servers) {
using Pair = std::pair<QString, QVariant>;
auto result = Util::LambdaOnObjectNoThrow<Pair>(server, [server] {
return Pair(server->prettyName(), server->stats());
}, timeout); // <-- limited timeout just in case
if (result) {
QVariantMap m;
m[result->first] = result->second;
serverList.push_back(m);
}
}
return serverList;
}