1 #ifndef REMOTECLIENT_H
2 #define REMOTECLIENT_H
3 
4 #include "abstractclient.h"
5 
6 #include <QTcpSocket>
7 #include <QWebSocket>
8 
9 class QTimer;
10 
11 class RemoteClient : public AbstractClient
12 {
13     Q_OBJECT
14 signals:
15     void maxPingTime(int seconds, int maxSeconds);
16     void serverTimeout();
17     void loginError(Response::ResponseCode resp, QString reasonStr, quint32 endTime, QList<QString> missingFeatures);
18     void registerError(Response::ResponseCode resp, QString reasonStr, quint32 endTime);
19     void activateError();
20     void socketError(const QString &errorString);
21     void protocolVersionMismatch(int clientVersion, int serverVersion);
22     void
23     sigConnectToServer(const QString &hostname, unsigned int port, const QString &_userName, const QString &_password);
24     void sigRegisterToServer(const QString &hostname,
25                              unsigned int port,
26                              const QString &_userName,
27                              const QString &_password,
28                              const QString &_email,
29                              int _gender,
30                              const QString &_country,
31                              const QString &_realname);
32     void sigActivateToServer(const QString &_token);
33     void sigDisconnectFromServer();
34     void notifyUserAboutUpdate();
35     void sigRequestForgotPasswordToServer(const QString &hostname, unsigned int port, const QString &_userName);
36     void sigForgotPasswordSuccess();
37     void sigForgotPasswordError();
38     void sigPromptForForgotPasswordReset();
39     void sigSubmitForgotPasswordResetToServer(const QString &hostname,
40                                               unsigned int port,
41                                               const QString &_userName,
42                                               const QString &_token,
43                                               const QString &_newpassword);
44     void sigPromptForForgotPasswordChallenge();
45     void sigSubmitForgotPasswordChallengeToServer(const QString &hostname,
46                                                   unsigned int port,
47                                                   const QString &_userName,
48                                                   const QString &_email);
49 private slots:
50     void slotConnected();
51     void readData();
52     void websocketMessageReceived(const QByteArray &message);
53     void slotSocketError(QAbstractSocket::SocketError error);
54     void slotWebSocketError(QAbstractSocket::SocketError error);
55     void ping();
56     void processServerIdentificationEvent(const Event_ServerIdentification &event);
57     void processConnectionClosedEvent(const Event_ConnectionClosed &event);
58     void loginResponse(const Response &response);
59     void registerResponse(const Response &response);
60     void activateResponse(const Response &response);
61     void
62     doConnectToServer(const QString &hostname, unsigned int port, const QString &_userName, const QString &_password);
63     void doRegisterToServer(const QString &hostname,
64                             unsigned int port,
65                             const QString &_userName,
66                             const QString &_password,
67                             const QString &_email,
68                             int _gender,
69                             const QString &_country,
70                             const QString &_realname);
71     void doLogin();
72     void doDisconnectFromServer();
73     void doActivateToServer(const QString &_token);
74     void doRequestForgotPasswordToServer(const QString &hostname, unsigned int port, const QString &_userName);
75     void requestForgotPasswordResponse(const Response &response);
76     void doSubmitForgotPasswordResetToServer(const QString &hostname,
77                                              unsigned int port,
78                                              const QString &_userName,
79                                              const QString &_token,
80                                              const QString &_newpassword);
81     void submitForgotPasswordResetResponse(const Response &response);
82     void doSubmitForgotPasswordChallengeToServer(const QString &hostname,
83                                                  unsigned int port,
84                                                  const QString &_userName,
85                                                  const QString &_email);
86     void submitForgotPasswordChallengeResponse(const Response &response);
87 
88 private:
89     static const int maxTimeout = 10;
90     int timeRunning, lastDataReceived;
91     QByteArray inputBuffer;
92     bool messageInProgress;
93     bool handshakeStarted;
94     bool usingWebSocket;
95     int messageLength;
96     QTimer *timer;
97     QTcpSocket *socket;
98     QWebSocket *websocket;
99     QString lastHostname;
100     unsigned int lastPort;
101 
102     QString getSrvClientID(const QString &_hostname);
103     bool newMissingFeatureFound(const QString &_serversMissingFeatures);
104     void clearNewClientFeatures();
105     void connectToHost(const QString &hostname, unsigned int port);
106 
107 protected slots:
108     void sendCommandContainer(const CommandContainer &cont) override;
109 
110 public:
111     explicit RemoteClient(QObject *parent = nullptr);
112     ~RemoteClient() override;
peerName()113     QString peerName() const
114     {
115         if (usingWebSocket) {
116             return websocket->peerName();
117         } else {
118             return socket->peerName();
119         }
120     }
121     void
122     connectToServer(const QString &hostname, unsigned int port, const QString &_userName, const QString &_password);
123     void registerToServer(const QString &hostname,
124                           unsigned int port,
125                           const QString &_userName,
126                           const QString &_password,
127                           const QString &_email,
128                           int _gender,
129                           const QString &_country,
130                           const QString &_realname);
131     void activateToServer(const QString &_token);
132     void disconnectFromServer();
133     void requestForgotPasswordToServer(const QString &hostname, unsigned int port, const QString &_userName);
134     void submitForgotPasswordResetToServer(const QString &hostname,
135                                            unsigned int port,
136                                            const QString &_userName,
137                                            const QString &_token,
138                                            const QString &_newpassword);
139     void submitForgotPasswordChallengeToServer(const QString &hostname,
140                                                unsigned int port,
141                                                const QString &_userName,
142                                                const QString &_email);
143 };
144 
145 #endif
146