ActiveClient.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. /**
  64. Returns the address of the server we are monitoring.
  65. @return The server address
  66. */
  67. const QString serverAddressString();
  68. /**
  69. Returns the full hostname of the server we are monitoring (host:port).
  70. @return The server address
  71. */
  72. const QString serverHostNameString();
  73. /**
  74. Returns the number of players on the server last time the query was ran.
  75. @return The number of players
  76. */
  77. quint8 playerCount() const;
  78. /**
  79. Sets the hash and start waiting for reply from central on that hash.
  80. @param hash The reply hash
  81. */
  82. void setReplyHashAndWaitForReply(const QString& hash);
  83. /**
  84. Returns the reply hash of the last reply.
  85. @return The hash
  86. */
  87. const QString& replyHash() const;
  88. /**
  89. Called to increment the internal reply counters
  90. Those reply counters are counters of how many people received the message
  91. that we just broadcasted.
  92. @param userCount The irc user count
  93. @param channelCount The irc channel count
  94. @param playerCount The QW player count
  95. @param serverCount The QW servers count
  96. */
  97. void incrementReplyCounters(int userCount, int channelCount, int playerCount, int serverCount);
  98. /**
  99. Tells us if we are wating for the count reply from the server.
  100. @return True if waiting for reply, false otherwise
  101. */
  102. bool isWaitingReply() const;
  103. /**
  104. Set hostname
  105. @param hostName The hostname
  106. */
  107. void setHostName(const QString& hostName);
  108. /**
  109. * @brief hasHostName
  110. * @return
  111. */
  112. bool hasHostName() const;
  113. /**
  114. Get hostname
  115. @return The hostname
  116. */
  117. const QString& hostName() const;
  118. /**
  119. Returns the server ping
  120. @return The ping
  121. */
  122. int ping() const;
  123. private slots:
  124. /**
  125. Called when a query gathering server information has just finished successfully.
  126. */
  127. void queryFinished();
  128. /**
  129. Called when a query fails on the process of gathering information the server.
  130. @param err Error code
  131. */
  132. void queryError(ServerQuery::Error err);
  133. private:
  134. // The application that has created us
  135. App* myApp;
  136. // The QW client connects to the server
  137. Client* myClient;
  138. // The information gathering query object
  139. ServerQuery* myQuery;
  140. // Timer to avoid too fast reconnections
  141. QTime* myDisconnectTime;
  142. // Count server reply while timer is active
  143. QString myReplyHash;
  144. QTimer* myBroadcastReplyTimer;
  145. // How much time to wait b4 next query for player information
  146. QTimer* myQueryTimer;
  147. quint32 myQueryInterval;
  148. // Server HostName used on reply
  149. QString myHostName;
  150. bool myHasHostNameFlag;
  151. // Reply counters
  152. int myUniqueUserCount;
  153. int myUniqueChannelCount;
  154. int myUniqueServerCount;
  155. int myUniquePlayerCount;
  156. bool myReplyTimerWasActive; // For printing the reply counters
  157. };
  158. #endif // ACTIVECLIENT_H