src/core/serverapi/server.h
00001 //------------------------------------------------------------------------------
00002 // server.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 "Blzut3" <admin@maniacsvault.net>
00022 //------------------------------------------------------------------------------
00023 
00024 #ifndef __SERVER_H__
00025 #define __SERVER_H__
00026 
00027 #include <QColor>
00028 #include <QDir>
00029 #include <QFileInfo>
00030 #include <QList>
00031 #include <QProcess>
00032 #include <QObject>
00033 #include <QHostAddress>
00034 #include <QString>
00035 #include <QMetaType>
00036 #include <QThread>
00037 #include <QTime>
00038 #include <QUdpSocket>
00039 
00040 #include "serverapi/rconprotocol.h"
00041 #include "serverapi/player.h"
00042 #include "serverapi/serverstructs.h"
00043 #include "global.h"
00044 
00045 class Binaries;
00046 class GameRunner;
00047 class PlayersList;
00048 class EnginePlugin;
00049 class TooltipGenerator;
00050 class QPixmap;
00051 
00052 // Some ports support optional wads.
00053 class MAIN_EXPORT PWad
00054 {
00055         public:
00056                 PWad(const QString &name, bool optional=false) : name(name), optional(optional) {}
00057 
00058                 QString name;
00059                 bool    optional;
00060 };
00061 
00062 class MAIN_EXPORT Server : public QObject
00063 {
00064         Q_OBJECT
00065 
00066         friend class RefreshingThread;
00067         friend class ServerBatch;
00068 
00069         public:
00070                 enum Response
00071                 {
00072                         RESPONSE_GOOD,          // Data is available
00073                         RESPONSE_TIMEOUT,       // Server didn't respond at all
00074                         RESPONSE_WAIT,          // Server responded with "wait"
00075                         RESPONSE_BAD,           // Probably refreshing too quickly
00076                         RESPONSE_BANNED,        // Won't recieve data from this server ever.
00077                         RESPONSE_NO_RESPONSE_YET        // "Dummy" response for servers that weren't refreshed yet
00078                 };
00079 
00080                 Server(const QHostAddress &address, unsigned short port);
00081                 Server(const Server &other);
00082                 virtual ~Server();
00083 
00091                 virtual Binaries*                                               binaries() const;
00092 
00098                 virtual QString         engineName() const;
00099 
00100                 const QHostAddress      &address() const { return serverAddress; }
00101                 QString                         addressWithPort() const { return QString("%1:%2").arg(address().toString()).arg(port()); }
00102                 const QString&          connectPassword() const { return passwordConnect; }
00103                 const QString&          eMail() const { return email; }
00104                 const DMFlags           &gameFlags() const { return dmFlags; }
00105                 const GameMode          &gameMode() const { return currentGameMode; }
00106                 unsigned char           gameSkill() const { return skill; }
00107                 virtual bool            hasRcon() const { return false; }
00108                 const QPixmap           &icon() const;
00109                 bool                            isBroadcastingToLAN() const { return broadcastToLAN; }
00110                 bool                            isBroadcastingToMaster() const { return broadcastToMaster; }
00111                 bool                            isCustom() const { return custom; }
00112                 bool                            isEmpty() const;
00113                 bool                            isFull() const;
00114                 bool                            isKnown() const { return bKnown; }
00115                 bool                            isLocked() const { return locked; }
00116                 bool                            isRefreshable() const;
00117                 bool                            isSetToDelete() const { return bDelete; }
00118                 const QString           &iwadName() const { return iwad; }
00119                 const QString&          joinPassword() const { return passwordJoin; }
00120                 int                                     lastResponse() const { return response; }
00121                 const QString           &map() const { return mapName; }
00122                 const QStringList&      mapsList() const { return mapList; }
00123                 unsigned short          maximumClients() const { return maxPlayers > maxClients ? maxPlayers : maxClients; }
00124                 unsigned short          maximumPlayers() const { return maxPlayers; }
00125                 const QString&          messageOfTheDay() const { return motd; }
00126                 virtual const GameCVar  *modifier() const { return NULL; }
00127                 const QString           &name() const { return serverName; }
00128                 int                                     numFreeClientSlots() const;
00129                 int                                     numFreeJoinSlots() const;
00130                 int                                     numFreeSpectatorSlots() const;
00131                 int                                     numWads() const { return wads.size(); }
00132                 unsigned int            ping() const { return currentPing; }
00133                 const Player&           player(int index) const;
00134                 const PlayersList*      playersList() const { return players; }
00135                 unsigned short          port() const { return serverPort; }
00136                 const QList<PWad>&      pwads() const { return wads; }
00137                 bool                            randomMapRotation() const { return mapRandomRotation; }
00138                 virtual RConProtocol    *rcon() { return NULL; }
00139                 const QString&          rconPassword() const { return passwordRCon; }
00140                 unsigned int            score(int team=0) const { return scores[team]; }
00141                 unsigned int            scoreLimit() const { return serverScoreLimit; }
00142                 virtual QRgb            teamColor(int team) const;
00143                 virtual QString         teamName(int team) const { return team < MAX_TEAMS && team >= 0 ? teamNames[team] : ""; }
00144                 unsigned short          timeLeft() const { return serverTimeLeft; }
00145                 unsigned short          timeLimit() const { return serverTimeLimit; }
00146                 const QString           version() const { return serverVersion; }
00147                 const PWad                      &wad(int index) const { return wads[index]; }
00148                 const QString           &website() const { return webSite; }
00149 
00150                 void                            setBroadcastToLAN(bool b) { broadcastToLAN = b; }
00151                 void                            setBroadcastToMaster(bool b) { broadcastToMaster = b; }
00152                 void                            setHostEmail(const QString& mail) { email = mail; }
00153                 void                            setGameMode(const GameMode& gameMode) { currentGameMode = gameMode; }
00154                 void                            setMap(const QString& name) { mapName = name; }
00155                 void                            setMapList(const QStringList& maplist) { mapList = maplist; }
00156                 void                            setMaximumClients(unsigned short i) { maxClients = i; }
00157                 void                            setMaximumPlayers(unsigned short i) { maxPlayers = i; }
00158                 void                            setMOTD(const QString& message) { motd = message; }
00159                 void                            setName(const QString& name) { serverName = name; }
00160                 void                            setPasswordConnect(const QString& str) { passwordConnect = str; }
00161                 void                            setPasswordJoin(const QString& str) { passwordJoin = str; }
00162                 void                            setPasswordRCon(const QString& str) { passwordRCon = str; }
00163                 void                            setPort(unsigned short i) { serverPort = i; }
00164                 void                            setRandomMapRotation(bool b) { mapRandomRotation = b; }
00165                 void                            setSkill(unsigned char newSkill) { skill = newSkill; }
00166                 void                            setWebsite(const QString& site) { webSite = site; }
00167 
00168                 void                            operator= (const Server &other);
00169                 void                            setCustom(bool b) { custom = b; }
00170                 void                            setToDelete(bool b);
00171 
00179                 virtual GameRunner*     gameRunner() const;
00180 
00181                 bool                            isRefreshing() const { return bIsRefreshing; }
00182 
00189                 virtual const EnginePlugin*             plugin() const = 0;
00190 
00199                 virtual TooltipGenerator*       tooltipGenerator() const;
00200 
00201                 friend class ServerPointer;
00202 
00203         public slots:
00207                 bool                    refresh();
00208 
00209         signals:
00210                 void                            begunRefreshing(Server* server);
00216                 void                            updated(Server *server, int response);
00217 
00218         protected:
00219                 void                            clearDMFlags();
00220 
00224                 void                            emitUpdated(int response) { emit updated(this, response); }
00225 
00232                 virtual Response                readRequest(QByteArray &data)=0;
00233 
00239                 virtual bool            sendRequest(QByteArray &data)=0;
00240 
00247                 bool                            bDelete;
00254                 bool                            bKnown;
00255 
00264                 bool                            bPingIsSet;
00265 
00266                 bool                            broadcastToLAN;
00267                 bool                            broadcastToMaster;
00268                 GameMode                        currentGameMode;
00269                 unsigned int            currentPing;
00270                 bool                            custom;
00271                 DMFlags                         dmFlags;
00272                 QString                         email;
00273                 QString                         iwad;
00274                 bool                            locked;
00275                 QStringList                     mapList;
00276                 QString                         mapName;
00277                 bool                            mapRandomRotation;
00278                 unsigned short          maxClients;
00279                 unsigned short          maxPlayers;
00280                 QString                         motd;
00281                 QString                         passwordConnect;
00282                 QString                         passwordJoin;
00283                 QString                         passwordRCon;
00284                 PlayersList*            players;
00285                 Response                        response;
00286                 unsigned int            scores[MAX_TEAMS];
00287                 QString                         serverName;
00288                 unsigned int            serverScoreLimit;
00289                 unsigned short          serverTimeLeft;
00290                 unsigned short          serverTimeLimit;
00291                 QString                         serverVersion;
00292                 unsigned char           skill;
00293                 QList<PWad>                     wads;
00294                 QString                         webSite;
00295 
00296                 static QString          teamNames[];
00297 
00298                 QTime                           time;
00299 
00300         protected slots:
00305                 void                            setResponse(Server* server, int response);
00306 
00307         private:
00311                 void                            refreshStarts();
00312 
00316                 void                            refreshStops();
00317 
00323                 bool                            sendRefreshQuery(QUdpSocket* socket);
00324 
00330                 bool                            bIsRefreshing;
00331                 QHostAddress            serverAddress;
00332                 unsigned short          serverPort;
00333 
00334                 int                                     triesLeft; 
00335 };
00336 
00337 class MAIN_EXPORT ServerPointer
00338 {
00339         private:
00340                 void copy(const ServerPointer& copyin)
00341                 {
00342                         ptr = copyin.ptr;
00343                 }
00344 
00345         public:
00346                 Server* ptr;
00347 
00348                 ServerPointer() {}
00349                 ServerPointer(Server* s)
00350                 {
00351                         ptr = s;
00352                 }
00353 
00354                 ServerPointer(const ServerPointer& copyin)
00355                 {
00356                         copy(copyin);
00357                 }
00358 
00359                 ServerPointer& operator=(const ServerPointer& rhs)
00360                 {
00361                         if (this != &rhs)
00362                         {
00363                                 copy(rhs);
00364                         }
00365 
00366                         return *this;
00367                 }
00368                 ~ServerPointer() {}
00369 
00370                 bool operator==(const Server* fPtr) const
00371                 {
00372                         return (ptr == fPtr);
00373                 }
00374 
00375                 friend bool operator==(const Server* fPtr, const ServerPointer& ref)
00376                 {
00377                         return (fPtr == ref.ptr);
00378                 }
00379 
00380                 Server* operator->()
00381                 {
00382                         return ptr;
00383                 }
00384 };
00385 
00386 Q_DECLARE_METATYPE(ServerPointer)
00387 
00388 #endif /* __SERVER_H__ */
 All Classes Files Functions Variables Typedefs Enumerations Enumerator