File indexing completed on 2024-07-21 06:28:25

0001 /*
0002     SPDX-FileCopyrightText: 2012 Jasem Mutlaq <mutlaqja@ikarustech.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #pragma once
0008 
0009 #include "indicommon.h"
0010 
0011 #include <QFile>
0012 #include <QObject>
0013 #include <QProcess>
0014 #include <QTcpSocket>
0015 #include <QTemporaryFile>
0016 #include <QFuture>
0017 
0018 #include <memory>
0019 
0020 class DriverInfo;
0021 
0022 /**
0023  * @class ServerManager
0024  * ServerManager is responsible for starting and shutting local INDI servers.
0025  *
0026  * @author Jasem Mutlaq
0027  */
0028 class ServerManager : public QObject
0029 {
0030         Q_OBJECT
0031 
0032     public:
0033         ServerManager(const QString &inHost, int inPort);
0034         ~ServerManager() override;
0035 
0036         bool start();
0037         void stop();
0038 
0039         QString getLogBuffer();
0040         const QString &getHost() const
0041         {
0042             return host;
0043         }
0044         int getPort() const
0045         {
0046             return port;
0047         }
0048 
0049         void setPendingDrivers(QList<QSharedPointer<DriverInfo>> drivers)
0050         {
0051             m_PendingDrivers = drivers;
0052         }
0053         const QList<QSharedPointer<DriverInfo>> &pendingDrivers() const
0054         {
0055             return m_PendingDrivers;
0056         }
0057 
0058         void startDriver(const QSharedPointer<DriverInfo> &driver);
0059         void stopDriver(const QSharedPointer<DriverInfo> &driver);
0060         bool restartDriver(const QSharedPointer<DriverInfo> &driver);
0061 
0062         const QList<QSharedPointer<DriverInfo>> &managedDrivers() const
0063         {
0064             return m_ManagedDrivers;
0065         }
0066         bool contains(const QSharedPointer<DriverInfo> &driver)
0067         {
0068             return m_ManagedDrivers.contains(driver);
0069         }
0070 
0071         void setMode(ServerMode inMode)
0072         {
0073             mode = inMode;
0074         }
0075         ServerMode getMode()
0076         {
0077             return mode;
0078         }
0079 
0080         QString errorString();
0081 
0082         int size()
0083         {
0084             return m_ManagedDrivers.size();
0085         }
0086 
0087     public slots:
0088         void processServerError(QProcess::ProcessError);
0089         void processStandardError();
0090 
0091     private:
0092         QTcpSocket serverSocket;
0093         QString host;
0094         int port;
0095         QTemporaryFile serverBuffer;
0096         std::unique_ptr<QProcess> serverProcess;
0097 
0098         void insertEnvironmentPath(QProcessEnvironment *env, const QString &variable, const QString &relativePath);
0099 
0100         ServerMode mode { SERVER_CLIENT };
0101 
0102         QList<QSharedPointer<DriverInfo>> m_ManagedDrivers;
0103 
0104         QList<QSharedPointer<DriverInfo>> m_PendingDrivers;
0105 
0106         QFile indiFIFO;
0107 
0108     signals:
0109         void started();
0110         void stopped();
0111         void failed(const QString &message);
0112         void terminated(const QString &message);
0113 
0114 
0115         void newServerLog();
0116 
0117         // Driver Signals
0118         void driverStarted(const QSharedPointer<DriverInfo> &driver);
0119         void driverStopped(const QSharedPointer<DriverInfo> &driver);
0120         void driverRestarted(const QSharedPointer<DriverInfo> &driver);
0121         void driverFailed(const QSharedPointer<DriverInfo> &driver, const QString &message);
0122 };