]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/qt-console/console/console.cpp
Fix bug #1481 -- bat consumes all console file descriptors
[bacula/bacula] / bacula / src / qt-console / console / console.cpp
index 4201d61ce2c809c224924566a240cdfa61fba290..16e17b0124032c953fbe201218e312ecf89ca9dc 100644 (file)
@@ -1,7 +1,7 @@
 /*
    Bacula® - The Network Backup Solution
 
-   Copyright (C) 2007-2009 Free Software Foundation Europe e.V.
+   Copyright (C) 2007-2010 Free Software Foundation Europe e.V.
 
    The main author of Bacula is Kern Sibbald, with contributions from
    many others, a complete list can be found in the file AUTHORS.
@@ -26,8 +26,6 @@
    Switzerland, email:ftf@fsfeurope.org.
 */
 /*
- *   Version $Id$
- *
  *  Console Class
  *
  *   Kern Sibbald, January MMVII
@@ -50,6 +48,10 @@ Console::Console(QTabWidget *parent)
    m_console = this;
    m_warningPrevent = false;
    m_dircommCounter = 0;
+
+   /* 
+    * Create a connection to the Director and put it in a hash table
+    */
    m_dircommHash.insert(m_dircommCounter, new DirComm(this, m_dircommCounter));
 
    setupUi(this);
@@ -94,8 +96,9 @@ void Console::stopTimer()
 void Console::poll_messages()
 {
    int conn;
-   if (!availableDirComm(conn))
+   if (!availableDirComm(conn)) {
       return;
+   }
    DirComm *dircomm = m_dircommHash.value(conn);
 
    if (mainWin->m_checkMessages && dircomm->m_at_main_prompt && hasFocus() && !mainWin->getWaitState()){
@@ -135,19 +138,12 @@ void Console::connect_dir()
  * A function created to separate out the population of the lists
  * from the Console::connect_dir function
  */
-void Console::populateLists(bool forcenew)
+void Console::populateLists(bool /*forcenew*/)
 {
    int conn;
-   if (forcenew) {
-      if (!newDirComm(conn)) {
-         Pmsg1(000, "newDirComm Seems to Failed to create a connection for populateLists %s\n", m_dir->name());
-         return;
-      }
-   } else {
-      if (!availableDirComm(conn)) {
-         Pmsg1(000, "availableDirComm Seems to Failed to find a connection for populateListsi %s\n", m_dir->name());
-         return;
-      }
+   if (!availableDirComm(conn) && !newDirComm(conn)) {
+      Emsg1(M_ABORT, 0, "Failed to connect to %s for populateLists.\n", m_dir->name());
+      return;
    }
    populateLists(conn);
 }
@@ -162,6 +158,8 @@ void Console::populateLists(int conn)
    storage_list.clear();
    type_list.clear();
    level_list.clear();
+   volstatus_list.clear();
+   mediatype_list.clear();
    dir_cmd(conn, ".jobs", job_list);
    dir_cmd(conn, ".clients", client_list);
    dir_cmd(conn, ".filesets", fileset_list);  
@@ -170,6 +168,9 @@ void Console::populateLists(int conn)
    dir_cmd(conn, ".storage", storage_list);
    dir_cmd(conn, ".types", type_list);
    dir_cmd(conn, ".levels", level_list);
+   dir_cmd(conn, ".volstatus", volstatus_list);
+   dir_cmd(conn, ".mediatypes", mediatype_list);
+   dir_cmd(conn, ".locations", location_list);
 
    if (mainWin->m_connDebug) {
       QString dbgmsg = QString("jobs=%1 clients=%2 filesets=%3 msgs=%4 pools=%5 storage=%6 types=%7 levels=%8 conn=%9 %10\n")
@@ -207,7 +208,7 @@ bool Console::dir_cmd(const char *cmd, QStringList &results)
       dir_cmd(conn, cmd, results);
       return true;
    } else {
-      Pmsg1(000, "dir_cmd Seems to Failed to find a connection %s\n", m_dir->name());
+      Pmsg1(000, "dir_cmd failed to connect to %s\n", m_dir->name());
       return false;
    }
 }
@@ -251,16 +252,18 @@ bool Console::sql_cmd(int &conn, QString &query, QStringList &results)
 bool Console::sql_cmd(QString &query, QStringList &results)
 {
    int conn;
-   if (!availableDirComm(conn))
+   if (!availableDirComm(conn)) {
       return false;
+   }
    return sql_cmd(conn, query.toUtf8().data(), results, true);
 }
 
 bool Console::sql_cmd(const char *query, QStringList &results)
 {
    int conn;
-   if (!availableDirComm(conn))
+   if (!availableDirComm(conn)) {
       return false;
+   }
    return sql_cmd(conn, query, results, true);
 }
 
@@ -318,16 +321,18 @@ bool Console::sql_cmd(int &conn, const char *query, QStringList &results, bool d
 int Console::write_dir(const char *msg)
 {
    int conn;
-   if (availableDirComm(conn))
+   if (availableDirComm(conn)) {
       write_dir(conn, msg);
+   }
    return conn;
 }
 
 int Console::write_dir(const char *msg, bool dowait)
 {
    int conn;
-   if (availableDirComm(conn))
+   if (availableDirComm(conn)) {
       write_dir(conn, msg, dowait);
+   }
    return conn;
 }
 
@@ -669,8 +674,9 @@ QString Console::returnFromPrompt(int conn)
 int Console::notifyOff()
 { 
    int conn = 0;
-   if (availableDirComm(conn))
+   if (availableDirComm(conn)) {
       notify(conn, false);
+   }
    return conn;
 }
 
@@ -814,19 +820,10 @@ bool Console::is_connected(int conn)
  */
 bool Console::availableDirComm(int &conn)
 {
-   QHash<int, DirComm*>::const_iterator iter = m_dircommHash.constBegin();
-   while (iter != m_dircommHash.constEnd()) {
-      DirComm *dircomm = iter.value();
-      if (dircomm->m_at_prompt && dircomm->m_at_main_prompt && dircomm->is_notify_enabled()) {
-         conn = dircomm->m_conn;
-         return true;
-      }
-      ++iter;
-   }
-   if (newDirComm(conn))
+   if (currentDirComm(conn)) {
       return true;
-   else
-      return false;
+   }
+   return newDirComm(conn);
 }
 
 
@@ -835,13 +832,19 @@ bool Console::availableDirComm(int &conn)
  */
 bool Console::currentDirComm(int &conn)
 {
+   int i = 1;
    QHash<int, DirComm*>::const_iterator iter = m_dircommHash.constBegin();
    while (iter != m_dircommHash.constEnd()) {
       DirComm *dircomm = iter.value();
-      if (dircomm->m_at_prompt && !dircomm->m_at_main_prompt && dircomm->is_notify_enabled()) {
+      if (dircomm->m_at_prompt && dircomm->m_at_main_prompt && dircomm->is_notify_enabled()) {
          conn = dircomm->m_conn;
          return true;
       }
+      if (mainWin->m_connDebug) {
+         Pmsg4(000, "currentDirComm=%d at_prompt=%d at_main=%d && notify=%d\n",                                      
+            i, dircomm->m_at_prompt, dircomm->m_at_main_prompt, dircomm->is_notify_enabled());
+         i++;
+      }
       ++iter;
    }
    return false;
@@ -852,18 +855,26 @@ bool Console::currentDirComm(int &conn)
  */
 bool Console::newDirComm(int &conn)
 {
-   m_dircommCounter += 1;
-   conn = m_dircommCounter;
-   if (mainWin->m_connDebug)
-      Pmsg2(000, "DirComm %i About to Create and Connect %s\n", m_dircommCounter, m_dir->name());
+   m_dircommCounter++;
+   if (mainWin->m_connDebug) {
+      Pmsg2(000, "newDirComm=%i to: %s\n", m_dircommCounter, m_dir->name());
+   }
    DirComm *dircomm = new DirComm(this, m_dircommCounter);
    m_dircommHash.insert(m_dircommCounter, dircomm);
    bool success = dircomm->connect_dir();
    if (mainWin->m_connDebug) {
-      if (success)
-         Pmsg2(000, "DirComm %i Connected %s\n", conn, m_dir->name());
-      else
-         Pmsg2(000, "DirComm %i NOT Connected %s\n", conn, m_dir->name());
+      if (success) {
+         Pmsg2(000, "newDirComm=%i Connected %s\n", m_dircommCounter, m_dir->name());
+      } else {
+         Emsg2(M_ERROR, 0, "DirComm=%i. Unable to connect to %s\n",
+               m_dircommCounter, m_dir->name());
+      }
    }
+   if (!success) {
+      m_dircommHash.remove(m_dircommCounter);
+      delete dircomm;
+      m_dircommCounter--;
+   }
+   conn = m_dircommCounter;
    return success;
 }