mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-03-30 11:55:35 +08:00
Ref T259, Ref T243 client provider used to share info about the other clients
Goal: this info can be used in interpolation to decide if ground flags, parts etc are available
This commit is contained in:
123
src/blackmisc/network/clientprovider.cpp
Normal file
123
src/blackmisc/network/clientprovider.cpp
Normal file
@@ -0,0 +1,123 @@
|
||||
/* Copyright (C) 2018
|
||||
* 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 "clientprovider.h"
|
||||
|
||||
using namespace BlackMisc::Aviation;
|
||||
|
||||
namespace BlackMisc
|
||||
{
|
||||
namespace Network
|
||||
{
|
||||
CClientList IClientProvider::getClients() const
|
||||
{
|
||||
QReadLocker l(&m_lockClient);
|
||||
return m_clients;
|
||||
}
|
||||
|
||||
void IClientProvider::setClients(const CClientList &clients)
|
||||
{
|
||||
QWriteLocker l(&m_lockClient);
|
||||
m_clients = clients;
|
||||
}
|
||||
|
||||
void IClientProvider::clearClients()
|
||||
{
|
||||
QWriteLocker l(&m_lockClient);
|
||||
m_clients.clear();
|
||||
}
|
||||
|
||||
CClientList IClientProvider::getClientsForCallsigns(const CCallsignSet &callsigns) const
|
||||
{
|
||||
return this->getClients().findByCallsigns(callsigns);
|
||||
}
|
||||
|
||||
CClient IClientProvider::getClientOrDefaultForCallsign(const CCallsign &callsign) const
|
||||
{
|
||||
const CClientList clients(this->getClients());
|
||||
return clients.findFirstByCallsign(callsign);
|
||||
}
|
||||
|
||||
bool IClientProvider::hasClientInfo(const CCallsign &callsign) const
|
||||
{
|
||||
return this->getClients().containsCallsign(callsign);
|
||||
}
|
||||
|
||||
bool IClientProvider::addNewClient(const CClient &client)
|
||||
{
|
||||
const CCallsign callsign = client.getCallsign();
|
||||
Q_ASSERT_X(!callsign.isEmpty(), Q_FUNC_INFO, "invalid callsign");
|
||||
if (this->hasClientInfo(callsign)) { return false; }
|
||||
QWriteLocker l(&m_lockClient);
|
||||
m_clients.push_back(client);
|
||||
return true;
|
||||
}
|
||||
|
||||
int IClientProvider::updateOrAddClient(const CCallsign &callsign, const CPropertyIndexVariantMap &vm, bool skipEqualValues)
|
||||
{
|
||||
Q_ASSERT_X(!callsign.isEmpty(), Q_FUNC_INFO, "Missing callsign");
|
||||
int c = 0;
|
||||
if (!this->hasClientInfo(callsign))
|
||||
{
|
||||
CClient client(callsign);
|
||||
c = client.apply(vm).size();
|
||||
this->addNewClient(client);
|
||||
}
|
||||
else
|
||||
{
|
||||
QWriteLocker l(&m_lockClient);
|
||||
c = m_clients.applyIfCallsign(callsign, vm, skipEqualValues);
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
int IClientProvider::removeClient(const CCallsign &callsign)
|
||||
{
|
||||
QWriteLocker l(&m_lockClient);
|
||||
return m_clients.removeByCallsign(callsign);
|
||||
}
|
||||
|
||||
CClientList CClientAware::getClients() const
|
||||
{
|
||||
if (m_clientProvider) { return m_clientProvider->getClients(); }
|
||||
return CClientList();
|
||||
}
|
||||
|
||||
CClient CClientAware::getClientOrDefaultForCallsign(const Aviation::CCallsign &callsign) const
|
||||
{
|
||||
if (m_clientProvider) { return m_clientProvider->getClientOrDefaultForCallsign(callsign); }
|
||||
return CClient();
|
||||
}
|
||||
|
||||
bool CClientAware::hasClientInfo(const CCallsign &callsign) const
|
||||
{
|
||||
if (m_clientProvider) { return m_clientProvider->hasClientInfo(callsign); }
|
||||
return false;
|
||||
}
|
||||
|
||||
bool CClientAware::addNewClient(const CClient &client)
|
||||
{
|
||||
if (m_clientProvider) { return m_clientProvider->addNewClient(client); }
|
||||
return false;
|
||||
}
|
||||
|
||||
int CClientAware::updateOrAddClient(const CCallsign &callsign, const CPropertyIndexVariantMap &vm, bool skipEqualValues)
|
||||
{
|
||||
if (m_clientProvider) { return m_clientProvider->updateOrAddClient(callsign, vm, skipEqualValues); }
|
||||
return 0;
|
||||
}
|
||||
|
||||
int CClientAware::removeClient(const CCallsign &callsign)
|
||||
{
|
||||
if (m_clientProvider) { return m_clientProvider->removeClient(callsign); }
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
// namespace
|
||||
} // namespace
|
||||
116
src/blackmisc/network/clientprovider.h
Normal file
116
src/blackmisc/network/clientprovider.h
Normal file
@@ -0,0 +1,116 @@
|
||||
/* Copyright (C) 2018
|
||||
* 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.
|
||||
*/
|
||||
|
||||
//! \file
|
||||
|
||||
#ifndef BLACKMISC_NETWORK_CLIENTPROVIDER_H
|
||||
#define BLACKMISC_NETWORK_CLIENTPROVIDER_H
|
||||
|
||||
#include "clientlist.h"
|
||||
#include "blackmisc/aviation/callsignset.h"
|
||||
#include "blackmisc/provider.h"
|
||||
#include <QReadWriteLock>
|
||||
|
||||
namespace BlackMisc
|
||||
{
|
||||
namespace Network
|
||||
{
|
||||
//! Direct in memory access to client (network client) data
|
||||
class BLACKMISC_EXPORT IClientProvider : public IProvider
|
||||
{
|
||||
public:
|
||||
//! Get other clients
|
||||
//! \threadsafe
|
||||
CClientList getClients() const;
|
||||
|
||||
//! Set other clients
|
||||
//! \threadsafe
|
||||
void setClients(const CClientList &clients);
|
||||
|
||||
//! Set other clients
|
||||
//! \threadsafe
|
||||
void clearClients();
|
||||
|
||||
//! Returns a list of other clients corresponding to the given callsigns
|
||||
//! \threadsafe
|
||||
BlackMisc::Network::CClientList getClientsForCallsigns(const Aviation::CCallsignSet &callsigns) const;
|
||||
|
||||
//! Other client for the given callsigns
|
||||
//! \threadsafe
|
||||
BlackMisc::Network::CClient getClientOrDefaultForCallsign(const Aviation::CCallsign &callsign) const;
|
||||
|
||||
//! Client info for given callsign?
|
||||
//! \threadsafe
|
||||
bool hasClientInfo(const Aviation::CCallsign &callsign) const;
|
||||
|
||||
//! Add a new client, if existing nothing will be added
|
||||
//! \threadsafe
|
||||
bool addNewClient(const CClient &client);
|
||||
|
||||
//! Update or add a client
|
||||
//! \threadsafe
|
||||
int updateOrAddClient(const Aviation::CCallsign &callsign, const CPropertyIndexVariantMap &vm, bool skipEqualValues = true);
|
||||
|
||||
//! Remove client
|
||||
//! \threadsafe
|
||||
int removeClient(const Aviation::CCallsign &callsign);
|
||||
|
||||
private:
|
||||
CClientList m_clients;
|
||||
mutable QReadWriteLock m_lockClient; //!< lock clients: m_clients
|
||||
};
|
||||
|
||||
//! Class which can be directly used to access an \sa IClientProvider object
|
||||
class BLACKMISC_EXPORT CClientAware : public IProviderAware<IClientProvider>
|
||||
{
|
||||
public:
|
||||
//! \copydoc IClientProvider::getClients
|
||||
CClientList getClients() const;
|
||||
|
||||
//! \copydoc IClientProvider::setClients
|
||||
void setClients(const CClientList &clients);
|
||||
|
||||
//! \copydoc IClientProvider::clearClients
|
||||
void clearClients();
|
||||
|
||||
//! \copydoc IClientProvider::getClientsForCallsigns
|
||||
BlackMisc::Network::CClientList getClientsForCallsigns(const Aviation::CCallsignSet &callsigns) const;
|
||||
|
||||
//! \copydoc IClientProvider::getClientOrDefaultForCallsign
|
||||
BlackMisc::Network::CClient getClientOrDefaultForCallsign(const Aviation::CCallsign &callsign) const;
|
||||
|
||||
//! \copydoc IClientProvider::hasClientInfo
|
||||
bool hasClientInfo(const Aviation::CCallsign &callsign) const;
|
||||
|
||||
//! \copydoc IClientProvider::addNewClient
|
||||
bool addNewClient(const CClient &client);
|
||||
|
||||
//! \copydoc IClientProvider::updateOrAddClient
|
||||
int updateOrAddClient(const Aviation::CCallsign &callsign, const CPropertyIndexVariantMap &vm, bool skipEqualValues);
|
||||
|
||||
//! \copydoc IClientProvider::removeClient
|
||||
int removeClient(const Aviation::CCallsign &callsign);
|
||||
|
||||
//! Provider
|
||||
void setClientProvider(IClientProvider *provider) { this->setProvider(provider); }
|
||||
|
||||
protected:
|
||||
//! Default constructor
|
||||
CClientAware() {}
|
||||
|
||||
//! Constructor
|
||||
CClientAware(IClientProvider *clientProvider) : m_clientProvider(clientProvider) { Q_ASSERT(clientProvider); }
|
||||
IClientProvider *m_clientProvider = nullptr; //!< access to object
|
||||
};
|
||||
} // namespace
|
||||
} // namespace
|
||||
|
||||
Q_DECLARE_INTERFACE(BlackMisc::Network::IClientProvider, "BlackMisc::Network::IClientProvider")
|
||||
|
||||
#endif // guard
|
||||
Reference in New Issue
Block a user