Forráskód Böngészése

Merge branch 'dev' into 'master'

Dev
Luiz 10 éve
szülő
commit
206e6ff7b4
5 módosított fájl, 98 hozzáadás és 32 törlés
  1. 30 2
      ActiveClient.cpp
  2. 20 2
      ActiveClient.h
  3. 35 16
      App.cpp
  4. 9 5
      App.h
  5. 4 7
      SshClient.cpp

+ 30 - 2
ActiveClient.cpp

@@ -19,6 +19,7 @@ along with this program.  If not, see < http://www.gnu.org/licenses/ >.
 
 #include <QTimer>
 #include <QTime>
+
 #include "ServerQuery.h"
 #include "ActiveClient.h"
 #include "Client.h"
@@ -35,6 +36,7 @@ ActiveClient::ActiveClient(App *app, const QString& password, QObject *parent):
   myBroadcastReplyTimer(new QTimer(this)),
   myQueryTimer(new QTimer(this)),
   myQueryInterval(Settings::globalInstance()->queryInterval()),
+  myHasHostNameFlag(false),
   myUniqueUserCount(0),
   myUniqueChannelCount(0),
   myUniqueServerCount(0),
@@ -69,12 +71,28 @@ quint8 ActiveClient::playerCount() const
 }
 
 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-void ActiveClient::setAddress(const QHostAddress &address, quint16 port)
+void ActiveClient::setAddress(const QString &hostName, const QHostAddress &hostAddress, quint16 port)
 {
-  myQuery->setAddress(address, port);
+  myQuery->setAddress(hostAddress, port);
+  setHostName(hostName);
   return;
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+bool ActiveClient::hasHostName() const {
+  return myHasHostNameFlag;
+}
+
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+const QHostAddress& ActiveClient::address() const {
+  return myQuery->address();
+}
+
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+quint16 ActiveClient::port() const {
+  return myQuery->port();
+}
+
 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 void ActiveClient::queryError(ServerQuery::Error)
 {
@@ -145,6 +163,12 @@ const QString ActiveClient::serverAddressString()
   return QString(myQuery->address().toString() + ':' + QString::number(myQuery->port()));
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+const QString ActiveClient::serverHostNameString()
+{
+  return QString(myHostName + ':' + QString::number(myQuery->port()));
+}
+
 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 void ActiveClient::setReplyHashAndWaitForReply(const QString &hash)
 {
@@ -183,6 +207,10 @@ void ActiveClient::incrementReplyCounters(int userCount, int channelCount, int p
 void ActiveClient::setHostName(const QString &hostName)
 {
   myHostName = hostName;
+  if (hostName == myQuery->address().toString()) // If hostname == ip address then we have nothing and we can query central for the DNS
+    myHasHostNameFlag = false;
+  else
+    myHasHostNameFlag = true;
 }
 
 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

+ 20 - 2
ActiveClient.h

@@ -72,10 +72,14 @@ public:
   /**
     Set the server that we are going to monitor.
 
-    @param address Server address
+    @param hostName Server hostname
+    @param hostAddress Server address
     @param port    Server port
   */
-  void              setAddress(const QHostAddress &address, quint16 port);
+  void              setAddress(const QString& hostName, const QHostAddress &hostAddress, quint16 port);
+
+  const QHostAddress& address() const;
+  quint16 port() const;
 
   /**
     Returns the address of the server we are monitoring.
@@ -84,6 +88,13 @@ public:
   */
   const QString     serverAddressString();
 
+  /**
+    Returns the full hostname of the server we are monitoring (host:port).
+
+    @return The server address
+  */
+  const QString     serverHostNameString();
+
   /**
     Returns the number of players on the server last time the query was ran.
 
@@ -131,6 +142,12 @@ public:
   */
   void              setHostName(const QString& hostName);
 
+  /**
+   * @brief hasHostName
+   * @return
+   */
+  bool              hasHostName() const;
+
   /**
     Get hostname
 
@@ -181,6 +198,7 @@ private:
 
   // Server HostName used on reply
   QString           myHostName;
+  bool              myHasHostNameFlag;
 
   // Reply counters
   int               myUniqueUserCount;

+ 35 - 16
App.cpp

@@ -64,12 +64,12 @@ App::App(int &argc, char **argv) :
     return;
   }
 
-  print("CIMS Bot Service v0.12\n========================================================\n");
+  print("CIMS Bot Service v0.13\n========================================================\n");
 
   setApplicationName("CIMSBOT");
-  setOrganizationDomain("http://redmine.b4r.org/projects/cimsqwbot/");
+  setOrganizationDomain("https://gitlab.netdome.biz/community-messaging-project/qwbot");
   setOrganizationName("CIMS");
-  setApplicationVersion("0.12");
+  setApplicationVersion("0.13");
 
   myClientsFrameTimerID = startTimer(0);
 
@@ -150,8 +150,8 @@ void App::saveServerList()
   foreach(ac, myClients)
   {
     Settings::Server sv;
-    sv.address = ac->serverAddressString().split(":").at(0);
-    sv.port = ac->serverAddressString().split(":").at(1).toUShort();
+    sv.address = ac->serverHostNameString().split(":").at(0);
+    sv.port = ac->serverHostNameString().split(":").at(1).toUShort();
     list.push_back(sv);
   }
   Settings::globalInstance()->setServerList(list);
@@ -175,11 +175,16 @@ void App::print(const QString &msg)
 }
 
 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-bool App::addClient(const QString &host, quint16 port, const QString& password)
+bool App::addClient(const QString &hostName, quint16 port, const QString& password)
 {
+  QHostInfo hi = QHostInfo::fromName(hostName);
+  if (hi.error() != QHostInfo::NoError) {
+    print("Couldn't add server " + hostName + ": " + hi.errorString());
+    return false;
+  }
+  QHostAddress hostAddress = hi.addresses().first();
+  QString fullAddress = hostAddress.toString() + ":" + QString::number(port);
   ActiveClient* ac;
-  QHostAddress ha(host);
-  QString fullAddress = host + ":" + QString::number(port);
   foreach(ac, myClients)
   {
     if(ac->serverAddressString() == fullAddress)
@@ -190,7 +195,7 @@ bool App::addClient(const QString &host, quint16 port, const QString& password)
   }
 
   ac = new ActiveClient(this, password, this);
-  ac->setAddress(ha, port);
+  ac->setAddress(hostName, hostAddress, port);
   ac->client()->setQuakeFolder(Settings::globalInstance()->quakeFolder().toLatin1().data());
   ac->client()->setName(Settings::globalInstance()->botName().toLatin1().data());
   ac->client()->setSpectator(Settings::globalInstance()->botSpectator());
@@ -206,11 +211,16 @@ bool App::addClient(const QString &host, quint16 port, const QString& password)
 }
 
 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-bool App::removeClient(const QString &host, quint16 port)
+bool App::removeClient(const QString &hostName, quint16 port)
 {
+  QHostInfo hi = QHostInfo::fromName(hostName);
+  if (hi.error() != QHostInfo::NoError) {
+    print("Couldn't add server " + hostName + ": " + hi.errorString());
+    return false;
+  }
+  QHostAddress hostAddress = hi.addresses().first();
+  QString fullAddress = hostAddress.toString() + ":" + QString::number(port);
   ActiveClient* ac;
-  QHostAddress ha(host);
-  QString fullAddress = host + ":" + QString::number(port);
   foreach(ac, myClients)
   {
     if(ac->serverAddressString() == fullAddress)
@@ -370,6 +380,11 @@ void App::setReplyHashAndWaitForReply(const QString &serverAddress, const QStrin
   }
 }
 
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+const QList<ActiveClient*> App::clients() const {
+  return myClients;
+}
+
 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 void App::incrementReplyCounters(const QString &hash, int userCount, int channelCount, int playerCount, int serverCount)
 {
@@ -390,8 +405,10 @@ void App::requestCachedHostNames()
   ActiveClient* ac;
   foreach(ac, myClients)
   {
-    print("Requested HostName for server " + ac->serverAddressString() + "\n");
-    mySshClient->write("REQ_DNS " + ac->serverAddressString() + "\n");
+    if (!ac->hasHostName()) {
+      print("Requested HostName for server " + ac->serverAddressString() + "\n");
+      mySshClient->write("REQ_DNS " + ac->serverAddressString() + "\n");
+    }
   }
 }
 
@@ -403,8 +420,10 @@ void App::setServerHostName(const QString &serverAddress, const QString &hostNam
   {
     if(ac->serverAddressString() == serverAddress)
     {
-      print("HostName for " + serverAddress + " set to " + hostName + "\n");
-      ac->setHostName(hostName);
+      if (!ac->hasHostName() && ac->hostName() != hostName) {
+        print("HostName for " + serverAddress + " set to " + hostName + "\n");
+        ac->setHostName(hostName);
+      }
       return;
     }
   }

+ 9 - 5
App.h

@@ -55,6 +55,8 @@ public:
   */
   ~App();
 
+  const QList<ActiveClient*> clients() const;
+
   /**
     Prints directly to console and to anyone connected via telnet.
 
@@ -145,14 +147,16 @@ public:
     @return  True if its correct, false otherwise
   */
 
-  bool                  addClient(const QString& host, quint16 port, const QString &password = "");
+  bool                  addClient(const QString& hostName, quint16 port, const QString &password = "");
+
   /**
-    Checks if the password specified by the user is correct.
+    Removes a client.
 
-    @param  password  The password
-    @return  True if its correct, false otherwise
+    @param  hostName  hostname
+    @param port port
+    @return  True if its removed, false otherwise
   */
-  bool                  removeClient(const QString& host, quint16 port);
+  bool                  removeClient(const QString& hostName, quint16 port);
 protected:
   /**
     App's MainLoop is here.

+ 4 - 7
SshClient.cpp

@@ -20,6 +20,8 @@ along with this program.  If not, see < http://www.gnu.org/licenses/ >.
 #include "SshClient.h"
 #include "App.h"
 #include "Settings.h"
+#include "ActiveClient.h"
+
 #include <QProcess>
 #include <QRegExp>
 #include <QDateTime>
@@ -266,13 +268,8 @@ void SshClient::parse(const QDateTime &time, const QString &command, const QStri
   if(command == "REQ_ASSIGNMENTS")
   {
     myApp->print("Server list requested... Sending it now!\n");
-    // TODO: Maybe fetching server data directly from ActiveClients list is a better idea
-    Settings::ServerList serverList = Settings::globalInstance()->serverList();
-    Settings::Server server;
-    foreach(server, serverList)
-    {
-      // Let the reply be send after the pinging is done
-      Pinger* pinger = new Pinger(QHostAddress(server.address), server.port, this);
+    foreach (ActiveClient* ac, myApp->clients()) {
+      Pinger* pinger = new Pinger(ac->address(), ac->port(), this);
       connect(pinger, SIGNAL(finished(QHostAddress,quint16,int)), SLOT(assignmentsReply(QHostAddress,quint16,int)));
       pinger->ping();
     }