ArnLib  4.0.x
Active Registry Network
ArnDiscover.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 ARNDISCOVER_HPP
33 #define ARNDISCOVER_HPP
34 
35 #include "XStringMap.hpp"
36 #include "MQFlags.hpp"
37 #include <QHostAddress>
38 #include <QVariantMap>
39 
40 class ArnDiscoverInfoPrivate;
41 class ArnDiscoverBrowserBPrivate;
42 class ArnDiscoverResolverPrivate;
43 class ArnDiscoverAdvertisePrivate;
45 class ArnZeroConfBrowser;
46 class QHostInfo;
47 
48 
49 namespace ArnDiscover
50 {
52 struct Type {
53  enum E {
60  };
62 };
63 }
64 
65 
67 
73 {
74  Q_DECLARE_PRIVATE(ArnDiscoverInfo)
75  friend class ArnDiscoverBrowserB;
76 
77 public:
79  struct State {
80  enum E {
92  HostIp
93  };
95  };
96 
98  ArnDiscoverInfo( const ArnDiscoverInfo& other);
101 
103 
106  bool inProgress() const;
107 
109 
112  bool isError() const;
113 
115 
118  State state() const;
119 
121 
127  State stopState() const;
128 
130 
134  ArnDiscover::Type type() const;
135 
137 
142  QStringList groups() const;
143 
145 
149  QString serviceName() const;
150 
152 
154  QString domain() const;
155 
157 
160  QString hostName() const;
161 
163 
166  quint16 hostPort() const;
167 
169 
171  QHostAddress hostIp() const;
172 
174 
180  Arn::XStringMap properties() const;
181 
183 
185  QString typeString() const;
186 
188 
191  QString hostPortString() const;
192 
194 
197  QString hostIpString() const;
198 
200 
204  QString hostWithInfo() const;
205 
207 
211  int resolvCode() const;
212 
214 protected:
215  ArnDiscoverInfo( ArnDiscoverInfoPrivate& dd);
216  ArnDiscoverInfoPrivate* const d_ptr;
218 
219 private:
220 };
221 
222 
224 class ArnDiscoverBrowserB : public QObject
225 {
226  Q_OBJECT
227  Q_DECLARE_PRIVATE(ArnDiscoverBrowserB)
228 
229 public:
230  explicit ArnDiscoverBrowserB( QObject *parent = arnNullptr);
232 
234 
236  int serviceCount() const;
237 
239 
248  const ArnDiscoverInfo& infoByIndex( int index);
249 
251 
258  const ArnDiscoverInfo& infoById( int id);
259 
261 
269  const ArnDiscoverInfo& infoByName( const QString& serviceName);
270 
272 
279  int indexToId( int index);
280 
282 
289  int IdToIndex( int id);
290 
292 
299  int serviceNameToId( const QString& name);
300 
302 
310 
312 
320 
322 
331  bool goTowardState( int index, ArnDiscoverInfo::State state);
332 
333 signals:
335 
342  void serviceAdded( int index, const QString& name);
343 
345 
348  void serviceRemoved( int index);
349 
351 
356  void infoUpdated( int index, ArnDiscoverInfo::State state);
357 
359 protected:
361 
364  bool isBrowsing() const;
365 
367 
372  void setFilter( ArnDiscover::Type typeFilter);
373 
375 
381  void setFilter( const QString& group);
382 
384 
389  void browse( bool enable = true);
390 
392 
394  void stopBrowse();
395 
397 
408  int resolve( const QString& serviceName, bool forceUpdate = true);
409 
410  ArnDiscoverBrowserB( ArnDiscoverBrowserBPrivate& dd, QObject* parent);
411  ArnDiscoverBrowserBPrivate* const d_ptr;
413 
414 private slots:
415  void onBrowseError( int code);
416  void onServiceAdded( int id, const QString& name, const QString& domain);
417  void onServiceRemoved( int id, const QString& name, const QString& domain);
418 
419  void onResolveError( int id, int code);
420  void onResolved( int id, const QByteArray& escFullDomain);
421 
422  void onLookupError( int id, int code);
423  void onLookuped( int id);
424 
425 private:
426  void init();
427  int newServiceInfo( int id, const QString& name, const QString& domain);
428  void removeServiceInfo( int index);
429  void doNextState( ArnDiscoverInfo& info);
430  void doHostIpLogic( ArnDiscoverInfo& info);
431 };
432 
433 
435 
478 {
479  Q_OBJECT
480 public:
481  explicit ArnDiscoverBrowser( QObject *parent = arnNullptr);
482 
484 
487  bool isBrowsing() const
488  {return ArnDiscoverBrowserB::isBrowsing();}
489 
491 
496  void setFilter( ArnDiscover::Type typeFilter)
497  {ArnDiscoverBrowserB::setFilter( typeFilter);}
498 
500 
506  void setFilter( const QString& group)
507  {ArnDiscoverBrowserB::setFilter( group);}
508 
509 public slots:
511 
516  void browse( bool enable = true)
517  {ArnDiscoverBrowserB::browse( enable);}
518 
520 
522  void stopBrowse()
523  {ArnDiscoverBrowserB::stopBrowse();}
524 };
525 
526 
528 
557 {
558  Q_OBJECT
559  Q_DECLARE_PRIVATE(ArnDiscoverResolver)
560 
561 public:
562  explicit ArnDiscoverResolver( QObject *parent = arnNullptr);
563 
565 
571  QString defaultService() const;
572 
574 
580  void setDefaultService( const QString& defaultService);
581 
582 public slots:
584 
595  int resolve( const QString& serviceName, bool forceUpdate = true);
596 
598 protected:
599  ArnDiscoverResolver( ArnDiscoverResolverPrivate& dd, QObject* parent);
601 
602 private:
603 };
604 
605 
607 
629 class ArnDiscoverAdvertise : public QObject
630 {
631  Q_OBJECT
632  Q_DECLARE_PRIVATE(ArnDiscoverAdvertise)
633 
634 public:
637  struct State {
638  enum E {
640  None = 0x0000,
644  Advertising = 0x0001,
646  Advertise = 0x0101
647  };
649  };
650 
651  explicit ArnDiscoverAdvertise( QObject *parent = arnNullptr);
653 
655 
658  QStringList groups() const;
659 
661 
668  void setGroups( const QStringList& groups);
669 
671 
675  void addGroup( const QString& group);
676 
678 
685  QString service() const;
686 
688 
695  QString currentService() const;
696 
698 
701  State state() const;
702 
704 
717  void advertiseService( ArnDiscover::Type discoverType, const QString& serviceName,
718  int port = -1, const QString& hostName = QString());
719 
721 
727 
729 
741 
743 
750  void addCustomProperty( const QString& key, const QString& val);
751 
752 signals:
754 
758  void serviceChanged( const QString& serviceName);
759 
761 
764  void serviceChangeError( int code);
765 
766 public slots:
768 
784  virtual void setService( const QString& service);
785 
787 protected:
788  bool hasSetupAdvertise() const;
789  void setHostIpList( const QStringList& hostIpList);
790 
791  ArnDiscoverAdvertise( ArnDiscoverAdvertisePrivate& dd, QObject* parent);
792  ArnDiscoverAdvertisePrivate* const d_ptr;
793 
794 protected slots:
796 
800  virtual void postSetupThis();
801 
803 
807  virtual void serviceRegistered( const QString& serviceName);
808 
810 
814  virtual void serviceRegistrationError( int code);
816 
817 private:
818  void init();
819 };
820 
822 
823 #endif // ARNDISCOVER_HPP
void setGroups(const QStringList &groups)
Set service discover groups used for filter browsing.
QStringList groups() const
Return service discover groups used for filter browsing.
bool goTowardState(int index, ArnDiscoverInfo::State state)
Command a service to go towards a stop state.
Registering a ZeroConfig service.
ArnDiscoverInfo::State defaultStopState() const
Return the default stop state for this service discover browser.
QString hostName() const
Return the host name for this service.
ArnDiscoverResolver(QObject *parent=arnNullptr)
Initialized null state.
Definition: ArnDiscover.hpp:82
State of Arn discover browse data. Can be tested by relative order.
Definition: ArnDiscover.hpp:79
QStringList groups() const
Return the groups for this service.
QString hostIpString() const
Return the printable host ip-address for this service.
void advertiseService(ArnDiscover::Type discoverType, const QString &serviceName, int port=-1, const QString &hostName=QString())
Start advertising the service.
State state() const
Return the state for this service.
void serviceRemoved(int index)
Indicate service has been removed.
State stopState() const
Return the stop state for this service.
void serviceChangeError(int code)
Indicate unsuccessfull advertise of service.
Container class with string representation for serialized data.
Definition: XStringMap.hpp:107
int resolvCode() const
Return the latest resolv error code for this service.
Advertise an Arn service.
Browsing for ZeroConfig services.
const ArnDiscoverInfo & infoByIndex(int index)
Return the discover service info by its index.
#define MQ_DECLARE_FLAGS(FEStruct)
Flags.
Definition: MQFlagsBase.hpp:49
ArnDiscoverInfo & operator=(const ArnDiscoverInfo &other)
Definition: ArnDiscover.cpp:78
void setFilter(ArnDiscover::Type typeFilter)
Set service discover filter using predefined types.
void addGroup(const QString &group)
Add a service discover group.
Got service name and domain (from browsing)
Definition: ArnDiscover.hpp:84
void serviceAdded(int index, const QString &name)
Indicate service has been added (discovered)
int indexToId(int index)
Return the discover service id by its index.
int resolve(const QString &serviceName, bool forceUpdate=true)
Resolve a specific service name.
Got error during resolving HostName, HostPort, type and properties.
Definition: ArnDiscover.hpp:86
Client Arn discover.
Definition: ArnDiscover.hpp:59
QString service() const
Returns the requested service name for this Advertise.
ArnDiscoverBrowserB(QObject *parent=arnNullptr)
QHostAddress hostIp() const
Return the host ip-address for this service.
void setFilter(const QString &group)
Set service discover filter using group name.
bool inProgress() const
Is discover in progress for this service.
Definition: ArnDiscover.cpp:93
Types of Arn discover advertise.
Definition: ArnDiscover.hpp:52
QString hostWithInfo() const
Get the the HostWithInfo string.
Is now advertising. Startup has finished sucessfully.
Arn::XStringMap customProperties() const
Return service custom properties.
QString currentService() const
Returns the current service name for this Advertise.
QString hostPortString() const
Return the printable host port for this service.
#define MQ_DECLARE_OPERATORS_FOR_FLAGS(FEStruct)
Definition: MQFlagsBase.hpp:65
QString serviceName() const
Return the service name for this service.
int IdToIndex(int id)
Return the discover service index by its id.
void stopBrowse()
Stop browsing.
int serviceNameToId(const QString &name)
Return the discover service id by its name.
void addCustomProperty(const QString &key, const QString &val)
Add service custom property.
States of DiscoverAdvertise / These values must be synced with: ArnZeroConf::State.
Arn::XStringMap properties() const
Return the properties for this service.
Got error during DNS lookup HostIp.
Definition: ArnDiscover.hpp:90
void setDefaultService(const QString &defaultService)
Set the default service name.
void setCustomProperties(const Arn::XStringMap &customProperties)
Set service custom properties.
ArnDiscoverBrowser(QObject *parent=arnNullptr)
void setDefaultStopState(ArnDiscoverInfo::State defaultStopState)
Set the default stop state for this service discover browser.
Also got HostName, HostPort, type and properties (from resolving)
Definition: ArnDiscover.hpp:88
Server Arn discover.
Definition: ArnDiscover.hpp:57
Browse() and resolve() together, may never be used to the same instance.
QString typeString() const
Return the printable type for this service.
void serviceChanged(const QString &serviceName)
Indicate successfull advertise of service.
void browse(bool enable=true)
Change state of browsing.
bool isError() const
Is in an error state for this service.
Browsing for Arn services.
Class for holding current discover info of one service.
Definition: ArnDiscover.hpp:72
Resolv an Arn service.
QString defaultService() const
Return the default service name.
void infoUpdated(int index, ArnDiscoverInfo::State state)
Indicate service has been updated.
State state() const
Returns the state for this Advertise.
const ArnDiscoverInfo & infoByName(const QString &serviceName)
Return the discover service info by its name.
quint16 hostPort() const
Return the port for this service.
bool isBrowsing() const
Return the status of the browsing.
int serviceCount() const
Return the number of active discover services.
isAny(): Startup advertising in progress or has finished sucessfully.
QString domain() const
Return the domain for this service.
#define MQ_DECLARE_ENUM(EStruct)
Enums.
Definition: MQFlagsBase.hpp:70
ArnDiscover::Type type() const
Return the discover type for this service.
Startup advertising in progress.
const ArnDiscoverInfo & infoById(int id)
Return the discover service info by its id.
ArnDiscoverAdvertise(QObject *parent=arnNullptr)
Undefined Arn discover.
Definition: ArnDiscover.hpp:55
virtual void setService(const QString &service)
Set the service name.