ActiveClient.cpp 6.9 KB

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