swift
clientprovider.cpp
1 // SPDX-FileCopyrightText: Copyright (C) 2018 swift Project Community / Contributors
2 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3 
5 
7 #include "misc/verify.h"
8 
9 using namespace swift::misc::aviation;
10 
11 namespace swift::misc::network
12 {
13  CClientList CClientProvider::getClients() const
14  {
15  QList<CClient> clients;
16  {
17  QReadLocker l(&m_lockClient);
18  clients = m_clients.values();
19  }
20  return CClientList(clients);
21  }
22 
23  void CClientProvider::setClients(const CClientList &clients)
24  {
25  const CClientPerCallsign perCallsign(clients.asCallsignHash());
26  QWriteLocker l(&m_lockClient);
27  m_clients = perCallsign;
28  }
29 
30  void CClientProvider::clearClients()
31  {
32  QWriteLocker l(&m_lockClient);
33  m_clients.clear();
34  }
35 
36  CClientList CClientProvider::getClientsForCallsigns(const CCallsignSet &callsigns) const
37  {
38  if (callsigns.isEmpty()) { return {}; }
39  return this->getClients().findByCallsigns(callsigns);
40  }
41 
42  CClient CClientProvider::getClientOrDefaultForCallsign(const CCallsign &callsign) const
43  {
44  if (callsign.isEmpty()) { return {}; }
45  {
46  QReadLocker l(&m_lockClient);
47  if (m_clients.contains(callsign)) { return m_clients.value(callsign); }
48  }
49  return CClient();
50  }
51 
52  bool CClientProvider::setOtherClient(const CClient &client)
53  {
54  const bool hasCallsign = !client.getCallsign().isEmpty();
55  SWIFT_VERIFY_X(hasCallsign, Q_FUNC_INFO, "Need callsign in client");
56  if (!hasCallsign) { return false; }
57  QWriteLocker l(&m_lockClient);
58  m_clients[client.getCallsign()] = client;
59  return true;
60  }
61 
62  bool CClientProvider::hasClientInfo(const CCallsign &callsign) const
63  {
64  if (callsign.isEmpty()) { return false; }
65  QReadLocker l(&m_lockClient);
66  return m_clients.contains(callsign);
67  }
68 
69  bool CClientProvider::addNewClient(const CClient &client)
70  {
71  const CCallsign callsign = client.getCallsign();
72  SWIFT_VERIFY_X(!callsign.isEmpty(), Q_FUNC_INFO, "Missing client callsign");
73  if (callsign.isEmpty()) { return false; }
74  if (this->hasClientInfo(callsign)) { return false; }
75  QWriteLocker l(&m_lockClient);
76  m_clients[callsign] = client;
77  return true;
78  }
79 
80  int CClientProvider::updateOrAddClient(const CCallsign &callsign, const CPropertyIndexVariantMap &vm,
81  bool skipEqualValues)
82  {
83  SWIFT_VERIFY_X(!callsign.isEmpty(), Q_FUNC_INFO, "Missing client callsign");
84  if (callsign.isEmpty()) { return 0; }
85  int c = 0;
86  if (this->hasClientInfo(callsign))
87  {
88  QWriteLocker l(&m_lockClient);
89  CClient &client = m_clients[callsign];
90  c = client.apply(vm, skipEqualValues).size();
91  }
92  else
93  {
94  CClient client(callsign);
95  c = client.apply(vm).size();
96  QWriteLocker l(&m_lockClient);
97  m_clients[callsign] = client;
98  }
99  return c;
100  }
101 
102  int CClientProvider::removeClient(const CCallsign &callsign)
103  {
104  QWriteLocker l(&m_lockClient);
105  return m_clients.remove(callsign);
106  }
107 
108  bool CClientProvider::autoAdjustCientGndCapability(const CAircraftSituation &situation)
109  {
110  if (situation.getCallsign().isEmpty()) { return false; } // no callsign
111  if (!situation.isOnGround()) { return false; } // nothing to adjust
113  {
114  return false;
115  } // not from network
116  return this->addClientGndCapability(situation.getCallsign());
117  }
118 
119  bool CClientProvider::addClientGndCapability(const CCallsign &callsign)
120  {
121  return this->setClientGndCapability(callsign, true);
122  }
123 
124  bool CClientProvider::setClientGndCapability(const CCallsign &callsign, bool supportGndFlag)
125  {
126  SWIFT_VERIFY_X(!callsign.isEmpty(), Q_FUNC_INFO, "Missing client callsign");
127  if (callsign.isEmpty()) { return 0; }
128 
129  CClient client = this->getClientOrDefaultForCallsign(callsign);
130 
131  // need to change?
132  if (!client.isValid()) { return false; } // no client
133  if (client.hasGndFlagCapability() == supportGndFlag) { return true; } // already set, but set
134  if (supportGndFlag) { client.addCapability(CClient::FsdWithGroundFlag); }
135  else { client.removeCapability(CClient::FsdWithGroundFlag); }
136  QWriteLocker l(&m_lockClient);
137  m_clients[callsign] = client;
138  return true;
139  }
140 
141  void CClientProvider::markAsSwiftClient(const CCallsign &callsign)
142  {
143  if (callsign.isEmpty()) { return; }
144  QWriteLocker l(&m_lockClient);
145  if (!m_clients.contains(callsign)) { return; }
146  m_clients[callsign].setSwiftClient(true);
147  }
148 
149  // Pin the vtable to this file
150  void CClientAware::anchor() {}
151 
152  CClientList CClientAware::getClients() const
153  {
154  if (this->provider()) { return this->provider()->getClients(); }
155  return CClientList();
156  }
157 
158  CClient CClientAware::getClientOrDefaultForCallsign(const aviation::CCallsign &callsign) const
159  {
160  if (this->provider()) { return this->provider()->getClientOrDefaultForCallsign(callsign); }
161  return CClient();
162  }
163 
164  bool CClientAware::hasClientInfo(const CCallsign &callsign) const
165  {
166  if (this->provider()) { return this->provider()->hasClientInfo(callsign); }
167  return false;
168  }
169 
170  bool CClientAware::addNewClient(const CClient &client)
171  {
172  if (this->provider()) { return this->provider()->addNewClient(client); }
173  return false;
174  }
175 
176  int CClientAware::updateOrAddClient(const CCallsign &callsign, const CPropertyIndexVariantMap &vm,
177  bool skipEqualValues)
178  {
179  if (this->provider()) { return this->provider()->updateOrAddClient(callsign, vm, skipEqualValues); }
180  return 0;
181  }
182 
183  int CClientAware::removeClient(const CCallsign &callsign)
184  {
185  if (this->provider()) { return this->provider()->removeClient(callsign); }
186  return 0;
187  }
188 
189  bool CClientAware::autoAdjustCientGndCapability(const CAircraftSituation &situation)
190  {
191  if (this->provider()) { return this->provider()->autoAdjustCientGndCapability(situation); }
192  return false;
193  }
194 
195  bool CClientAware::addClientGndCapability(const CCallsign &callsign)
196  {
197  if (this->provider()) { return this->provider()->addClientGndCapability(callsign); }
198  return false;
199  }
200 
201  void CClientAware::markAsSwiftClient(const CCallsign &callsign)
202  {
203  if (!this->provider()) { return; }
204  this->provider()->markAsSwiftClient(callsign);
205  }
206 
207  CClientProviderDummy *CClientProviderDummy::instance()
208  {
209  static CClientProviderDummy *dummy = new CClientProviderDummy();
210  return dummy;
211  }
212 } // namespace swift::misc::network
bool isEmpty() const
Synonym for empty.
Definition: collection.h:191
Specialized value object compliant map for variants, based on indexes.
size_type size() const
Returns number of elements in the sequence.
Definition: sequence.h:273
Value object encapsulating information of an aircraft's situation.
aviation::COnGroundInfo getOnGroundInfo() const
On ground info.
const CCallsign & getCallsign() const
Corresponding callsign.
Value object encapsulating information of a callsign.
Definition: callsign.h:30
bool isEmpty() const
Is empty?
Definition: callsign.h:63
Value object for a set of callsigns.
Definition: callsignset.h:26
@ InFromNetwork
received from network
Definition: ongroundinfo.h:38
OnGroundDetails getGroundDetails() const
Get ground details.
CONTAINER findByCallsigns(const CCallsignSet &callsigns) const
Find 0..n aircraft matching any of a set of callsigns.
QHash< CCallsign, OBJ > asCallsignHash() const
Turn into callsign hash.
CPropertyIndexList apply(const CPropertyIndexVariantMap &indexMap, bool skipEqualValues=false)
Update by variant map.
Another client software.
Definition: client.h:27
void addCapability(Capability capability)
Add capability.
Definition: client.cpp:33
bool hasGndFlagCapability() const
Supports gnd.flag?
Definition: client.cpp:68
bool isValid() const
Is valid.
Definition: client.cpp:31
void removeCapability(Capability capability)
Remove capability.
Definition: client.cpp:40
const aviation::CCallsign & getCallsign() const
Callsign used with other client.
Definition: client.h:68
Value object encapsulating a list of voice rooms.
Definition: clientlist.h:27
Client provider dummy for testing.
#define SWIFT_VERIFY_X(COND, WHERE, WHAT)
A weaker kind of assert.
Definition: verify.h:26