File indexing completed on 2024-05-12 05:39:48

0001 /***************************************************************************
0002  *  Copyright (C) 2019 by Renaud Guezennec                               *
0003  *   http://www.rolisteam.org/contact                                      *
0004  *                                                                         *
0005  *   This software is free software; you can redistribute it and/or modify *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  *                                                                         *
0010  *   This program is distributed in the hope that it will be useful,       *
0011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0013  *   GNU General Public License for more details.                          *
0014  *                                                                         *
0015  *   You should have received a copy of the GNU General Public License     *
0016  *   along with this program; if not, write to the                         *
0017  *   Free Software Foundation, Inc.,                                       *
0018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
0019  ***************************************************************************/
0020 #ifndef CONNECTIONCONTROLLER_H
0021 #define CONNECTIONCONTROLLER_H
0022 
0023 #include "controllerinterface.h"
0024 
0025 #include <QHash>
0026 #include <QObject>
0027 #include <QPointer>
0028 #include <memory>
0029 
0030 #include "network/networkmessage.h"
0031 #include "network/networkreceiver.h"
0032 #include <core_global.h>
0033 
0034 #include "model/profilemodel.h"
0035 #include "network/channelmodel.h"
0036 
0037 class ClientManager;
0038 class RServer;
0039 class QThread;
0040 class HeartBeatSender;
0041 class QAbstractItemModel;
0042 class GameController;
0043 class IpChecker;
0044 class CountDownObject;
0045 
0046 class CORE_EXPORT NetworkController : public AbstractControllerInterface, public NetWorkReceiver
0047 {
0048     Q_OBJECT
0049     Q_PROPERTY(bool isGM READ isGM NOTIFY isGMChanged)
0050     Q_PROPERTY(bool connected READ connected NOTIFY connectedChanged)
0051     Q_PROPERTY(bool connecting READ connecting NOTIFY connectingChanged)
0052     Q_PROPERTY(bool hosting READ hosting NOTIFY hostingChanged)
0053     Q_PROPERTY(bool askForGM READ askForGM NOTIFY askForGMChanged)
0054     Q_PROPERTY(QString host READ host NOTIFY hostChanged)
0055     Q_PROPERTY(int port READ port NOTIFY portChanged)
0056     Q_PROPERTY(QByteArray adminPassword READ adminPassword WRITE setAdminPassword NOTIFY adminPasswordChanged)
0057     Q_PROPERTY(QByteArray serverPassword READ serverPassword NOTIFY serverPasswordChanged)
0058     Q_PROPERTY(ProfileModel* profileModel READ profileModel CONSTANT)
0059     Q_PROPERTY(ChannelModel* channelModel READ channelModel CONSTANT)
0060     Q_PROPERTY(QString ipv4 READ ipv4 NOTIFY ipv4Changed)
0061     Q_PROPERTY(QString lastError READ lastError NOTIFY lastErrorChanged)
0062     Q_PROPERTY(int selectedProfileIndex READ selectedProfileIndex WRITE setSelectedProfileIndex NOTIFY
0063                    selectedProfileIndexChanged FINAL)
0064 public:
0065     explicit NetworkController(QObject* parent= nullptr);
0066     ~NetworkController() override;
0067     bool isGM() const;
0068     bool connected() const;
0069     bool connecting() const;
0070     bool hosting() const;
0071     bool askForGM() const;
0072     QString host() const;
0073     int port() const;
0074     QString ipv4() const;
0075     QString lastError() const;
0076 
0077     ProfileModel* profileModel() const;
0078     ChannelModel* channelModel() const;
0079 
0080     QByteArray adminPassword() const;
0081     QByteArray serverPassword() const;
0082 
0083     void setGameController(GameController* game) override;
0084 
0085     void insertNetWortReceiver(NetWorkReceiver*, NetMsg::Category cat);
0086     NetWorkReceiver::SendType processMessage(NetworkMessageReader* msg) override;
0087 
0088     int selectedProfileIndex() const;
0089     void setSelectedProfileIndex(int newSelectedProfileIndex);
0090     ConnectionProfile* currentProfile() const;
0091 
0092 signals:
0093     void isGMChanged(bool);
0094     void connectedChanged(bool);
0095     void connectingChanged(bool);
0096     void hostingChanged();
0097     void askForGMChanged();
0098     void hostChanged();
0099     void portChanged();
0100     void serverPasswordChanged();
0101     void adminPasswordChanged();
0102     void ipv4Changed();
0103     void downloadingData(quint64 readData, quint64 size);
0104     void tableChanged();
0105     void lastErrorChanged(const QString& error);
0106     void infoMessage(const QString& msg);
0107 
0108     void authentificationFail();
0109 
0110     void selectedProfileIndexChanged();
0111 
0112 public slots:
0113     // network
0114     void startConnection();
0115     void stopConnecting();
0116     void stopConnection();
0117     void setAdminPassword(const QByteArray& array);
0118     void setConnected(bool b);
0119     void setConnecting(bool b);
0120     void removeProfile(int pos);
0121     void closeServer();
0122     void saveData();
0123 
0124 private slots:
0125     void sendOffConnectionInfo();
0126 
0127     void setLastError(const QString& error);
0128     void dispatchMessage(QByteArray array);
0129 
0130 private:
0131     void startServer();
0132     void startClient();
0133     void stopClient();
0134 
0135 private:
0136     std::unique_ptr<ClientManager> m_clientManager;
0137     std::unique_ptr<RServer> m_server;
0138     std::unique_ptr<QThread> m_serverThread;
0139     std::unique_ptr<HeartBeatSender> m_hbSender;
0140     std::unique_ptr<ProfileModel> m_profileModel;
0141     std::unique_ptr<ChannelModel> m_channelModel;
0142     std::unique_ptr<IpChecker> m_ipChecker;
0143     std::unique_ptr<CountDownObject> m_countDown;
0144     QPointer<GameController> m_gameCtrl;
0145     QHash<NetMsg::Category, NetWorkReceiver*> m_receiverMap;
0146 
0147     QMap<QString, QVariant> m_serverParameters;
0148 
0149     // QByteArray m_serverPw;
0150     QByteArray m_admindPw;
0151 
0152     // QString m_host;
0153     // int m_port= 6660;
0154     QString m_ipv4Address;
0155     QString m_lastError;
0156 
0157     // Data
0158     // bool m_isGM= true;
0159     // bool m_hosting= false;
0160     // bool m_askForGM= true;
0161     bool m_connected= false;
0162     bool m_connecting= false;
0163 
0164     int m_selectedProfileIndex{0};
0165 };
0166 
0167 #endif // CONNECTIONCONTROLLER_H