refs #849, use timestamp with server to indicate when login started

* CServer -> ITimestampBased
* added compare functions
This commit is contained in:
Klaus Basan
2017-01-05 02:14:40 +01:00
committed by Mathew Sutcliffe
parent 5f18707c9c
commit dcfaca431c
7 changed files with 111 additions and 29 deletions

View File

@@ -21,7 +21,7 @@ namespace BlackGui
namespace Models
{
CServerListModel::CServerListModel(QObject *parent) :
CListModelBase("ModelServerList", parent)
CListModelBase("ServerListModel", parent)
{
this->m_columns.addColumn(CColumn::standardString("name", CServer::IndexName));
this->m_columns.addColumn(CColumn::standardString("description", CServer::IndexDescription));
@@ -31,12 +31,12 @@ namespace BlackGui
this->m_columns.addColumn(CColumn::standardString("userid", { CServer::IndexUser, CUser::IndexId}));
// force strings for translation in resource files
(void)QT_TRANSLATE_NOOP("ModelServerList", "name");
(void)QT_TRANSLATE_NOOP("ModelServerList", "description");
(void)QT_TRANSLATE_NOOP("ModelServerList", "address");
(void)QT_TRANSLATE_NOOP("ModelServerList", "port");
(void)QT_TRANSLATE_NOOP("ModelServerList", "realname");
(void)QT_TRANSLATE_NOOP("ModelServerList", "userid");
(void)QT_TRANSLATE_NOOP("ServerListModel", "name");
(void)QT_TRANSLATE_NOOP("ServerListModel", "description");
(void)QT_TRANSLATE_NOOP("ServerListModel", "address");
(void)QT_TRANSLATE_NOOP("ServerListModel", "port");
(void)QT_TRANSLATE_NOOP("ServerListModel", "realname");
(void)QT_TRANSLATE_NOOP("ServerListModel", "userid");
}
} // class

View File

@@ -25,10 +25,9 @@ namespace BlackGui
namespace Models
{
//! Server list model
class BLACKGUI_EXPORT CServerListModel : public CListModelBase<BlackMisc::Network::CServer, BlackMisc::Network::CServerList>
class BLACKGUI_EXPORT CServerListModel : public CListModelBase<BlackMisc::Network::CServer, BlackMisc::Network::CServerList, true>
{
public:
//! Constructor
explicit CServerListModel(QObject *parent = nullptr);

View File

@@ -13,6 +13,7 @@
#include "blackmisc/logcategorylist.h"
#include "blackmisc/propertyindex.h"
#include "blackmisc/statusmessage.h"
#include "blackmisc/comparefunctions.h"
#include "blackmisc/variant.h"
#include <Qt>
@@ -28,12 +29,17 @@ namespace BlackMisc
QString CServer::convertToQString(bool i18n) const
{
QString s(this->m_name);
s.append(" ").append(this->m_description);
s.append(" ").append(this->m_address);
s.append(" ").append(QString::number(this->m_port));
s.append(" ").append(this->m_user.toQString(i18n));
s.append(" ").append(boolToYesNo(this->m_isAcceptingConnections));
s.append(" ").append(this->m_fsdSetup.toQString(i18n));
s.append(' ').append(this->m_description);
s.append(' ').append(this->m_address);
s.append(' ').append(QString::number(this->m_port));
s.append(' ').append(this->m_user.toQString(i18n));
s.append(' ').append(boolToYesNo(this->m_isAcceptingConnections));
s.append(' ').append(this->m_fsdSetup.toQString(i18n));
if (this->isConnected())
{
s.append(" connected: ").append(this->getFormattedUtcTimestampHms());
}
return s;
}
@@ -65,6 +71,11 @@ namespace BlackMisc
return m_port > 0 && !m_address.isEmpty();
}
bool CServer::isConnected() const
{
return this->m_timestampMSecsSinceEpoch >= 0;
}
CStatusMessageList CServer::validate() const
{
static const CLogCategoryList cats(CLogCategoryList(this).join({ CLogCategory::validation()}));
@@ -79,10 +90,19 @@ namespace BlackMisc
return msgs;
}
QString CServer::getServerSessionId() const
{
if (!this->isConnected()) { return ""; }
const QString session("%1 %2:%3 %4 %5");
return session.arg(this->getName(), this->getAddress()).arg(this->getPort()).arg(this->getUser().getRealName(), this->getFormattedUtcTimestampHms());
}
CVariant CServer::propertyByIndex(const BlackMisc::CPropertyIndex &index) const
{
if (index.isMyself()) { return CVariant::from(*this); }
ColumnIndex i = index.frontCasted<ColumnIndex>();
if (ITimestampBased::canHandleIndex(index)) { return ITimestampBased::propertyByIndex(index); }
const ColumnIndex i = index.frontCasted<ColumnIndex>();
switch (i)
{
case IndexAddress:
@@ -107,7 +127,9 @@ namespace BlackMisc
void CServer::setPropertyByIndex(const CPropertyIndex &index, const CVariant &variant)
{
if (index.isMyself()) { (*this) = variant.to<CServer>(); return; }
ColumnIndex i = index.frontCasted<ColumnIndex>();
if (ITimestampBased::canHandleIndex(index)) { ITimestampBased::setPropertyByIndex(index, variant); return; }
const ColumnIndex i = index.frontCasted<ColumnIndex>();
switch (i)
{
case IndexAddress:
@@ -136,5 +158,33 @@ namespace BlackMisc
break;
}
}
int CServer::comparePropertyByIndex(const CPropertyIndex &index, const CServer &compareValue) const
{
if (index.isMyself()) { return this->getName().compare(compareValue.getName()); }
if (ITimestampBased::canHandleIndex(index)) { return ITimestampBased::comparePropertyByIndex(index, compareValue);}
const ColumnIndex i = index.frontCasted<ColumnIndex>();
switch (i)
{
case IndexAddress:
return this->getAddress().compare(compareValue.getAddress(), Qt::CaseInsensitive);
case IndexDescription:
return this->getDescription().compare(compareValue.getDescription(), Qt::CaseInsensitive);
case IndexFsdSetup:
return this->getFsdSetup().toQString().compare(compareValue.getFsdSetup().toQString());
case IndexName:
return this->getName().compare(compareValue.getName(), Qt::CaseInsensitive);
case IndexIsAcceptingConnections:
return Compare::compare(this->isAcceptingConnections(), compareValue.isAcceptingConnections());
case IndexPort:
return Compare::compare(this->getPort(), compareValue.getPort());
case IndexUser:
return this->getUser().comparePropertyByIndex(index.copyFrontRemoved(), compareValue.getUser());
default:
break;
}
Q_ASSERT_X(false, Q_FUNC_INFO, "No compare function");
return 0;
}
} // namespace
} // namespace

View File

@@ -18,6 +18,7 @@
#include "blackmisc/network/fsdsetup.h"
#include "blackmisc/propertyindex.h"
#include "blackmisc/statusmessagelist.h"
#include "blackmisc/timestampbased.h"
#include "blackmisc/valueobject.h"
#include "blackmisc/variant.h"
@@ -29,7 +30,9 @@ namespace BlackMisc
namespace Network
{
//! Value object encapsulating information of a server
class BLACKMISC_EXPORT CServer : public CValueObject<CServer>
class BLACKMISC_EXPORT CServer :
public CValueObject<CServer>,
public BlackMisc::ITimestampBased
{
public:
//! Properties by index
@@ -89,10 +92,10 @@ namespace BlackMisc
//! Set port
void setPort(int port) { m_port = port; }
//! Server is accepting connections (allows to disable server temporarily)
//! Server is accepting connections (allows to disable server temporarily or generally)
bool isAcceptingConnections() const { return m_isAcceptingConnections; }
//! Set whether server is accepting connections (allows to disable server temporarily)
//! Set whether server is accepting connections (allows to disable server temporarily or generally)
void setIsAcceptingConnections(bool value) { m_isAcceptingConnections = value; }
//! Is valid for login?
@@ -107,15 +110,33 @@ namespace BlackMisc
//! Set setup
void setFsdSetup(const CFsdSetup &setup) { this->m_fsdSetup = setup; }
//! Connected since
QDateTime getConnectedSince() const { return this->getUtcTimestamp(); }
//! Mark as connected since now
void setConnectedSinceNow() { this->setCurrentUtcTime(); }
//! Mark as diconnected
void markAsDisconnected() { this->setTimestampToNull(); }
//! Is connected?
bool isConnected() const;
//! Validate, provide details about issues
BlackMisc::CStatusMessageList validate() const;
//! Identifying a session, if not connected empty
QString getServerSessionId() const;
//! \copydoc BlackMisc::Mixin::Index::propertyByIndex
CVariant propertyByIndex(const BlackMisc::CPropertyIndex &index) const;
//! \copydoc BlackMisc::Mixin::Index::setPropertyByIndex
void setPropertyByIndex(const BlackMisc::CPropertyIndex &index, const CVariant &variant);
//! Compare by index
int comparePropertyByIndex(const CPropertyIndex &index, const CServer &compareValue) const;
//! \copydoc BlackMisc::Mixin::String::toQString()
QString convertToQString(bool i18n = false) const;
@@ -126,7 +147,7 @@ namespace BlackMisc
int m_port = -1;
CUser m_user;
CFsdSetup m_fsdSetup;
bool m_isAcceptingConnections = true; //!< temp. disable server
bool m_isAcceptingConnections = true; //!< disable server for connections
BLACK_METACLASS(
CServer,
@@ -136,7 +157,8 @@ namespace BlackMisc
BLACK_METAMEMBER(port),
BLACK_METAMEMBER(user),
BLACK_METAMEMBER(fsdSetup),
BLACK_METAMEMBER(isAcceptingConnections)
BLACK_METAMEMBER(isAcceptingConnections),
BLACK_METAMEMBER(timestampMSecsSinceEpoch, 0, DisabledForJson | DisabledForComparison)
);
};
} // namespace

View File

@@ -30,9 +30,15 @@ namespace BlackMisc
QDateTime ITimestampBased::getUtcTimestamp() const
{
if (this->m_timestampMSecsSinceEpoch < 0) { return QDateTime(); }
return QDateTime::fromMSecsSinceEpoch(this->m_timestampMSecsSinceEpoch, Qt::UTC);
}
void ITimestampBased::setTimestampToNull()
{
this->m_timestampMSecsSinceEpoch = -1;
}
void ITimestampBased::setByYearMonthDayHourMinute(const QString &yyyyMMddhhmmsszzz)
{
// yyyy MM dd hh mm ss zzz

View File

@@ -44,6 +44,9 @@ namespace BlackMisc
//! Timestamp as ms value
qint64 getMSecsSinceEpoch() const { return m_timestampMSecsSinceEpoch; }
//! Set to null
void setTimestampToNull();
//! Timestamp as ms value
void setMSecsSinceEpoch(qint64 mSecsSinceEpoch) { m_timestampMSecsSinceEpoch = mSecsSinceEpoch; }

View File

@@ -20,6 +20,7 @@
#include "blackmisc/simulation/distributorlist.h"
#include "blackmisc/simulation/aircraftmodellist.h"
#include "blackmisc/simulation/distributorlist.h"
#include "blackmisc/simulation/matchingstatistics.h"
#include "blackmisc/statusmessagelist.h"
#include "blackmisc/timestampobjectlist.h"
#include "blackmisc/predicates.h"
@@ -225,19 +226,20 @@ namespace BlackMisc
// see here for the reason of thess forward instantiations
// http://www.parashift.com/c++-faq/separate-template-class-defn-from-decl.html
//! \cond PRIVATE
template class BLACKMISC_EXPORT_DEFINE_TEMPLATE ITimestampObjectList<BlackMisc::Aviation::CAircraftSituation, BlackMisc::Aviation::CAircraftSituationList>;
template class BLACKMISC_EXPORT_DEFINE_TEMPLATE ITimestampObjectList<BlackMisc::Aviation::CAircraftParts, BlackMisc::Aviation::CAircraftPartsList>;
template class BLACKMISC_EXPORT_DEFINE_TEMPLATE ITimestampObjectList<BlackMisc::Aviation::CLivery, BlackMisc::Aviation::CLiveryList>;
template class BLACKMISC_EXPORT_DEFINE_TEMPLATE ITimestampObjectList<BlackMisc::CCountry, BlackMisc::CCountryList>;
template class BLACKMISC_EXPORT_DEFINE_TEMPLATE ITimestampObjectList<BlackMisc::CIdentifier, BlackMisc::CIdentifierList>;
template class BLACKMISC_EXPORT_DEFINE_TEMPLATE ITimestampObjectList<BlackMisc::CStatusMessage, BlackMisc::CStatusMessageList>;
template class BLACKMISC_EXPORT_DEFINE_TEMPLATE ITimestampObjectList<BlackMisc::Aviation::CAircraftIcaoCode, BlackMisc::Aviation::CAircraftIcaoCodeList>;
template class BLACKMISC_EXPORT_DEFINE_TEMPLATE ITimestampObjectList<BlackMisc::Aviation::CAircraftParts, BlackMisc::Aviation::CAircraftPartsList>;
template class BLACKMISC_EXPORT_DEFINE_TEMPLATE ITimestampObjectList<BlackMisc::Aviation::CAircraftSituation, BlackMisc::Aviation::CAircraftSituationList>;
template class BLACKMISC_EXPORT_DEFINE_TEMPLATE ITimestampObjectList<BlackMisc::Aviation::CAirlineIcaoCode, BlackMisc::Aviation::CAirlineIcaoCodeList>;
template class BLACKMISC_EXPORT_DEFINE_TEMPLATE ITimestampObjectList<BlackMisc::Aviation::CAirport, BlackMisc::Aviation::CAirportList>;
template class BLACKMISC_EXPORT_DEFINE_TEMPLATE ITimestampObjectList<BlackMisc::Aviation::CLivery, BlackMisc::Aviation::CLiveryList>;
template class BLACKMISC_EXPORT_DEFINE_TEMPLATE ITimestampObjectList<BlackMisc::Db::CDbInfo, BlackMisc::Db::CDbInfoList>;
template class BLACKMISC_EXPORT_DEFINE_TEMPLATE ITimestampObjectList<BlackMisc::Network::CTextMessage, BlackMisc::Network::CTextMessageList>;
template class BLACKMISC_EXPORT_DEFINE_TEMPLATE ITimestampObjectList<BlackMisc::Simulation::CAircraftModel, BlackMisc::Simulation::CAircraftModelList>;
template class BLACKMISC_EXPORT_DEFINE_TEMPLATE ITimestampObjectList<BlackMisc::Simulation::CDistributor, BlackMisc::Simulation::CDistributorList>;
template class BLACKMISC_EXPORT_DEFINE_TEMPLATE ITimestampObjectList<BlackMisc::Network::CTextMessage, BlackMisc::Network::CTextMessageList>;
template class BLACKMISC_EXPORT_DEFINE_TEMPLATE ITimestampObjectList<BlackMisc::CStatusMessage, BlackMisc::CStatusMessageList>;
template class BLACKMISC_EXPORT_DEFINE_TEMPLATE ITimestampObjectList<BlackMisc::CIdentifier, BlackMisc::CIdentifierList>;
template class BLACKMISC_EXPORT_DEFINE_TEMPLATE ITimestampObjectList<BlackMisc::CCountry, BlackMisc::CCountryList>;
template class BLACKMISC_EXPORT_DEFINE_TEMPLATE ITimestampObjectList<BlackMisc::Simulation::CMatchingStatisticsEntry, BlackMisc::Simulation::CMatchingStatistics>;
//! \endcond
} // namespace