ActiveClient.h 4.8 KB

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