src/core/refresher.h
00001 //------------------------------------------------------------------------------
00002 // refresher.h
00003 //------------------------------------------------------------------------------
00004 //
00005 // This program is free software; you can redistribute it and/or
00006 // modify it under the terms of the GNU General Public License
00007 // as published by the Free Software Foundation; either version 2
00008 // of the License, or (at your option) any later version.
00009 //
00010 // This program is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with this program; if not, write to the Free Software
00017 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00018 // 02110-1301, USA.
00019 //
00020 //------------------------------------------------------------------------------
00021 // Copyright (C) 2009 "Zalewa" <zalewapl@gmail.com>
00022 //------------------------------------------------------------------------------
00023 
00024 #ifndef __REFRESHER_H_
00025 #define __REFRESHER_H_
00026 
00027 #include <QHash>
00028 #include <QMutex>
00029 #include <QThread>
00030 #include <QTime>
00031 #include <QRunnable>
00032 #include <QSet>
00033 #include <QUdpSocket>
00034 #include <QWaitCondition>
00035 
00036 class MasterClient;
00037 class MasterClientSignalProxy;
00038 class Server;
00039 
00040 class ServerBatch
00041 {
00042         public:
00043                 QList<Server*>  servers;
00044                 QTime                   time;
00045 
00051                 void                    sendQueries(QUdpSocket *socket, QList<Server*>& rejectedServers, int resendDelay=1000, bool firstQuery=false);
00052 };
00053 
00054 class RefreshingThread : public QThread, public QRunnable
00055 {
00056         Q_OBJECT
00057 
00058         public:
00059                 RefreshingThread();
00060                 ~RefreshingThread();
00061 
00066                 void    quit();
00067 
00073                 void    registerMaster(MasterClient* pMaster);
00074 
00080                 void    registerServer(Server* server);
00081                 void    run();
00082 
00087                 void    setDelayBetweenResends(int delay) { delayBetweenResends = qMax(delay, 100); }
00088 
00089         signals:
00093                 void    block();
00094 
00095                 void    finishedQueryingMaster(MasterClient* master);
00096 
00101                 void    sleepingModeEnter();
00102 
00107                 void    sleepingModeExit();
00108 
00109         protected:
00110                 class MasterClientInfo
00111                 {
00112                         public:
00113                                 MasterClientInfo(MasterClient* pMaster, RefreshingThread* pParent);
00114                                 ~MasterClientInfo();
00115 
00116                                 int                                                     numOfChallengesSent;
00117                                 QTime                                           timeLastChallengeSent;
00118 
00119                         protected:
00120                                 MasterClientSignalProxy*        pReceiver;
00121                 };
00122 
00123                 typedef QHash<MasterClient*, MasterClientInfo*>                         MasterHashtable;
00124                 typedef QHash<MasterClient*, MasterClientInfo*>::iterator       MasterHashtableIt;
00125 
00126                 static const int                                                        MASTER_SERVER_TIMEOUT_DELAY = 10000;
00127 
00128                 QTime                                                                           batchTime;
00129                 bool                                                                            bKeepRunning;
00130                 int                                                                                     delayBetweenResends;
00131                 QList<ServerBatch>                                                      registeredBatches;
00132                 MasterHashtable                                                         registeredMasters;
00133 
00138                 QSet<Server*>                                                           registeredServers;
00139                 QUdpSocket*                                                                     socket;
00140                 QList<Server*>                                                          unbatchedServers;
00141                 QSet<MasterClient*>                                                     unchallengedMasters;
00142 
00146                 QMutex                                  thisMutex;
00147 
00152                 QWaitCondition                  thisWaitCondition;
00153 
00154                 void                                    attemptTimeoutMasters();
00155 
00156                 void                                    gotoSleep();
00157 
00158                 bool                                    isAnythingToRefresh() const;
00159 
00163                 Server*                                 obtainServerFromBatch(ServerBatch& batch, const QHostAddress& address, quint16 port);
00164 
00165                 void                                    readPendingDatagrams();
00166 
00167                 void                                    sendMasterQueries();
00168 
00172                 unsigned                                sendQueriesForBatch(ServerBatch& batch, int resetDelay, bool firstQuery);
00173 
00174                 void                                    sendServerQueries();
00175 
00180                 bool                                    shouldBlockRefreshingProcess() const;
00181 
00182                 void                                    unregisterMaster(MasterClient* pMaster);
00183 
00184         protected slots:
00185                 void                                    masterFinishedRefreshing(MasterClient* pMaster);
00186 };
00187 
00188 #endif
 All Classes Files Functions Variables Typedefs Enumerations Enumerator