ActiveClient.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 "ServerQueryThread.h"
  18. #include "ActiveClient.h"
  19. #include "Client.h"
  20. #include "App.h"
  21. ActiveClient::ActiveClient(App *app):
  22. myApp(app),
  23. myClient(new Client(app)),
  24. myQueryThread(new ServerQueryThread()),
  25. myDisconnectTime(new QTime()),
  26. myBroadcastReplyTimer(new QTimer()),
  27. myUniqueUserCount(0),
  28. myUniqueChannelCount(0),
  29. myUniqueServerCount(0),
  30. myUniquePlayerCount(0),
  31. myReplyTimerWasActive(false)
  32. {
  33. }
  34. ActiveClient::~ActiveClient()
  35. {
  36. delete myClient;
  37. if(myQueryThread->isRunning())
  38. myQueryThread->stopQueryThread();
  39. while(myQueryThread->isRunning());
  40. delete myQueryThread;
  41. delete myBroadcastReplyTimer;
  42. delete myDisconnectTime;
  43. }
  44. Client* ActiveClient::client()
  45. {
  46. return myClient;
  47. }
  48. ServerQueryThread* ActiveClient::queryThread()
  49. {
  50. return myQueryThread;
  51. }
  52. void ActiveClient::setAddressAndStartThread(const QHostAddress &address, quint16 port)
  53. {
  54. myQueryThread->setAddress(address, port);
  55. myQueryThread->startQueryThread();
  56. return;
  57. }
  58. void ActiveClient::run()
  59. {
  60. /* If the client is disconnect check if there is at least one player connected to the server */
  61. if(myClient->state() == Client::DisconnectedState)
  62. {
  63. int playerCount = myQueryThread->playerCount();
  64. if(playerCount > 0 && myDisconnectTime->elapsed() > 10000)
  65. {
  66. myApp->print("Players online on server " + myQueryThread->serverAddress().toString() + ":" + QString::number(myQueryThread->serverPort()) + ". Joining...\n");
  67. myClient->connect(myQueryThread->serverAddress().toString().toAscii(), myQueryThread->serverPort());
  68. }
  69. return;
  70. }
  71. /* If the client is connected and left alone on the server, disconnect yourself */
  72. if(myClient->state() == Client::ConnectedState)
  73. {
  74. int playerCount = myQueryThread->playerCount();
  75. if(playerCount == 1 && myClient->isOnServer())
  76. {
  77. myApp->print("I was left alone on " + myQueryThread->serverAddress().toString() + ":" + QString::number(myQueryThread->serverPort()) + ". Leaving...\n");
  78. myClient->disconnect();
  79. myDisconnectTime->restart();
  80. return;
  81. }
  82. }
  83. /* Say the broadcast count */
  84. if(!myBroadcastReplyTimer->isActive() && myReplyTimerWasActive)
  85. {
  86. myApp->activeClientsReplyCounters(&myUniqueServerCount, &myUniquePlayerCount); //add my internal counter to the list
  87. myClient->say("::cims:: Sent to " + QString::number(myUniqueChannelCount) + " channels, " + QString::number(myUniqueUserCount) + " unique users.");
  88. myClient->say("::cims:: Sent to " + QString::number(myUniqueServerCount) + " servers, " + QString::number(myUniquePlayerCount) + " unique players.");
  89. myUniqueServerCount = myUniquePlayerCount = myUniqueChannelCount = myUniqueUserCount = 0;
  90. }
  91. myReplyTimerWasActive = myBroadcastReplyTimer->isActive();
  92. myClient->run();
  93. }
  94. const QString ActiveClient::serverAddressString()
  95. {
  96. return QString(myQueryThread->serverAddress().toString() + ':' + QString::number(myQueryThread->serverPort()));
  97. }
  98. void ActiveClient::setReplyHash(const QString &hash)
  99. {
  100. myReplyHash = hash;
  101. /* Wait for 5 seconds */
  102. myBroadcastReplyTimer->setSingleShot(true);
  103. myBroadcastReplyTimer->start(5000);
  104. }
  105. const QString& ActiveClient::replyHash() const
  106. {
  107. return myReplyHash;
  108. }
  109. bool ActiveClient::isWaitingReply() const
  110. {
  111. return myBroadcastReplyTimer->isActive();
  112. }
  113. void ActiveClient::incrementReplyCounters(int userCount, int channelCount, int playerCount, int serverCount)
  114. {
  115. if(!myBroadcastReplyTimer->isActive())
  116. return;
  117. myUniqueUserCount += userCount;
  118. myUniqueChannelCount += channelCount;
  119. myUniqueServerCount += serverCount;
  120. myUniquePlayerCount += playerCount;
  121. }