ActiveClient.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. GNU General Public License version 3 notice
  3. Copyright (C) 2012 Mihawk <luiz@netdome.biz>. All rights reserved.
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see < http://www.gnu.org/licenses/ >.
  14. */
  15. #ifndef ACTIVECLIENT_H
  16. #define ACTIVECLIENT_H
  17. #include <QObject>
  18. #include <QString>
  19. #include "ServerQuery.h"
  20. class QHostAddress;
  21. class QTimer;
  22. class QTime;
  23. class Client;
  24. class App;
  25. /**
  26. Takes care of joining the client to the server in the case there are players
  27. and leaving when there are nobody.
  28. It constantly queryies the server using myQuery to gather information from the servers.
  29. @author Mihawk <luiz@netdome.biz>
  30. @file ActiveClient.h
  31. */
  32. class ActiveClient: public QObject
  33. {
  34. Q_OBJECT
  35. public:
  36. /**
  37. Creates a server monitoring object.
  38. @param app The application creator of this object
  39. @param password The password needed to connect to this server if any
  40. @param parent The parent object (should be the same as the app param)
  41. */
  42. ActiveClient(App* app, const QString &password = "", QObject *parent = 0);
  43. /**
  44. Destructor
  45. */
  46. virtual ~ActiveClient();
  47. /**
  48. Returns the QW Client used.
  49. @return The client used by us
  50. */
  51. Client* client();
  52. /**
  53. Makes this class work, should be called every frame after creation of the object.
  54. */
  55. void run();
  56. /**
  57. Set the server that we are going to monitor.
  58. @param hostName Server hostname
  59. @param hostAddress Server address
  60. @param port Server port
  61. */
  62. void setAddress(const QString& hostName, const QHostAddress &hostAddress, quint16 port);
  63. const QHostAddress& address() const;
  64. quint16 port() const;
  65. /**
  66. Returns the address of the server we are monitoring.
  67. @return The server address
  68. */
  69. const QString serverAddressString();
  70. /**
  71. Returns the full hostname of the server we are monitoring (host:port).
  72. @return The server address
  73. */
  74. const QString serverHostNameString();
  75. /**
  76. Returns the number of players on the server last time the query was ran.
  77. @return The number of players
  78. */
  79. quint8 playerCount() const;
  80. /**
  81. Sets the hash and start waiting for reply from central on that hash.
  82. @param hash The reply hash
  83. */
  84. void setReplyHashAndWaitForReply(const QString& hash);
  85. /**
  86. Returns the reply hash of the last reply.
  87. @return The hash
  88. */
  89. const QString& replyHash() const;
  90. /**
  91. Called to increment the internal reply counters
  92. Those reply counters are counters of how many people received the message
  93. that we just broadcasted.
  94. @param userCount The irc user count
  95. @param channelCount The irc channel count
  96. @param playerCount The QW player count
  97. @param serverCount The QW servers count
  98. */
  99. void incrementReplyCounters(int userCount, int channelCount, int playerCount, int serverCount);
  100. /**
  101. Tells us if we are wating for the count reply from the server.
  102. @return True if waiting for reply, false otherwise
  103. */
  104. bool isWaitingReply() const;
  105. /**
  106. Set hostname
  107. @param hostName The hostname
  108. */
  109. void setHostName(const QString& hostName);
  110. /**
  111. * @brief hasHostName
  112. * @return
  113. */
  114. bool hasHostName() const;
  115. /**
  116. Get hostname
  117. @return The hostname
  118. */
  119. const QString& hostName() const;
  120. /**
  121. Returns the server ping
  122. @return The ping
  123. */
  124. int ping() const;
  125. private slots:
  126. /**
  127. Called when a query gathering server information has just finished successfully.
  128. */
  129. void queryFinished();
  130. /**
  131. Called when a query fails on the process of gathering information the server.
  132. @param err Error code
  133. */
  134. void queryError(ServerQuery::Error err);
  135. private:
  136. // The application that has created us
  137. App* myApp;
  138. // The QW client connects to the server
  139. Client* myClient;
  140. // The information gathering query object
  141. ServerQuery* myQuery;
  142. // Timer to avoid too fast reconnections
  143. QTime* myDisconnectTime;
  144. // Count server reply while timer is active
  145. QString myReplyHash;
  146. QTimer* myBroadcastReplyTimer;
  147. // How much time to wait b4 next query for player information
  148. QTimer* myQueryTimer;
  149. quint32 myQueryInterval;
  150. // Server HostName used on reply
  151. QString myHostName;
  152. bool myHasHostNameFlag;
  153. // Reply counters
  154. int myUniqueUserCount;
  155. int myUniqueChannelCount;
  156. int myUniqueServerCount;
  157. int myUniquePlayerCount;
  158. bool myReplyTimerWasActive; // For printing the reply counters
  159. };
  160. #endif // ACTIVECLIENT_H