diff options
author | Javier S. Pedro <maemo@javispedro.com> | 2012-09-17 23:03:03 +0200 |
---|---|---|
committer | Javier S. Pedro <maemo@javispedro.com> | 2012-09-17 23:03:03 +0200 |
commit | c3a1946675855b299a2b36550cdf2c2f69d153aa (patch) | |
tree | f7adf66404cdc47994225b616bcaf082a07dd168 /distfoldd/discoverer.h | |
download | distfold-c3a1946675855b299a2b36550cdf2c2f69d153aa.tar.gz distfold-c3a1946675855b299a2b36550cdf2c2f69d153aa.zip |
initial import
Diffstat (limited to 'distfoldd/discoverer.h')
-rw-r--r-- | distfoldd/discoverer.h | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/distfoldd/discoverer.h b/distfoldd/discoverer.h new file mode 100644 index 0000000..ae106de --- /dev/null +++ b/distfoldd/discoverer.h @@ -0,0 +1,82 @@ +#ifndef DISCOVERER_H +#define DISCOVERER_H + +#include <QtCore/QTimer> +#include <QtCore/QUuid> +#include <QtCore/QDateTime> +#include <QtNetwork/QNetworkConfigurationManager> +#include <QtNetwork/QHostAddress> +#include <QtNetwork/QUdpSocket> +#include <QtSystemInfo/QSystemNetworkInfo> +#include <QtSystemInfo/QSystemAlignedTimer> + +QTM_USE_NAMESPACE + +class Discoverer : public QObject +{ + Q_OBJECT + +public: + Discoverer(const QUuid& uuid, uint port, const QString& serviceName, QObject *parent = 0); + + static const uint servicePort = 17451; + static const int dateTimeCompareMinDelta = 5; + + // In seconds + static const int idleBroadcastInterval = 15 * 60; + static const int activeBroadcastInterval = 5; + static const int lostHostTimeout = idleBroadcastInterval * 1.4f; + +signals: + void foundMoreRecentHost(const QHostAddress& address, uint port, const QDateTime& dateTime); + +public slots: + void setLastModifiedTime(const QDateTime& dateTime); + +private: +#pragma pack(push) +#pragma pack(1) + struct Message { + quint32 mtime; + char hostUuid[38]; + quint16 port; + char folderUuid[38]; + char name[]; + }; +#pragma pack() + struct HostInfo { + QDateTime lastSeen; + }; + + QByteArray encodeMessage() const; + + void switchToActiveMode(); + +private slots: + void broadcastMessage(); + void handleDataAvailable(); + void handleBroadcastMessage(const QHostAddress& sender, const QByteArray &data); + void handleHostSeen(const QUuid& hostUuid); + void handleTimerTimeout(); + void handleTimerError(); + void handleOnlineStateChanged(bool online); + +private: + QUuid _folderUuid; + QUuid _hostUuid; + QString _serviceName; + uint _port; + QDateTime _mtime; + bool _mtimeChanged; + QUdpSocket *_receiver; + QUdpSocket *_sender; + quint32 _myId; + QNetworkConfigurationManager *_netConfig; + QSystemNetworkInfo *_netInfo; + QDateTime _lastBroadcast; + QSystemAlignedTimer *_timer; + QTimer *_fallbackTimer; + QHash<QUuid, HostInfo> _knownHosts; +}; + +#endif // DISCOVERER_H |