refresher.h
1 //------------------------------------------------------------------------------
2 // refresher.h
3 //------------------------------------------------------------------------------
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 // 02110-1301 USA
19 //
20 //------------------------------------------------------------------------------
21 // Copyright (C) 2009 "Zalewa" <zalewapl@gmail.com>
22 //------------------------------------------------------------------------------
23 #ifndef DOOMSEEKER_REFRESHER_REFRESHER_H
24 #define DOOMSEEKER_REFRESHER_REFRESHER_H
25 
26 #include "serverapi/serverptr.h"
27 #include <QMutex>
28 #include <QObject>
29 
30 #define gRefresher (Refresher::instance())
31 
32 class MasterClient;
33 class Server;
34 class QHostAddress;
35 
36 class Refresher : public QObject
37 {
38  Q_OBJECT
39 
40 public:
41  ~Refresher() override;
42 
47  void quit();
48 
52  void registerMaster(MasterClient *pMaster);
53 
60  bool registerServer(ServerPtr server);
61 
66  void setDelayBetweenResends(int delay);
67 
68  bool start();
69 
70  static Refresher *instance();
71  static bool isInstantiated();
72  static void deinstantiate();
73 
74 signals:
78  void block();
79 
80  void finishedQueryingMaster(MasterClient *master);
81 
86  void sleepingModeEnter();
87 
92  void sleepingModeExit();
93 
94 private:
95  class Data;
96  class MasterClientInfo;
97 
98  static const int MASTER_SERVER_TIMEOUT_DELAY = 10000;
99  static Refresher *staticInstance;
100  static QMutex instanceMutex;
101 
102  Data *d;
103 
104  Refresher();
105 
106  void concludeRefresh();
107  bool isAnythingToRefresh() const;
108  ServerPtr findRefreshingServer(const QHostAddress &address, unsigned short port);
109 
110  void readPendingDatagram();
111 
112  void startNewServerRefresh();
113  void resendCurrentServerRefreshesIfTimeout();
114 
115  // TODO: Constify 'address' and 'packet' args.
120  bool tryReadDatagramByMasterClient(QHostAddress &address,
121  unsigned short port, QByteArray &packet);
126  bool tryReadDatagramByServer(const QHostAddress &address,
127  unsigned short port, QByteArray &packet);
128 
129  void unregisterMaster(MasterClient *pMaster);
130 
131 private slots:
132  void attemptTimeoutMasters();
133  void masterFinishedRefreshing();
134  void readAllPendingDatagrams();
135  void sendMasterQueries();
136  void sendServerQueries();
137 };
138 
139 #endif