ArnLib  4.0.x
Active Registry Network
ArnItemNet.cpp
Go to the documentation of this file.
1 // Copyright (C) 2010-2022 Michael Wiklund.
2 // All rights reserved.
3 // Contact: arnlib@wiklunden.se
4 //
5 // This file is part of the ArnLib - Active Registry Network.
6 // Parts of ArnLib depend on Qt and/or other libraries that have their own
7 // licenses. Usage of these other libraries is subject to their respective
8 // license agreements.
9 //
10 // GNU Lesser General Public License Usage
11 // This file may be used under the terms of the GNU Lesser General Public
12 // License version 2.1 as published by the Free Software Foundation and
13 // appearing in the file LICENSE_LGPL.txt included in the packaging of this
14 // file. In addition, as a special exception, you may use the rights described
15 // in the Nokia Qt LGPL Exception version 1.1, included in the file
16 // LGPL_EXCEPTION.txt in this package.
17 //
18 // GNU General Public License Usage
19 // Alternatively, this file may be used under the terms of the GNU General Public
20 // License version 3.0 as published by the Free Software Foundation and appearing
21 // in the file LICENSE_GPL.txt included in the packaging of this file.
22 //
23 // Other Usage
24 // Alternatively, this file may be used in accordance with the terms and conditions
25 // contained in a signed written agreement between you and Michael Wiklund.
26 //
27 // This program is distributed in the hope that it will be useful, but WITHOUT ANY
28 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
29 // PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
30 //
31 
32 #include "ArnItemNet.hpp"
33 #include "ArnLink.hpp"
34 #include "ArnInc/ArnMonEvent.hpp"
35 #include "ArnInc/ArnEvent.hpp"
36 #include "ArnInc/XStringMap.hpp"
37 #include "ArnInc/ArnLib.hpp"
38 #include <QCoreApplication>
39 #include <QThread>
40 #include <QDebug>
41 
42 
43 void ArnItemNet::init()
44 {
45  _netId = 0;
46  _dirty = false;
47  _dirtyMode = false;
48  _disable = false;
49  _isMonitor = false;
50  _blockEcho = false;
51  _iniMaster = false;
52  _iniSlave = false;
53  _syncFlux = false;
54  _saveFlux = false;
55  _curEchoSeq = -1;
56  _updateCountStop = 0;
57 
58  setUncrossed();
59  setIgnoreSameValue( false);
60 }
61 
62 
63 ArnItemNet::ArnItemNet( void* sessionHandler)
64 {
65  init();
66  _sessionHandler = sessionHandler;
67 }
68 
69 
70 ArnItemNet::~ArnItemNet()
71 {
72 #if 0
73  if (path() == "/") {
74  qDebug() << "ArnItemNet destruct root: eventH=" << eventHandler() << " sessionH=" << _sessionHandler
75  << " isMon=" << _isMonitor << " netId=" << _netId
76  << " itemNet=" << this;
77  }
78 #endif
79 }
80 
81 
82 bool ArnItemNet::openWithFlags( const QString& path, Arn::LinkFlags linkFlags)
83 {
84 #if 0
85  if (path == "/") {
86  qDebug() << "ArnItemNet open root: eventH=" << eventHandler() << " sessionH=" << _sessionHandler
87  << " isMon=" << _isMonitor << " netId=" << _netId
88  << " itemNet=" << this;
89  }
90 #endif
91 
92  return ArnBasicItem::openWithFlags( path, linkFlags);
93 }
94 
95 
96 void ArnItemNet::setNetId( uint netId)
97 {
98  _netId = netId;
99 }
100 
101 
102 uint ArnItemNet::netId() const
103 {
104  return _netId;
105 }
106 
107 
108 void* ArnItemNet::sessionHandler() const
109 {
110  return _sessionHandler;
111 }
112 
113 
114 void ArnItemNet::addSyncModeString( const QByteArray& smode, bool linkShare)
115 {
116  Arn::ObjectSyncMode syncMode;
117 
118  setIniMaster( smode.contains("ima"));
119  setIniSlave( smode.contains("isl"));
120  bool isMaster = smode.contains("mas");
121  isMaster |= _iniSlave; // Implicitly is Master when iniSlave
122  syncMode.set( syncMode.Master, isMaster);
123  syncMode.set( syncMode.AutoDestroy, smode.contains("autodestroy"));
124  syncMode.set( syncMode.Monitor, smode.contains("mon"));
125 
126  addSyncMode( syncMode, linkShare);
127 }
128 
129 
130 QByteArray ArnItemNet::getSyncModeString() const
131 {
132  QByteArray smode;
134 
135  if (_iniSlave) smode += "isl ";
136  else if (syncMode.is( syncMode.Master)) smode += "master "; // Can be "mas" for remoteVer >= 3.0
137  else if (_iniMaster) smode += "ima ";
138 
139  if (syncMode.is( syncMode.AutoDestroy)) smode += "autodestroy ";
140  if (syncMode.is( syncMode.Monitor)) smode += "mon ";
141 
142  return smode.trimmed();
143 }
144 
145 
146 Arn::ObjectMode ArnItemNet::stringToObjectMode( const QByteArray& modeString)
147 {
148  Arn::ObjectMode mode;
149  if (modeString.contains('P')) mode.set( mode.Pipe);
150  if (modeString.contains('V')) mode.set( mode.BiDir); // Legacy
151  if (modeString.contains('B')) mode.set( mode.BiDir);
152  if (modeString.contains('S')) mode.set( mode.Save);
153 
154  return mode;
155 }
156 
157 
158 QByteArray ArnItemNet::ObjectModeToString( Arn::ObjectMode mode)
159 {
160  QByteArray modeString;
161  if (mode.is( mode.Pipe)) modeString += "P";
162  if (mode.is( mode.BiDir)) modeString += "B";
163  if (mode.is( mode.Save)) modeString += "S";
164 
165  return modeString;
166 }
167 
168 
169 void ArnItemNet::setModeString( const QByteArray& modeString)
170 {
171  Arn::ObjectMode mode = stringToObjectMode( modeString);
172  addMode( mode);
173 }
174 
175 
176 QByteArray ArnItemNet::getModeString() const
177 {
178  Arn::ObjectMode mode = getMode();
179  return ObjectModeToString( mode);
180 }
181 
182 
184 void ArnItemNet::sendNewItemMonEvent( const QString& path, bool isOld)
185 {
187  sendMonEvent( type, path.toUtf8(), true);
188 }
189 
190 
192 void ArnItemNet::sendMonEvent( int type, const QByteArray& data, bool isLocal)
193 {
194  ArnEvMonitor ev( type, data, isLocal, _sessionHandler);
195  sendArnEventLink( &ev);
196 }
197 
198 
199 void ArnItemNet::setBlockEcho( bool blockEcho)
200 {
201  _blockEcho = blockEcho;
202 }
203 
204 
205 void ArnItemNet::setDisable( bool disable)
206 {
207  _disable = disable;
208 }
209 
210 
211 bool ArnItemNet::isDisable() const
212 {
213  return _disable;
214 }
215 
216 
217 bool ArnItemNet::isMonitor() const
218 {
219  return _isMonitor;
220 }
221 
222 
223 void ArnItemNet::setMonitor( bool isMonitor)
224 {
225  _isMonitor = isMonitor;
226 }
227 
228 
229 void ArnItemNet::setQueueNum( int num)
230 {
231  _queueNum = num;
232 }
233 
234 
235 int ArnItemNet::queueNum() const
236 {
237  return _queueNum;
238 }
239 
240 
241 void ArnItemNet::nextEchoSeq()
242 {
243  _curEchoSeq = (_curEchoSeq + 1) % 100;
244 }
245 
246 
247 void ArnItemNet::resetEchoSeq()
248 {
249  _curEchoSeq = -1;
250 }
251 
252 
253 void ArnItemNet::setEchoSeq( qint8 echoSeq)
254 {
255  _curEchoSeq = echoSeq;
256 }
257 
258 
259 qint8 ArnItemNet::echoSeq() const
260 {
261  return isBiDirMode() ? -1 : _curEchoSeq;
262 }
263 
264 
265 bool ArnItemNet::isEchoSeqOld( qint8 receivedEchoSeq)
266 {
267  if ((_curEchoSeq < 0) || (receivedEchoSeq < 0)) return false;
268  return receivedEchoSeq != _curEchoSeq;
269 }
270 
271 
272 void ArnItemNet::resetDirtyValue()
273 {
274  _dirty = false;
275  _iniMaster = false;
276  _iniSlave = false;
277  _syncFlux = false;
278  _saveFlux = false;
279  resetOnlyEcho();
280 }
281 
282 
283 void ArnItemNet::resetDirtyMode()
284 {
285  _dirtyMode = false;
286 }
287 
288 
289 bool ArnItemNet::isDirtyValue() const
290 {
291  return _dirty;
292 }
293 
294 
295 bool ArnItemNet::isDirtyMode() const
296 {
297  return _dirtyMode;
298 }
299 
300 
301 bool ArnItemNet::isLeadValueUpdate()
302 {
303  if (_dirty) return false;
304 
305  _dirty = true;
306  return true;
307 }
308 
309 
310 bool ArnItemNet::isLeadModeUpdate()
311 {
312  if (_dirtyMode) return false;
313 
314  _dirtyMode = true;
315  return true;
316 }
317 
318 
319 bool ArnItemNet::isBlock( quint32 sendId)
320 {
321  return (_blockEcho && (sendId == itemId())); // Update was initiated from this Item, it can be blocked ...
322 }
323 
324 
325 void ArnItemNet::setIniMaster( bool iniMaster)
326 {
327  _iniMaster = iniMaster;
328  _iniSlave &= !iniMaster;
329 }
330 
331 
332 void ArnItemNet::setIniSlave( bool iniSlave)
333 {
334  _iniSlave = iniSlave;
335  _iniMaster &= !iniSlave;
336 }
337 
338 
339 bool ArnItemNet::isMasterAtStart() const
340 {
341  return !_iniSlave && (_iniMaster || isMaster());
342 }
343 
344 
345 void ArnItemNet::setSyncFlux( bool isSyncFlux)
346 {
347  _syncFlux = isSyncFlux;
348 }
349 
350 
351 bool ArnItemNet::isSyncFlux() const
352 {
353  return _syncFlux;
354 }
355 
356 
357 void ArnItemNet::setSaveFlux( bool saveFlux)
358 {
359  _saveFlux = saveFlux;
360 }
361 
362 
363 bool ArnItemNet::isSaveFlux() const
364 {
365  return _saveFlux;
366 }
367 
368 
369 quint32 ArnItemNet::localUpdateSinceStop() const
370 {
371  return localUpdateCount() - _updateCountStop;
372 }
373 
374 
375 void ArnItemNet::onConnectStop()
376 {
377  _updateCountStop = localUpdateCount();
378 }
379 
380 
381 void ArnItemNet::arnEvent( QEvent* ev, bool isAlienThread)
382 {
383  ArnBasicItem::arnEvent( ev, isAlienThread);
384  if (ArnEvent::isArnEvent( ev->type())) {
385  ArnEvent* e = static_cast<ArnEvent*>( ev);
386  if (!e->target()) return; // This ArnItemNet has been deleted
387  }
388 
391  if (!_isMonitor) return;
392 
393  int evIdx = ev->type() - ArnEvent::baseType();
394  switch (evIdx) {
396  {
397  ArnEvLinkCreate* e = static_cast<ArnEvLinkCreate*>( ev);
398  if (e->isLastLink()) {
399  // qDebug() << "ArnItemNet Mon create: path=" << e->path() << " inPath=" << path();
400  sendNewItemMonEvent( e->path());
401  }
402  break;
403  }
405  {
406  ArnEvModeChange* e = static_cast<ArnEvModeChange*>( ev);
407  Arn::XStringMap xsm;
408  xsm.add("path", e->path()).add("mode", ObjectModeToString( e->mode()));
409  // qDebug() << "ArnItemNet Mode change: data=" << xsm.toXString() << " inPath=" << path();
410  sendMonEvent( ArnMonEventType::ItemModeChg, xsm.toXString(), true);
411  break;
412  }
413  default:;
414  }
415 }
416 
417 
418 
419 ArnItemNetEar::ArnItemNetEar( QObject* parent)
420  : ArnItem( parent)
421 {
422 }
423 
424 
425 void ArnItemNetEar::customEvent( QEvent* ev)
426 {
427  int evIdx = ev->type() - ArnEvent::baseType();
428  switch (evIdx) {
430  {
431  ArnEvLinkCreate* e = static_cast<ArnEvLinkCreate*>( ev);
432  if (e->isLastLink() && e->arnLink()->isFolder()) {
433  // qDebug() << "ArnItemNetEar create tree: path=" << e->path();
434  emit arnTreeCreated( e->path());
435  }
436  break;
437  }
439  {
440  ArnEvRetired* e = static_cast<ArnEvRetired*>( ev);
441  QString destroyPath = e->isBelow() ? e->startLink()->linkPath() : path();
442  if (Arn::debugLinkDestroy) qDebug() << "ArnItemNetEar retired: path=" << destroyPath
443  << " isGlobal=" << e->isGlobal()
444  << "inPath=" << path();
445  if (e->startLink()->isFolder()) {
446  emit arnTreeDestroyed( destroyPath, e->isGlobal());
447  }
448  break;
449  }
450  default:;
451  }
452 
453  return ArnItem::customEvent( ev);
454 }
static int baseType(int setVal=-1)
Definition: ArnEvent.cpp:62
Monitor of server object for client.
Definition: Arn.hpp:145
Arn::ObjectSyncMode syncMode() const
Newly created Arn object.
Definition: ArnMonEvent.hpp:47
bool debugLinkDestroy
Definition: ArnLib.cpp:40
Implies BiDir and all data is preserved as a stream.
Definition: Arn.hpp:128
Container class with string representation for serialized data.
Definition: XStringMap.hpp:107
Arn::DataType type() const
The type stored in the Arn Data Object
Definition: ArnItem.hpp:135
const QString & path() const
Definition: ArnEvent.hpp:162
bool isGlobal() const
Definition: ArnEvent.hpp:216
An Arn object changed mode.
Definition: ArnMonEvent.hpp:53
Arn::ObjectMode mode() const
Definition: ArnEvent.hpp:168
A two way object, typically for validation or pipe.
Definition: Arn.hpp:126
static bool isArnEvent(int evType)
Definition: ArnEvent.cpp:89
ArnLink * startLink() const
Definition: ArnEvent.hpp:210
QByteArray toXString() const
Destroy this Arn Data Object when client (tcp/ip) closes.
Definition: Arn.hpp:149
bool isBelow() const
Definition: ArnEvent.hpp:213
XStringMap & add(const char *key, const QByteArray &val)
Found a present Arn object.
Definition: ArnMonEvent.hpp:49
Data is persistent and will be saved.
Definition: Arn.hpp:130
void * target() const
Definition: ArnEvent.hpp:116
Handle for an Arn Data Object.
Definition: ArnItem.hpp:72
The client is default generator of data.
Definition: Arn.hpp:147