ActiveClient.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. #include <QTimer>
  16. #include <QTime>
  17. #include "ServerQuery.h"
  18. #include "ActiveClient.h"
  19. #include "Client.h"
  20. #include "App.h"
  21. #include "Settings.h"
  22. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  23. ActiveClient::ActiveClient(App *app, const QString& password, QObject *parent):
  24. QObject(parent),
  25. myApp(app),
  26. myClient(new Client(app, this)),
  27. myQuery(new ServerQuery(this)),
  28. myDisconnectTime(new QTime()),
  29. myBroadcastReplyTimer(new QTimer(this)),
  30. myQueryTimer(new QTimer(this)),
  31. myQueryInterval(Settings::globalInstance()->queryInterval()),
  32. myUniqueUserCount(0),
  33. myUniqueChannelCount(0),
  34. myUniqueServerCount(0),
  35. myUniquePlayerCount(0),
  36. myReplyTimerWasActive(false)
  37. {
  38. connect(myQuery, SIGNAL(finished()), SLOT(queryFinished()));
  39. connect(myQuery, SIGNAL(error(ServerQuery::Error)), SLOT(queryError(ServerQuery::Error)));
  40. myQueryTimer->setSingleShot(true);
  41. myClient->setPassword(password.toAscii().data());
  42. }
  43. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  44. ActiveClient::~ActiveClient()
  45. {
  46. delete myClient;
  47. delete myDisconnectTime;
  48. }
  49. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  50. Client* ActiveClient::client()
  51. {
  52. return myClient;
  53. }
  54. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  55. quint8 ActiveClient::playerCount() const
  56. {
  57. return myQuery->playerList().size();
  58. }
  59. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  60. void ActiveClient::setAddress(const QHostAddress &address, quint16 port)
  61. {
  62. myQuery->setAddress(address, port);
  63. return;
  64. }
  65. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  66. void ActiveClient::queryError(ServerQuery::Error)
  67. {
  68. // myQueryTimer->start(myQueryInterval);
  69. }
  70. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  71. void ActiveClient::queryFinished()
  72. {
  73. PlayerList playerList = myQuery->playerList();
  74. int playerCount = playerList.size();
  75. myClient->setPlayerList(playerList);
  76. myClient->setMaxClients(myQuery->serverRuleValue("maxclients").toInt());
  77. // myQueryTimer->start(myQueryInterval);
  78. /* If the client is disconnect check if there is at least one player connected to the server */
  79. if(myClient->state() == Client::DisconnectedState)
  80. {
  81. if(playerCount > 0 && myDisconnectTime->elapsed() > 10000)
  82. {
  83. myApp->print("Players online on server " + serverAddressString() + ". Joining...\n");
  84. myClient->connect(myQuery->address().toString().toAscii(), myQuery->port());
  85. }
  86. return;
  87. }
  88. /* If the client is connected and left alone on the server, disconnect yourself */
  89. if(myClient->state() == Client::ConnectedState)
  90. {
  91. if(playerCount == 1 && myClient->isOnServer())
  92. {
  93. myApp->print("I was left alone on " + serverAddressString() + ". Leaving...\n");
  94. myClient->disconnect();
  95. myDisconnectTime->restart();
  96. return;
  97. }
  98. }
  99. }
  100. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  101. void ActiveClient::run()
  102. {
  103. /* Say the broadcast count */
  104. if(!myBroadcastReplyTimer->isActive() && myReplyTimerWasActive)
  105. {
  106. myApp->activeClientsReplyCounters(&myUniqueServerCount, &myUniquePlayerCount, this); //add our servers to the list
  107. myClient->say("Sent to " + QString::number(myUniqueChannelCount) + " channels " + QString::number(myUniqueUserCount) + " users and to " + QString::number(myUniqueServerCount) + " servers " + QString::number(myUniquePlayerCount) + " players.");
  108. myUniqueServerCount = myUniquePlayerCount = myUniqueChannelCount = myUniqueUserCount = 0;
  109. }
  110. myReplyTimerWasActive = myBroadcastReplyTimer->isActive();
  111. /* Query the serverinfo for player count if not already querying */
  112. if(!myQueryTimer->isActive())
  113. {
  114. myQuery->query();
  115. myQueryTimer->start(myQueryInterval);
  116. }
  117. /* Run the client */
  118. if(myClient->state() != Client::DisconnectedState)
  119. myClient->run();
  120. }
  121. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  122. const QString ActiveClient::serverAddressString()
  123. {
  124. return QString(myQuery->address().toString() + ':' + QString::number(myQuery->port()));
  125. }
  126. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  127. void ActiveClient::setReplyHashAndWaitForReply(const QString &hash)
  128. {
  129. myReplyHash = hash;
  130. /* Wait for reply */
  131. myBroadcastReplyTimer->setSingleShot(true);
  132. myBroadcastReplyTimer->start(Settings::globalInstance()->timeToWaitForCountReply()*1000);
  133. }
  134. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  135. const QString& ActiveClient::replyHash() const
  136. {
  137. return myReplyHash;
  138. }
  139. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  140. bool ActiveClient::isWaitingReply() const
  141. {
  142. return myBroadcastReplyTimer->isActive();
  143. }
  144. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  145. void ActiveClient::incrementReplyCounters(int userCount, int channelCount, int playerCount, int serverCount)
  146. {
  147. if(!myBroadcastReplyTimer->isActive())
  148. return;
  149. myUniqueUserCount += userCount;
  150. myUniqueChannelCount += channelCount;
  151. myUniqueServerCount += serverCount;
  152. myUniquePlayerCount += playerCount;
  153. }
  154. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  155. void ActiveClient::setHostName(const QString &hostName)
  156. {
  157. myHostName = hostName;
  158. }
  159. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  160. const QString& ActiveClient::hostName() const
  161. {
  162. return myHostName;
  163. }
  164. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  165. int ActiveClient::ping() const
  166. {
  167. return myQuery->ping();
  168. }