swift
Loading...
Searching...
No Matches
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
9using namespace swift::misc::aviation;
10
11namespace swift::misc::network
12{
14 {
15 QList<CClient> clients;
16 {
17 QReadLocker l(&m_lockClient);
18 clients = m_clients.values();
19 }
20 return { clients };
21 }
22
24 {
25 const CClientPerCallsign perCallsign(clients.asCallsignHash());
26 QWriteLocker l(&m_lockClient);
27 m_clients = perCallsign;
28 }
29
31 {
32 QWriteLocker l(&m_lockClient);
33 m_clients.clear();
34 }
35
37 {
38 if (callsigns.isEmpty())
39 {
40 return {};
41 }
42 return this->getClients().findByCallsigns(callsigns);
43 }
44
46 {
47 if (callsign.isEmpty())
48 {
49 return {};
50 }
51 {
52 QReadLocker l(&m_lockClient);
53 if (m_clients.contains(callsign))
54 {
55 return m_clients.value(callsign);
56 }
57 }
58 return {};
59 }
60
62 {
63 const bool hasCallsign = !client.getCallsign().isEmpty();
64 SWIFT_VERIFY_X(hasCallsign, Q_FUNC_INFO, "Need callsign in client");
65 if (!hasCallsign)
66 {
67 return false;
68 }
69 QWriteLocker l(&m_lockClient);
70 m_clients[client.getCallsign()] = client;
71 return true;
72 }
73
74 bool CClientProvider::hasClientInfo(const CCallsign &callsign) const
75 {
76 if (callsign.isEmpty())
77 {
78 return false;
79 }
80 QReadLocker l(&m_lockClient);
81 return m_clients.contains(callsign);
82 }
83
85 {
86 const CCallsign callsign = client.getCallsign();
87 SWIFT_VERIFY_X(!callsign.isEmpty(), Q_FUNC_INFO, "Missing client callsign");
88 if (callsign.isEmpty())
89 {
90 return false;
91 }
92 if (this->hasClientInfo(callsign))
93 {
94 return false;
95 }
96 QWriteLocker l(&m_lockClient);
97 m_clients[callsign] = client;
98 return true;
99 }
100
102 bool skipEqualValues)
103 {
104 SWIFT_VERIFY_X(!callsign.isEmpty(), Q_FUNC_INFO, "Missing client callsign");
105 if (callsign.isEmpty())
106 {
107 return 0;
108 }
109 int c = 0;
110 if (this->hasClientInfo(callsign))
111 {
112 QWriteLocker l(&m_lockClient);
113 CClient &client = m_clients[callsign];
114 c = client.apply(vm, skipEqualValues).size();
115 }
116 else
117 {
118 CClient client(callsign);
119 c = client.apply(vm).size();
120 QWriteLocker l(&m_lockClient);
121 m_clients[callsign] = client;
122 }
123 return c;
124 }
125
127 {
128 QWriteLocker l(&m_lockClient);
129 return m_clients.remove(callsign);
130 }
131
133 {
134 if (situation.getCallsign().isEmpty())
135 {
136 return false;
137 } // no callsign
138 if (!situation.isOnGround())
139 {
140 return false;
141 } // nothing to adjust
143 {
144 return false;
145 } // not from network
146 return this->addClientGndCapability(situation.getCallsign());
147 }
148
150 {
151 return this->setClientGndCapability(callsign, true);
152 }
153
154 bool CClientProvider::setClientGndCapability(const CCallsign &callsign, bool supportGndFlag)
155 {
156 SWIFT_VERIFY_X(!callsign.isEmpty(), Q_FUNC_INFO, "Missing client callsign");
157 if (callsign.isEmpty())
158 {
159 return false;
160 }
161
162 CClient client = this->getClientOrDefaultForCallsign(callsign);
163
164 // need to change?
165 if (!client.isValid())
166 {
167 return false;
168 } // no client
169 if (client.hasGndFlagCapability() == supportGndFlag)
170 {
171 return true;
172 } // already set, but set
173 if (supportGndFlag)
174 {
176 }
177 else
178 {
180 }
181 QWriteLocker l(&m_lockClient);
182 m_clients[callsign] = client;
183 return true;
184 }
185
187 {
188 if (callsign.isEmpty())
189 {
190 return;
191 }
192 QWriteLocker l(&m_lockClient);
193 if (!m_clients.contains(callsign))
194 {
195 return;
196 }
197 m_clients[callsign].setSwiftClient(true);
198 }
199
200 // Pin the vtable to this file
201 void CClientAware::anchor() {}
202
204 {
205 if (this->provider())
206 {
207 return this->provider()->getClients();
208 }
209 return {};
210 }
211
213 {
214 if (this->provider())
215 {
216 return this->provider()->getClientOrDefaultForCallsign(callsign);
217 }
218 return {};
219 }
220
221 bool CClientAware::hasClientInfo(const CCallsign &callsign) const
222 {
223 if (this->provider())
224 {
225 return this->provider()->hasClientInfo(callsign);
226 }
227 return false;
228 }
229
231 {
232 if (this->provider())
233 {
234 return this->provider()->addNewClient(client);
235 }
236 return false;
237 }
238
240 bool skipEqualValues)
241 {
242 if (this->provider())
243 {
244 return this->provider()->updateOrAddClient(callsign, vm, skipEqualValues);
245 }
246 return 0;
247 }
248
250 {
251 if (this->provider())
252 {
253 return this->provider()->removeClient(callsign);
254 }
255 return 0;
256 }
257
259 {
260 if (this->provider())
261 {
262 return this->provider()->autoAdjustCientGndCapability(situation);
263 }
264 return false;
265 }
266
268 {
269 if (this->provider())
270 {
271 return this->provider()->addClientGndCapability(callsign);
272 }
273 return false;
274 }
275
277 {
278 if (!this->provider())
279 {
280 return;
281 }
282 this->provider()->markAsSwiftClient(callsign);
283 }
284
286 {
287 static auto dummy = new CClientProviderDummy();
288 return dummy;
289 }
290} // namespace swift::misc::network
bool isEmpty() const
Synonym for empty.
Definition collection.h:197
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
OnGroundDetails getGroundDetails() const
Get ground details.
QHash< CCallsign, OBJ > asCallsignHash() const
Turn into callsign hash.
CONTAINER findByCallsigns(const CCallsignSet &callsigns) const
Find 0..n aircraft matching any of a set of callsigns.
CPropertyIndexList apply(const CPropertyIndexVariantMap &indexMap, bool skipEqualValues=false)
Update by variant map.
bool addClientGndCapability(const aviation::CCallsign &callsign)
Add gnd.flag capability.
int updateOrAddClient(const aviation::CCallsign &callsign, const CPropertyIndexVariantMap &vm, bool skipEqualValues)
Update or add a client.
bool hasClientInfo(const aviation::CCallsign &callsign) const
Client info for given callsign?
CClientList getClients() const
clientprovider
swift::misc::network::CClient getClientOrDefaultForCallsign(const aviation::CCallsign &callsign) const
Other client for the given callsigns.
bool autoAdjustCientGndCapability(const aviation::CAircraftSituation &situation)
Adjust gnd.flag capability from situation.
bool addNewClient(const CClient &client)
Add a new client, if existing nothing will be added.
void markAsSwiftClient(const aviation::CCallsign &callsign)
Mark as other swift client.
int removeClient(const aviation::CCallsign &callsign)
Remove client.
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:71
bool isValid() const
Is valid.
Definition client.cpp:31
void removeCapability(Capability capability)
Remove capability.
Definition client.cpp:40
@ FsdWithGroundFlag
supports gnd.
Definition client.h:52
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.
static CClientProviderDummy * instance()
Dummy instance.
int removeClient(const aviation::CCallsign &callsign)
Remove client.
int updateOrAddClient(const aviation::CCallsign &callsign, const CPropertyIndexVariantMap &vm, bool skipEqualValues=true)
Update or add a client.
bool hasClientInfo(const aviation::CCallsign &callsign) const
Client info for given callsign?
bool addClientGndCapability(const aviation::CCallsign &callsign)
Add gnd.flag capability.
CClientList getClientsForCallsigns(const aviation::CCallsignSet &callsigns) const
Returns a list of other clients corresponding to the given callsigns.
void markAsSwiftClient(const aviation::CCallsign &callsign)
Mark as other swift client.
bool setOtherClient(const swift::misc::network::CClient &client)
Set client for its callsign.
bool addNewClient(const CClient &client)
Add a new client, if existing nothing will be added.
void setClients(const CClientList &clients)
Set other clients.
CClientList getClients() const
clientprovider
void clearClients()
Set other clients.
CClient getClientOrDefaultForCallsign(const aviation::CCallsign &callsign) const
Other client for the given callsigns.
bool setClientGndCapability(const aviation::CCallsign &callsign, bool supportGndFlag)
Set gnd.flag capability.
bool autoAdjustCientGndCapability(const aviation::CAircraftSituation &situation)
Adjust gnd.flag capability from situation.
QHash< aviation::CCallsign, CClient > CClientPerCallsign
Client per callsign.
Definition clientlist.h:40
#define SWIFT_VERIFY_X(COND, WHERE, WHAT)
A weaker kind of assert.
Definition verify.h:26