mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-27 02:55:44 +08:00
refs #318 Event originator with machine information
Mats ammended version (non-static methods).
This commit is contained in:
124
src/blackmisc/evoriginator.cpp
Normal file
124
src/blackmisc/evoriginator.cpp
Normal file
@@ -0,0 +1,124 @@
|
|||||||
|
/* Copyright (C) 2014
|
||||||
|
* swift Project Community / Contributors
|
||||||
|
*
|
||||||
|
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
|
||||||
|
* directory of this distribution and at http://www.swift-project.org/license.html. No part of Swift Project,
|
||||||
|
* including this file, may be copied, modified, propagated, or distributed except according to the terms
|
||||||
|
* contained in the LICENSE file.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "evoriginator.h"
|
||||||
|
#include <QCoreApplication>
|
||||||
|
#include <QDBusConnection>
|
||||||
|
|
||||||
|
namespace BlackMisc
|
||||||
|
{
|
||||||
|
namespace Event
|
||||||
|
{
|
||||||
|
// Default constructor
|
||||||
|
COriginator::COriginator()
|
||||||
|
: m_machineId(QDBusConnection::localMachineId()),
|
||||||
|
m_processId(QCoreApplication::applicationPid()),
|
||||||
|
m_processName(QCoreApplication::applicationName())
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
// Copy Constructor
|
||||||
|
COriginator::COriginator(const COriginator &other)
|
||||||
|
: CValueObject(other),
|
||||||
|
m_originatorName(other.m_originatorName),
|
||||||
|
m_machineId(other.m_machineId),
|
||||||
|
m_primaryIpAddress(other.m_primaryIpAddress),
|
||||||
|
m_objectId(other.m_objectId),
|
||||||
|
m_processId(other.m_processId),
|
||||||
|
m_processName(other.m_processName)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
// Hash
|
||||||
|
uint COriginator::getValueHash() const
|
||||||
|
{
|
||||||
|
return qHash(TupleConverter<COriginator>::toTuple(*this));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Register metadata
|
||||||
|
void COriginator::registerMetadata()
|
||||||
|
{
|
||||||
|
qRegisterMetaType<COriginator>();
|
||||||
|
qDBusRegisterMetaType<COriginator>();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool COriginator::isFromLocalMachine() const
|
||||||
|
{
|
||||||
|
return QDBusConnection::localMachineId() == getMachineId();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool COriginator::isFromSameProcess() const
|
||||||
|
{
|
||||||
|
return QCoreApplication::applicationPid() == getProcessId() && isFromLocalMachine();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool COriginator::isFromSameProcessName() const
|
||||||
|
{
|
||||||
|
return QCoreApplication::applicationName() == getProcessName();
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Convert to string
|
||||||
|
*/
|
||||||
|
QString COriginator::convertToQString(bool /* i18n */) const
|
||||||
|
{
|
||||||
|
QString s;
|
||||||
|
s.append(m_originatorName);
|
||||||
|
s.append(" ").append(m_machineId);
|
||||||
|
s.append(" ").append(m_primaryIpAddress);
|
||||||
|
s.append(" ").append(m_objectId);
|
||||||
|
s.append(" ").append(m_processId);
|
||||||
|
s.append(" ").append(m_processName);
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* metaTypeId
|
||||||
|
*/
|
||||||
|
int COriginator::getMetaTypeId() const
|
||||||
|
{
|
||||||
|
return qMetaTypeId<COriginator>();
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* is a
|
||||||
|
*/
|
||||||
|
bool COriginator::isA(int metaTypeId) const
|
||||||
|
{
|
||||||
|
if (metaTypeId == qMetaTypeId<COriginator>()) { return true; }
|
||||||
|
|
||||||
|
return this->CValueObject::isA(metaTypeId);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Compare
|
||||||
|
*/
|
||||||
|
int COriginator::compareImpl(const CValueObject &otherBase) const
|
||||||
|
{
|
||||||
|
const auto &other = static_cast<const COriginator &>(otherBase);
|
||||||
|
return compare(TupleConverter<COriginator>::toTuple(*this), TupleConverter<COriginator>::toTuple(other));
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Marshall to DBus
|
||||||
|
*/
|
||||||
|
void COriginator::marshallToDbus(QDBusArgument &argument) const
|
||||||
|
{
|
||||||
|
argument << TupleConverter<COriginator>::toTuple(*this);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Unmarshall from DBus
|
||||||
|
*/
|
||||||
|
void COriginator::unmarshallFromDbus(const QDBusArgument &argument)
|
||||||
|
{
|
||||||
|
argument >> TupleConverter<COriginator>::toTuple(*this);
|
||||||
|
}
|
||||||
|
} // namespace Event
|
||||||
|
}
|
||||||
100
src/blackmisc/evoriginator.h
Normal file
100
src/blackmisc/evoriginator.h
Normal file
@@ -0,0 +1,100 @@
|
|||||||
|
/* Copyright (C) 2014
|
||||||
|
* swift Project Community / Contributors
|
||||||
|
*
|
||||||
|
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
|
||||||
|
* directory of this distribution and at http://www.swift-project.org/license.html. No part of Swift Project,
|
||||||
|
* including this file, may be copied, modified, propagated, or distributed except according to the terms
|
||||||
|
* contained in the LICENSE file.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef BLACKMISC_EVENT_ORIGINATOR_H
|
||||||
|
#define BLACKMISC_EVENT_ORIGINATOR_H
|
||||||
|
|
||||||
|
//! \file
|
||||||
|
|
||||||
|
#include "valueobject.h"
|
||||||
|
#include "blackmiscfreefunctions.h"
|
||||||
|
#include <QByteArray>
|
||||||
|
#include <QString>
|
||||||
|
|
||||||
|
namespace BlackMisc
|
||||||
|
{
|
||||||
|
namespace Event
|
||||||
|
{
|
||||||
|
|
||||||
|
//! Value object encapsulating information about the originiator
|
||||||
|
class COriginator : public BlackMisc::CValueObject
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
|
||||||
|
//! Default constructor.
|
||||||
|
COriginator();
|
||||||
|
|
||||||
|
//! Copy constructor
|
||||||
|
COriginator(const COriginator &other);
|
||||||
|
|
||||||
|
//! Get machine id
|
||||||
|
QByteArray getMachineId() const {return m_machineId;}
|
||||||
|
|
||||||
|
//! Get process id
|
||||||
|
qint32 getProcessId() const {return m_processId;}
|
||||||
|
|
||||||
|
//! Get process name
|
||||||
|
QString getProcessName() const {return m_processName;}
|
||||||
|
|
||||||
|
//! \copydoc CValueObject::toQVariant
|
||||||
|
virtual QVariant toQVariant() const override { return QVariant::fromValue(*this); }
|
||||||
|
|
||||||
|
//! \copydoc CValueObject::convertFromQVariant
|
||||||
|
virtual void convertFromQVariant(const QVariant &variant) override { BlackMisc::setFromQVariant(this, variant); }
|
||||||
|
|
||||||
|
//! \copydoc CValueObject::getValueHash
|
||||||
|
virtual uint getValueHash() const override;
|
||||||
|
|
||||||
|
//! \brief Register metadata
|
||||||
|
static void registerMetadata();
|
||||||
|
|
||||||
|
//! Check if originating from the same local machine
|
||||||
|
bool isFromLocalMachine() const;
|
||||||
|
|
||||||
|
//! Check if originating from the same process id
|
||||||
|
bool isFromSameProcess() const;
|
||||||
|
|
||||||
|
//! Check if originating from the same process name
|
||||||
|
bool isFromSameProcessName() const;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
//! \copydoc CValueObject::convertToQString
|
||||||
|
virtual QString convertToQString(bool i18n = false) const override;
|
||||||
|
|
||||||
|
//! \copydoc CValueObject::getMetaTypeId
|
||||||
|
virtual int getMetaTypeId() const override;
|
||||||
|
|
||||||
|
//! \copydoc CValueObject::isA
|
||||||
|
virtual bool isA(int metaTypeId) const override;
|
||||||
|
|
||||||
|
//! \copydoc CValueObject::compareImpl
|
||||||
|
virtual int compareImpl(const CValueObject &other) const override;
|
||||||
|
|
||||||
|
//! \copydoc CValueObject::marshallToDbus
|
||||||
|
virtual void marshallToDbus(QDBusArgument &argument) const override;
|
||||||
|
|
||||||
|
//! \copydoc CValueObject::unmarshallFromDbus
|
||||||
|
virtual void unmarshallFromDbus(const QDBusArgument &argument) override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
BLACK_ENABLE_TUPLE_CONVERSION(COriginator)
|
||||||
|
QString m_originatorName;
|
||||||
|
QByteArray m_machineId;
|
||||||
|
QByteArray m_primaryIpAddress;
|
||||||
|
QByteArray m_objectId;
|
||||||
|
qint32 m_processId;
|
||||||
|
QString m_processName;
|
||||||
|
};
|
||||||
|
} // namespace Event
|
||||||
|
}
|
||||||
|
|
||||||
|
BLACK_DECLARE_TUPLE_CONVERSION(BlackMisc::Event::COriginator, (o.m_originatorName, o.m_machineId, o.m_primaryIpAddress, o.m_objectId, o.m_processId, o.m_processName))
|
||||||
|
Q_DECLARE_METATYPE(BlackMisc::Event::COriginator)
|
||||||
|
|
||||||
|
#endif // BLACKMISC_EVENT_ORIGINATOR_H
|
||||||
Reference in New Issue
Block a user