File indexing completed on 2024-04-21 14:45:04

0001 /*
0002     SPDX-FileCopyrightText: 2023 Jasem Mutlaq <mutlaqja@ikarustech.com>
0003 
0004     Node Manager
0005 
0006     SPDX-License-Identifier: GPL-2.0-or-later
0007 */
0008 
0009 #pragma once
0010 
0011 #include <QtWebSockets/QWebSocket>
0012 #include <QJsonObject>
0013 #include <QNetworkAccessManager>
0014 #include <QPointer>
0015 #include <QNetworkReply>
0016 #include <memory>
0017 
0018 #include "node.h"
0019 
0020 namespace EkosLive
0021 {
0022 class NodeManager : public QObject
0023 {
0024     Q_PROPERTY(QUrl serviceURL MEMBER m_ServiceURL)
0025     Q_PROPERTY(QUrl websocketURL MEMBER m_WebsocketURL)
0026     Q_OBJECT
0027 
0028     public:
0029         explicit NodeManager(uint32_t mask);
0030         virtual ~NodeManager() = default;
0031 
0032         bool isConnected() const;
0033 
0034         typedef enum
0035         {
0036             Message = 1 << 0,
0037             Media   = 1 << 1,
0038             Cloud   = 1 << 2
0039         } Channels;
0040 
0041         void setURLs(const QUrl &service, const QUrl &websocket);
0042         void setCredentials(const QString &username, const QString &password);
0043         void setAuthResponse(const QJsonObject &response)
0044         {
0045             m_AuthResponse = response;
0046         }
0047 
0048         Node *message() {return m_Nodes[Message];}
0049         Node *media() {return m_Nodes[Media];}
0050         Node *cloud() {return m_Nodes.contains(Cloud) ? m_Nodes[Cloud] : nullptr;}
0051 
0052     signals:
0053         void connected();
0054         void disconnected();
0055         void authenticationError(QString);
0056 
0057     public slots:
0058         void authenticate();
0059         void disconnectNodes();
0060 
0061         void setConnected();
0062         void setDisconnected();
0063 
0064       protected slots:
0065        void onResult(QNetworkReply *reply);
0066 
0067       private:
0068         QJsonObject m_AuthResponse;
0069         uint16_t m_ReconnectTries {0};
0070         QUrl m_ServiceURL, m_WebsocketURL;
0071         QString m_Username, m_Password;
0072 
0073         QPointer<QNetworkAccessManager> m_NetworkManager;
0074         QMap<Channels, Node*> m_Nodes;
0075 
0076         // Retry every 5 seconds in case remote server is down
0077         static const uint16_t RECONNECT_INTERVAL = 5000;
0078         // Retry authentication up to 3 times
0079         static const uint16_t RECONNECT_MAX_TRIES = 3;
0080         // Throttle interval
0081         static const uint16_t THROTTLE_INTERVAL = 1000;
0082 };
0083 }