ActiveClient.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. ActiveClient::ActiveClient(App *app, QObject *parent):
  23. QObject(parent),
  24. myApp(app),
  25. myClient(new Client(app)),
  26. myQuery(new ServerQuery(this)),
  27. myDisconnectTime(new QTime()),
  28. myBroadcastReplyTimer(new QTimer(this)),
  29. myQueryTimer(new QTimer(this)),
  30. myQueryInterval(Settings::globalInstance()->queryInterval()),
  31. myUniqueUserCount(0),
  32. myUniqueChannelCount(0),
  33. myUniqueServerCount(0),
  34. myUniquePlayerCount(0),
  35. myReplyTimerWasActive(false)
  36. {
  37. connect(myQuery, SIGNAL(finished()), SLOT(queryFinished()));
  38. connect(myQuery, SIGNAL(error(ServerQuery::Error)), SLOT(queryError(ServerQuery::Error)));
  39. myQueryTimer->setSingleShot(true);
  40. }
  41. ActiveClient::~ActiveClient()
  42. {
  43. delete myClient;
  44. delete myDisconnectTime;
  45. }
  46. Client* ActiveClient::client()
  47. {
  48. return myClient;
  49. }
  50. quint8 ActiveClient::playerCount() const
  51. {
  52. return myQuery->playerList().size();
  53. }
  54. void ActiveClient::setAddress(const QHostAddress &address, quint16 port)
  55. {
  56. myQuery->setAddress(address, port);
  57. return;
  58. }
  59. void ActiveClient::queryError(ServerQuery::Error)
  60. {
  61. myQueryTimer->start(myQueryInterval);
  62. }
  63. void ActiveClient::queryFinished()
  64. {
  65. int playerCount = myQuery->playerList().size();
  66. myQueryTimer->start(myQueryInterval);
  67. /* If the client is disconnect check if there is at least one player connected to the server */
  68. if(myClient->state() == Client::DisconnectedState)
  69. {
  70. if(playerCount > 0 && myDisconnectTime->elapsed() > 10000)
  71. {
  72. myApp->print("Players online on server " + serverAddressString() + ". Joining...\n");
  73. myClient->connect(myQuery->address().toString().toAscii(), myQuery->port());
  74. }
  75. return;
  76. }
  77. /* If the client is connected and left alone on the server, disconnect yourself */
  78. if(myClient->state() == Client::ConnectedState)
  79. {
  80. if(playerCount == 1 && myClient->isOnServer())
  81. {
  82. myApp->print("I was left alone on " + serverAddressString() + ". Leaving...\n");
  83. myClient->disconnect();
  84. myDisconnectTime->restart();
  85. return;
  86. }
  87. }
  88. }
  89. void ActiveClient::run()
  90. {
  91. /* Say the broadcast count */
  92. if(!myBroadcastReplyTimer->isActive() && myReplyTimerWasActive)
  93. {
  94. myApp->activeClientsReplyCounters(&myUniqueServerCount, &myUniquePlayerCount); //add my internal counter to the list
  95. myClient->say("::cims:: Sent to " + QString::number(myUniqueChannelCount) + " channels, " + QString::number(myUniqueUserCount) + " unique users.");
  96. myClient->say("::cims:: Sent to " + QString::number(myUniqueServerCount) + " servers, " + QString::number(myUniquePlayerCount) + " unique players.");
  97. myUniqueServerCount = myUniquePlayerCount = myUniqueChannelCount = myUniqueUserCount = 0;
  98. }
  99. myReplyTimerWasActive = myBroadcastReplyTimer->isActive();
  100. /* Query the serverinfo for player count if not already querying */
  101. if(!myQuery->isActive() && !myQueryTimer->isActive())
  102. myQuery->query();
  103. else
  104. myQuery->run();
  105. /* Run the client */
  106. if(myClient->state() != Client::DisconnectedState)
  107. myClient->run();
  108. }
  109. const QString ActiveClient::serverAddressString()
  110. {
  111. return QString(myQuery->address().toString() + ':' + QString::number(myQuery->port()));
  112. }
  113. void ActiveClient::setReplyHash(const QString &hash)
  114. {
  115. myReplyHash = hash;
  116. /* Wait for 5 seconds */
  117. myBroadcastReplyTimer->setSingleShot(true);
  118. myBroadcastReplyTimer->start(5000);
  119. }
  120. const QString& ActiveClient::replyHash() const
  121. {
  122. return myReplyHash;
  123. }
  124. bool ActiveClient::isWaitingReply() const
  125. {
  126. return myBroadcastReplyTimer->isActive();
  127. }
  128. void ActiveClient::incrementReplyCounters(int userCount, int channelCount, int playerCount, int serverCount)
  129. {
  130. if(!myBroadcastReplyTimer->isActive())
  131. return;
  132. myUniqueUserCount += userCount;
  133. myUniqueChannelCount += channelCount;
  134. myUniqueServerCount += serverCount;
  135. myUniquePlayerCount += playerCount;
  136. }