ArnLib  4.0.x
Active Registry Network
ArnSync.hpp
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 #ifndef ARNSYNC_HPP
33 #define ARNSYNC_HPP
34 
35 #include "ArnInc/ArnLib_global.hpp"
36 #include "ArnInc/ArnClient.hpp"
37 #include "ArnInc/XStringMap.hpp"
38 #include "ArnItemNet.hpp"
39 #include "ArnInc/MQFlags.hpp"
40 #include <QTimer>
41 #include <QByteArray>
42 #include <QMap>
43 #include <QQueue>
44 
45 #define ARNRECNAME ""
46 
47 class QTcpSocket;
48 class ArnSyncLogin;
49 
50 
52 class ArnSync : public QObject
53 {
54  Q_OBJECT
55 
56 public:
57  struct State {
58  enum E {
60  Init = 0,
62  Version,
64  Info,
66  Login,
68  Normal
69  };
70  MQ_DECLARE_ENUM( State)
71  };
72 
75  struct InfoType {
76  enum E {
77  Start = 1000, // Start marker
79  FreePaths = 1001,
81  WhoIAm = 1002,
82  End // End marker
83  };
84  MQ_DECLARE_ENUM( InfoType)
85  };
86 
89  struct MessageType {
90  enum E {
91  KillRequest = 1001,
92  AbortKillRequest = 1002,
93  ChatNormal = 1008,
94  ChatPrio = 1009,
95  End // End marker
96  };
97  MQ_DECLARE_ENUM( MessageType)
98  };
99 
100  typedef QString (*ConVertPathCB)(void* context, const QString& path);
101 
102  ArnSync( QTcpSocket* socket, bool clientSide, QObject *parent);
103  ~ArnSync();
104 
105  void setArnLogin( ArnSyncLogin* arnLogin);
106  void start();
107  bool isDemandLogin() const;
108  void setDemandLogin( bool isDemandLogin);
109  void addFreePath( const QString& path);
110  QStringList freePaths() const;
111 
112  ArnItemNet* newNetItem( const QString& path,
114  bool* isNewPtr = arnNullptr);
115  void connectStarted();
116  void connected();
117  void close();
118  void sendXSMap( const Arn::XStringMap& xsMap);
119  void send( const QByteArray& xString);
120  void sendNoSync( const QString& path);
121  void sendSetTree( const QString& path);
122  void sendDelete( const QString& path);
123  void sendInfo( int type, const QByteArray& data = QByteArray());
124  void sendMessage( int type, const QByteArray& data = QByteArray());
125  void sendExit();
126  uint remoteVer( uint index);
127  void loginToArn( const QString& userName, const QString& passwordHash,
128  Arn::Allow allow = Arn::Allow::All);
129  void loginToArn();
130 
131  static void setupMonitorItem( ArnItemNet* itemNet);
132  static void doChildsToEvent( ArnItemNet* itemNet);
133 
134  void setClientSyncMode( Arn::ClientSyncMode clientSyncMode);
135  void setSessionHandler( void* sessionHandler);
136  void setToRemotePathCB( ConVertPathCB toRemotePathCB);
137  static QString nullConvertPath( void* context, const QString& path);
138  void setWhoIAm( const QByteArray& whoIAm);
139  QByteArray remoteWhoIAm() const;
140  QString loginUserName() const;
141  Arn::Allow getAllow() const;
142  void getTraffic( quint64& in, quint64& out) const;
143 
144 signals:
145  void replyRecord( Arn::XStringMap& replyMap);
146  void xcomDelete( const QString& path);
147  void stateChanged( int state);
148  void infoReceived( int type);
149  void messageReceived( int type, const QByteArray& data);
151  void loginRequired( int contextCode);
152  void loginCompleted();
153 
154 protected:
155  virtual void customEvent( QEvent* ev);
156 
157 private slots:
158  void disConnected();
159  void socketInput();
160  void doLoginSeq0End();
161  void sendNext();
162  void doArnMonEvent( int type, const QByteArray& data, bool isLocal, ArnItemNet* itemNet);
163 
164 private:
165  struct FluxRec {
166  QByteArray xString;
167  int queueNum;
168  };
169 
170  void doInfoInternal( int infoType, const QByteArray& data = QByteArray());
171  void startLogin();
172  void startNormalSync();
173  void setupItemNet( ArnItemNet* itemNet, uint netId);
174  void itemValueUpdater( const ArnLinkHandle& handleData, const QByteArray* valueData,
175  ArnItemNet* itemNet);
176  void itemModeUpdater( ArnItemNet* itemNet);
177  FluxRec* getFreeFluxRec();
178  QByteArray makeFluxString( const ArnItemNet* itemNet, const ArnLinkHandle& handleData,
179  const QByteArray* valueData);
180  void addToFluxQue( const ArnLinkHandle& handleData, const QByteArray* valueData,
181  ArnItemNet* itemNet);
182  void addToModeQue( ArnItemNet* itemNet);
183  void sendFluxItem( const ArnItemNet* itemNet);
184  void sendSyncItem( ArnItemNet* itemNet);
185  void sendModeItem( ArnItemNet* itemNet);
186  void sendLogin( int seq, const Arn::XStringMap& xsMap);
187  void eventToFluxQue( uint netId, int type, const QByteArray& data);
188  void atomicOpToFluxQue( int op, const QVariant& arg1, const QVariant& arg2, const ArnItemNet* itemNet);
189  void destroyToFluxQue( ArnItemNet* itemNet);
190  void removeItemNetRefs( ArnItemNet* itemNet);
191  void closeFinal();
192  void clearNonPipeQueues();
193  void clearAllQueues();
194  void setRemoteVer( const QByteArray& remVer);
195  void setState( State state);
196  bool isFreePath( const QString& path) const;
197 
198  void doCommands();
199  uint doCommandSync();
200  uint doCommandMode();
201  uint doCommandNoSync();
202  uint doCommandFlux();
203  uint doCommandAtomOp();
204  uint doCommandEvent();
205  uint doCommandSet();
206  uint doCommandGet();
207  uint doCommandLs();
208  uint doCommandDelete();
209  uint doCommandMessage();
210  uint doCommandInfo();
211  uint doCommandRInfo();
212  uint doCommandVer();
213  uint doCommandRVer();
214  uint doCommandLogin();
215 
216  QTcpSocket* _socket;
217  ArnSyncLogin* _arnLogin;
218  void* _sessionHandler;
219  ConVertPathCB _toRemotePathCB;
220 
221  QByteArray _dataReadBuf;
222  QByteArray _dataRemain;
223  Arn::XStringMap _commandMap;
224  Arn::XStringMap _replyMap;
225  Arn::XStringMap _syncMap;
226  Arn::XStringMap _customMap;
227  QStringList _freePathTab;
228  QByteArray _whoIAm;
229  QByteArray _remoteWhoIAm;
230 
231  QMap<uint,ArnItemNet*> _itemNetMap;
232 
233  QList<FluxRec*> _fluxRecPool;
234  QQueue<FluxRec*> _fluxPipeQueue;
235  QQueue<ArnItemNet*> _fluxItemQueue;
236 
237  QQueue<ArnItemNet*> _syncQueue;
238  QQueue<ArnItemNet*> _modeQueue;
239 
240  State _state;
241  InfoType _curInfoType;
242  int _queueNumCount;
243  int _queueNumDone;
244  bool _isConnectStarted;
245  bool _isConnected;
246  bool _isSending;
247  bool _isClosed;
248  bool _isClientSide; // True if this is the client side of the connection
249  bool _isDemandLogin;
250  uint _remoteVer[2];
251  int _loginNextSeq;
252  int _loginReqCode;
253  uint _loginSalt1;
254  uint _loginSalt2;
255  quint64 _trafficIn;
256  quint64 _trafficOut;
257  QString _loginUserName;
258  QString _loginPwHash;
259  QTimer _loginDelayTimer;
260  Arn::Allow _allow;
261  Arn::Allow _remoteAllow;
262  Arn::ClientSyncMode _clientSyncMode;
263 };
265 
266 #endif // ARNSYNC_HPP
Container class with string representation for serialized data.
Definition: XStringMap.hpp:107
The Client session Sync mode at connect & reconnect.
Definition: Arn.hpp:155
Convenience, allow all.
Definition: Arn.hpp:226
#define MQ_DECLARE_ENUM(EStruct)
Enums.
Definition: MQFlagsBase.hpp:70