Przeglądaj źródła

Hostnames can be added on the config file.

Mihawk 10 lat temu
rodzic
commit
bfb5b6dfbb
4 zmienionych plików z 73 dodań i 23 usunięć
  1. 21 2
      ActiveClient.cpp
  2. 17 2
      ActiveClient.h
  3. 28 14
      App.cpp
  4. 7 5
      App.h

+ 21 - 2
ActiveClient.cpp

@@ -19,6 +19,8 @@ along with this program.  If not, see < http://www.gnu.org/licenses/ >.
 
 #include <QTimer>
 #include <QTime>
+#include <QHostInfo>
+
 #include "ServerQuery.h"
 #include "ActiveClient.h"
 #include "Client.h"
@@ -35,6 +37,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 +72,18 @@ 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;
+}
+
 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 void ActiveClient::queryError(ServerQuery::Error)
 {
@@ -145,6 +154,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 +198,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;
 }
 
 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

+ 17 - 2
ActiveClient.h

@@ -72,10 +72,11 @@ 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);
 
   /**
     Returns the address of the server we are monitoring.
@@ -84,6 +85,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 +139,12 @@ public:
   */
   void              setHostName(const QString& hostName);
 
+  /**
+   * @brief hasHostName
+   * @return
+   */
+  bool              hasHostName() const;
+
   /**
     Get hostname
 
@@ -181,6 +195,7 @@ private:
 
   // Server HostName used on reply
   QString           myHostName;
+  bool              myHasHostNameFlag;
 
   // Reply counters
   int               myUniqueUserCount;

+ 28 - 14
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);
 
@@ -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)
@@ -390,8 +400,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 +415,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;
     }
   }

+ 7 - 5
App.h

@@ -145,14 +145,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.