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

0001 /*
0002     SPDX-FileCopyrightText: 2023 Jasem Mutlaq <mutlaqja@ikarustech.com>
0003 
0004     Message Channel
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 <memory>
0014 
0015 namespace EkosLive
0016 {
0017 class Node : public QObject
0018 {
0019     Q_PROPERTY(QString name MEMBER m_Name)
0020     Q_PROPERTY(QUrl url MEMBER m_URL READ url)
0021     Q_OBJECT
0022 
0023     public:
0024         explicit Node(const QString &name);
0025         virtual ~Node() = default;
0026 
0027         const QUrl url() const {return m_URL;}
0028         void sendResponse(const QString &command, const QJsonObject &payload);
0029         void sendResponse(const QString &command, const QJsonArray &payload);
0030         void sendResponse(const QString &command, const QString &payload);
0031         void sendResponse(const QString &command, bool payload);
0032 
0033         void sendTextMessage(const QString &message);
0034         void sendBinaryMessage(const QByteArray &message);
0035         bool isConnected() const {return m_isConnected;}        
0036 
0037         void setAuthResponse(const QJsonObject &response)
0038         {
0039             m_AuthResponse = response;
0040         }
0041 
0042     signals:
0043         void connected();
0044         void disconnected();
0045         void onTextReceived(const QString &message);
0046         void onBinaryReceived(const QByteArray &message);
0047 
0048     public slots:
0049         void connectServer();
0050         void disconnectServer();
0051 
0052     private slots:
0053         // Connection
0054         void onConnected();
0055         void onDisconnected();
0056         void onError(QAbstractSocket::SocketError error);
0057 
0058    private:
0059         QWebSocket m_WebSocket;
0060         QJsonObject m_AuthResponse;
0061         uint16_t m_ReconnectTries {0};
0062         QUrl m_URL;
0063         QString m_Name;
0064         QString m_Path;
0065 
0066         bool m_isConnected { false };
0067         bool m_sendBlobs { true};
0068 
0069         QMap<int, bool> m_Options;        
0070 
0071         // Retry every 5 seconds in case remote server is down
0072         static const uint16_t RECONNECT_INTERVAL = 5000;
0073         // Retry for 1 hour before giving up
0074         static const uint16_t RECONNECT_MAX_TRIES = 720;
0075         // Throttle interval
0076         static const uint16_t THROTTLE_INTERVAL = 1000;
0077 };
0078 }